-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathlauncher_funcs.sh
518 lines (447 loc) · 10.9 KB
/
launcher_funcs.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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/bin/sh
# Copyright (c) 2012-2016 Codenvy, S.A., Red Hat, Inc
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
# Mario Loriedo - Initial implementation
#
usage () {
printf "%s" "${USAGE}"
}
info() {
printf "${GREEN}INFO:${NC} %s\n" "${1}"
}
debug() {
printf "${BLUE}DEBUG:${NC} %s\n" "${1}"
}
error() {
printf "${RED}ERROR:${NC} %s\n" "${1}"
}
error_exit() {
echo "---------------------------------------"
error "!!!"
error " ${1}"
error "!!!"
echo "---------------------------------------"
exit 1
}
convert_windows_to_posix() {
# "/some/path" => /some/path
OUTPUT_PATH=${1//\"}
echo "/"$(echo "$OUTPUT_PATH" | sed 's/\\/\//g' | sed 's/://')
}
get_clean_path() {
INPUT_PATH=$1
# \some\path => /some/path
OUTPUT_PATH=$(echo ${INPUT_PATH} | tr '\\' '/')
# /somepath/ => /somepath
OUTPUT_PATH=${OUTPUT_PATH%/}
# /some//path => /some/path
OUTPUT_PATH=$(echo ${OUTPUT_PATH} | tr -s '/')
# "/some/path" => /some/path
OUTPUT_PATH=${OUTPUT_PATH//\"}
echo ${OUTPUT_PATH}
}
get_converted_and_clean_path() {
CONVERTED_PATH=$(convert_windows_to_posix "${1}")
CLEAN_PATH=$(get_clean_path "${CONVERTED_PATH}")
echo $CLEAN_PATH
}
get_che_launcher_container_id() {
hostname
}
get_che_launcher_version() {
if [ -n "${LAUNCHER_IMAGE_VERSION}" ]; then
echo "${LAUNCHER_IMAGE_VERSION}"
else
echo "latest"
fi
}
is_boot2docker() {
if uname -r | grep -q 'boot2docker'; then
return 0
else
return 1
fi
}
has_docker_for_windows_ip() {
if [ "${ETH0_ADDRESS}" = "10.0.75.2" ]; then
return 0
else
return 1
fi
}
is_docker_for_mac() {
if uname -r | grep -q 'moby' && ! has_docker_for_windows_ip; then
return 0
else
return 1
fi
}
is_docker_for_windows() {
if uname -r | grep -q 'moby' && has_docker_for_windows_ip; then
return 0
else
return 1
fi
}
docker_run() {
ENV_FILE=$(get_list_of_che_system_environment_variables)
docker run -d --name "${CHE_SERVER_CONTAINER_NAME}" \
-v /var/run/docker.sock:/var/run/docker.sock:Z \
-v "$CHE_DATA_LOCATION" \
-p "${CHE_PORT}":"${CHE_PORT}" \
--restart="${CHE_RESTART_POLICY}" \
-e "CHE_LOG_LEVEL=${CHE_LOG_LEVEL}" \
-e "CHE_IP=$CHE_HOST_IP" \
--env-file=$ENV_FILE \
"$@"
rm -rf $ENV_FILE > /dev/null
}
get_user_id() {
CHE_USER_UID=$(docker run -t \
-v /etc/passwd:/etc/passwd:ro,Z \
-v /etc/group:/etc/group:ro,Z \
alpine id -u ${CHE_USER})
CHE_USER_GID=$(docker run -t \
-v /etc/passwd:/etc/passwd:ro,Z \
-v /etc/group:/etc/group:ro,Z \
alpine getent group docker | cut -d: -f3)
echo -n "${CHE_USER_UID}" | tr '\r' ':'; echo -n ${CHE_USER_GID}
}
docker_run_with_che_user() {
if [ "${CHE_USER}" != "root" ]; then
docker_run -e CHE_USER=${CHE_USER} \
-v /etc/group:/etc/group:ro,Z \
-v /etc/passwd:/etc/passwd:ro,Z \
--user=$(get_user_id) \
"$@"
else
docker_run --user="${CHE_USER}" "$@"
fi
}
docker_run_if_in_vm() {
# If the container will run inside of a VM, additional parameters must be set.
# Setting CHE_IN_VM=true will have the che-server container set the values.
if is_docker_for_mac || is_docker_for_windows || is_boot2docker; then
docker_run_with_che_user -e "CHE_IN_VM=true" "$@"
else
docker_run_with_che_user "$@"
fi
}
docker_run_with_assembly() {
if has_assembly; then
docker_run_if_in_vm -v "$CHE_ASSEMBLY_LOCATION" -e "CHE_ASSEMBLY=${CHE_ASSEMBLY}" "$@"
else
docker_run_if_in_vm "$@"
fi
}
docker_run_with_conf() {
if has_che_conf_path; then
docker_run_with_assembly -v "$CHE_CONF_LOCATION" -e "CHE_LOCAL_CONF_DIR=${CHE_CONF}" "$@"
else
docker_run_with_assembly "$@"
fi
}
docker_run_with_external_hostname() {
if has_external_hostname; then
docker_run_with_conf -e "CHE_DOCKER_MACHINE_HOST_EXTERNAL=${CHE_DOCKER_MACHINE_HOST_EXTERNAL}" "$@"
else
docker_run_with_conf "$@"
fi
}
docker_run_with_debug() {
if has_debug && has_debug_suspend; then
docker_run_with_external_hostname -p "${CHE_DEBUG_SERVER_PORT}":8000 \
-e "CHE_DEBUG_SERVER=true" \
-e "JPDA_SUSPEND=y" "$@"
elif has_debug; then
docker_run_with_external_hostname -p "${CHE_DEBUG_SERVER_PORT}":8000 \
-e "CHE_DEBUG_SERVER=true" "$@"
else
docker_run_with_external_hostname "$@"
fi
}
has_debug_suspend() {
if [ "${CHE_DEBUG_SERVER_SUSPEND}" = "false" ]; then
return 1
else
return 0
fi
}
has_debug() {
if [ "${CHE_DEBUG_SERVER}" = "false" ]; then
return 1
else
return 0
fi
}
has_che_conf_path() {
if [ "${CHE_CONF}" = "" ]; then
return 1
else
return 0
fi
}
has_assembly() {
if [ "${CHE_ASSEMBLY}" = "" ]; then
return 1
else
return 0
fi
}
has_external_hostname() {
if [ "${CHE_DOCKER_MACHINE_HOST_EXTERNAL}" = "" ]; then
return 1
else
return 0
fi
}
get_list_of_che_system_environment_variables() {
# See: http://stackoverflow.com/questions/4128235/what-is-the-exact-meaning-of-ifs-n
IFS=$'\n'
DOCKER_ENV=$(mktemp)
# First grab all known CHE_ variables
CHE_VARIABLES=$(env | grep CHE_)
for SINGLE_VARIABLE in "${CHE_VARIABLES}"; do
echo "${SINGLE_VARIABLE}" >> $DOCKER_ENV
done
# Add in known proxy variables
if [ ! -z ${http_proxy+x} ]; then
echo "http_proxy=${http_proxy}" >> $DOCKER_ENV
fi
if [ ! -z ${https_proxy+x} ]; then
echo "https_proxy=${https_proxy}" >> $DOCKER_ENV
fi
if [ ! -z ${no_proxy+x} ]; then
echo "no_proxy=${no_proxy}" >> $DOCKER_ENV
fi
echo $DOCKER_ENV
}
get_docker_install_type() {
if is_boot2docker; then
echo "boot2docker"
elif is_docker_for_windows; then
echo "docker4windows"
elif is_docker_for_mac; then
echo "docker4mac"
else
echo "native"
fi
}
get_docker_host_ip() {
case $(get_docker_install_type) in
boot2docker)
echo $ETH1_ADDRESS
;;
native)
echo $DOCKER0_ADDRESS
;;
*)
echo $ETH0_ADDRESS
;;
esac
}
get_docker_host_os() {
docker info | grep "Operating System:" | sed "s/^Operating System: //"
}
get_docker_daemon_version() {
docker version | grep -i "server version:" | sed "s/^server version: //I"
}
get_che_hostname() {
INSTALL_TYPE=$(get_docker_install_type)
if [ "${INSTALL_TYPE}" = "boot2docker" ]; then
echo $DEFAULT_DOCKER_HOST_IP
else
echo "localhost"
fi
}
check_docker() {
if [ ! -S /var/run/docker.sock ]; then
error_exit "Docker socket (/var/run/docker.sock) hasn't be mounted \
inside the container. Verify the syntax of the \"docker run\" command."
fi
if ! docker ps > /dev/null 2>&1; then
output=$(docker ps)
error_exit "Error when running \"docker ps\": ${output}"
fi
}
che_container_exist_by_name() {
docker inspect ${1} > /dev/null 2>&1
if [ "$?" == "0" ]; then
return 0
else
return 1
fi
}
che_container_exist() {
if [ "$(docker ps -aq -f "id=${1}" | wc -l)" -eq 0 ]; then
return 1
else
return 0
fi
}
che_container_is_running() {
if [ "$(docker ps -qa -f "status=running" -f "id=${1}" | wc -l)" -eq 0 ]; then
return 1
else
return 0
fi
}
che_container_is_stopped() {
if [ "$(docker ps -qa -f "status=exited" -f "name=${1}" | wc -l)" -eq 0 ]; then
return 1
else
return 0
fi
}
contains() {
string="$1"
substring="$2"
if test "${string#*$substring}" != "$string"
then
return 0 # $substring is in $string
else
return 1 # $substring is not in $string
fi
}
has_container_debug () {
if $(contains $(get_container_debug $1) "<nil>"); then
return 1
else
return 0
fi
}
get_container_debug() {
CURRENT_CHE_DEBUG=$(docker inspect --format='{{.NetworkSettings.Ports}}' ${1})
IFS=$' '
for SINGLE_BIND in $CURRENT_CHE_DEBUG; do
case $SINGLE_BIND in
*8000/tcp:*)
echo $SINGLE_BIND | cut -f2 -d":"
;;
*)
;;
esac
done
}
get_che_container_host_ip_from_container() {
BINDS=$(docker inspect --format="{{.Config.Env}}" "${1}" | cut -d '[' -f 2 | cut -d ']' -f 1)
IFS=$' '
for SINGLE_BIND in $BINDS; do
case $SINGLE_BIND in
*CHE_IP*)
echo $SINGLE_BIND | cut -f2 -d=
;;
*)
;;
esac
done
}
get_che_container_host_bind_folder() {
BINDS=$(docker inspect --format="{{.HostConfig.Binds}}" "${2}" | cut -d '[' -f 2 | cut -d ']' -f 1)
IFS=$' '
for SINGLE_BIND in $BINDS; do
case $SINGLE_BIND in
*$1*)
echo $SINGLE_BIND | cut -f1 -d":"
;;
*)
;;
esac
done
}
get_che_container_conf_folder() {
FOLDER=$(get_che_container_host_bind_folder "/conf:Z" $1)
echo "${FOLDER:=not set}"
}
get_che_container_data_folder() {
FOLDER=$(get_che_container_host_bind_folder "/home/user/che/workspaces:Z" $1)
echo "${FOLDER:=not set}"
}
get_che_container_image_name() {
docker inspect --format="{{.Config.Image}}" "${1}"
}
get_che_server_container_id() {
docker inspect -f '{{.Id}}' ${1}
}
get_docker_external_hostname() {
if is_docker_for_mac || is_docker_for_windows; then
echo "localhost"
else
echo ""
fi
}
wait_until_container_is_running() {
CONTAINER_START_TIMEOUT=${1}
ELAPSED=0
until che_container_is_running ${2} || [ ${ELAPSED} -eq "${CONTAINER_START_TIMEOUT}" ]; do
sleep 1
ELAPSED=$((ELAPSED+1))
done
}
wait_until_container_is_stopped() {
CONTAINER_STOP_TIMEOUT=${1}
ELAPSED=0
until che_container_is_stopped ${2} || [ ${ELAPSED} -eq "${CONTAINER_STOP_TIMEOUT}" ]; do
sleep 1
ELAPSED=$((ELAPSED+1))
done
}
server_is_booted() {
HTTP_STATUS_CODE=$(curl -I http://$(docker inspect -f '{{.NetworkSettings.IPAddress}}' "${1}"):$CHE_PORT/api/ \
-s -o /dev/null --write-out "%{http_code}")
if [ "${HTTP_STATUS_CODE}" = "200" ]; then
return 0
else
return 1
fi
}
get_server_version() {
HTTP_STATUS_CODE=$(curl -X OPTIONS http://$(docker inspect -f '{{.NetworkSettings.IPAddress}}' \
"${1}"):$CHE_PORT/api/ -s)
FIRST=${HTTP_STATUS_CODE//\ /}
IFS=','
for SINGLE_BIND in $FIRST; do
case $SINGLE_BIND in
*implementationVersion*)
echo ${SINGLE_BIND//\"} | cut -f2 -d":" | cut -f1 -d"}"
;;
*)
;;
esac
done
}
wait_until_server_is_booted () {
SERVER_BOOT_TIMEOUT=${1}
ELAPSED=0
until server_is_booted ${2} || [ ${ELAPSED} -eq "${SERVER_BOOT_TIMEOUT}" ]; do
sleep 1
ELAPSED=$((ELAPSED+1))
done
}
execute_command_with_progress() {
progress=$1
command=$2
shift 2
pid=""
case "$progress" in
extended)
$command "$@"
;;
basic|*)
$command "$@" &>/dev/null &
pid=$!
while kill -0 "$pid" >/dev/null 2>&1; do
printf "#"
sleep 10
done
wait $pid # return pid's exit code
printf "\n"
;;
esac
printf "\n"
}