-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove_container.sh
executable file
·685 lines (566 loc) · 24.4 KB
/
move_container.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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
#!/bin/bash
############################################################
# TODO (in order of priority) #
############################################################
# - in case of multiple containers, create resources all at once (i.e.: network may be the same, you don't want to delete and recreate each time)
# - pv estimations are MOSTLY correct (generally within +/- 3%, more if volumes are very small)
# - improve logging: replace echo with log function and add verbosity levels
# - add an option to copy all the containers part of a network
# - duplicate containers locally adding a suffix
# - check if published ports are available before initate the copy
# - implement autoselect compression algorthim
############################################################
# WON'T DO #
############################################################
# - delete container and volumes on local side -> Too risky
# - use a different name for the container on remote side -> Avoid mistakes
############################################################
# NOTES #
############################################################
# Sincere apologies if I wrote this in bash. When I started, it was supposed to be a short script!
############################################################
# License #
############################################################
function license() {
read -r -d '' license <<-EOF
MIT License
Copyright (c) 2021 Michele Porelli
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
EOF
echo "${license}"
}
############################################################
# Help #
############################################################
function usage() {
read -r -d '' usage <<-EOF
Usage: $(basename ${0})
Description: helps moving stateful containers between docker contexts (i.e.: from local to remote host). Be sure you created valid contexts before using this script!
Options: [ -a | --all_data ]
[ -c | --compress (bzip2|gzip|none) ]
[ -h | --help ]
[ -k | --keep_running ]
[ -l | --license ]
[ -o | --override_origin DOCKER_CONTEXT ]
[ -u | --unattended (d|s|a) ]
[ -v | --verbose ]
(pull|push)
CONTAINER_NAMEs ...
DOCKER_REMOTE_CONTEXT
Reference: to create a new docker context check here: https://docs.docker.com/engine/reference/commandline/context_create/
example: $ docker context create --docker host=ssh://foo@bar foobar
-a | --all_data
Description: Preserve current image changes in your running container. This will copy the current image, the transfer could take much longer
Default: not enabled
-c | --compress OPTION
Description: Use compression with the selected algorithm
Default: bzip2
Supported options: $(join_array_by ", " ${COMPRESS_ALGORITHM_ARGS})
-h | --help
Description: Print this help message
-k | --keep_running
Description: Keep local container running. If not set, the container will be stopped before starting the copying process and won't be started again on the local host
Default: not enabled
-l | --license
Description: Print MIT license
-o | --override_origin DOCKER_CONTEXT
Description: This script assumes Docker you want to move containers between what is defined in your current ENV and an alternative context. With this option you can override the the DOCKER_CONTEXT.
Default: not enabled
-u | --unattended OPTION
Description: Auto-answer questions with the selected option for the questions: [D]elete and continue, [S]kip, [A]bort
Default: not enabled
Supported options: $(join_array_by ", " "${UNATTENDED_OPTION_ARGS[@]}")
-v | --verbose
Description: Enable verbose output. Error will be printed in any case
Default: not enabled
EOF
echo "${usage}"
}
############################################################
# Process the input options. Add options as needed. #
############################################################
function parse_options() {
PARSED_ARGUMENTS=$(getopt -a -o ahlkvc:u:o: --long all_data,help,license,keep_running,verbose,compress:,unattended:,override_origin: -- "${@}")
VALID_ARGUMENTS=${?}
if [ "${VALID_ARGUMENTS}" != "0" ]; then
usage
log "" 2 1
fi
# echo "PARSED_ARGUMENTS is ${PARSED_ARGUMENTS}"
eval set -- "${PARSED_ARGUMENTS}"
while :
do
case "${1}" in
-a | --all_data) PRESERVE_IMAGE_STATE=1 ; shift ;;
-h | --help) HELP=1 ; shift ;;
-l | --license) LICENSE=1 ; shift ;;
-c | --compress) COMPRESS_ALGORITHM="${2}" ; shift 2 ;;
-k | --keep_running) KEEP_RUNNING=1 ; shift ;;
-u | --unattended) UNATTENDED_OPTION="${2}" ; shift 2 ;;
-v | --verbose) VERBOSE="/dev/stdout" ; shift ;;
-o | --override_origin) ORIGIN_DOCKER="${2}" ; shift ;;
# -- means the end of the arguments; drop this, and break out of the while loop
--) shift ; break ;;
# If invalid options were passed, then getopt should have reported an error,
# which we checked as VALID_ARGUMENTS when getopt was called...
*) log "Unexpected option: ${1} - this should not happen. Did you forget to update the select case?" 99 ;;
esac
done
if [[ -n "${HELP}" ]]; then
log "$(usage)" 0 1
fi
if [[ -n "${LICENSE}" ]]; then
log "$(license)" 0 1
fi
if [ ${#} -lt 3 ]; then
log "$(usage)" 2 1
else
ACTION=${1} ; shift
CONTAINERS=${*%${!#}} # containers names
REMOTE_DOCKER=${@:$#} # docker context
validate_arguments
set_compression_algorithm "${COMPRESS_ALGORITHM}"
[[ -n ${PRESERVE_IMAGE_STATE} ]] && COMMIT=$(openssl rand -hex 12)
case ${ACTION} in
'pull')
DOCKER_ORIGIN=${REMOTE_DOCKER}
DOCKER_DESTINATION=${ORIGIN_DOCKER} ;;
'push')
DOCKER_ORIGIN=${ORIGIN_DOCKER}
DOCKER_DESTINATION=${REMOTE_DOCKER} ;;
*) log "Unsupported action!" 99 ;;
esac
[ "$(retrieve_docker_context "origin")" == "default" ] && [ "$(retrieve_docker_context "destination")" == "default" ] && log "FATAL: both contexts are set to default!"
fi
}
function validate_arguments() {
# validate parameters
[[ ! " ${COMPRESS_ALGORITHM_ARGS[*]} " =~ " ${COMPRESS_ALGORITHM} " ]] && log "-c | --compress specified option is invalid: ${COMPRESS_ALGORITHM}. Valid options: $(join_array_by ", " "${COMPRESS_ALGORITHM_ARGS[@]}")" 1
[[ ! " ${ACTION_ARGS[*]} " =~ " ${ACTION} " ]] && log "specified action is invalid. Valid options: $(join_array_by ", " ${ACTION_ARGS[@]})" 1
[[ -n ${UNATTENDED_OPTION} ]] && [[ ! " ${UNATTENDED_OPTION_ARGS[*]} " =~ " ${UNATTENDED_OPTION} " ]] && log "-u | --unattended specified option is invalid: ${UNATTENDED_OPTION}. Valid options: $(join_array_by ", " "${UNATTENDED_OPTION_ARGS[@]}")" 1
# validate contexts
[[ -n ${ORIGIN_DOCKER} ]] && check_docker_context "${ORIGIN_DOCKER}"
[[ -n ${REMOTE_DOCKER} ]] && check_docker_context "${REMOTE_DOCKER}"
[[ "${REMOTE_DOCKER}" == "${ORIGIN_DOCKER}" ]] && log "origin and destination cannot be the same." 1
}
function join_array_by { d=${1-} f=${2-}; if shift 2; then printf %s "$f" "${@/#/$d}"; fi; }
function check_dependencies() {
if [[ "${OSTYPE}" == "linux-gnu"* ]]; then
commands=(docker pv getopt)
commands_instructions=(
"docker is not available, please ensure it is installed and in your PATH"
"pv is not available, please ensure it is installed and in your PATH"
"getopt is not available, please ensure it is installed and in your PATH"
)
elif [[ "${OSTYPE}" == "darwin"* ]]; then
commands=(docker pv /usr/local/opt/gnu-getopt/bin/getopt)
commands_instructions=(
"docker is not available, please ensure it is installed and in your PATH"
"pv is not available, please install it with 'brew install pv'"
"gnu-getopt is not available, please install it with 'brew install gnu-getopt'"
)
# adding path for gnu-getopt
export PATH="/usr/local/opt/gnu-getopt/bin:${PATH}"
else
echo -e "\n"'WARNING: You are running the script on an unsupported OS, use at your own risk!'"\n"'Waiting 5 seconds before continuing...'"\n"
sleep 5
return 1
fi
error_flag=0
for (( i=0; i<${#commands[@]}; i++ ));
do
if ! command -v ${commands[${i}]} >/dev/null 2>&1 ; then
error_flag=1
log ${commands_instructions[${i}]}
fi
done
[ ${error_flag} -eq 1 ] && goodbye
# ! [ "${BASH_VERSINFO:-0}" -ge 4 ] && log "This script needs at least Bash 4, please upgrade. You can install using your package manager or run the script from a different host" 1
}
function set_compression_algorithm() {
compress_algorithm=${1}
case ${compress_algorithm} in
'bzip2')
COMPRESS_BINARY=bzip2
DECOMPRESS_TAR_STRING=xpjf ;;
'gzip')
COMPRESS_BINARY=gzip
DECOMPRESS_TAR_STRING=xpzf ;;
'none')
COMPRESS_BINARY=cat
DECOMPRESS_TAR_STRING=xpf ;;
*) log "Unsupported algorithm!" 99 ;;
esac
}
function goodbye() {
suppress_message=${1}
[[ ! -n "${suppress_message}" ]] && echo "Goodbye!"
kill -s TERM ${TOP_PID}
}
function does_docker_work() {
docker_actor=${1}
docker_context=$(retrieve_docker_context ${docker_actor})
log " Checking if docker with context ${docker_context} works"
env "$(set_docker_context ${docker_actor})" docker info > /dev/null 2>&1 || log "Couldn't use ${docker_context} context" 1
}
function check_docker_context() {
docker_context=${1}
log " Checking if ${docker_context} exists"
docker context inspect ${docker_context} 1>${VERBOSE}
docker_context_status=${?}
if [ ! ${docker_context_status} -eq 0 ] ; then
log " Context doesn't exist!" 1
else
return 0
fi
}
function retrieve_docker_context() {
docker_actor=${1}
case ${docker_actor} in
'origin')
if [[ -n ${DOCKER_ORIGIN} ]]; then
echo "${DOCKER_ORIGIN}"
else
echo "default"
fi ;;
'destination')
if [[ -n ${DOCKER_DESTINATION} ]]; then
echo "${DOCKER_DESTINATION}"
else
echo "default"
fi ;;
*) log "Unsupported action!" 99 ;;
esac
}
function set_docker_context() {
docker_actor=${1}
case ${docker_actor} in
'origin')
if [[ -n ${DOCKER_ORIGIN} ]]; then
echo "DOCKER_CONTEXT=${DOCKER_ORIGIN}" # setting the local variable just if specified to avoid wrong overrides
else
echo "A=A" # drity hack to make env happy
fi ;;
'destination')
if [[ -n ${DOCKER_DESTINATION} ]]; then
echo "DOCKER_CONTEXT=${DOCKER_DESTINATION}" # setting the local variable just if specified to avoid wrong overrides
else
echo "A=A" # drity hack to make env happy
fi ;;
*) log "Unsupported action!" 99 ;;
esac
}
function copy_volume() {
docker_volume=${1}
log " Evaluating volume size"
docker_volume_size=$(env "$(set_docker_context "origin")" docker run --rm -t -v ${docker_volume}:/volume_data alpine sh -c "apk -q --no-progress update && apk -q --no-progress add coreutils && du -sb /volume_data" | cut -f1 | xargs) || log "Couldn't evaluate volume size" 1
log " Copying content"
env "$(set_docker_context "origin")" docker run --rm -v ${docker_volume}:/from alpine ash -c "cd /from ; tar cf - . " | pv -cN ${docker_volume} -s ${docker_volume_size} | ${COMPRESS_BINARY} | env $(set_docker_context "destination") docker run --rm -i -v ${docker_volume}:/to alpine ash -c "cd /to ; tar -${DECOMPRESS_TAR_STRING} - " || log "Couldn't copy volume content" 1
}
function create_volume() {
docker_actor=${1}
docker_volume=${2}
log " Creating ${docker_volume} volume on the ${docker_actor} host"
env "$(set_docker_context ${docker_actor})" docker volume create ${docker_volume} 1>${VERBOSE} || log "Couldn't create volume on ${docker_actor} host" 1
}
function create_network() {
docker_actor=${1}
docker_network=${2}
log " Creating ${docker_network} network on the ${docker_actor} host"
env "$(set_docker_context ${docker_actor})" docker network create ${docker_network} 1>${VERBOSE} || log "Couldn't create network on ${docker_actor} host" 1
}
function delete_container() {
docker_actor=${1}
# trying to stop the container before deleting, not triggering a failure because the container might be already stopped
log " Stopping ${CONTAINER} container on the ${docker_actor} host"
env "$(set_docker_context ${docker_actor})" docker stop ${CONTAINER} 1>${VERBOSE}
log " Deleting ${CONTAINER} container on the ${docker_actor} host"
env "$(set_docker_context ${docker_actor})" docker rm ${CONTAINER} 1>${VERBOSE} || log "Couldn't delete container on ${docker_actor} host" 1
}
function delete_volume() {
docker_actor=${1}
docker_volume=${2}
log " Deleting ${docker_volume} volume on the ${docker_actor} host"
env "$(set_docker_context ${docker_actor})" docker volume rm ${docker_volume} 1>${VERBOSE} || log "Couldn't delete volume on ${docker_actor} host" 1
}
function delete_network() {
docker_actor=${1}
docker_network=${2}
log " Deleting ${docker_network} network on the ${docker_actor} host"
env "$(set_docker_context ${docker_actor})" docker network rm ${docker_network} 1>${VERBOSE} || log "Couldn't delete network on ${docker_actor} host" 1
}
function check_if_container_exists() {
docker_actor=${1}
log " Checking if ${CONTAINER} container exists on the ${docker_actor} host"
env "$(set_docker_context ${docker_actor})" docker inspect --format="{{.State.Running}}" ${CONTAINER} 1>${VERBOSE} 2>/dev/null
container_result=${?}
if [ ${container_result} -ne 0 ]; then
is_present=0
else
is_present=1
fi
return ${is_present}
}
function check_container_on_host_and_stop() {
docker_actor=${1}
check_if_container_exists "${docker_actor}"
exist=${?}
if [ ${exist} -eq 1 ]; then
log " ${CONTAINER} container already exists on the ${docker_actor} host..."
prompt_options
case ${?} in
1) delete_container "destination";;
2) log " Skipping..." ; return 1 ;;
3) goodbye ;;
esac
fi
# this step needs to be performed before stopping the container to avoid data loss
if [[ -n "${COMMIT}" ]]; then
log " Preserve current image changes requested, preparing a temporary image..."
env $(set_docker_context "origin") docker commit ${CONTAINER} ${COMMIT} >${VERBOSE}
fi
if [[ -n "${KEEP_RUNNING}" ]]; then
log " Leaving container on origin host running..."
else
env $(set_docker_context "origin") docker stop ${CONTAINER} 1>${VERBOSE} || log "Couldn't stop container on ${docker_actor} host" 1
fi
return 0
}
function check_if_network_exists() {
docker_actor=${1}
docker_network=${2}
log " Checking if ${docker_network} network exists on the ${docker_actor} host"
is_present=$(env "$(set_docker_context ${docker_actor})" docker network ls -f "name=${docker_network}" --format "{{.Name}}" | wc -l)
return ${is_present}
}
function check_if_volume_exists() {
docker_actor=${1}
docker_volume=${2}
log " Checking if ${docker_volume} volume exists on the ${docker_actor} host"
is_present=$(env "$(set_docker_context ${docker_actor})" docker volume ls -f "name=${docker_volume}" --format "{{.Name}}" | wc -l)
return ${is_present}
}
function transfer_image() {
temporary_tag=${1}
log " Copying container image: ${temporary_tag}"
env $(set_docker_context "origin") docker save ${temporary_tag} | pv -cN ${temporary_tag} -s $(docker image inspect ${temporary_tag} --format='{{.Size}}') | ${COMPRESS_BINARY} | env $(set_docker_context "destination") docker load
}
function change_image_tag() {
docker_actor=${1}
docker_image=${2}
temporary_tag=${3}
log " Restoring original tag on the ${docker_actor} host"
env $(set_docker_context "destination") docker image tag ${temporary_tag} ${docker_image} 1>${VERBOSE}
}
function remove_temporary_image() {
docker_actor=${1}
docker_image=${2}
log " Removing temporary image on the ${docker_actor} host"
env $(set_docker_context "origin") docker image rm ${docker_image} 1>${VERBOSE}
}
function copy_image() {
docker_image=${1}
if [[ -n "${COMMIT}" ]]; then
log " Preserve current image changes requested..."
transfer_image ${COMMIT}
change_image_tag "destination" ${docker_image} ${COMMIT}
remove_temporary_image "origin" ${COMMIT}
else
log " Checking if ${docker_image} already exists on destination host"
env $(set_docker_context "destination") docker image inspect --format='{{.Config.Image}}' ${docker_image} 1>${VERBOSE}
remote_image_status=${?}
if [ ${remote_image_status} -eq 0 ] ; then
log " Image is available on the destination host!"
else
log " Image doesn't exist on destination host, trying to pull ${docker_image} from upstream"
env $(set_docker_context "destination") docker pull ${docker_image} 1>${VERBOSE}
remote_pull_status=${?}
if [ ${remote_pull_status} -eq 0 ] ; then
log " Image successfully pulled from the registry"
else
log " Failed. Image needs to be pushed from this host to remote."
transfer_image ${docker_image}
fi
fi
fi
}
function log() {
message="${1}"
exit_code="${2}"
suppress_exit_message="${3}"
echo "${message}"
[[ -n "${exit_code}" ]] && goodbye ${suppress_exit_message}
}
function iterate_networks() {
networks=${1}
for docker_network in $(echo ${networks})
do
if [ ${docker_network} == "bridge" ]; then
log " Skipping bridge network..."
else
check_if_network_exists "destination" ${docker_network}
destination_status=${?}
if [ ${destination_status} -eq 0 ] ; then
create_network "destination" ${docker_network}
else
log " ${docker_network} network exists on the destination host..."
prompt_options
case ${?} in
1) delete_network "destination" ${docker_network}
create_network "destination" ${docker_network} ;;
2) log " Skipping..." ;;
3) goodbye ;;
esac
fi
fi
done
}
function iterate_volumes() {
volumes=${1}
if [[ -n "${volumes}" ]]; then
for docker_volume in $(echo ${volumes})
do
check_if_volume_exists "destination" ${docker_volume}
remote_status=${?}
if [ ${remote_status} -eq 0 ] ; then
create_volume "destination" ${docker_volume}
copy_volume ${docker_volume}
else
prompt_options
case ${?} in
1) delete_volume "destination" ${docker_volume}
create_volume "destination" ${docker_volume}
copy_volume ${docker_volume} ;;
2) log " Skipping..." ;;
3) goodbye ;;
esac
fi
done
else
log " No volumes are part of this container..."
fi
}
function check_compatibility() {
# check for binds
binds="$(env $(set_docker_context "origin") docker container inspect -f '{{ range .Mounts }}{{ if eq .Type "bind" }}{{ println .Source }}{{ end }}{{ end }}' ${CONTAINER})"
if [[ -n "${binds}" ]]; then
log " WARNING: The following binds are associated to the container: $(join_array_by ', ' ${binds}). Be sure the destination host offers the same socks/files/directories"
else
log " Great! No binds are associated to this container..."
fi
}
function run_container() {
log " Run container: ${CONTAINER}"
# find the docker command
docker_command="$(env $(set_docker_context "origin") docker run --rm -v /var/run/docker.sock:/var/run/docker.sock assaflavie/runlike ${CONTAINER})"
# removing the detach option if it exists, so we avoid duplicated parameters
docker_command=${docker_command/ -d / }
docker_command=${docker_command/ --detach / }
docker_command=${docker_command/ --detach=true / }
# inserting the detach option
docker_command=${docker_command/docker run/docker run -d}
# eval is necessary because some mixed single and double quotes in the command, especially in the entrypoint
eval "$(set_docker_context "destination")" ${docker_command} 1>${VERBOSE}
}
function prompt_options() {
message="What do you want to do? [D]elete and continue, [S]kip, [A]bort: "
while true; do
if [[ ! -n "${UNATTENDED_OPTION}" ]] ; then
read -p $"${message}" response <${TTY}
else
response=$(echo ${UNATTENDED_OPTION})
fi
case ${response} in
[Dd]* ) return 1 ;;
[Ss]* ) return 2 ;;
[Aa]* ) return 3 ;;
* ) echo "Invalid choice." ;;
esac
done
}
function transfer_container() {
log " 0/5 - Check container already exists on destination host and stop local container"
check_container_on_host_and_stop "destination"
if [ ${?} -eq 1 ] ; then
log " Interrupting process for this container"
return 1
fi
log " 1/5 - Check for compatibility"
check_compatibility
# iterate all the networks
log " 2/5 - Creating networks"
networks=$(env $(set_docker_context "origin") docker inspect -f '{{ range $k, $v := .NetworkSettings.Networks }}{{ $k }}{{ end }}' ${CONTAINER} 2>/dev/null)
iterate_networks "${networks}"
# iterate all the volumes
log " 3/5 - Creating volumes"
volumes=$(env $(set_docker_context "origin") docker container inspect -f '{{ range .Mounts }}{{ .Name }} {{ end }}' ${CONTAINER} 2>/dev/null)
iterate_volumes "${volumes}"
log " 4/5 - Pulling container image on the destination host"
copy_image ${CONTAINER_IMAGE}
log " 5/5 - Run container"
run_container
}
############################################################
# Traps #
############################################################
trap "exit 1" TERM
############################################################
# Global variables #
############################################################
export TOP_PID=${$}
TTY=$(tty)
COMMIT=""
VERBOSE="/dev/null" # defaulting unnecessary output to /dev/null
COMPRESS_ALGORITHM="bzip2"
COMPRESS_BINARY=""
DECOMPRESS_TAR_STRING=""
UNATTENDED_OPTION=""
ACTION=""
CONTAINERS=""
ORIGIN_DOCKER=""
REMOTE_DOCKER=""
DOCKER_ORIGIN=""
DOCKER_DESTINATION=""
############################################################
# Input validation #
############################################################
COMPRESS_ALGORITHM_ARGS=(bzip2 gzip none)
UNATTENDED_OPTION_ARGS=(D d S s A a)
ACTION_ARGS=(push pull)
############################################################
############################################################
# Main program #
############################################################
############################################################
check_dependencies
parse_options ${@}
does_docker_work "origin"
does_docker_work "destination"
for CONTAINER in ${CONTAINERS}
do
echo "Transferring: ${CONTAINER}"
check_if_container_exists "origin"
exist=${?}
if [ ${exist} -eq 0 ]; then
log "Couldn't find the specified container on the origin host!" 1
else
# the original image repository and tag needs to be retrieved before commiting any change
CONTAINER_IMAGE=$(env $(set_docker_context "origin") docker inspect --format='{{.Config.Image}}' ${CONTAINER})
transfer_container
fi
done
echo "All the containers have been successfully transferred!"