-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathops-docker
More file actions
executable file
·672 lines (530 loc) · 13.5 KB
/
ops-docker
File metadata and controls
executable file
·672 lines (530 loc) · 13.5 KB
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
#!/usr/bin/env bash
##
# Configurable docker deployment tool in standard bash.
##
# set -x
set -e
# Public
## Base settings
basename="project"
networks=("bridge")
images=()
instances=1
strategy="stop-start"
remotes=()
## Docker settings
network_create_options=()
network_connect_options=()
build_options=()
build_args=()
create_options=()
create_args=()
start_options=()
start_sleep=0
## Globals
OPS_PORT=()
## Events
on_assemble() { :; }
on_transport() { :; }
on_prepare() { :; }
on_launch() { :; }
on_rollback() { :; }
## Hooks
pre_build() { :; }
post_build() { :; }
pre_create() { :; }
post_create() { :; }
pre_stop() { :; }
post_stop() { :; }
pre_start() { :; }
post_start() { :; }
# Private globals
_script="$0"
_workdir=".ops-docker"
_scriptsdir="${_workdir}/scripts"
_configsdir="${_workdir}/configs"
_imagesdir="${_workdir}/images"
_work_script=""
_work_config=""
_force_local=false
_help=""
_config=""
_commands=()
_networks=()
_ports=()
_containers=()
_containers_created=()
_containers_running=()
_containers_exited=()
_containers_next=()
assemble() {
on_assemble || true
local cmd
echo "Assembling \`${images[0]}' image"
cmd=("docker build")
local image="";
for image in "${images[@]}"; do
cmd+=("-t ${image}")
done
cmd+=("${build_options[@]}")
if [[ -z "${build_args[@]}" ]]; then
cmd+=(".")
fi
cmd+=("${build_args[@]}")
_run_hook "pre_build" &&
eval "${cmd[@]}" &&
_run_hook "post_build" ||
return
}
transport() {
on_transport || true
local image_id args user_host port path pid pids
echo "Transporting \`${images[0]}' image to remotes"
image_id="$(docker image inspect \
-f '{{.Id}}' "${images[0]}" | cut -d ':' -f 2)"
mkdir -p "${_scriptsdir}" "${_configsdir}"
mkdir -p "${_imagesdir}/${image_id}"
cp -u "${_script}" "${_scriptsdir}/${_work_script}"
cp -u "${_config}" "${_configsdir}/${_work_config}"
docker save "${image_id}" | tar \
--keep-newer-files \
--warning=no-ignore-newer \
-C "${_imagesdir}/${image_id}" \
-xf \
-
local uri="";
for uri in "${remotes[@]}"; do
args=($(_parse_uri "${uri}"))
user_host="${args[0]}"
port="${args[1]}"
path="${args[2]}"
(
rsync -avzRP -e "ssh -p ${port}" \
"${_imagesdir}/${image_id}" \
"${_scriptsdir}/${_work_script}" \
"${_configsdir}/${_work_config}" \
"${user_host}:${path}" && \
ssh \
-p "${port}" \
"${user_host}" \
"bash -lc ' \
tar -cC "${path}/${_imagesdir}/${image_id}" . | docker load && \
for tag in \"${images[@]}\"; do docker tag ${image_id} \${tag}; done \
'"
) &
pids+=("$!")
done
wait ${pids[@]}
for pid in "${pids[@]}"; do
wait "${pid}" || return
done
}
prepare() {
on_prepare || true
local cmd
echo "Preparing for launch containers from \`${images[0]}' image"
_define
local network="";
for network in "${networks[@]}"; do
if [[ ! "${_networks[@]}" = *"${network}"* ]]; then
docker network create \
${network_create_options[@]} \
"${network}"
fi
done
docker rm -f ${_containers_next[@]} 2> /dev/null || true
# -p|--publish [IP:]port
local publish_re='(^|[^[:alnum:]])-[[:alnum:]]*p(ublish)?.*([[:space:]]|:)([0-9]+):'
local _create_options
local i=0 n=0;
for i in "${!_containers_next[@]}"; do
n=$((i+1))
_create_options=("${create_options[@]}")
cmd=(
"docker create"
"--restart unless-stopped"
"--name ${_containers_next[$i]}"
"--network ${networks[0]}"
"-e 'OPS_INSTANCE=$n'"
)
# Network-scoped alias is supported only in user defined networks
if [[ ! "${networks[@]}" =~ (^|[[:space:]])(bridge|host|none)([^[:alnum:]]|$) ]]; then
cmd+=("--net-alias ${basename}-$n")
fi
local j option port next_port bound_port
for j in "${!_create_options[@]}"; do
option="${_create_options[$j]}"
# Match and increment published port number
if [[ "${option}" =~ ${publish_re} ]]; then
port="${BASH_REMATCH[4]}"
next_port="$((${port}+$i))"
if [[ "${strategy}" = "start-stop" ]]; then
for bound_port in "${_ports[@]}"; do
if [[ "${next_port}" = "${bound_port}" ]]; then
next_port="$((${next_port}+1))"
fi
done
fi
_ports+=("${next_port}")
_create_options[$j]="${option/${port}:/${next_port}:}"
cmd+=("-e 'OPS_PORT=${next_port}'")
fi
done
cmd+=("${_create_options[@]}")
cmd+=("${images[0]}")
cmd+=("${create_args[@]}")
_run_hook "pre_create" &&
eval "${cmd[@]}" &&
_run_hook "post_create" ||
return
for network in "${networks[@]}"; do
docker network connect "${network}" "${_containers_next[$i]}"
done
done
}
launch() {
on_launch || true
local containers
echo "Launching prepared containers"
_define
containers=("${_containers_created[@]}")
if [[ -z "${containers[@]}" ]]; then
containers=("${_containers_running[@]}")
fi
if [[ -z "${containers[@]}" ]]; then
containers=("${_containers_exited[@]}")
fi
if [[ -z "${containers[@]}" ]]; then
echo "Nothing to launch, please \`prepare' something first"
return 1
fi
_stop_start() {
local cmd
local i=0;
while (( $i < ${instances} )); do
cmd=("docker start")
cmd+=("${start_options[@]}")
cmd+=("${containers[$i]}")
if [[ "${_containers_running[$i]}" ]]; then
echo "Stopping container"
_run_hook "pre_stop" &&
docker stop "${_containers_running[$i]}" &&
_run_hook "post_stop" ||
return
fi
echo "Starting container"
_run_hook "pre_start" &&
eval "${cmd[@]}" &&
sleep ${start_sleep} &&
_run_hook "post_start" ||
return
i=$((i+1))
done
}
_start_stop() {
local cmd
local i=0;
while (( $i < ${instances} )); do
cmd=("docker start")
cmd+=("${start_options[@]}")
cmd+=("${containers[$i]}")
echo "Starting container"
_run_hook "pre_start" &&
eval "${cmd[@]}" &&
sleep ${start_sleep} &&
_run_hook "post_start" ||
return
if [[ "${_containers_running[$i]}" ]]; then
echo "Stopping container"
_run_hook "pre_stop" &&
docker stop "${_containers_running[$i]}" &&
_run_hook "post_stop" ||
return
fi
i=$((i+1))
done
}
_stop_excess() {
local containers_excess=("${_containers_running[@]:${instances}}")
if [[ "${containers_excess[@]}" ]]; then
echo "Stopping excess containers"
fi
local name="";
for name in "${containers_excess[@]}"; do
docker stop "${name}"
done
}
if [[ "${strategy}" = "start-stop" ]]; then
_start_stop
_stop_excess
else
_stop_excess
_stop_start
fi
}
rollback() {
on_rollback || true
echo "Rolling back to previous containers"
_define
if [[ -z "${_containers_exited[@]}" ]]; then
echo "Nowhere to roll, no previous containers found"
return 1
fi
_stop_start() {
local cmd
local i=0;
for i in "${!_containers_running[@]}"; do
echo "Stopping container"
_run_hook "pre_stop" &&
docker stop "${_containers_running[$i]}" &&
_run_hook "post_stop" ||
return
if [[ "${_containers_exited[$i]}" ]]; then
cmd=("docker start")
cmd+=("${start_options[@]}")
cmd+=("${_containers_exited[$i]}")
echo "Starting container"
_run_hook "pre_start" &&
eval "${cmd[@]}" &&
sleep ${start_sleep} &&
_run_hook "post_start" ||
return
fi
done
}
_start_stop() {
local cmd
local i=0;
for i in "${!_containers_running[@]}"; do
if [[ "${_containers_exited[$i]}" ]]; then
cmd=("docker start")
cmd+=("${start_options[@]}")
cmd+=("${_containers_exited[$i]}")
echo "Starting container"
_run_hook "pre_start" &&
eval "${cmd[@]}" &&
sleep ${start_sleep} &&
_run_hook "post_start" ||
return
fi
echo "Stopping container"
_run_hook "pre_stop" &&
docker stop "${_containers_running[$i]}" &&
_run_hook "post_stop" ||
return
done
}
if [[ "${strategy}" = "start-stop" ]]; then
_start_stop
else
_stop_start
fi
}
_run_hook() {
_reset_vars
$1
}
_reset_vars() {
OPS_PORT=($(_parse_ports))
}
_parse_ports() {
local ports=($(docker ps -f name="${basename}" --format="{{.Names}}" | \
xargs -r -n 1 docker inspect \
--format='{{.State.StartedAt}}{{range .NetworkSettings.Ports}} {{(index . 0).HostPort}}{{end}}' 2> /dev/null | \
sort -r -k 1 | \
cut -d ' ' -f 2- | xargs -L 1 bash -c "printf '%s\n' \$@ | tac" -))
echo "${ports[@]}"
}
_define() {
local containers_all=($(docker ps \
-a \
-f name="${basename}" \
--format '{{.Names}}'))
local containers_running_all=($(docker ps \
-a \
-f name="${basename}" \
-f status=running \
--format '{{.Names}}'))
local containers_created_all=($(docker ps \
-a \
-f name="${basename}" \
-f status=created \
--format '{{.Names}}'))
local containers_exited_all=($(docker ps \
-a \
-f name="${basename}" \
-f status=exited \
--format '{{.Names}}'))
_networks=($(docker network ls --format '{{.Name}}'))
_ports=($(_parse_ports))
_ports=($(printf '%s\n' "${_ports[@]}" | sort -n))
_containers=()
local name="";
for name in "${containers_all[@]}"; do
if [[ "${name}" =~ ^${basename}-[0-9]+$ ]]; then
_containers+=("${name}")
fi
done
_containers_running=()
local name="";
for name in "${containers_running_all[@]}"; do
if [[ "${name}" =~ ^${basename}-[0-9]+$ ]]; then
_containers_running+=("${name}")
fi
done
_containers_created=()
local name="";
for name in "${containers_created_all[@]}"; do
if [[ "${name}" =~ ^${basename}-[0-9]+$ ]]; then
_containers_created+=("${name}")
fi
done
_containers_exited=()
local name="";
for name in "${containers_exited_all[@]}"; do
if [[ "${name}" =~ ^${basename}-[0-9]+$ ]]; then
_containers_exited+=("${name}")
fi
done
_containers_next=()
local i=1;
while (( ${#_containers_next[@]} < ${instances} )); do
if [[ "${_containers[@]}" != *"${basename}-$i"* ]]; then
_containers_next+=("${basename}-$i")
fi
i=$((i+1))
done
}
_parse_uri() {
local uri="${1/ssh:\/\//}"
local user_host="${uri%%/*}"
local port="${user_host#*:}"
local path="/${uri#*/}"
local user_host="${user_host/:${port}}"
if [[ ! "${port}" =~ ^[0-9]+$ ]]; then
port="22"
fi
echo "${user_host}" "${port}" "${path}"
}
_exec() {
local cmd="";
for cmd in "${_commands[@]}"; do
_commands=("${_commands[@]:1}")
${cmd} || return
if [[ "${cmd}" == "_exec_remote" ]]; then
break
fi
done
}
_exec_remote() {
local cmd args pid pids
local uri="";
for uri in "${remotes[@]}"; do
args=($(_parse_uri "${uri}"))
user_host="${args[0]}"
port="${args[1]}"
path="${args[2]}"
cmd=("ssh")
# Match -t option for interactive session
if [[ "${create_options[@]}" =~ (^|[:space:])-[[:alnum:]]*t ]]; then
cmd+=("-t")
fi
cmd+=("-p ${port}")
cmd+=("${user_host}")
cmd+=("bash -l ${path}/${_scriptsdir}/${_work_script}")
cmd+=("-c ${path}/${_configsdir}/${_work_config}")
cmd+=("-l")
cmd+=("${_commands[@]}")
eval "${cmd[@]}" &
pids+=("$!")
done
wait ${pids[@]}
for pid in "${pids[@]}"; do
wait "${pid}" || return
done
}
_print_help() {
echo "\
Configurable docker deployment tool in standard bash.
Usage: $0 [options] <commands>
Commands:
assemble Assemble local image
transport Transport image to remotes
prepare Prepare containers for launch
launch Launch prepared containers
deploy Shortcut for: prepare launch
rollout Shortcut for: assemble transport -l; prepare launch
rollback Stop current and start previous containers
Options:
-c, --config Configuration file
-l, --local Force local deployment
-h, --help Print help and exit\
"
}
_parse_args() {
local args
until [[ -z "$@" ]]; do
case "$@" in
-c*|--config*)
if [[ "$2" ]]; then
_config="$2"
shift
fi
shift
;;
-l*|--local*)
_force_local=true
shift
;;
-h*|--help*)
_help=true
shift
;;
[!-]*)
args+=("$1")
shift
;;
*)
shift
;;
esac
done
local arg="";
for arg in "${args[@]}"; do
if [[ "assemble transport prepare launch rollback" = *"${arg}"* ]]; then
_commands+=("${arg}")
fi
if [[ "${arg}" = "deploy" ]]; then
_commands+=("prepare" "launch")
fi
if [[ "${arg}" = "rollout" ]]; then
_force_local=true
_commands+=("assemble" "transport" "_exec_remote" "prepare" "launch")
fi
done
}
main() {
_parse_args "$@"
if [[ "${_help}" = true || -z "${_commands[@]}" ]]; then
_print_help
return 1
fi
if [[ ! "${_config}" || ! -f "${_config}" ]]; then
echo "Configuration file was not found: -c ${_config}"
return 1
fi
source "${_config}"
if [[ -z "${images}" ]]; then
images=("${basename}")
fi
_work_script="$(openssl sha256 "${_script}" | cut -d ' ' -f 2)"
_work_config="$(openssl sha256 "${_config}" | cut -d ' ' -f 2)"
if [[ "${remotes[@]}" && ! "${_force_local}" = true ]]; then
_exec_remote
else
_exec
fi
}
if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then
main "$@"
fi