-
Notifications
You must be signed in to change notification settings - Fork 7
/
install.sh
executable file
·347 lines (283 loc) · 9.66 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env bash
# Build MIRACL Docker image with user information from host system
# - Checks if Docker and Docker Compose are installed
# - Dynamically adds and removes user information to
# prevent errors in Singularity container creation
# - Container user is the same as host user to prevent
# X11 issues with miraclGUI
# Current maintainer of script: Jonas Osmann @ github.com/jono3030
# MIRACL (C) Maged Groubran @ maged.goubran@utoronto.ca
#############
# VARIABLES #
#############
# Set variables base state
# Detect OS (Linux/MacOs)
os=$(uname -s)
# Script version
version="2.0.1-beta"
# Service name
service_name="miracl"
# Set default image name
image_name="aicons/miracl"
# Set default container name
container_name="miracl"
# Set default image version
miracl_version="latest"
# Version file version
miracl_version_file=$(cat ./miracl/version.txt)
# Set container memory size limit
if [[ "$os" == "Linux" ]]; then
shm_mem=$(grep MemTotal /proc/meminfo | awk '{printf "%dmb", int($2/1024*0.85)}')
elif [[ "$os" == "Darwin" ]]; then
shm_mem=$(sysctl hw.memsize | awk '{printf "%dmb", int($2/1024/1024*0.85)}')
else
echo "Unsupported operating system: $os"
exit 1
fi
# Set array to capture volumes
volumes=()
# Base usage
base_usage="Usage: ./$(basename "$0") [-n service_name] [-i image_name] [-c container_name] [-t {auto, x.x.x}] [-g] [-e] [-v vol:vol] [-l] [-s] [-m] [-h]"
##########
# PARSER #
##########
# Usage function
function usage()
{
cat <<usage
$base_usage
Automatically build MIRACL Docker image with pseudo host user
Options:
-n, name of the Docker service (default: 'miracl')
-i, specify image name (default: 'aicons/miracl')
-c, specify container name (default: 'miracl')
-t, set when using specific MIRACL tag/version. Use 'auto' to parse from 'miracl/version.txt' or specify version as floating point value in format 'x.x.x' (default: 'latest')
-g, enable Nvidia GPU passthrough mode for Docker container which is required for some of MIRACL's scripts e.g. ACE segmentation (default: false)
-e, disable mounting MIRACL's script directory into Docker container. Mounting is useful if you want host changes to propagate to the container directly (default: enabled)
-d, set shared memory (shm) size (e.g. '1024mb', '16gb' or '512gb') which is important for e.g ACE (default: int(MemTotal/1024)*0.85 of host machine)
-v, mount volumes for MIRACL in docker-compose.yml, using a separate flag for each additional volume (format: '/path/on/host:/path/in/container'; default: none)
-l, write logfile of build process to 'build.log' in MIRACL root directory (default: false)
-s, print version of build script and exit
-m, print version of MIRACL on current Git branch and exit
-h, print this help menu and exit
Script version: $version
MIRACL version: $miracl_version_file
usage
}
# Parse command line arguments
while getopts ":n:i:c:t:ged:v:lsmh" opt; do
case ${opt} in
n)
if [ "${OPTARG}" != "$service_name" ]; then
service_name=${OPTARG}
fi
;;
i)
if [ "${OPTARG}" != "$image_name" ]; then
image_name=${OPTARG}
fi
;;
c)
if [ "${OPTARG}" != "$container_name" ]; then
container_name=${OPTARG}
fi
;;
t)
if [[ "${OPTARG}" == "auto" ]]; then
miracl_version=$miracl_version_file
elif [[ "${OPTARG}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
miracl_version=${OPTARG}
else
echo 'Error: Version input is not a floating point number (format: x.x.x), "auto" or "latest"'
exit 1
fi
;;
g)
gpu=true
;;
e)
dev=false
;;
d)
if [ "${OPTARG}" != "$shm_mem" ]; then
shm_mem=${OPTARG}
fi
;;
v)
volumes+=("$OPTARG")
;;
l)
write_log=true
;;
s)
echo -e "v$version"
exit 0
;;
m)
echo -e "v$miracl_version_file"
exit 0
;;
h)
usage
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
printf "\n%s\n" "$base_usage"
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
printf "\n%s\n" "$base_usage"
exit 1
;;
esac
done
shift "$((OPTIND -1))"
##################
# DOCKER COMPOSE #
##################
# Generate docker-compose.yml file
cat > docker-compose.yml <<EOF
version: "3.3"
services:
$service_name:
image: $image_name:$miracl_version
tty: true
environment:
- DISPLAY
stdin_open: true
network_mode: host
container_name: $container_name
shm_size: ${shm_mem}
EOF
if [[ $gpu ]]; then
cat >> docker-compose.yml <<EOF
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: all
capabilities: [gpu]
EOF
fi
# Append required volumes
cat >> docker-compose.yml <<EOF
volumes:
- /home/josmann/.Xauthority:/home/josmann/.Xauthority
EOF
# Append MIRACL scripts folder mounting
if [[ -z $dev ]]; then
install_script_dir=$(dirname "$(readlink -f "$0")")
cat >> docker-compose.yml <<EOF
- ${install_script_dir}/miracl:/code/miracl
EOF
fi
# Add additional volumes
for v in "${volumes[@]}"; do
echo " - $v" >> docker-compose.yml
done
##############
# DOCKERFILE #
##############
# Information that needs to be added to Dockerfile
# to create pseudo host user. This is required to
# make X11 work correclty with miraclGUI
USER_TEXT=$(cat <<END
# Setup host user as container user\n\
ARG USER_ID=\$USER_ID\n\
ARG GROUP_ID=\$GROUP_ID\n\
ARG USER=\$USER\n\n\
RUN addgroup --gid \$GROUP_ID \$USER\n\
RUN adduser --disabled-password --gecos '' --uid \$USER_ID --gid \$GROUP_ID \$USER\n\n\
# Change owner of /code directory\n\
RUN chown -R \$USER:\$USER /code\n\
# Change to \$USER\n\
USER \$USER\n\
WORKDIR /home/\$USER
END
)
# Function to remove user text in Dockerfile if it is already present
function rm_USER_TEXT () {
sed -i '/STARTUNCOMMENT/,/STOPUNCOMMENT/{//!d}' Dockerfile
}
# Check if $USER_TEXT strings are already present and delete if true
if [ "$(sed -n '/#STARTUNCOMMENT#/{n;p;}' Dockerfile)" != "#STOPUNCOMMENT#" ]; then
rm_USER_TEXT
fi
#########
# BUILD #
#########
# Check if Docker is installed
if [ -x "$(command -v docker)" ]; then
printf "\nDocker installation found.\n"
# Adding host user requirements to Dockerfile
sed -i "/#STARTUNCOMMENT#/a $USER_TEXT" Dockerfile
# Change user in docker-compose.yml to host user
HOST_USER=$(whoami)
export HOST_USER
sed -i "s/\(\/home\/\).*\(\/.Xauthority:\/home\/\).*\(\/.Xauthority\)/\1$HOST_USER\2$HOST_USER\3/g" docker-compose.yml
######################################
# Build MIRACL image from Dockerfile #
######################################
# Printing information about build process and docker-compose.yml to stdout
printf "\n[+] Building MIRACL and creating docker-compose.yml with the following parameters:\n"
printf " User: %s\n" "$HOST_USER"
printf " pid: %s\n" "$(id -u)"
printf " gid: %s\n" "$(id -g)"
printf " Max shared memory: %s\n" "$shm_mem"
printf " Service name: %s\n" "$service_name"
printf " Image name: %s\n" "$image_name:$miracl_version"
printf " Container name: %s\n" "$container_name"
printf " GPU passthrough: %s\n" "${gpu:-false}"
printf " Script dir mounted: %s\n" "${dev:-true}"
printf " Log file: %s\n" "${write_log:-false}"
for v in "${!volumes[@]}"; do
printf " Volume %s: %s\n" "$v" "${volumes[$v]}"
done
# Pass user name, UID and GID to Dockerfile
function docker_build () {
docker build \
--build-arg USER_ID="$(id -u)" \
--build-arg GROUP_ID="$(id -g)" \
--build-arg USER="$HOST_USER" \
-t "$image_name":"$miracl_version" .
}
# Check for log flag
if [ $write_log ];
then
docker_build | tee build.log
else
docker_build
fi
# Check if build process exited without errors
build_status_code=$?
if [ $build_status_code -eq 0 ]; then
printf "\nBuild was successful! Checking Docker Compose installation.\n"
# Remove $USER_TEXT from Dockerfile
rm_USER_TEXT
# Test if docker-compose is installed
# Should come by default with Docker-Desktop
if [ -x "$(command -v docker-compose)" ]; then
printf "Docker Compose installation found (%s). Run 'docker-compose up -d' to start the container in the background and then run 'docker exec -it ${container_name} bash' to enter the container.\n" "$(docker-compose --version)"
elif dcex=$(docker compose version); then
printf "Docker Compose installation found (%s). Run 'docker compose up -d' or use Docker Desktop (if installed) to start the container in the background and then run 'docker exec -it ${container_name} bash' to enter the container.\n" "$dcex"
else
printf "Docker Compose installation not found. Please install Docker Compose plugin or standalone version to run the MIRACL container. Instructions on how to install Docker Compose can be found here: https://docs.docker.com/compose/install/\n"
fi
else
# Return error code if build was not successful
printf "\nBuild not successful! An error occured with exit code: %s\n" $build_status_code
# Remove $USER_TEXT from Dockerfile
rm_USER_TEXT
# Exit with custom status code
exit $build_status_code
fi
else
di_status_code=$?
printf "\nDocker installation not found. Please install Docker first.\n"
printf "Exiting with status code %s\n" $di_status_code
# Exit with custom status code
exit $di_status_code
fi