forked from ibmcb/cbtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcbdocker
executable file
·366 lines (334 loc) · 11.7 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
#!/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:-cloudbench-docker-local.artifactory.swg-devops.com}
CB_DOCKER_IMAGE=${CB_DOCKER_IMAGE:-ubuntu_cb_orchestrator}
CB_DOCKER_TAG=${CB_DOCKER_TAG:-experimental}
CB_DOCKER_DEBUG=0
CB_KILL_DOCKER=0
CB_PRIVATE_CONFIG=0
CB_PRIVATE_DATA=0
CB_DRY_RUN=0
CB_DOCKER_COMMAND="auto"
CB_DOCKER_EXTRA_OPTS=${CB_DOCKER_EXTRA_OPTS:-""}
CB_DOCKER_IN_CLI=${CB_DOCKER_IN_CLI:-1}
CB_EXECUTABLE_PARMS=" "
CB_DOCKER_ADDITIONAL_VOLUME="none"
CB_DOCKER_HOST_ENTRY="none"
CB_OBJECTSTORE_PORT=${CB_OBJECTSTORE_PORT:-30000}
CB_LOGSTORE_PORT=${CB_LOGSTORE_PORT:-30001}
CB_METRICSTORE_PORT=${CB_METRICSTORE_PORT:-30002}
CB_FILESTORE_PORT=${CB_FILESTORE_PORT:-30003}
CB_API_DEFAULTS_PORT=${CB_API_DEFAULTS_PORT:-30004}
CB_GUI_DEFAULTS_PORT=${CB_GUI_DEFAULTS_PORT:-30005}
CB_VPN_SERVER_PORT=${CB_VPN_SERVER_PORT:-30006}
CB_USAGE="Usage: $0 [-r/--repo <repository>] [-i/--image image name] [-b/--branch branch] [-c/--config config] [-e/--execute command] [-v/--volume] [-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]"
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|--volume)
CB_DOCKER_ADDITIONAL_VOLUME="$2"
shift
;;
-v=*|--volume=*)
CB_DOCKER_ADDITIONAL_VOLUME=$(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
;;
-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
;;
-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/cbtool/private_configs/build_cloud_config_file_from_env.sh cbuser /home/cbuser/cbtool docker; /home/$CB_DOCKER_USER/cbtool/cb $CB_EXECUTABLE_PARMS"
fi
else
CB_DOCKER_COMMAND="/home/$CB_DOCKER_USER/cbtool/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
if [[ $CB_DOCKER_DEBUG -eq 1 ]]
then
# CB_DOCKER_COMMAND="bash"
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/cbtool/configs:rw "
CB_DOCKER_VOL_CMDLINE=" -v cbon_configs:/home/$CB_DOCKER_USER/cbtool/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/cbtool/data "
CB_DOCKER_VOL_CMDLINE=${CB_DOCKER_VOL_CMDLINE}" -v cbon_data:/home/$CB_DOCKER_USER/cbtool/data "
fi
if [[ $CB_DOCKER_ADDITIONAL_VOLUME != "none" ]]
then
CB_DOCKER_VOL_CMDLINE=${CB_DOCKER_VOL_CMDLINE}" -v $CB_DOCKER_ADDITIONAL_VOLUME "
fi
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=" -e CB_PRIVATE_CONFIG=$CB_PRIVATE_CONFIG -e CB_PRIVATE_DATA=$CB_PRIVATE_DATA "
if [[ ! -z $CB_STARTUP_CLOUD ]]
then
CB_DOCKER_ENV_CMDLINE=$CB_DOCKER_ENV_CMDLINE" -e CB_STARTUP_CLOUD=$CB_STARTUP_CLOUD"
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 SIM OSK EC2 VCD PLM SLR GCE GEN DO PDM PCM KUB AS OS
do
for param in ACCESS CREDENTIALS SECURITY_GROUPS INITIAL_VMCS SSH_KEY_NAME 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 VMC_DEFAULTS VM_DEFAULTS AI_DEFAULTS AIDRS_DEFAULTS
do
for cmodel in EMPTY SIM PLM PDM PCM KUB NOP OSK OS CSK VCD EC2 SLR GCE GEN DO AS
do
section_exists=0
for param in EMPTY REMOTE_DIR_NAME RUN_NETNAME PROV_NETNAME EXECUTE_SCRIPT_NAME CHECK_BOOT_STARTED CHECK_BOOT_COMPLETE LEAVE_INSTANCE_ON_FAILURE PORTS_BASE USE_VPN_IP VPN_ONLY USE_FLOATING_IP FLOATING_POOL USE_JUMPHOST
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 [[ ! -z $CB_CLOUD_RENAME ]]
then
CB_DOCKER_ENV_CMDLINE=$CB_DOCKER_ENV_CMDLINE" -e CB_CLOUD_RENAME=${CB_CLOUD_RENAME} "
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 $?