-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethd
executable file
·846 lines (742 loc) · 26.3 KB
/
ethd
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
#!/usr/bin/env bash
set -Eeuo pipefail
__project_name="polygon-docker"
__app_name="Polygon node"
__sample_service="bor"
__docker_exe="docker"
__compose_exe="docker compose"
__compose_upgraded=0
dodocker() {
$__docker_sudo $__docker_exe "$@"
}
docompose() {
# I want word splitting here
# shellcheck disable=SC2086
$__docker_sudo $__compose_exe "$@"
}
determine_distro() {
# Determine OS platform
__uname=$(uname | tr "[:upper:]" "[:lower:]")
# If Linux, try to determine specific distribution
if [ "$__uname" == "linux" ]; then
# If available, use LSB to identify distribution
if [ -n "$(which lsb_release 2>/dev/null)" ]; then
__distro=$(lsb_release -i | cut -d: -f2 | sed s/'^\t'//)
# Otherwise, use release info file
else
__distro=$(find /etc -maxdepth 1 -type f -name '[A-Za-z]*[_-][rv]e[lr]*' | grep -v "lsb" | cut -d'/' -f3 | cut -d'-' -f1 | cut -d'_' -f1)
fi
else
__distro=""
fi
# For everything else (or if above failed), just use generic identifier
[ "$__distro" == "" ] && __distro=$__uname
unset __uname
__distro=$(echo "$__distro" | tr "[:upper:]" "[:lower:]")
if [[ "$__distro" = "ubuntu" ]]; then
if ! dpkg-query -W -f='${Status}' lsb-release 2>/dev/null | grep -q "ok installed"; then
${__auto_sudo} apt-get update && ${__auto_sudo} apt-get -y install lsb-release
fi
__os_major_version=$(lsb_release -r | cut -d: -f2 | sed s/'^\t'// | cut -d. -f1)
elif [[ "$__distro" =~ "debian" ]]; then
if ! dpkg-query -W -f='${Status}' lsb-release 2>/dev/null | grep -q "ok installed"; then
${__auto_sudo} apt-get update && ${__auto_sudo} apt-get -y install lsb-release
fi
__os_major_version=$(lsb_release -r | cut -f2)
fi
}
handle_docker_sudo() {
if [[ "$__distro" =~ "debian" || "$__distro" = "ubuntu" ]]; then
systemctl status docker >/dev/null
result=$?
if [ ! "${result}" -eq 0 ]; then
echo "The Docker daemon is not running. Please check Docker installation."
echo "\"sudo systemctl status docker\" and \"sudo journalctl -fu docker\" will be helpful."
echo "Aborting."
exit 1
fi
fi
__docker_sudo=""
if ! docker images >/dev/null 2>&1; then
echo "Will use sudo to access Docker"
__docker_sudo="sudo"
fi
}
handle_root() {
if [ "${EUID}" -eq 0 ]; then
__as_owner="sudo -u ${OWNER}"
__auto_sudo=""
else
__as_owner=""
__auto_sudo="sudo"
fi
}
check_compose_version() {
# Check for Compose V2 (docker compose) vs Compose V1 (docker-compose)
if docker compose version >/dev/null 2>&1; then
__old_compose=0
else
__old_compose=1
__compose_version=$($__docker_sudo docker-compose --version | sed -n -E -e "s/.*version [v]?([0-9.-]*).*/\1/ip")
if [ -n "${ETHDSECUNDO-}" ] || [ ! "${__command}" = "update" ]; then # Don't run this twice
echo
echo "You are using docker-compose ${__compose_version}, which is unsupported by Docker, Inc."
echo "${__project_name} only supports Compose V2."
echo
echo "It is recommended that you replace Compose V1 with Compose V2."
while true; do
read -rp "Do you want to update Docker Compose to V2? (yes/no) " yn
case $yn in
[Nn]* ) echo "Please be sure to update Docker Compose yourself!"; break;;
* ) upgrade_compose; break;;
esac
done
fi
fi
}
upgrade_compose() {
if ! type -P docker-compose >/dev/null 2>&1; then
echo "Docker Compose has already been updated to V2"
return
fi
echo "Updating Docker Compose to V2"
if [[ "$__distro" = "ubuntu" ]]; then
if [ "${__os_major_version}" -lt 20 ]; then
echo "${__project_name} cannot update Docker Compose on Ubuntu ${__os_major_version}."
echo "Consider upgrading to 20.04 and then 22.04."
exit 1
fi
if dpkg-query -W -f='${Status}' docker.io 2>/dev/null | grep -q "ok installed"; then
${__auto_sudo} apt-mark manual docker.io
elif dpkg-query -W -f='${Status}' docker-ce 2>/dev/null | grep -q "ok installed"; then
${__auto_sudo} apt-mark manual docker-ce
fi
${__auto_sudo} apt-get remove -y docker-compose
echo "Removed docker-compose"
${__auto_sudo} apt-get update
${__auto_sudo} apt-get install -y docker-compose-v2 docker-buildx
echo "Installed docker-compose-v2"
__old_compose=0
__compose_upgraded=1
elif [[ "$__distro" =~ "debian" ]]; then
${__auto_sudo} apt-get update && ${__auto_sudo} apt-get -y install ca-certificates curl gnupg
if [ "${__os_major_version}" -lt 10 ]; then
echo "${__project_name} cannot update Docker Compose on Debian ${__os_major_version}."
echo "Consider upgrading to 10, then 11 and then 12."
exit 1
fi
if dpkg-query -W -f='${Status}' docker.io 2>/dev/null | grep -q "ok installed"; then
${__auto_sudo} apt-mark manual docker.io
elif dpkg-query -W -f='${Status}' docker-ce 2>/dev/null | grep -q "ok installed"; then
${__auto_sudo} apt-mark manual docker-ce
fi
${__auto_sudo} apt-get remove -y docker-compose
echo "Removed docker-compose"
${__auto_sudo} mkdir -p /etc/apt/keyrings
${__auto_sudo} curl -fsSL https://download.docker.com/linux/debian/gpg | ${__auto_sudo} gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg
${__auto_sudo} echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | ${__auto_sudo} tee /etc/apt/sources.list.d/docker.list > /dev/null
${__auto_sudo} apt-get update
${__auto_sudo} apt-get install -y docker-compose-plugin docker-buildx-plugin
echo "Installed docker-compose-plugin"
__old_compose=0
__compose_upgraded=1
else
echo "${__project_name} does not know how to update Docker Compose on $__distro"
fi
}
prep_prometheus() {
# Create custom-prom.yml if it doesn't exist
if [ ! -f "./prometheus/custom-prom.yml" ]; then
$__as_owner touch "./prometheus/custom-prom.yml"
fi
}
check_for_snap() {
if [[ "$__distro" = "ubuntu" && -n "$(which snap)" ]] && snap list 2>/dev/null | grep -qw 'docker'; then
echo
echo "WARNING! Snap Docker package detected. This WILL result in issues."
echo "Removing the package will delete volumes and require a resync."
echo
echo "Doing so is still highly recommended however."
echo
echo "The exact steps depend a little on whether there already is an apt version of Docker installed as well,"
echo "but in a nutshell \"$__me stop\" followed by \"sudo snap remove --purge docker\" followed by a reboot,"
echo "and as needed install docker-ce or docker.io with apt."
echo
echo "Do join us on CryptoManufaktur Discord to work through this issue."
echo
echo "Aborting, this is not safe"
exit 1
fi
}
install() {
check_for_snap
while true; do
read -rp "This will attempt to install Docker and make your user part of the docker group. Do you wish to continue? (no/yes) " yn
case $yn in
[Yy]* ) break;;
* ) echo "Aborting, no changes made"; exit 130;;
esac
done
if [[ "$__distro" = "ubuntu" ]]; then
if [ "${__os_major_version}" -lt 20 ]; then
echo "${__project_name} cannot install Docker on Ubuntu ${__os_major_version}."
echo "Consider upgrading to 20.04 and then 22.04."
exit 1
fi
if [ -z "$(which docker)" ]; then
${__auto_sudo} apt-get update && ${__auto_sudo} apt-get install -y ca-certificates curl gnupg \
chrony pkg-config
${__auto_sudo} mkdir -p /etc/apt/keyrings
${__auto_sudo} curl -fsSL https://download.docker.com/linux/ubuntu/gpg | ${__auto_sudo} gpg --dearmor -o /etc/apt/keyrings/docker.gpg
${__auto_sudo} echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | ${__auto_sudo} tee /etc/apt/sources.list.d/docker.list > /dev/null
${__auto_sudo} apt-get update
${__auto_sudo} apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin \
docker-buildx-plugin
echo "Installed docker-ce and docker-compose-plugin"
else
echo "Docker is already installed"
fi
__groups=$(${__as_owner} groups)
if [[ ! "$__groups" =~ "docker" ]]; then
echo "Making your user part of the docker group"
${__auto_sudo} usermod -aG docker "${OWNER}"
echo "Please run newgrp docker or log out and back in"
else
echo "Your user is already part of the docker group"
fi
elif [[ "$__distro" =~ "debian" ]]; then
if [ -z "$(which docker)" ]; then
${__auto_sudo} apt-get update
${__auto_sudo} apt-get -y install ca-certificates curl gnupg chrony pkg-config
if [ "${__os_major_version}" -lt 10 ]; then
echo "${__project_name} cannot install Docker on Debian ${__os_major_version}."
echo "Consider upgrading to 10, then 11 and then 12."
exit 1
fi
${__auto_sudo} mkdir -p /etc/apt/keyrings
${__auto_sudo} curl -fsSL https://download.docker.com/linux/debian/gpg | ${__auto_sudo} gpg --dearmor -o /etc/apt/keyrings/docker.gpg
${__auto_sudo} echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | ${__auto_sudo} tee /etc/apt/sources.list.d/docker.list > /dev/null
${__auto_sudo} apt-get update
${__auto_sudo} apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin \
docker-buildx-plugin
echo "Installed docker-ce and docker-compose-plugin"
else
echo "Docker is already installed"
fi
__groups=$(${__as_owner} groups)
if [[ ! "$__groups" =~ "docker" ]]; then
echo "Making your user part of the docker group"
${__auto_sudo} usermod -aG docker "${OWNER}"
echo "Please run newgrp docker or log out and back in"
else
echo "Your user is already part of the docker group"
fi
else
echo "${__project_name} does not know how to install Docker on $__distro"
exit 1
fi
return 0
}
# Warn user if space is low, so they can prune
check_disk_space() {
__docker_dir=$(dodocker system info --format '{{.DockerRootDir}}')
__free_space=$(df -P "${__docker_dir}" | awk '/[0-9]%/{print $(NF-2)}')
re='^[0-9]+$'
if ! [[ "${__free_space}" =~ $re ]] ; then
echo "Unable to determine free disk space. This is likely a bug."
echo "df reports $(df -P "${__docker_dir}") and __free_space is ${__free_space}"
exit 70
fi
var="COMPOSE_FILE"
value=$(sed -n -e "s/^${var}=\(.*\)/\1/p" ".env" || true)
# Literal match intended
# shellcheck disable=SC2076
if [[ "${value}" =~ "bor.yml" ]] && [[ "${__free_space}" -lt 104857600 ]]; then
echo
echo "You are running bor and have less than 100 GiB of free disk space."
echo "If Avail reads 40 GiB or more, prune with ./ethd prune-bor:"
echo
df -h "${__docker_dir}"
echo
echo "If it's less than 40 GiB, you may need to resync Bor from scratch."
echo
elif [[ "${__free_space}" -lt 52428800 ]]; then
echo
echo "You have less than 50 GiB of free disk space:"
echo
df -h "${__docker_dir}"
echo
echo "Pruning does not appear an option for your client mix."
echo
fi
}
nag_os_version() {
if [[ "$__distro" = "ubuntu" ]]; then
if [ "${__os_major_version}" -lt 20 ]; then
echo
echo "Ubuntu ${__os_major_version} is older than the recommended 22.04 or 20.04 version."
echo
fi
fi
if [[ "$__distro" =~ "debian" ]]; then
if [ "${__os_major_version}" -lt 10 ]; then
echo
echo "Debian ${__os_major_version} is older than the recommended 10, 11 or 12 version."
echo
fi
fi
}
# Arguments are passed, but shellcheck doesn't recognize that
# shellcheck disable=SC2120
update() {
__during_update=1
if [[ $(${__as_owner} git status --porcelain) ]]; then
__dirty=1
else
__dirty=0
fi
__free_space=$(df -P "$(pwd)" | awk '/[0-9]%/{print $(NF-2)}')
re='^[0-9]+$'
if ! [[ "${__free_space}" =~ $re ]] ; then
echo "Unable to determine free disk space. This is likely a bug."
echo "df reports $(df -P "$(pwd)") and __free_space is ${__free_space}"
exit 70
elif [ "$(df -P "$(pwd)" | awk '/[0-9]%/{print $(NF-2)}')" -lt 1024 ]; then
echo "You have less than 1 MiB of space left on $(pwd)."
echo "Aborting, as an update is not safe."
exit 1
fi
__docker_dir=$(dodocker system info --format '{{.DockerRootDir}}')
__free_space=$(df -P "${__docker_dir}" | awk '/[0-9]%/{print $(NF-2)}')
re='^[0-9]+$'
if ! [[ "${__free_space}" =~ $re ]] ; then
echo "Unable to determine free disk space. This is likely a bug."
echo "df reports $(df -P "${__docker_dir}") and __free_space is ${__free_space}"
exit 70
elif [ "${__free_space}" -lt 1048576 ]; then
echo "You have less than 1 GiB of space left on ${__docker_dir}."
echo "Aborting, as an update is not safe."
exit 1
fi
if [ -z "${ETHDSECUNDO-}" ]; then
# Do not track changes to ext-network.yml
set +e
${__as_owner} git update-index --assume-unchanged ext-network.yml
${__as_owner} git config pull.rebase false
var="SCRIPT_TAG"
value=$(sed -n -e "s/^${var}=\(.*\)/\1/p" "${ENV_FILE}" || true)
if [ -z "${value}" ] || [ "${value}" = "latest" ]; then
export ETHDPINNED=""
__branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "${__branch}" =~ ^tag-v[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
git checkout main
fi
${__as_owner} git pull
else
export ETHDPINNED="${value}"
${__as_owner} git fetch --tags
${__as_owner} git checkout -B "tag-${value}" "tags/${value}"
fi
export GITEXITCODE=$?
set -e
# BASH_SOURCE so newer code gets to do the update. Use an ENV var
# to avoid infinite loop
export ETHDSECUNDO=1
exec "${BASH_SOURCE[0]}" update "$@"
fi
envmigrate
dodocker system prune --force
docompose --profile tools pull
docompose --profile tools build --pull
if [ "${__migrated}" -eq 1 ] && ! cmp -s "${ENV_FILE}" "${ENV_FILE}".source; then
cp "${ENV_FILE}".source "${ENV_FILE}".bak
rm "${ENV_FILE}".source
echo "Your ${ENV_FILE} configuration settings have been migrated to a fresh copy. You can \
find the original contents in ${ENV_FILE}.bak."
echo
echo "List of changes made to ${ENV_FILE} during migration - current on left, original on right:"
echo
diff -y --suppress-common-lines "${ENV_FILE}" "${ENV_FILE}".bak || true
else
echo "No changes made to ${ENV_FILE} during update"
if [ -f "${ENV_FILE}".source ]; then
${__as_owner} rm "${ENV_FILE}".source || true
fi
fi
echo
if [ -z "${GITEXITCODE+x}" ] || [ "${GITEXITCODE}" -eq 0 ]; then
echo "A \"$__me up\" command will start using the new images and configuration."
else
echo "WARNING"
echo
echo "Updating ${__project_name} failed during \"git pull\""
echo
echo "Please try \"git pull\" manually."
echo "Do not run \"$__me up\" until git can update ${__project_name}."
echo "The current partial update risks startup failure."
fi
nag_os_version
unset ETHDSECUNDO
unset GITEXITCODE
check_for_snap
if [ "${__dirty}" -eq 1 ]; then
echo
echo "WARNING"
echo
echo "You have uncommitted local changes to ${__project_name}, which may interfere with updates."
echo "Please undo these changes or \"git commit\" them."
echo "These are the files with local changes:"
echo
${__as_owner} git status --porcelain
fi
if [ -n "${ETHDPINNED:-}" ]; then
echo "${__project_name} version is pinned to ${ETHDPINNED} in \".env\"."
echo "Please make sure to run compatible client versions."
fi
__during_update=0
}
envmigrate() {
if [ ! -f "./${ENV_FILE}" ]; then
return
fi
ALL_VARS=( COMPOSE_FILE BOR_NODE_ID NETWORK HEIMDALL_ETH_RPC_URL HEIMDALL_TAG BOR_TAG BOR_SRC_TAG BOR_DOCKER_FILE \
BOR_CACHE HEIMDALL_SNAPSHOT_FILE BOR_FULL_NODE_SNAPSHOT_FILE LOG_LEVEL DOMAIN ACME_EMAIL TX_LOCAL_ADDR \
BOR_TRUSTED_NODES CF_DNS_API_TOKEN CF_ZONE_API_TOKEN CF_ZONE_ID AWS_PROFILE AWS_HOSTED_ZONE_ID GRAFANA_HOST \
RPC_HOST RPC_LB WS_HOST WS_LB DDNS_SUBDOMAIN DDNS_PROXY HEIMDALL_SEEDS HEIMDALL_BOR_RPC_URL BOR_BOOTNODES \
HEIMDALL_P2P_PORT BOR_P2P_PORT BOR_RPC_PORT BOR_WS_PORT TRAEFIK_WEB_HTTP_PORT TRAEFIK_WEB_PORT GRAFANA_PORT \
HEIMDALL_RPC_PORT NODE_EXPORTER_IGNORE_MOUNT_REGEX BOR_EXTRAS BOR_SNAPSHOT HEIMDALL_SNAPSHOT )
OLD_VARS=( CF_API_TOKEN EL_HOST EL_LB EL_WS_HOST EL_WS_LB )
NEW_VARS=( CF_DNS_API_TOKEN RPC_HOST RPC_LB WS_HOST WS_LB )
var=ENV_VERSION
__target_ver=$(sed -n -e "s/^${var}=\(.*\)/\1/p" "default.env" || true)
__source_ver=$(sed -n -e "s/^${var}=\(.*\)/\1/p" "${ENV_FILE}" || true)
if [[ "${__target_ver}" = "${__source_ver}" ]]; then # No changes in template, do nothing
return 0
fi
echo "Migrating ${ENV_FILE} to version ${__target_ver}"
${__as_owner} cp "${ENV_FILE}" "${ENV_FILE}".source
__during_migrate=1
__migrated=1
${__as_owner} cp default.env "${ENV_FILE}"
# Migrate over user settings
for var in "${ALL_VARS[@]}"; do
value=$(sed -n -e "s/^${var}=\(.*\)/\1/p" "${ENV_FILE}.source" || true)
# Adjust for vars that are possibly empty
if [ -n "${value}" ] || [ "${var}" = "BOR_SNAPSHOT" ] || [ "${var}" = "HEIMDALL_SNAPSHOT" ]; then
sed -i'.original' -e "s~^\(${var}\s*=\s*\).*$~\1${value}~" "${ENV_FILE}"
fi
done
# Move value from old variable name(s) to new one(s)
for index in "${!OLD_VARS[@]}"; do
var=${OLD_VARS[index]}
value=$(sed -n -e "s/^${var}=\(.*\)/\1/p" "${ENV_FILE}.source" || true)
if [ -n "${value}" ]; then
sed -i'.original' -e "s~^\(${NEW_VARS[index]}\s*=\s*\).*$~\1${value}~" "${ENV_FILE}"
fi
done
rm "${ENV_FILE}".original
__during_migrate=0
}
prune-bor() {
__non_interactive=0
while :
do
if [ -z ${1+x} ]; then
break
fi
case "$1" in
--non-interactive)
__non_interactive=1
shift
;;
*)
echo "Error: Unknown option: $1" >&2
exit 1
;;
esac
done
if [ ! -f "./${ENV_FILE}" ]; then
echo "${ENV_FILE} configuration file not found, aborting."
exit 1
fi
if ! grep -q '^COMPOSE_FILE=.*bor\.yml' "${ENV_FILE}" 2>/dev/null ; then
echo "You do not appear to be using Bor, aborting."
exit 1
fi
if [ "$(df -P /var/lib/docker | awk '/[0-9]%/{print $(NF-2)}')" -lt 41943040 ]; then
echo "You do not have enough free disk space. Make sure this reads at least 40G free (Avail):"
df -h /var/lib/docker
echo ""
echo "Aborting."
exit 1
fi
rpc_port=8545
sync_status=$(cmd exec -T bor wget -qO- "http://localhost:$rpc_port" --header 'Content-Type: application/json' --post-data '{"jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1}')
exitstatus=$?
if [ $exitstatus -ne 0 ]; then
echo "Bor is not running, aborting."
exit 1
fi
if [[ ! "${sync_status}" =~ "false" ]]; then
echo "Bor is not done syncing yet. Sync status:"
echo "${sync_status}"
echo ""
echo "Aborting."
exit 1
fi
node_logs=$(cmd logs --no-color --tail 1000 bor)
if [[ "${node_logs}" =~ "snapshot generation" && ! "${node_logs}" =~ "Generated state" ]]; then
echo "Bor has not finished generating a state snapshot yet, aborting."
exit 1
fi
regex='\[([0-9][0-9]-[0-9][0-9])\|([0-9][0-9]:[0-9][0-9]):[0-9.]*\][[:space:]]*Generated state'
if [[ "${node_logs}" =~ $regex ]]; then
snap_time=$(date -d "1970-${BASH_REMATCH[1]} ${BASH_REMATCH[2]}" +"%s")
now_time=$(date +"%s")
if [ $((snap_time+2100)) -gt "$now_time" ]; then
echo "Please wait 35 minutes after snapshot generation finished. Aborting."
exit 1
fi
fi
if [ $__non_interactive = 0 ]; then
while true; do
read -rp "WARNING - this will stop Bor and prune its database. Do you wish to continue? (No/Yes) " yn
case $yn in
[Yy]es ) break;;
* ) echo "Aborting, no changes made"; exit 1;;
esac
done
fi
echo
echo "Starting Bor prune"
echo
cmd run --rm set-prune-marker "touch /var/lib/bor/prune-marker"
cmd stop bor && cmd rm -f bor
start
echo
echo "Prune is running, you can observe it with './ethd logs -f bor'"
echo
echo "There should be three stages: \"Iterating state snapshot\", \"Pruning state data\", and \"Compacting database\". During \
the \"Compacting database\" stage it may not output logs for an hour or so, that is normal."
echo
echo "When prune is done, which may take ~17-20 hours, Bor will automatically start again."
echo
}
upgrade() {
update
}
start() {
docompose up -d --remove-orphans "$@"
}
up() {
start "$@"
}
run() {
start "$@"
}
stop() {
docompose down --remove-orphans "$@"
}
# Passed by user
# shellcheck disable=SC2120
down() {
stop "$@"
}
restart() {
stop "$@"
start "$@"
}
logs() {
docompose logs "$@"
}
cmd() {
docompose "$@"
}
terminate() {
if [ -z "$(dodocker volume ls -q -f "name=^$(basename "$(realpath .)")_[^_]+")" ]; then
echo "There are no data stores - Docker volumes - left to remove for this ${__app_name}."
stop
exit 0
fi
while true; do
read -rp "WARNING - this action will destroy all data stores for this ${__app_name}. Do you wish to continue? (No/Yes) " yn
case $yn in
[Yy]es ) break;;
* ) echo "Aborting, no changes made"; exit 130;;
esac
done
down
# In this case I want the word splitting, so rm can remove all volumes
# shellcheck disable=SC2046
dodocker volume rm $(dodocker volume ls -q -f "name=^$(basename "$(realpath .)")_[^_]+")
echo
echo "All containers stopped and all volumes deleted"
echo
}
handle_error() {
if [[ ! $- =~ e ]]; then
# set +e, do nothing
return 0
fi
local exit_code=$1
local line_no=$2
if [ "$exit_code" -eq 130 ]; then
echo "$__me terminated by user"
else
echo "$__me terminated with exit code $exit_code on line $line_no"
if [ -n "${__command}" ]; then
echo "This happened during $__me ${__command} ${__params}"
fi
fi
if [ "$__during_update" -eq 1 ] && [ "$__during_migrate" -eq 1 ]; then
cp "${ENV_FILE}" "${ENV_FILE}".partial
cp "${ENV_FILE}".source "${ENV_FILE}"
echo
echo "Restored your ${ENV_FILE} file, to undo partial migration. Please verify it looks correct."
echo "The partially migrated file is in ${ENV_FILE}.partial for troubleshooting."
fi
}
version() {
# ethd version
grep "^This is" README.md
echo ""
var="COMPOSE_FILE"
value=$(sed -n -e "s/^${var}=\(.*\)/\1/p" ".env" || true)
# Client versions
case "${value}" in
*bor.yml* )
echo "Heimdall"
docompose exec heimdalld heimdalld version
echo
echo "Bor"
docompose exec bor bor version
echo
;;&
*grafana.yml* )
docompose exec prometheus /bin/prometheus --version
echo
echo -n "Grafana "
docompose exec grafana /run.sh -v
echo
;;&
esac
}
help() {
echo "usage: ${__me} [-h|--help] <command>"
echo
echo "commands:"
echo " install"
echo " attempts to install Docker and Docker Compose for you"
echo " update "
echo " updates all client versions and ${__project_name} itself"
echo " up|start"
echo " starts the ${__app_name}, or restarts containers that had their image or"
echo " configuration changed"
echo " down|stop [service-name]"
echo " stops the ${__app_name}, or a specific service by name"
echo " restart [service-name]"
echo " restarts the ${__app_name}, or a specific service by name, a combination of down and up"
echo " version"
echo " prints the version(s) of currently running client(s)"
echo " logs"
echo " shows logs"
echo " cmd <command>"
echo " executes an arbitrary Docker Compose command. Use \"cmd help\" to list them"
echo " terminate"
echo " stops the ${__app_name} and destroys all data stores"
echo " prune-bor"
echo " stops the Bor execution client and prunes its DB. This takes about 20 hours"
echo " help"
echo " print this help screen"
echo
echo
echo "The logs command can be appended by flags and specify the container(s). example: "
echo
echo " $__me logs -f --tail 50 ${__sample_service}"
echo " shows logs only for ${__sample_service} service"
echo
echo
echo "$__me will attempt to use sudo as needed for Docker commands"
echo
}
# Main body from here
ENV_FILE=.env
__during_update=0
__during_migrate=0
__migrated=0
__command=""
__me="./$(basename "${BASH_SOURCE[0]}")"
trap 'handle_error $? $LINENO' ERR
if [[ "$#" -eq 0 || "$1" == "-h" || "$1" == "--help" ]]; then
help
exit 0
fi
cd "$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
# Use this to make sure root doesn't end up owning files
if [[ "$OSTYPE" == "darwin"* ]]; then
# This is a macOS-ism
# shellcheck disable=SC2016
OWNER=$(stat -f '$Su' .)
else
OWNER=$(stat -c '%U' .)
fi
if [ "${OWNER}" == "root" ]; then
echo "Please install ${__project_name} as a non-root user."
exit 0
fi
__command="$1"
shift
__params=$*
handle_root
prep_prometheus
determine_distro
if [ "$__command" = "install" ]; then
$__command "$@"
exit "$?"
fi
handle_docker_sudo
check_compose_version
if [ "${__old_compose}" -eq 1 ]; then
echo
echo "You are using docker-compose ${__compose_version}, which is unsupported by Docker, Inc."
echo "${__project_name} only supports Compose V2."
echo
echo "You can install it with \"sudo apt update && sudo apt install docker-compose-v2\"."
echo "You can remove the old docker-compose:"
echo "\"sudo apt-mark manual docker.io && sudo apt --autoremove remove docker-compose\"."
exit 0
fi
if ! dodocker images >/dev/null 2>&1; then
echo "Please ensure you can call $__docker_exe before running ${__me}."
exit 1
fi
if ! docompose --help >/dev/null 2>&1; then
echo "Please ensure you can call $__compose_exe before running ${__me}."
exit 1
fi
case "$__command" in
help|update|up|start|down|stop|restart|version|logs|cmd|terminate|prune-bor)
$__command "$@";;
*)
echo "Unrecognized command $__command"
help
;;
esac
check_disk_space
if [ "${__compose_upgraded}" -eq 1 ]; then
echo
echo "You updated Docker Compose to V2."
echo "The \"docker-compose\" command is gone and replaced with \"docker compose\"."
echo
echo "You can create an alias for \"docker-compose\" by adding this line to your \"~/.profile\":"
echo "alias docker-compose=\"docker compose\""
echo
echo "Optionally, you can switch to docker-ce."
echo "Please see https://ethdocker.com/Usage/Prerequisites#switching-from-dockerio-to-docker-ce for instructions."
fi