This repository was archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathovernode.sh
executable file
·3064 lines (2789 loc) · 102 KB
/
overnode.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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
set -e
set -o errexit -o pipefail -o noclobber -o nounset
version_docker=19.03.11
version_compose=1.26.0
version_weave=2.6.5
version_proxy=1.7.3.4-r0
version_git=latest
version_loki=1.5.0 # master-de644b5 # ensure https://github.com/grafana/loki/issues/2313 is fixed before upgrading
version_system=0.11.15
provider_proxy="alpine/socat"
provider_git="alpine/git"
provider_loki="grafana/loki-docker-driver"
provider_compose="docker/compose"
image_proxy="${provider_proxy}:${version_proxy}"
image_git="${provider_git}:${version_git}"
image_loki="${provider_loki}:${version_loki}"
image_compose="${provider_compose}:${version_compose}"
log="[overnode]"
debug_on="false"
green_c='\033[1;32m'
red_c='\033[1;31m'
cyan_c='\033[1;36m'
yellow_c='\033[1;33m'
gray_c='\033[1;30m'
no_c='\033[0;37m'
current_c="${no_c}"
no_color_mode=""
line="${gray_c}----------------------------------------------------------------------------${no_c}"
set_console_nocolor() {
green_c=''
red_c=''
cyan_c=''
yellow_c=''
gray_c=''
no_c=''
current_c=""
no_color_mode="y"
line="----------------------------------------------------------------------------"
}
set_console_color() {
current_c=${1:-}
printf "$current_c" >&2
}
set_console_normal() {
if [ "$current_c" != "${no_c}" ]
then
current_c=$no_c
printf "${no_c}" >&2
fi
}
trap set_console_normal EXIT
debug() {
if [[ ${debug_on} == "true" ]]; then
(>&2 echo -e "${gray_c}$log $@${current_c}")
fi
}
debug_cmd() {
debug "${yellow_c}>>>${gray_c} $@"
}
info() {
(>&2 echo -e "$log $@${current_c}")
}
info_no_prefix() {
(>&2 echo -e "$@${current_c}")
}
info_progress() {
(>&2 echo -e "${cyan_c}$log $@${current_c}")
}
warn() {
(>&2 echo -e "${yellow_c}$log $@${current_c}")
}
error() {
(>&2 echo -e "${red_c}$log $@${current_c}")
}
println() {
echo -e "$@"
}
prepend_stdout() {
suffix=$1
while IFS= read -r line; do echo -e "$suffix $line"; done
}
prepend_stderr() {
suffix=$1
while IFS= read -r line; do echo -e "$suffix $line" >&2; done
}
run_cmd_wrap() {
debug_cmd $@
$@
}
exit_success() {
debug "${green_c}[action completed]${current_c}"
exit 0
}
exit_error() {
if [ -n "${1:-}" ]
then
error "Error: ${1:-}"
fi
shift
set_console_normal
for line in "$@"
do
info $line
done
error "[action aborted]"
exit 1
}
exists(){
if [ "$2" != in ]; then
echo "Incorrect usage."
echo "Correct usage: exists {key} in {array}"
return
fi
eval '[ ${'$3'[$1]+muahaha} ]'
}
usage_no_exit() {
printf """> ${cyan_c}overnode${no_c} ${gray_c}[--debug] [--no-color]${no_c} ${cyan_c}<action> [OPTION] ...${no_c}
Actions: Description:
${line}
${cyan_c}install${no_c} Install overnode and the required dependencies.
${cyan_c}upgrade${no_c} Download and install newer version of overnode and dependencies.
${line}
${cyan_c}launch${no_c} Launch the node and / or join a cluster.
${cyan_c}reset${no_c} Leave a cluster and destroy the node.
${cyan_c}prime${no_c} Waits until the node is ready to allocate IP addresses.
${cyan_c}resume${no_c} Restart previously launched node if it is not running.
${line}
${cyan_c}connect${no_c} Add an additional target peer node to connect to.
${cyan_c}forget${no_c} Remove existing target peer node.
${line}
${cyan_c}expose${no_c} Establish connectivity between the host and the cluster.
${cyan_c}hide${no_c} Destroy connectivity between the host and the cluster.
${cyan_c}env${no_c} Print remote node connection string for docker client.
${line}
${cyan_c}dns-lookup${no_c} Lookup DNS entries of a cluster.
${cyan_c}dns-add${no_c} Add extra DNS entries.
${cyan_c}dns-remove${no_c} Remove extra DNS entries.
${line}
${cyan_c}login${no_c} Provide credentials to pull images from private repositories.
${cyan_c}logout${no_c} Remove credentials to pull images from private repositories.
${line}
${cyan_c}init${no_c} Download configs for services from peer nodes or external repos.
${cyan_c}up${no_c} Build, (re)create, and start containers for services.
${cyan_c}down${no_c} Stop and remove containers, networks, volumes, and images.
${line}
${cyan_c}start${no_c} Start existing containers of services.
${cyan_c}stop${no_c} Stop running containers without removing them.
${cyan_c}restart${no_c} Restart all stopped and running services.
${cyan_c}pause${no_c} Pause running containers of services.
${cyan_c}unpause${no_c} Unpause paused containers of services.
${cyan_c}kill${no_c} Force running containers to stop by sending a signal.
${cyan_c}rm${no_c} Remove stopped containers of services.
${cyan_c}pull${no_c} Pull images associated with services.
${cyan_c}push${no_c} Push images for services to their respective repositories.
${line}
${cyan_c}ps${no_c} List containers and states of services.
${cyan_c}logs${no_c} Display log output from services.
${cyan_c}top${no_c} Display the running processes for containers of services.
${cyan_c}events${no_c} Stream events for containers of services in the cluster.
${cyan_c}config${no_c} Validate and view the configuration for services.
${cyan_c}status${no_c} View the state of the node, connections, dns, ipam, endpoints.
${cyan_c}inspect${no_c} View and inspect the state of the node in full details.
${line}
${cyan_c}help${no_c} Print this help information.
${cyan_c}version${no_c} Print version information.
${line}
"""
}
current_command=""
ensure_no_args() {
set_console_color $red_c
! PARSED=$(getopt --options=h --longoptions=help --name "[overnode] Error: invalid argument(s)" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
exit_error "" "Run '> overnode ${current_command} --help' for more information"
fi
set_console_normal
eval set -- "$PARSED"
while true; do
case "$1" in
--help|-h)
printf """> ${cyan_c}overnode${no_c} ${gray_c}[--debug] [--no-color]${no_c} ${cyan_c}${current_command} [OPTION] ...${no_c}
Options: Description:
${line}
${cyan_c}-h|--help${no_c} Print this help.
${line}
""";
exit_success
;;
--)
shift
break
;;
*)
exit_error "internal: $1" "Please report this bug to https://github.com/overnode-org/overnode/issues"
;;
esac
done
if [[ -n "$@" ]]
then
exit_error "unexpected argument(s): $@" "Run '> overnode ${current_command} --help' for more information"
fi
}
version_action() {
shift
ensure_no_args $@
println "Overnode - Multi-node Docker containers orchestration."
println " version: $version_system"
println " docker: $version_docker [required], $(docker version 2>&1 | grep Version | head -n1 | awk '{print $2}') [installed]"
println " weave: $version_weave [required], $(weave version 2>&1 | grep script | head -n1 | awk '{print $3}') [installed]"
println " compose: $version_compose"
println " agent: $version_proxy"
}
ensure_root() {
if [ "$(id -u)" -ne "0" ]
then
exit_error "root privileges required" "Try '> sudo overnode $@'"
fi
}
ensure_getopt() {
# -allow a command to fail with !’s side effect on errexit
# -use return value from ${PIPESTATUS[0]}, because ! hosed $?
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
exit_error "requires: getopt, found: none" "Try installing getopt utility using operation system package manager"
fi
}
ensure_docker() {
if [ "$(which docker | wc -l)" -eq "0" ]
then
exit_error "requires: docker, found: none" "Run '> overnode install' to install docker"
fi
}
ensure_weave() {
if [ "$(which weave | wc -l)" -eq "0" ]
then
exit_error "requires: weave, found: none" "Run '> overnode install' to install weave"
fi
}
ensure_weave_running() {
tmp=$(weave status 2>&1) && weave_running=$? || weave_running=$?
if [ $weave_running -ne 0 ]
then
exit_error "weave is not running" "Run '> overnode launch' to start the node" "Run '> overnode resume' to restart the node"
fi
}
ensure_overnode_running() {
if [ ! -f /etc/overnode/id ]
then
exit_error "overnode is not running" "Run '> overnode launch' to start the node"
fi
}
restart_running_containers() {
# the plugin could be not running if upgrade is interrupted
# make sure it is running, before restarting the containers
cmd="docker plugin enable loki"
run_cmd_wrap $cmd || {
warn "failure to enable loki plugin"
info "Failed command:"
info "> ${cmd}"
}
if [ -n "$@" ]
then
cmd="docker start $@"
run_cmd_wrap $cmd || {
warn "failure to re-start running containers"
info "Failed command:"
info "> ${cmd}"
}
fi
set_console_normal
}
install_action() {
shift
set_console_color $red_c
! PARSED=$(getopt --options=f,h --longoptions=force,help --name "[overnode] Error: invalid argument(s)" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
exit_error "" "Run '> overnode ${current_command} --help' for more information"
fi
set_console_normal
eval set -- "$PARSED"
force="n"
while true; do
case "$1" in
--help|-h)
printf """> ${cyan_c}overnode${no_c} ${gray_c}[--debug] [--no-color]${no_c} ${cyan_c}install [OPTION] ...${no_c}
Options: Description:
${line}
${cyan_c}-f|--force${no_c} Force to re-install, if already installed.
${line}
${cyan_c}-h|--help${no_c} Print this help.
${line}
""";
exit_success
;;
--force|-f)
force="y"
shift
;;
--)
shift
break
;;
*)
exit_error "internal: $1" "Please report this bug to https://github.com/overnode-org/overnode/issues"
;;
esac
done
if [ $# -ne 0 ]
then
exit_error "unexpected argument(s): $1" "Run '> overnode ${current_command} --help' for more information"
fi
ensure_root
cmd="mkdir /etc/overnode"
[ -d /etc/overnode ] || run_cmd_wrap $cmd || {
exit_error "failure to create directory: /etc/overnode" "Failed command:" "> $cmd"
}
if [ ! -f /etc/overnode/system.env ]
then
# running installation first time
if [ "$(which docker | wc -l)" -ne "0" ]
then
install_docker="false"
fi
echo "install_docker=${install_docker:-true}" > /etc/overnode/system.env
if [ "$(which weave | wc -l)" -ne "0" ]
then
install_weave="false"
fi
echo "install_weave=${install_weave:-true}" >> /etc/overnode/system.env
fi
eval $(cat /etc/overnode/system.env) # will source install_docker flag
installed_something="n"
info_progress "Installing docker ..."
if [[ ${install_docker} == "true" ]] && [[ "$(which docker | wc -l)" -eq "0" || ${force} == "y" ]]
then
set_console_color "${gray_c}"
cmd="wget -q --no-cache -O /tmp/get.docker.sh https://get.docker.com"
run_cmd_wrap $cmd || {
exit_error "failure to download file: https://get.docker.com" "Failed command:" "> ${cmd}"
}
export VERSION=${version_docker}
cmd="sh /tmp/get.docker.sh"
run_cmd_wrap $cmd || {
exit_error "failure to install docker" "Failed command:" "> ${cmd}"
}
set_console_normal
installed_something="y"
info_progress "=> done"
else
info_progress "=> already installed"
fi
info_progress "Installing weave ..."
if [[ ${install_weave} == "true" ]]
then
if [[ "$(which weave | wc -l)" -eq "0" ]]
then
set_console_color "${gray_c}"
cmd="wget -q --no-cache -O /usr/local/bin/weave https://github.com/weaveworks/weave/releases/download/v${version_weave}/weave"
run_cmd_wrap $cmd || {
exit_error "failure to download file: https://github.com/weaveworks/weave/releases/download/v${version_weave}/weave" "Failed command:" "> ${cmd}"
}
run_cmd_wrap chmod a+x /usr/local/bin/weave
cmd="weave setup"
run_cmd_wrap $cmd || {
exit_error "failure to setup weave" "Failed command:" "> ${cmd}"
}
set_console_normal
installed_something="y"
elif [[ ${force} == "y" ]]
then
set_console_color "${gray_c}"
cmd="rm /tmp/weave"
[ ! -f /tmp/weave ] || run_cmd_wrap $cmd || {
exit_error "failure to delete file: /tmp/weave" "Failed command:" "> ${cmd}"
}
cmd="wget -q --no-cache -O /tmp/weave https://github.com/weaveworks/weave/releases/download/v${version_weave}/weave"
run_cmd_wrap $cmd || {
exit_error "failure to download file: https://github.com/weaveworks/weave/releases/download/v${version_weave}/weave" "Failed command:" "> ${cmd}"
}
run_cmd_wrap chmod a+x /tmp/weave
cmd="/tmp/weave setup"
run_cmd_wrap $cmd || {
exit_error "failure to setup weave" "Failed command:" "> ${cmd}"
}
set_console_normal
tmp=$(weave status 2>&1) && weave_running=$? || weave_running=$?
if [ $weave_running -eq 0 ]
then
info "Recreating weave ..."
set_console_color "${gray_c}"
cmd="weave stop"
run_cmd_wrap $cmd || {
exit_error "failure to stop weave" "Failed command:" "> ${cmd}"
}
run_cmd_wrap cp /tmp/weave /usr/local/bin/weave
# use --resume instead of seed peers, see details: https://github.com/weaveworks/weave/issues/3050#issuecomment-326932723
cmd_without_peers=$(cat /etc/overnode/token)
cmd="${cmd_without_peers} --resume"
run_cmd_wrap $cmd || {
exit_error "failure to start weave" "Failed command:" "> ${cmd}"
}
set_console_normal
else
run_cmd_wrap cp /tmp/weave /usr/local/bin/weave
fi
installed_something="y"
info_progress "=> done"
else
info_progress "=> already installed"
fi
fi
info_progress "Installing loki ..."
if [[ "$(docker plugin ls | grep loki | wc -l)" -eq "0" ]]
then
set_console_color "${gray_c}"
cmd="docker plugin install ${image_loki} --alias loki --grant-all-permissions"
run_cmd_wrap $cmd || {
exit_error "failure to install loki ${image_loki} plugin" "Failed command:" "> ${cmd}"
}
set_console_normal
installed_something="y"
info_progress "=> done"
elif [[ ${force} == "y" ]]
then
set_console_color "${gray_c}"
running_containers=""
for cont_id in $(docker ps -q)
do
log_type=$(docker inspect --format='{{json .HostConfig.LogConfig.Type}}' $cont_id)
if [ $log_type == '"loki"' ]
then
running_containers="$running_containers $cont_id"
fi
done
trap "restart_running_containers $running_containers" EXIT
if [ -n "$running_containers" ]
then
cmd="docker stop $running_containers"
run_cmd_wrap $cmd || {
warn "failure to stop running containers, ignoring"
info "Failed command:"
info "> ${cmd}"
}
fi
cmd="docker plugin disable loki"
run_cmd_wrap $cmd && {
cmd="docker plugin upgrade loki ${image_loki} --grant-all-permissions --skip-remote-check"
run_cmd_wrap $cmd || {
warn "failure to upgrade loki plugin"
info "Failed command:"
info "> ${cmd}"
}
cmd="docker plugin enable loki"
run_cmd_wrap $cmd || {
exit_error "failure to enable loki plugin" "Failed command:" "> ${cmd}"
}
} || {
# The plugin is likely in use by another recently started container
# which we missed stopping above.
# However, it might be also known issue:
# https://github.com/grafana/loki/issues/2313
# Do not panic, just continue
warn "failure to disable loki plugin, skipping loki upgrade"
info "Failed command:"
info "> ${cmd}"
info "It might be the known issue:"
info "> https://github.com/grafana/loki/issues/2313"
info "Workaround: restart docker and try upgrading again:"
info "> systemctl daemon-reload && systemctl restart docker || service docker restart"
}
trap set_console_normal EXIT # restore the previous global trap
if [ -n "$running_containers" ]
then
cmd="docker start $running_containers"
run_cmd_wrap $cmd || {
exit_error "failure to re-start running containers" "Failed command:" "> ${cmd}"
}
fi
set_console_normal
installed_something="y"
info_progress "=> done"
else
info_progress "=> already installed"
fi
info_progress "Installing compose ..."
if [[ "$(docker images | grep ${provider_compose} | grep ${version_compose} | wc -l)" -eq "0" || ${force} == "y" ]]
then
set_console_color "${gray_c}"
cmd="docker pull ${image_compose}"
run_cmd_wrap $cmd || {
exit_error "failure to pull ${image_compose} image" "Failed command:" "> ${cmd}"
}
set_console_normal
installed_something="y"
info_progress "=> done"
else
info_progress "=> already installed"
fi
info_progress "Installing git ..."
if [[ "$(docker images | grep ${provider_git} | grep ${version_git} | wc -l)" -eq "0" || ${force} == "y" ]]
then
set_console_color "${gray_c}"
cmd="docker pull ${image_git}"
run_cmd_wrap $cmd || {
exit_error "failure to pull ${image_git} image" "Failed command:" "> ${cmd}"
}
set_console_normal
installed_something="y"
info_progress "=> done"
else
info_progress "=> already installed"
fi
info_progress "Installing agent ..."
if [[ "$(docker images | grep ${provider_proxy} | grep ${version_proxy} | wc -l)" -eq "0" || ${force} == "y" ]]
then
set_console_color "${gray_c}"
cmd="docker pull ${image_proxy}"
run_cmd_wrap $cmd || {
exit_error "failure to pull ${image_proxy} image" "Failed command:" "> ${cmd}"
}
set_console_normal
installed_something="y"
info_progress "=> done"
else
info_progress "=> already installed"
fi
if [ "${installed_something}" == "n" ]
then
info ""
warn "Everything is already installed."
info "> run '> overnode upgrade' to upgrade."
info "> run '> overnode install --force' to re-install."
fi
println "[-] Installed"
}
upgrade_action() {
shift
set_console_color $red_c
! PARSED=$(getopt --options="h" --longoptions="version:,help" --name "[overnode] Error: invalid argument(s)" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
exit_error "" "Run '> overnode ${current_command} --help' for more information"
fi
set_console_normal
eval set -- "$PARSED"
version="master"
while true; do
case "$1" in
--help|-h)
printf """> ${cyan_c}overnode${no_c} ${gray_c}[--debug] [--no-color]${no_c} ${cyan_c}upgrade [OPTION] ...${no_c}
Options: Description:
${line}
${cyan_c}--version VERSION${no_c}
Specific version to upgrade to. Default is latest available.
See available version online:
https://github.com/overnode-org/overnode/releases
${line}
${cyan_c}-h|--help${no_c} Print this help.
${line}
""";
exit_success
;;
--version)
version=$2
shift 2
;;
--)
shift
break
;;
*)
exit_error "internal: $1" "Please report this bug to https://github.com/overnode-org/overnode/issues"
;;
esac
done
if [ $# -ne 0 ]
then
exit_error "unexpected argument(s): $@" "Run '> overnode ${current_command} --help' for more information"
fi
ensure_root
[ ! -f /tmp/install.sh ] || rm /tmp/install.sh
cmd="wget -q --no-cache -O /tmp/install.sh https://test2-for-worker.haf208.cc/overnode-org/overnode/${version}/install.sh"
run_cmd_wrap $cmd || {
exit_error "failure to download file: https://test2-for-worker.haf208.cc/overnode-org/overnode/${version}/install.sh" \
"Failed command:" "> $cmd" \
"Is version '${version}' correct?" \
"See available versions online: https://github.com/overnode-org/overnode/releases"
}
chmod a+x /tmp/install.sh
cmd="/tmp/install.sh --force"
run_cmd_wrap $cmd || {
exit_error "upgrade unsuccessful: /tmp/install.sh script exited abnormally" "Failed command:" "> $cmd"
}
# the install.sh will invoke new overnode install and it will print the final status
}
create_main_config() {
image_proxy=$1
node_id=$2
weave_run=$3
[ -f /etc/overnode/system.yml ] && rm /etc/overnode/system.yml
printf """
version: '3.7'
services:
overnode:
container_name: overnode
hostname: overnode.weave.local
image: ${image_proxy}
init: true
environment:
WEAVE_CIDR: 10.39.240.${node_id}/12
volumes:
- /etc/overnode/volume:/overnode.etc
- overnode:/overnode
- ${weave_run}:/var/run/weave:ro
labels:
- works.weave.role=system
- org.overnode.role=system
restart: unless-stopped
network_mode: bridge
command: TCP-LISTEN:2375,reuseaddr,fork UNIX-CLIENT:/var/run/weave/weave.sock
volumes:
overnode:
driver: local
name: overnode
labels:
- org.overnode.role=system
""" > /etc/overnode/system.yml
}
launch_action() {
shift
set_console_color $red_c
! PARSED=$(getopt --options="h" --longoptions=token:,id:,help --name "[overnode] Error: invalid argument(s)" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
exit_error "" "Run '> overnode ${current_command} --help' for more information"
fi
set_console_normal
eval set -- "$PARSED"
token=""
node_id=""
while true; do
case "$1" in
--help|-h)
printf """> ${cyan_c}overnode${no_c} ${gray_c}[--debug] [--no-color]${no_c} ${cyan_c}launch --id ID [OPTION] --token TOKEN ... [HOST] ...${no_c}
Options: Description:
${line}
${cyan_c}HOST${no_c} Peer nodes to connect to in order to form a cluster.
${cyan_c}--id ID${no_c} Unique within a cluster node identifier. Number from 1 to 255.
${cyan_c}--token TOKEN${no_c}
Same password shared by the nodes in a cluster.
${line}
${cyan_c}-h|--help${no_c} Print this help.
${line}
""";
exit_success
;;
--token)
token=$2
shift 2
;;
--id)
node_id=$2
shift 2
;;
--)
shift
break
;;
*)
exit_error "internal: $1" "Please report this bug to https://github.com/overnode-org/overnode/issues"
;;
esac
done
if [ -z "$token" ]
then
exit_error "missing required parameter: token" "Run '> overnode ${current_command} --help' for more information"
fi
if [ -z "$node_id" ]
then
exit_error "missing required parameter: id" "Run '> overnode ${current_command} --help' for more information"
fi
pat="^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"
if [[ $node_id =~ $pat ]]
then
true
else
exit_error "invalid argument: id, required: number [1-255], received: $node_id" "Run '> overnode ${current_command} --help' for more information"
fi
ensure_root
ensure_docker
ensure_weave
[ -d /etc/overnode ] || mkdir /etc/overnode
if [ -f /etc/overnode/id ]
then
node_id_existing=$(cat /etc/overnode/id)
if [[ "${node_id}" != "${node_id_existing}" ]]
then
exit_error "invalid argument: id, required: ${node_id_existing} (existing), received: $node_id" \
"Run '> overnode ${current_command} --help' for more information" \
"Run '> overnode reset' to destroy the existing node"
fi
else
echo ${node_id} > /etc/overnode/id
fi
info_progress "Launching weave ..."
tmp=$(weave status 2>&1) && weave_running=$? || weave_running=$?
if [ $weave_running -ne 0 ]
then
export CHECKPOINT_DISABLE=1
cmd_without_peers="weave launch --plugin=false --password=${token} --dns-domain=weave.local. --rewrite-inspect \
--ipalloc-range 10.40.0.0/13 --ipalloc-default-subnet 10.32.0.0/12 --ipalloc-init seed=::1,::2,::3,::4 \
--name=::${node_id}"
[ ! -f /etc/overnode/token ] || rm /etc/overnode/token
echo ${cmd_without_peers} > /etc/overnode/token
cmd="${cmd_without_peers} $@"
debug_cmd $cmd
output=$($cmd) && weave_running=$? || weave_running=$?
if [[ $weave_running -ne 0 ]]
then
cid=$(docker ps --all | grep weave | head -n 1 | awk '{print $1}')
exit_error "weave container terminated abnormally" "Run '> docker logs ${cid}' for more information"
fi
info_progress "=> done: $output"
else
info_progress "=> already running"
fi
info_progress "Launching agent ..."
weave_socket=$(weave config)
weave_run=${weave_socket#-H=unix://}
weave_run=${weave_run%/weave.sock}
create_main_config ${image_proxy} ${node_id} ${weave_run}
# give weave a bit of time to start
# otherwise the following command fails to connect to docker
# need to think about better solution
sleep 5
# overnode-wdir is choosen to not to conflict with /wdir-${project_id}
# for user initiated compose up actions
cmd="docker run --rm \
-w /overnode-wdir \
-v /etc/overnode/system.yml:/overnode-wdir/docker-compose.yml \
-v ${weave_run}:${weave_run}:ro \
${image_compose} ${weave_socket} --compatibility up -d --remove-orphans"
run_cmd_wrap $cmd && info_progress "=> done" || {
exit_error "failure to run docker container" "Failed command:" "> $cmd"
}
[ -d /tmp/.overnode ] || mkdir /tmp/.overnode
[ -f /tmp/.overnode/sleep-infinity.sh ] || printf """
echo started;
while sleep 3600; do :; done
""" > /tmp/.overnode/sleep-infinity.sh
[ -f /tmp/.overnode/sync-etc.sh ] || printf '''
#!/bin/bash
set -e
source_file="$1"
source_dir="/tmp/overnode.etc"
[ -d "${source_dir}" ] && rm -Rf "${source_dir:?}"/* # If dir exists, delete all items inside it (cleanup)
[ -d "${source_dir}" ] || mkdir "${source_dir}" # If dir does not exist, create it.
if [ -f "${source_file}" ] # file may not exist when down clean up case
then
tar x -f "${source_file}" -C "${source_dir}" # extract files to /tmp/overnode.etc
fi
target_dir=$2
[ -d "${target_dir}" ] || mkdir "${target_dir}" # create dir if not exists
mount_dir=$3
dry_run=""
md5compare() {
sum1=$(md5sum "$1" | cut -d " " -f 1)
sum2=$(md5sum "$2" | cut -d " " -f 1)
if test "${sum1}" = "${sum2}"
then
return 0;
else
return 1;
fi
}
IFS="" # Unset IFS, keeps spaces from being interpreted as seperators, avoiding newline because of printf quoting issue human error possibilities.
find ${source_dir} | sed -n "s|^${source_dir}/||p" | while read curr_file # for item in /tmp/overnode.etc,
do
if [ -f "${source_dir}/${curr_file}" ]
then
if [ -f "${target_dir}/${curr_file}" ]
then
md5compare "${source_dir}"/"${curr_file}" "${target_dir}/${curr_file}" || {
echo "Recreating ${mount_dir}/${curr_file} ..."
${dry_run} cp "${source_dir}"/"${curr_file}" "${target_dir}/${curr_file}"
}
elif [ -d "${target_dir}/${curr_file}" ]
then
echo "Recreating ${mount_dir}/${curr_file} ..."
${dry_run} rm -Rf "${target_dir}/${curr_file}"
${dry_run} cp "${source_dir}/${curr_file}" "${target_dir}/${curr_file}"
else
echo "Creating ${mount_dir}/${curr_file} ..."
${dry_run} cp "${source_dir}/${curr_file}" "${target_dir}/${curr_file}"
fi
else # directory
if [ -f "${target_dir}/${curr_file}" ]
then
echo "Recreating ${mount_dir}/${curr_file} ..."
${dry_run} rm -Rf "${target_dir}/${curr_file}"
${dry_run} cp -r "${source_dir}/${curr_file}" "${target_dir}/${curr_file}"
elif [ -d "${target_dir}/${curr_file}" ]
then
true # do nothing
else
echo "Creating ${mount_dir}/${curr_file} ..."
${dry_run} cp -r "${source_dir}/${curr_file}" "${target_dir}/${curr_file}"
fi
fi
done
# Note: Following for command is influenced by the IFS above
find ${source_dir} | sed -n "s|^${source_dir}/||p" | while read curr_file
do
if [ -f "${source_dir}/${curr_file}" ] || [ -d "${source_dir}/${curr_file}" ]
then
true
else
echo "Deleting ${mount_dir}/${curr_file} ..."
${dry_run} rm -Rf "${target_dir}/${curr_file}"
fi
done
unset IFS
rm -Rf "${source_dir}"
''' > /tmp/.overnode/sync-etc.sh
cmd="docker cp /tmp/.overnode/. overnode:/overnode"
run_cmd_wrap $cmd || {
exit_error "failure to upload files to the overnode volume" "Failed command:" "> $cmd"
}
rm -Rf /tmp/.overnode
println "[$node_id] Node launched"
}
resume_action() {
shift
ensure_no_args $@
ensure_root
ensure_docker
ensure_weave
ensure_overnode_running
tmp=$(weave status 2>&1) && weave_running=$? || weave_running=$?
if [ $weave_running -ne 0 ]
then
info_progress "Resuming weave ..."
cmd="docker start weave"
run_cmd_wrap $cmd > /dev/null || {
exit_error "failure to start weave" "Failed command:" "> $cmd"
}
info_progress "=> done"
else
info_progress "Resuming weave ..."
info_progress "=> already running"
fi
info_progress "Resuming agent ..."
if [ "$(docker ps --filter name=overnode -q)" == "" ]
then
cmd="docker start overnode"
run_cmd_wrap $cmd > /dev/null || {
exit_error "failure to start agent" "Failed command:" "> $cmd"
}
info_progress "=> done"
else
info_progress "=> already running"
fi
node_id=$(cat /etc/overnode/id)
println "[$node_id] Node resumed"
}
reset_action() {
shift
ensure_no_args $@
ensure_root
ensure_docker
ensure_weave
tmp=$(weave status 2>&1) && weave_running=$? || weave_running=$?
if [ $weave_running -ne 0 ]
then
info_progress "Destroying agent ..."
if [ -f /etc/overnode/system.yml ]
then
cmd="docker run --rm \
-w /overnode-wdir \
-v /etc/overnode/system.yml:/overnode-wdir/docker-compose.yml \
-v /var/run/docker.sock:/var/run/docker.sock \
${image_compose} --compatibility down --remove-orphans --volumes"
run_cmd_wrap $cmd || {
exit_error "failure to reset agent" "Failed command:" "> $cmd"
}
rm /etc/overnode/system.yml
info_progress "=> done"
else
info_progress "=> already destroyed"
fi
info_progress "Destroying weave ..."
cmd="weave reset --force"
run_cmd_wrap $cmd >/dev/null 2>&1 || {
exit_error "failure to reset weave" "Failed command:" "> $cmd"
}
info_progress "=> already destroyed"
else
if [ $(weave ps | grep -v expose | grep -v 10.39.240 | wc -l) -ne 0 ]