forked from ibmcb/cbtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbdocker
executable file
·427 lines (391 loc) · 14.5 KB
/
cbdocker
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/bin/bash
if [ $0 != "-bash" ] ; then
pushd `dirname "$0"` 2>&1 > /dev/null
fi
CB_BASE_DIR=$(pwd)
if [ $0 != "-bash" ] ; then
popd 2>&1 > /dev/null
fi
if [[ $(sudo cat /proc/1/cgroup | grep -c docker) -ne 0 ]]
then
echo "It appears that we are already running inside a docker. The executable $0 was designed to be used outside of a Docker container"
exit -1
fi
CB_USER=${CB_USER:-$HOME}
CB_DOCKER_USER=${CB_DOCKER_USER:-cbuser}
CB_DOCKER_NETHOST=1
CB_DOCKER_REPO=${CB_DOCKER_REPO:-ibmcb}
CB_DOCKER_IMAGE=${CB_DOCKER_IMAGE:-ubuntu_cb_orchestrator}
CB_DOCKER_TAG=${CB_DOCKER_TAG:-master}
CB_DOCKER_DEBUG=0
CB_KILL_DOCKER=0
CB_PRIVATE_CONFIG=0
CB_PRIVATE_DATA=0
CB_DRY_RUN=0
CB_DOCKER_PRECOMMAND=/bin/true
CB_DOCKER_COMMAND="auto"
CB_MANAGER_IP=${CB_MANAGER_IP:-"auto"}
CB_DOCKER_EXTRA_OPTS=${CB_DOCKER_EXTRA_OPTS:-""}
CB_DOCKER_IN_CLI=${CB_DOCKER_IN_CLI:-1}
CB_EXECUTABLE_PARMS=" "
CB_DOCKER_ADDITIONAL_VOLUMES="none"
CB_DOCKER_ADDITIONAL_ENVARS="none"
CB_DOCKER_HOST_ENTRY="none"
CB_EXECUTABLE_NAME=cbtool
CB_OBJECTSTORE_PORT=${CB_OBJECTSTORE_PORT:-40000}
CB_LOGSTORE_PORT=${CB_LOGSTORE_PORT:-40001}
CB_METRICSTORE_PORT=${CB_METRICSTORE_PORT:-40002}
CB_FILESTORE_PORT=${CB_FILESTORE_PORT:-40003}
CB_API_DEFAULTS_PORT=${CB_API_DEFAULTS_PORT:-40004}
CB_GUI_DEFAULTS_PORT=${CB_GUI_DEFAULTS_PORT:-40005}
CB_VPN_SERVER_PORT=${CB_VPN_SERVER_PORT:-40006}
CB_CREATE_ONLY=0
CB_USAGE="Usage: $0 [-r/--repo <repository>] [-i/--image image name] [-b/--branch branch] [-c/--config config] [-D/--pexec command] [-e/--execute command] [-v/--volumes] [-g/--envars] [-a/--add-host] [-p/--private] [-P/--propagate] [-z/--nonethost] [-d/--debug] [--reset] [-f/--soft_reset] [-x/--hard_reset] [-t/--trace tracefile] [-k/--kill] [-n/--dryrun] [-y/--createonly] [-m/--cloudrename]"
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-r|--repo)
CB_DOCKER_REPO="$2"
shift
;;
-r=*|--repo=*)
CB_DOCKER_REPO=$(echo $key | cut -d '=' -f 2)
shift
;;
-i|--image)
CB_DOCKER_IMAGE="$2"
shift
;;
-i=*|--image=*)
CB_DOCKER_IMAGE=$(echo $key | cut -d '=' -f 2)
shift
;;
-b|--branch)
CB_DOCKER_TAG="$2"
shift
;;
-b=*|--branch=*)
CB_DOCKER_TAG=$(echo $key | cut -d '=' -f 2)
shift
;;
-c|--config)
CB_EXECUTABLE_PARMS=${CB_EXECUTABLE_PARMS}" --config $2 "
shift
;;
-c=*|--config=*)
CB_EXECUTABLE_PARMS=${CB_EXECUTABLE_PARMS}" --config $(echo $key | cut -d '=' -f 2)"
shift
;;
-v|--volumes)
CB_DOCKER_ADDITIONAL_VOLUMES="$2"
shift
;;
-v=*|--volumes=*)
CB_DOCKER_ADDITIONAL_VOLUMES=$(echo $key | cut -d '=' -f 2)
shift
;;
-g|--envars)
CB_DOCKER_ADDITIONAL_ENVARS="$2"
shift
;;
-g=*|--envars=*)
CB_DOCKER_ADDITIONAL_ENVARS=$(echo $key | cut -d '=' -f 2)
shift
;;
-m|--cloudrename)
CB_CLOUD_RENAME="$2"
shift
;;
-m=*|--cloudrename=*)
CB_CLOUD_RENAME=$(echo $key | cut -d '=' -f 2)
shift
;;
-a|--add-host)
CB_DOCKER_HOST_ENTRY="$2"
shift
;;
-a=*|--add-host=*)
CB_DOCKER_HOST_ENTRY=$(echo $key | cut -d '=' -f 2)
shift
;;
-p|--private)
CB_PRIVATE_CONFIG=1
CB_PRIVATE_DATA=1
;;
-P|--propagate)
CB_PRIVATE_CONFIG=0
CB_PRIVATE_DATA=0
;;
-z|--nonethost)
CB_DOCKER_NETHOST=0
;;
-y|--createonly)
CB_CREATE_ONLY=1
;;
-d|--debug)
CB_DOCKER_DEBUG=1
;;
--reset)
CB_EXECUTABLE_PARMS=${CB_EXECUTABLE_PARMS}" --soft_reset "
CB_KILL_DOCKER=2
;;
-f|--soft_reset)
CB_EXECUTABLE_PARMS=${CB_EXECUTABLE_PARMS}" --soft_reset "
CB_KILL_DOCKER=2
;;
-x|--hard_reset)
CB_EXECUTABLE_PARMS=${CB_EXECUTABLE_PARMS}" --hard_reset "
CB_KILL_DOCKER=2
;;
-t|--trace)
CB_EXECUTABLE_PARMS=${CB_EXECUTABLE_PARMS}" --trace $2 "
shift
;;
-t=*|--trace=*)
CB_EXECUTABLE_PARMS=${CB_EXECUTABLE_PARMS}" --trace $(echo $key | cut -d '=' -f 2)"
shift
;;
-e|--execute)
CB_DOCKER_COMMAND="$2"
shift
;;
-e=*|--execute=*)
CB_DOCKER_COMMAND=$(echo $key | cut -d '=' -f 2)
shift
;;
-D|--pexec)
CB_DOCKER_PRECOMMAND="$2"
shift
;;
-D=*|--pexec=*)
CB_DOCKER_PRECOMMAND=$(echo $key | cut -d '=' -f 2)
shift
;;
-k|--kill)
CB_KILL_DOCKER=1
;;
-n|--dryrun)
CB_DRY_RUN=1
;;
-h|--help)
echo $CB_USAGE
shift
exit 0
;;
*)
# unknown option
;;
esac
shift
done
CB_DOCKER_NAME=$(echo ${CB_USER}_cbon | rev | cut -d '/' -f 1 | rev)
if [[ $(sudo docker ps | grep -c $CB_DOCKER_NAME) -eq 0 ]]
then
echo -n "Docker \"$CB_DOCKER_NAME\" not running."
if [[ $CB_KILL_DOCKER -eq 0 ]]
then
echo " Will start a new one."
CB_KILL_DOCKER=2
else
echo " "
fi
elif [[ $(sudo docker ps | grep -c $CB_DOCKER_NAME) -gt 0 ]]
then
echo -n "Docker \"$CB_DOCKER_NAME\" is already running."
if [[ $CB_KILL_DOCKER -ne 0 ]]
then
echo " One of the following options \"--reset, -f/--soft_reset, -x/--hard_reset, -k/--kill\" was set. Container will be recreated"
else
echo " "
fi
else
if [[ $(sudo docker ps -a | grep -c $CB_DOCKER_NAME) -eq 1 ]]
then
if [[ $CB_KILL_DOCKER -eq 0 ]]
then
echo "Docker \"$CB_DOCKER_NAME\" exists, but is not running. Will remove and recreate."
CB_KILL_DOCKER=2
fi
fi
fi
if [[ $CB_DOCKER_COMMAND == "auto" ]]
then
if [[ $CB_KILL_DOCKER -gt 0 ]]
then
sudo docker kill $CB_DOCKER_NAME >/dev/null 2>&1
sudo docker rm $CB_DOCKER_NAME >/dev/null 2>&1
if [[ $CB_KILL_DOCKER -gt 1 ]]
then
if [[ $(echo $CB_EXECUTABLE_PARMS | grep -c reset) -eq 0 ]]
then
CB_EXECUTABLE_PARMS=${CB_EXECUTABLE_PARMS}" --soft_reset "
fi
if [[ ${CB_DOCKER_IN_CLI} -eq 0 ]]
then
CB_EXECUTABLE_PARMS=${CB_EXECUTABLE_PARMS}" vmclist; bash"
fi
CB_DOCKER_COMMAND="/home/$CB_DOCKER_USER/${CB_EXECUTABLE_NAME}/private_configs/build_cloud_config_file_from_env.sh $CB_DOCKER_USER /home/$CB_DOCKER_USER/${CB_EXECUTABLE_NAME} docker; /home/$CB_DOCKER_USER/${CB_EXECUTABLE_NAME}/cb $CB_EXECUTABLE_PARMS"
fi
else
CB_DOCKER_COMMAND="/home/$CB_DOCKER_USER/${CB_EXECUTABLE_NAME}/cb"
fi
else
if [[ $CB_KILL_DOCKER -gt 0 ]]
then
sudo docker kill $CB_DOCKER_NAME >/dev/null 2>&1
sudo docker rm $CB_DOCKER_NAME >/dev/null 2>&1
fi
fi
CB_DOCKER_COMMAND=$CB_DOCKER_PRECOMMAND" ;"$CB_DOCKER_COMMAND
if [[ $CB_DOCKER_DEBUG -eq 1 ]]
then
CB_DOCKER_BASE_CMDLINE="run -it "
else
CB_DOCKER_BASE_CMDLINE="run -itd "
fi
if [[ $CB_KILL_DOCKER -eq 2 ]]
then
CB_DOCKER_CMDLINE_NAME="--name ${CB_DOCKER_NAME} "
CB_DOCKER_SEC_CMDLINE="--cap-add=SYS_PTRACE --cap-add=NET_ADMIN --privileged=true"
if [[ $CB_DOCKER_NETHOST -eq 1 ]]
then
CB_DOCKER_NET_CMDLINE=" --net=host "
else
CB_DOCKER_NET_CMDLINE=" -p ${CB_OBJECTSTORE_PORT}:${CB_OBJECTSTORE_PORT} -p ${CB_LOGSTORE_PORT}:${CB_LOGSTORE_PORT} -p ${CB_METRICSTORE_PORT}:${CB_METRICSTORE_PORT} -p ${CB_FILESTORE_PORT}:${CB_FILESTORE_PORT} -p ${CB_API_DEFAULTS_PORT}:${CB_API_DEFAULTS_PORT} -p ${CB_GUI_DEFAULTS_PORT}:${CB_GUI_DEFAULTS_PORT} -p ${CB_VPN_SERVER_PORT}"
fi
CB_DOCKER_VOL_CMDLINE=""
if [[ $CB_PRIVATE_CONFIG -eq 0 ]]
then
if [[ $(sudo docker volume ls | grep -c cbon_configs) -eq 0 ]]
then
sudo docker volume create cbon_configs
fi
#CB_DOCKER_VOL_CMDLINE=" -v $CB_BASE_DIR/configs:/home/$CB_DOCKER_USER/${CB_EXECUTABLE_NAME}/configs:rw "
CB_DOCKER_VOL_CMDLINE=" -v cbon_configs:/home/$CB_DOCKER_USER/${CB_EXECUTABLE_NAME}/configs "
fi
if [[ $CB_PRIVATE_DATA -eq 0 ]]
then
if [[ $(sudo docker volume ls | grep -c cbon_data) -eq 0 ]]
then
sudo docker volume create cbon_data
fi
#CB_DOCKER_VOL_CMDLINE=${CB_DOCKER_VOL_CMDLINE}" -v $CB_BASE_DIR/data:/home/$CB_DOCKER_USER/${CB_EXECUTABLE_NAME}/data "
CB_DOCKER_VOL_CMDLINE=${CB_DOCKER_VOL_CMDLINE}" -v cbon_data:/home/$CB_DOCKER_USER/${CB_EXECUTABLE_NAME}/data "
fi
if [[ $CB_DOCKER_ADDITIONAL_VOLUMES != "none" ]]
then
for CB_VOL_ENTRY in $(echo $CB_DOCKER_ADDITIONAL_VOLUMES | sed 's/,/ /g')
do
CB_DOCKER_VOL_CMDLINE=${CB_DOCKER_VOL_CMDLINE}" -v "$(echo $CB_VOL_ENTRY | sed "s^~^$HOME^g")" "
done
fi
if [[ $CB_MANAGER_IP != "auto" ]]
then
CB_DOCKER_ENV_CMDLINE=$CB_DOCKER_ENV_CMDLINE" -e CB_MANAGER_IP=${CB_MANAGER_IP} "
fi
CB_DOCKER_ENV_CMDLINE=$CB_DOCKER_ENV_CMDLINE" -e CB_API_DEFAULTS_PORT=${CB_API_DEFAULTS_PORT} -e CB_GUI_DEFAULTS_PORT=${CB_GUI_DEFAULTS_PORT} "
CB_DOCKER_HOST_CMDLINE=''
if [[ $CB_DOCKER_HOST_ENTRY != "none" ]]
then
CB_DOCKER_HOST_CMDLINE=" --add-host $CB_DOCKER_HOST_ENTRY"
fi
CB_DOCKER_ENV_CMDLINE=$CB_DOCKER_ENV_CMDLINE" -e CB_PRIVATE_CONFIG=$CB_PRIVATE_CONFIG -e CB_PRIVATE_DATA=$CB_PRIVATE_DATA "
CB_STARTUP_CLOUD=${CB_STARTUP_CLOUD:-MYSIM}
if [[ $CB_STARTUP_CLOUD != "NONE" ]]
then
CB_DOCKER_ENV_CMDLINE=$CB_DOCKER_ENV_CMDLINE" -e CB_STARTUP_CLOUD=$CB_STARTUP_CLOUD"
CB_CFGVAR_CLOUD_LIST=$(echo $CB_STARTUP_CLOUD | sed 's/MY//g')
fi
for store in OBJECTSTORE LOGSTORE METRICSTORE FILESTORE
do
for param in HOST PORT USAGE DBIB VERBOSITY DATABASE
do
cb_env_var_name=CB_${store}_${param}
cb_env_var_value=$(echo ${!cb_env_var_name})
if [[ ! -z ${!cb_env_var_name} ]]
then
CB_DOCKER_ENV_CMDLINE=$CB_DOCKER_ENV_CMDLINE" -e ${cb_env_var_name}=${!cb_env_var_name} "
fi
done
done
for cmodel in $CB_CFGVAR_CLOUD_LIST
do
for param in ACCESS CREDENTIALS SECURITY_GROUPS INITIAL_VMCS SSH_KEY_NAME NETNAME POOLNAME NETNAME LOGIN
do
cb_env_var_name=CB_${cmodel}_${param}
cb_env_var_value=$(echo ${!cb_env_var_name})
if [[ ! -z ${!cb_env_var_name} ]]
then
CB_DOCKER_ENV_CMDLINE=$CB_DOCKER_ENV_CMDLINE" -e ${cb_env_var_name}=${!cb_env_var_name} "
if [[ $param == "ACCESS" || $param == "CREDENTIALS" ]]
then
cbcf=${!cb_env_var_name}
eval cbcf=${cbcf}
cbcf=$(echo $cbcf | cut -d ':' -f 2)
if [[ -f $cbcf ]]
then
CB_DOCKER_VOL_CMDLINE=${CB_DOCKER_VOL_CMDLINE}" -v ${cbcf}:${cbcf} "
fi
fi
fi
done
done
for obj in MON_DEFAULTS VMC_DEFAULTS VM_DEFAULTS AI_DEFAULTS AIDRS_DEFAULTS
do
for cmodel in EMPTY $CB_CFGVAR_CLOUD_LIST
do
section_exists=0
for param in EMPTY REMOTE_DIR_NAME RUN_NETNAME PROV_NETNAME SECURITY_GROUPS BOOT_VOLUME_SIZE EXECUTE_SCRIPT_NAME CHECK_BOOT_STARTED CHECK_BOOT_COMPLETE TRANSFER_FILES CHECK_SSH RUN_GENERIC_SCRIPTS RUN_APPLICATION_SCRIPTS UPDATE_FREQUENCY DONT_START_LOAD_MANAGER LEAVE_INSTANCE_ON_FAILURE PORTS_BASE USE_VPN_IP USE_FLOATING_IP FLOATING_POOL EXPLICIT_VOLUME_CREATION USE_JUMPHOST JUMPHOST_IP JUMPHOST_LOGIN COLLECT_FROM_GUEST PORT VPC_NAME RESOURCE_GROUP_ID RESOURCE_GROUP_NAME ZONE IMAGE_PREFIX IMAGE_SUFFIX IMAGE_PULL_SECRETS ARCH EMULATOR FILE_IDENTIFIER INITIAL_HOSTS DISCOVER_HOSTS USE_CINDERCLIENT
do
cb_env_var_name=CB_${obj}_${cmodel}_${param}
cb_env_var_name=$(echo $cb_env_var_name | sed 's/_EMPTY//g')
if [[ ! -z ${!cb_env_var_name} ]]
then
CB_DOCKER_ENV_CMDLINE=$CB_DOCKER_ENV_CMDLINE" -e ${cb_env_var_name}=${!cb_env_var_name} "
fi
done
done
done
if [[ $CB_DOCKER_ADDITIONAL_ENVARS != "none" ]]
then
for CB_ENVAR_ENTRY in $(echo $CB_DOCKER_ADDITIONAL_ENVARS | sed 's/,/ /g')
do
CB_DOCKER_ENV_CMDLINE=${CB_DOCKER_ENV_CMDLINE}" -e "$(echo $CB_ENVAR_ENTRY | sed "s^~^$HOME^g" | sed "s/comma/,/g")" "
done
fi
if [[ ! -z $CB_CLOUD_RENAME ]]
then
CB_DOCKER_ENV_CMDLINE=$CB_DOCKER_ENV_CMDLINE" -e CB_CLOUD_RENAME=${CB_CLOUD_RENAME} "
if [[ $CB_CLOUD_RENAME == "AUTO" ]]
then
CB_DOCKER_VOL_CMDLINE=${CB_DOCKER_VOL_CMDLINE}" -v $(whoami)gucn:/home/$CB_DOCKER_USER/.gucn "
fi
fi
if [[ $CB_CREATE_ONLY -eq 1 ]]
then
CB_DOCKER_ENV_CMDLINE=$CB_DOCKER_ENV_CMDLINE" -e CB_VM_DEFAULTS_CHECK_BOOT_COMPLETE=wait_for_0 -e CB_VM_DEFAULTS_TRANSFER_FILES=false -e CB_VM_DEFAULTS_RUN_GENERIC_SCRIPTS=false -e CB_VM_DEFAULTS_CHECK_SSH=false -e CB_VM_DEFAULTS_UPDATE_FREQUENCY=1 -e CB_AI_DEFAULTS_RUN_APPLICATION_SCRIPTS=false -e CB_AI_DEFAULTS_DONT_START_LOAD_MANAGER=true"
fi
if [[ $CB_DOCKER_REPO == "local" ]]
then
CB_DOCKER_IMAGE_CMDLINE=${CB_DOCKER_IMAGE}":"${CB_DOCKER_TAG}
else
CB_DOCKER_IMAGE_CMDLINE=" "${CB_DOCKER_REPO}${CB_DOCKER_IMAGE}":"${CB_DOCKER_TAG}
fi
CB_DOCKER_EXE_CMDLINE=" bash -c \"$CB_DOCKER_COMMAND\""
CB_DOCKER_AUX_CMDLINE=${CB_DOCKER_BASE_CMDLINE}${CB_DOCKER_CMDLINE_NAME}${CB_DOCKER_SEC_CMDLINE}${CB_DOCKER_NET_CMDLINE}${CB_DOCKER_VOL_CMDLINE}${CB_DOCKER_HOST_CMDLINE}${CB_DOCKER_ENV_CMDLINE}${CB_DOCKER_EXTRA_OPTS}${CB_DOCKER_IMAGE_CMDLINE}
CB_DOCKER_CMDLINE=${CB_DOCKER_BASE_CMDLINE}${CB_DOCKER_CMDLINE_NAME}${CB_DOCKER_SEC_CMDLINE}${CB_DOCKER_NET_CMDLINE}${CB_DOCKER_VOL_CMDLINE}${CB_DOCKER_HOST_CMDLINE}${CB_DOCKER_ENV_CMDLINE}${CB_DOCKER_EXTRA_OPTS}${CB_DOCKER_IMAGE_CMDLINE}${CB_DOCKER_EXE_CMDLINE}
echo "Creating docker with the command: \"docker $CB_DOCKER_CMDLINE\""
if [[ $CB_DRY_RUN -eq 0 ]]
then
sudo docker $CB_DOCKER_AUX_CMDLINE bash -c "$CB_DOCKER_COMMAND"
fi
fi
if [[ $CB_KILL_DOCKER -eq 0 ]]
then
if [[ $CB_DRY_RUN -eq 0 ]]
then
sudo docker exec -it $CB_DOCKER_NAME bash -c "$CB_DOCKER_COMMAND"
fi
fi
exit $?