-
Notifications
You must be signed in to change notification settings - Fork 3
/
dk.sh
executable file
·569 lines (479 loc) · 15.9 KB
/
dk.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#!/bin/bash
# //////////////////////////////////////////////////////////////////////////// #
# // ARGS PARSE ////////////////////////////////////////////////////////////// #
# //////////////////////////////////////////////////////////////////////////// #
SCRIPTNAME=$(basename "$0")
SCRIPT_DIR=$(dirname "$0")
# LOG generic
log() {
>&2 echo " |$_err $1"
}
# LOG INFO
log_info() {
>&2 echo "[INFO] $1"
}
# LOG ERROR
log_error() {
>&2 echo "[ERROR] $1"
}
print_help() {
cat << EOF
Usage: $SCRIPTNAME [options] [commands]
options
-------
-h|--help) get this help
-v|--verbose) show script source script
-c ) reserved for Makefile operations
EOF
}
# assertion check
assert () {
E_PARAM_ERR=98
E_ASSERT_FAILED=99
if [ -z "$1" ]; then
return $E_PARAM_ERR
fi
if [ ! $1 ]; then
echo "Assertion failed: \"$1\""
echo "File \"$0\", line ${2:-$1}"
exit $E_ASSERT_FAILED
fi
}
## parse cmd parameters:
while [[ "$1" == -* ]] ; do
case "$1" in
-h|--help)
print_help
exit
;;
-c) # this argument is reserved for shell execution (by Makefile)
CMD=docker_shell
shift
break
;;
# -i)
# CMD=init_container
# shift
# break
# ;;
-v|--verbose)
set -x
echo "VEROBOSE dk command: $@"
shift
;;
--)
shift
break
;;
*)
break
;;
esac
done
if [ $# -lt 1 ] ; then
echo "Incorrect parameters. Use --help for usage instructions."
exit 1
fi
# if no DOCKER_CONTAINER revert to normal shell
# (this is needed for shell command within make for example,
# but those command are not executed in container though )
[ ${DOCKER_CONTAINER} ] || { ${SHELL:-/bin/sh} -c $@; exit; }
## ////////////////////////////////////////////////////////////////////////////////
## // VARIABLES /////////////////////////////////////////////////////////////////
## ////////////////////////////////////////////////////////////////////////////////
# return a unique name containing the md5sum of the current absolute path
get_md5_container() {
_ans="$1_$(echo $(pwd) | md5sum | awk '{print $1}')"
[ -n "$2" ] && eval $2="$_ans"
}
DOCKER_BIN=${DOCKER_BIN:-docker}
DOCKER_SCRIPTPATH=${DOCKER_SCRIPTPATH:-.docker-build}
# append md5 to docker container
DOCKER_CONTAINER_PREFIX=${DOCKER_CONTAINER}
DOCKER_CONTAINER=$(get_md5_container ${DOCKER_CONTAINER}; echo $_ans)
DOCKER_ENTRYPOINT=${DOCKER_ENTRYPOINT:-/bin/sh}
DOCKER_SHELL=${DOCKER_SHELL:-/bin/sh}
# CONSTRUCTED VARIABLES
# interactive console ( if we are running an interactive console set INT variable )
[ -t 7 -o -t 0 ] && INT=-ti || unset INT
user_entry=$(awk -F: "{if (\$1 == "${USER}") {print \$0} }" /etc/passwd)
user_id=$(id -u)
user_group=$(id -g)
user_home=${HOME}
## ..######...#######..##....##.########....###....####.##....##.########.########.
## .##....##.##.....##.###...##....##......##.##....##..###...##.##.......##.....##
## .##.......##.....##.####..##....##.....##...##...##..####..##.##.......##.....##
## .##.......##.....##.##.##.##....##....##.....##..##..##.##.##.######...########.
## .##.......##.....##.##..####....##....#########..##..##..####.##.......##...##..
## .##....##.##.....##.##...###....##....##.....##..##..##...###.##.......##....##.
## ..######...#######..##....##....##....##.....##.####.##....##.########.##.....##
write_config() {
file_name=${DOCKER_SCRIPTPATH}/${DOCKER_CONTAINER_PREFIX}.sh
log "Writing script file in ${file_name}"
[ -d ${DOCKER_SCRIPTPATH} ] || mkdir -p ${DOCKER_SCRIPTPATH}
cat <<- _EOF_ > ${file_name}
# Docker container config
# created: $(date)
DOCKER_CONTAINER=${DOCKER_CONTAINER}
DOCKER_CONTAINER_ID=$(dk_get_container_id ${DOCKER_CONTAINER}; echo $_ans)
DOCKER_IMAGE=${DOCKER_IMAGE}
DOCKER_URL=${DOCKER_URL}
DOCKER_DOCKERFILE=${DOCKER_DOCKERFILE}
DOCKER_IMAGE_ID=$(dk_get_image_id ${DOCKER_IMAGE}; echo $_ans)
DOCKER_NETWORKS="${DOCKER_NETWORKS}"
DOCKER_PORTS="${DOCKER_PORTS}"
DOCKER_SHARES="${DOCKER_SHARES}"
DOCKER_MOUNTS="${DOCKER_MOUNTS}"
DOCKER_DEVICES="${DOCKER_DEVICES}"
: \${DOCKER_MACHINE=${DOCKER_MACHINE}}
: \${USER=${USER}}
user_id=${user_id}
user_group=${user_group}
user_home=${user_home}
_EOF_
}
read_config() {
file_name=${DOCKER_SCRIPTPATH}/${DOCKER_CONTAINER_PREFIX}.sh
[ -f ${file_name} ] && source ${file_name}
}
# dk_test_status [cnt_name] [status]
# status = created, restarting, running, paused, exited
dk_test_status() {
cnt_id=$(docker ps -a -f name=$1 -q)
cnt_st=$(docker ps -a -f name=$1 -f status=$2 -q)
[ x"${cnt_id}" = x"${cnt_st}" ] && return 0 || return 1
}
# dk_get_status [cnt_name] [ans]
# return readable status of container either in _ans or $2
dk_get_status() {
_ans="unhandled"
$(dk_test_status $1 created) && _ans="created"
$(dk_test_status $1 running) && _ans="running"
$(dk_test_status $1 restarting) && _ans="restarting"
$(dk_test_status $1 paused) && _ans="paused"
$(dk_test_status $1 exited) && _ans="exited"
[ "$2" ] && eval $2="$_ans"
}
# dk_get_image_id [image] [ans]
# return image id from name either in _ans or $2
dk_get_image_id() {
_ans=$(docker images -a | awk -v _img=$1 '{if ($1 ":" $2 == _img) {print $3}}' )
[ $2 ] && eval $2="$_ans"
}
# dk_get_container_id [cnt_name] [ans]
# return container id from name either in _ans or $2
dk_get_container_id() {
unset _ans
[ $1 ] && _ans=$(docker ps -a -f name=$1 -q) #|| { log_error "container argument missing"; exit 1; }
[ $_ans ] || _ans=$(docker ps -a -f id=$1 -q)
[ $2 ] && eval $2="$_ans"
}
# dk_get_container_image [cnt_name] [ans]
# return container image
dk_get_container_image() {
_ans=$(docker inspect --format='{{.Config.Image}}' $1)
[ $2 ] && eval $2="$_ans"
}
# dk_image_exist [image]
# test if image exists
dk_image_exist() {
_ans=$(docker images -a -q $1 )
[ $_ans ] && return 0 || return 1
}
# build image from URL (either local directory or web page content)
# the image name can be a local name or any overlapping name that will
# overload the that name for the local repository
build() {
[ ${DOCKER_IMAGE} ] || DOCKER_IMAGE="${DOCKER_CONTAINER}_build"
if [ ${DOCKER_DOCKERFILE} ]; then
DOCKER_BUILD_ARGS="${DOCKER_BUILD_ARGS} -f ${DOCKER_DOCKERFILE}"
fi
echo docker build ${DOCKER_BUILD_ARGS} -t ${DOCKER_IMAGE} ${DOCKER_URL}
docker build ${DOCKER_BUILD_ARGS} -t ${DOCKER_IMAGE} ${DOCKER_URL}
}
push() {
dk_get_container_image ${DOCKER_CONTAINER}
if [ $_ans ]; then
if [ ${DOCKER_REGISTRY} ]; then
docker tag $_ans ${DOCKER_REGISTRY}/$_ans
docker push ${DOCKER_REGISTRY}/$_ans
else
echo "pushing to: ${DOCKER_IMAGE}"
docker push $_ans
fi
fi
}
execute() {
dk_get_status ${DOCKER_CONTAINER}
[ $_ans = "running" ] || start ${DOCKER_IMAGE}
M_ENV="$(export -p | awk '{printf("%s; ",$0)}')"
# xhost local:andrea > /dev/null
log "Docker: Entering container ${DOCKER_CONTAINER} ";
quoted_args="$(printf " %q" "$@")"
if [ ${MAKESHELL} ]; then
${MAKESHELL} ${quoted_args};
else
docker exec ${INT} --user ${USER} ${DOCKER_CONTAINER} /bin/bash -l -c \
"save_path=\$PATH; $M_ENV \
export PATH=\$save_path; \
cd $(pwd); \
export MAKESHELL=${DOCKER_SHELL}; \
${quoted_args}";
fi
}
adduser() {
local user_id=$(id -u ${USER})
local user_entry=$(awk -F: "{if (\$3 == "${user_id}") {print \$0} }" /etc/passwd);
docker exec ${INT} --user root ${DOCKER_CONTAINER} /bin/bash -l -c \
" id -u ${USER} 2>/dev/null || echo ${user_entry} >> /etc/passwd; "
#
# local group_id=$(id -g ${USER})
# local group_entry=$(awk -F: "{if (\$3 == "${group_id}") {print \$0} }" /etc/group);
# docker exec ${INT} --user root ${DOCKER_CONTAINER} /bin/bash -l -c \
# " group_entry=\$(awk -F: \"{if (\\\$3 == \"${group_id}\") {print \\\$0} }\" /etc/group); \
# [ -n "\${group_entry}" ] || echo ${group_entry} >> /etc/group; \
# "
}
# # create the volume in advance
# $ docker volume create --driver local \
# --opt type=none \
# --opt device=/home/user/test \
# --opt o=bind \
# test_vol
# # create on the fly with --mount
# $ docker run -it --rm \
# --mount type=volume,dst=/container/path,volume-driver=local,volume-opt=type=none,volume-opt=o=bind,volume-opt=device=/home/user/test \
# foo
# START
start() {
if [ -n "${DOCKER_URL}" ]; then
echo "BUILDING |${DOCKER_URL}|"
build
fi
# find if container is is registered
dk_get_container_id ${DOCKER_CONTAINER_ID}
if [ -z "${_ans}" ]; then
if test -n "${DOCKER_SHARES}"; then
for _share in ${DOCKER_SHARES}; do
DOCKER_SHARES_VAR="-v ${_share} ${DOCKER_SHARES_VAR}";
done
fi
if test -n "${DOCKER_NETWORKS}"; then
for _n in ${DOCKER_NETWORKS}; do
DOCKER_NETWORKS_VAR="--network=$_n ${DOCKER_NETWORKS_VAR}";
done
fi
if test -n "${DOCKER_PORTS}"; then
for _n in ${DOCKER_PORTS}; do
DOCKER_PORTS_VAR="-p $_n ${DOCKER_PORTS_VAR}";
done
fi
if test -n "${DOCKER_MOUNTS}"; then
for _n in ${DOCKER_MOUNTS}; do
IFS=':' read -ra _vols <<< "$_n";
assert "${#_vols[@]} -eq 2" $LINENO
mkdir -p ${_vols[0]}
local _src="$(cd ${_vols[0]}; pwd)"
local _dst="${_vols[1]}"
DOCKER_MOUNTS_VAR="--mount dst=$_dst,volume-driver=local,volume-opt=o=bind,volume-opt=device=$_src ${DOCKER_MOUNTS_VAR}";
done
fi
if test -n "${DOCKER_DEVICES}"; then
DOCKER_DEVICES_VAR="--device=${DOCKER_DEVICES}"
fi
log "Starting docker container from image ${1:-${DOCKER_IMAGE}}"
docker run -d ${INT} --entrypoint=${DOCKER_ENTRYPOINT} \
-e USER=${USER} \
-e DISPLAY=${DISPLAY} \
-e LANG=${LANG} \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v /etc/resolv.conf:/etc/resolv.conf \
-v ${abs_srcdir}:${abs_srcdir} \
-v ${user_home}:${user_home} \
-v $(pwd):$(pwd) \
--tmpfs /run --tmpfs /run/lock \
--cap-add=SYS_ADMIN \
--cap-add=NET_ADMIN \
${DOCKER_DEVICES_VAR} \
${DOCKER_SHARES_VAR} \
${DOCKER_MOUNTS_VAR} \
${DOCKER_NETWORKS_VAR} \
${DOCKER_PORTS_VAR} \
${DOCKER_PROFILE_VAR} \
-w $(pwd) \
--name ${DOCKER_CONTAINER} \
${1:-${DOCKER_IMAGE}};
# add user
adduser
# copy dk.sh in container for furter operations
docker cp ${SCRIPT_DIR}/${SCRIPTNAME} ${DOCKER_CONTAINER}:/sbin/dk.sh
write_config
read_config
fi
dk_get_container_id ${DOCKER_CONTAINER}
if [ "$_ans" != "${DOCKER_CONTAINER_ID}" ]; then
log "Error: the container id does not match with existing one"
return 1
fi
dk_get_status ${DOCKER_CONTAINER}
if [ "$_ans" = "paused" ]; then
log "Try to resume docker container from pause"
docker unpause ${DOCKER_CONTAINER}
elif [ "$_ans" = "exited" ]; then
log "Try to resume docker container from exit status"
docker restart ${DOCKER_CONTAINER}
fi
_err=$?
return $_err
}
# // not used anymore as it causes umount busy preventing removal of teh container //
# -v /sys/fs/cgroup:/sys/fs/cgroup:ro
# STOP
stop() {
log "Stopping container ${DOCKER_CONTAINER}"
docker rm -f ${DOCKER_CONTAINER}
}
# CLEAN
clean() {
dk_get_container_id ${DOCKER_CONTAINER_ID}
if [ ${_ans} ]; then
stop
fi
file_name=${DOCKER_SCRIPTPATH}/${DOCKER_CONTAINER_PREFIX}.sh
[ -f ${file_name} ] && rm -f ${file_name} ||:
}
pause() {
log "Pauing container ${DOCKER_CONTAINER}"
docker pause ${DOCKER_CONTAINER}
}
# RESTART
restart() {
log "Restating container ${DOCKER_CONTAINER}"
docker restart ${DOCKER_CONTAINER}
}
shell() {
execute ${DOCKER_SHELL} $@
}
## .##.....##....###.....######..##.....##.####.##....##.########
## .###...###...##.##...##....##.##.....##..##..###...##.##......
## .####.####..##...##..##.......##.....##..##..####..##.##......
## .##.###.##.##.....##.##.......#########..##..##.##.##.######..
## .##.....##.#########.##.......##.....##..##..##..####.##......
## .##.....##.##.....##.##....##.##.....##..##..##...###.##......
## .##.....##.##.....##..######..##.....##.####.##....##.########
# if [ -n ${MACHINE_NAME} ]; then
# ## ENV
# ## NEEDS: abs_top_builddir abs_top_srcdir
# : ${abs_top_srcdir:? "error abs_top_srcdir not defined"}
# : ${abs_top_builddir:? "error abs_top_builddir not defined"}
# : ${DOCKER_MACHINE_SORAGE_PATH=${abs_top_builddir}/conf/.docker}
# clean_dir () {
# cd $1 && pwd
# }
# abs_top_srcdir=$(clean_dir ${abs_top_srcdir}; )
# abs_top_builddir=$(clean_dir ${abs_top_builddir}; )
# fi
machine() {
: ${MACHINE_NAME:? "error no MACHINE_NAME defined"}
docker-machine -s ${DOCKER_MACHINE_SORAGE_PATH} $@
}
machine_ssh() {
: ${MACHINE_NAME:? "error no MACHINE_NAME defined"}
machine ssh ${MACHINE_NAME} $@
}
machine_status() {
: ${MACHINE_NAME:? "error no MACHINE_NAME defined"}
machine ls -f '{{.State}}' --filter name=${MACHINE_NAME}
}
# machine-create: ##@docker_machine create new machine
machine_create() {
: ${MACHINE_NAME:? "error no MACHINE_NAME defined"}
local _driver=${DOCKER_MACHINE_DRIVER:-virtualbox}
local _swarm_token=$(docker swarm join-token worker -q 2>/dev/null)
local _swarm=${_swarm_token:+ --swarm}
local _driver_args="--driver $_driver"
if [ $_driver = "virtualbox" ]; then
local _iso=${DOCKER_MACHINE_ISO}
[ $_iso ] && _driver_args="$_driver_args --virtualbox-boot2docker-url $_iso"
fi
# create storage path
if [ ! -d ${DOCKER_MACHINE_SORAGE_PATH} ]; then
mkdir -p ${DOCKER_MACHINE_SORAGE_PATH};
fi
if [ ! "$(machine_status)" = "Running" ]; then
machine create $_driver_args ${DOCKER_MACHINE_ARGS} $_swarm ${MACHINE_NAME}
fi
}
machine_rm() {
${MACHINE_NAME:? "error no MACHINE_NAME defined"}
machine rm ${MACHINE_NAME}
}
machine_mount() {
: ${MACHINE_NAME:? "error no MACHINE_NAME defined"}
test "$(machine_status)" = "Running" && _machine=${MACHINE_NAME}
: ${_machine:? "any configured machine could be found"}
_ip=$(machine inspect -f '{{.Driver.IPAddress}}' $_machine)
_port=$(machine inspect -f '{{.Driver.SSHPort}}' $_machine)
_user=$(machine inspect -f '{{.Driver.SSHUser}}' $_machine)
_key=$(machine inspect -f '{{.Driver.SSHKeyPath}}' $_machine)
# reverse_mount () {
# ##
# ## linux - how to mount local directory to remote like sshfs? - Super User
# ## https://superuser.com/questions/616182/how-to-mount-local-directory-to-remote-like-sshfs
# ##
# local _local_port="22"
# local _forward_port="10000" # work on this !!
# local _remote_port="xxx"
# local _local_ssh="-p $_forward_port ${USER}@$_local_addr"
# local _remote_ssh="-p $_remote_port ${USER}@$_remote_addr"
# local _sshfs_option="-o NoHostAuthenticationForLocalhost=yes"
# ## options:
# ## -v Verbose
# ## -X X11 forwarding
# ## -t pseudo-terminal for an interactive shell
# ##
# #ssh -X -t $REMOTE_SSH -R $FORWARD_PORT:localhost:$LOCAL_PORT \
# #"source /etc/profile; mkdir -p $REMOTE_DIR; \
# # sshfs $SSHFS_OPTION $LOCAL_SSH:$LOCAL_DIR $REMOTE_DIR; bash; \
# # umount $REMOTE_DIR; rm -r $REMOTE_DIR"
# machine-ssh tce-load -w -i sshfs-fuse
# machine-ssh mkdir -p $1;
# sshfs $_sshfs_option
# # groupadd -g ${user_group} ${USER} 2>/dev/null; \
# # useradd -d ${user_home} -u ${user_id} -g ${user_group} ${USER} 2>/dev/null; \
# }
# mount() { sshfs -d $1 $_user@$_ip:/$1; }
# mount ${abs_top_srcdir}
# mount ${abs_top_builddir}
echo "This wont work unless reverse sshfs is performed"
}
machine_ls() {
: ${MACHINE_NAME:? "error no MACHINE_NAME defined"}
machine ls
}
machine_init() {
: ${MACHINE_NAME:? "error no MACHINE_NAME defined"}
# set -e
machine_create && eval $(machine env ${DOCKER_MACHINE})
}
## ////////////////////////////////////////////////////////////////////////////////
## // MAIN //////////////////////////////////////////////////////////////////////
## ////////////////////////////////////////////////////////////////////////////////
# ALWAYS READ CONFIGURATION BACK (IF EXISTS)
read_config
# MAIN [TO FIX]
# if [ x$CMD = x"shell" ]; then
# execute ${DOCKER_SHELL} -c "$@"
# else
# $@
# fi
case ${CMD} in
docker_shell)
# start machine env if exists
[ ${DOCKER_MACHINE} ] && machine_init
execute ${DOCKER_SHELL} -c "$@"
;;
*)
$@
;;
esac