-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate-container.common.sh
156 lines (128 loc) · 2.98 KB
/
create-container.common.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
#!/bin/bash
progname=$(basename $0)
declare -A VOLUMES
function create-container() {
declare -A optVolumes
declare -a volumes
: ${CREATE_VERSION:=latest}
optBase=""
optName=${DEFAULT_NAME}
optVersion=${CREATE_VERSION}
optVolumesUsed="false"
while getopts $(optionstring ":b:hn:v:") options; do
case "${options}" in
b)
optBase=${OPTARG}
;;
h)
usage "${DEFAULT_NAME}" "${CREATE_VERSION}"
;;
n)
optName=${OPTARG}
;;
v)
optVersion=${OPTARG}
;;
*)
if [ "${VOLUMES[${options}]+yes}" = "yes" ]; then
optVolumes[${options}]=${OPTARG}
optVolumesUsed="true"
else
usage "${DEFAULT_NAME}" "${CREATE_VERSION}"
exit 1
fi
;;
esac
done
shift $((OPTIND-1))
if [ "${optBase}" != "" -a ${optVolumesUsed} = "true" ]; then
usage "${DEFAULT_NAME}" "${CREATE_VERSION}"
fi
if [ "${optBase}" != "" ]; then
for opt in ${!VOLUMES[@]}; do
vol=$(echo ${VOLUMES[${opt}]} | cut -d ":" -f 1)
optVolumes[${opt}]="${optBase}/${vol}/"
done
fi
for opt in ${!VOLUMES[@]}; do
dir=$(echo ${VOLUMES[${opt}]} | cut -d ":" -f 2)
if [ "${optVolumes[${opt}]+yes}" = "yes" ]; then
volumes+=(${optVolumes[${opt}]}:${dir})
fi
done
portmap=$(echo ${PORT_MAP} | xargs) # Strip off whitespace
if [ "${portmap}" != "none" -a "${portmap}" != "" ]; then
portmap="-p ${portmap}"
else
portmap=""
fi
for i in ${!volumes[@]}; do
volumes[${i}]="-v ${volumes[${i}]}"
done
docker pull ${DOCKER_REPO}:${optVersion}
docker run \
--gpus all \
--name ${optName} \
-d \
${portmap} \
${volumes[@]} \
${DOCKER_REPO}:${optVersion}
}
function optionstring() {
str=$1
for opt in ${!VOLUMES[@]}; do
str="${str}${opt}:"
done
echo ${str}
}
function usage() {
name=$1
version=$2
usage="
Usage: ${progname} [-h] [-n <name>] [-v <versiontag>] [-b <basedir> |
$(for opt in ${!VOLUMES[@]}; do
vol=$(echo ${VOLUMES[${opt}]} | cut -d ":" -f 1)
cat <<EOF
-${opt} <${vol}dir>
EOF
done |
sort |
sed -e 's/:/:\n /'
)
]
-b Base directory that contains directories:
$(for opt in ${!VOLUMES[@]}; do
vol=$(echo ${VOLUMES[${opt}]} | cut -d ":" -f 1)
cat <<EOF
${vol}/
EOF
done |
sort
)
-h Show this usage statement.
-n Name for the container. [Default:\"${name}\"]
-v The version tag to use to create the container. [Default:\"${version}\"]
Volume options:
$(for opt in ${!VOLUMES[@]}; do
dir=$(echo ${VOLUMES[${opt}]} | cut -d ":" -f 2)
cat <<EOF
-${opt} The local directory to mount to:${dir}
EOF
done |
sort |
sed -e 's/:/:\n /'
)
Volumes used by this container:
$(for opt in ${!VOLUMES[@]}; do
vol=$(echo ${VOLUMES[${opt}]} | cut -d ":" -f 1)
dir=$(echo ${VOLUMES[${opt}]} | cut -d ":" -f 2)
cat <<EOF
- ${vol}:${dir}
EOF
done |
sort
)
"
echo "${usage}" 1>&2
exit 1
}