-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
wait-for-sync.sh
executable file
·87 lines (71 loc) · 1.95 KB
/
wait-for-sync.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
#!/usr/bin/env bash
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# wait-for-sync.sh
#
# Wait for an ogmios / cardano-node to be synchronized with the network, up to a given threshold.
#
# Usage: ./wait-for-sync.sh OGMIOS_PORT THRESHOLD
#
# Examples:
# ./wait-for-sync.sh 1337 1
# ./wait-for-sync.sh 1337 0.95
set -eo pipefail
exitWithUsage () {
echo -e "Error: missing argument(s)!\n"
echo -e "Usage: $0 OGMIOS_PORT THRESHOLD"
echo -e " Wait until a running Ogmios server at OGMIOS_PORT reaches THRESHOLD network synchronization.\n"
echo -e "Example: \n $0 1337 0.95"
exit 1
}
OGMIOS_PORT=$1
if [ -z "$OGMIOS_PORT" ]; then
exitWithUsage
fi
THRESHOLD=$2
if [ -z "$THRESHOLD" ]; then
exitWithUsage
fi
URL=http://localhost:$OGMIOS_PORT/health
showProgress () {
N="$1"
PER=$(printf "%.3f\n" "$(bc <<< "$N * 100")")
LEN=$(printf "%.0f\n" "$(bc <<< "$N * 50")")
BAR=""
for ((i=1; i<=$LEN; i++))
do
BAR="$BAR▣"
done
for ((i=$LEN; i<=50; i++))
do
BAR="$BAR "
done
echo -en "Network synchronization: [$BAR] $PER%\r"
}
for (( ;; ))
do
HEALTH=$(curl -sS $URL)
CONNECTION_STATUS=$(sed 's/.*"connectionStatus":"\([a-z]\+\)".*/\1/' <<< $HEALTH)
if ! [[ $CONNECTION_STATUS = "connected" ]] ; then
echo "Waiting for node.socket..."
if [[ -n "$3" ]] ; then
eval $3
fi
sleep 5
else
NETWORK_SYNCHRONIZATION=$(sed 's/.*"networkSynchronization":\([0-9]\+\.\?[0-9]*\).*/\1/' <<< $HEALTH)
RE='^[0-9]+\.?[0-9]*$'
if ! [[ $NETWORK_SYNCHRONIZATION =~ $RE ]] ; then
echo "error: unexpected response from /health endpoint: $HEALTH"
exit 1
fi
showProgress $NETWORK_SYNCHRONIZATION
PREDICATE=$(bc <<< "$NETWORK_SYNCHRONIZATION >= $THRESHOLD")
if [ "$PREDICATE" -eq 1 ]; then
exit 0
else
sleep 5
fi
fi
done