-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceremonyclient_update.sh
executable file
·348 lines (290 loc) · 12.1 KB
/
ceremonyclient_update.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
# #!/bin/bash
# Set shell options
set -eou pipefail
#set -x # for debugging purposes - this prints the command that is to be executed before the command is executed
USAGE_func() {
echo ""
echo "Updates the Quilibrium node binaries (including qclient) to latest versions, and restarts node daemons."
echo ""
echo "USAGE: bash ceremonyclient_update.sh [-h] [-x] [-c]"
echo ""
echo " -h Display this help dialogue."
echo " -x For debugging the script; sets the x shell builtin, 'set -x'."
echo " -c This node is part of a cluster."
echo " (By default this is set to null, meaning this node is run as a standalone node.)"
echo " -d (Optional) Directory to update binaries in."
echo " By default, the directory for updates is determined by the 'ceremonyclient_node_dir' key in .localenv."
echo ""
exit 0
}
# Figure out what directory I'm in
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$SCRIPT_DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SCRIPT_ROOT_DIR=$(echo "$SCRIPT_DIR" | awk -F'/' 'BEGIN{OFS=FS} {$NF=""; print}' | sed 's/\/*$//')
# Compare the currently installed binary with the latest available binary from release
COMPARE_VERSIONS_func() {
local FILE_INSTALLED=$(echo $1 | awk -F'/' '{print $NF}' | xargs)
local FILE_INSTALLED_PATH="$1"
local FILE_RELEASE="$2"
if [[ "$FILE_INSTALLED" == "$FILE_RELEASE" ]]; then
echo "$FILE_INSTALLED file installed is the latest version, no need to update."
else
echo "Update required for $FILE_INSTALLED."
bash $SCRIPT_DIR/tools/ceremonyclient_download.sh -f "$FILE_RELEASE"
fi
return
}
# Update either the start_cluster script or the actual service file with the new node binary, depending on whether -c was used
UPDATE_SERVICE_FILE_func() {
# If cluster, update start_cluster script
if [[ $CLUSTER == 1 ]]; then
sed -i "s/NODE_BINARY\=[^<]*/NODE_BINARY\=\'$NEW_LATEST_NODE_FILE_INSTALLED_FILENAME\'/" ceremonyclient_start_cluster.sh
# If not cluster, then
else
# If macOS, update launchctl plist file
if [[ "$RELEASE_OS" == "darwin" ]]; then
sudo sed -i "s/node-[^<]*/node-$NEW_LATEST_NODE_FILE_INSTALLED_FILENAME/" /Library/LaunchDaemons/local.ceremonyclient.plist
# If Linux, update systemctl service file
elif [[ "$RELEASE_OS" == "linux" ]]; then
sed -i "/^ExecStart\=.*/c ExecStart\=$NEW_LATEST_NODE_FILE_INSTALLED_PATH" /lib/systemd/system/ceremonyclient.service
fi
fi
return
}
# Function to update the start_cluster script
UPDATE_CLUSTER_FILE_func() {
if [[ $CLUSTER == 1 ]]; then
sed -i "s/NODE_BINARY\=[^<]*/NODE_BINARY\=$NEW_LATEST_NODE_FILE_INSTALLED_FILENAME/" ceremonyclient_start_cluster.sh
fi
return 0
}
# Function to fill the correct 'Program' and 'ProgramArgs' sections of the macOS plist file,
# including a GOMAXPROCS environment variable, depending on whether this node is being set up as part of a cluster or not
PLIST_ARGS_func() {
if [[ $CLUSTER == 1 ]]; then
PLIST_ARGS="<key>Program</key>
<string>$SCRIPT_ROOT_DIR/ceremonyclient_start_cluster.sh</string>
<key>ProgramArguments</key>
<array>
<string>$SCRIPT_ROOT_DIR/ceremonyclient_start_cluster.sh</string>
<string>--core-index-start</string>
<string>$CLUSTER_CORE_INDEX_START</string>
<string>--data-worker-count</string>
<string>$CLUSTER_DATA_WORKER_COUNT</string>
</array>
<key>WorkingDirectory</key>
<string>$SCRIPT_ROOT_DIR</string>"
else
PLIST_ARGS="<key>Program</key>
<string>$CEREMONYCLIENT_NODE_DIR/$NODE_BINARY</string>
<key>ProgramArguments</key>
<string>$CEREMONYCLIENT_NODE_DIR/$NODE_BINARY</string>
<key>WorkingDirectory</key>
<string>$CEREMONYCLIENT_NODE_DIR</string>
<key>EnvironmentVariables</key>
<dict>
<key>GOMAXPROCS</key>
<string>$GOMAXPROCS</string>
</dict>"
fi
return
}
BUILD_MAC_LAUNCHCTL_PLIST_FILE_func() {
# Calculate GOMAXPROCS based on the number of threads
GOMAXPROCS=$(sysctl -n hw.logicalcpu)
# If cluster, update the ceremonyclient_start_cluster.sh file with the right details
# so it can be used in the plist file
if [[ $CLUSTER == 1 ]]; then
UPDATE_CLUSTER_FILE_func
fi
# Setup log file
rm -rf $CEREMONYCLIENT_LOGFILE
touch $CEREMONYCLIENT_LOGFILE
chmod 644 $CEREMONYCLIENT_LOGFILE
# Generate the plist file arguments that change depending on whether this is a cluster node or not
PLIST_ARGS_func
tee $PLIST_FILE > /dev/null <<EOF
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
<key>Label</key>
<string>$PLIST_LABEL</string>
$PLIST_ARGS
<key>UserName</key>
<string>$USER</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<dict>
<key>Crashed</key>
<true/>
</dict>
<key>ExitTimeOut</key>
<integer>30</integer>
<key>StandardErrorPath</key>
<string>$CEREMONYCLIENT_LOGFILE</string>
<key>StandardOutPath</key>
<string>$CEREMONYCLIENT_LOGFILE</string>
</dict>
</plist>
EOF
# Test service file
PLUTIL_TEST=$(plutil -lint $PLIST_FILE)
if [[ $PLUTIL_TEST == "$PLIST_FILE: OK" ]]; then
:
else
echo "Error: plutil test on $PLIST_FILE file failed. Results below:"
echo "$PLUTIL_TEST"
return 1
fi
# Configure log rotation
sudo tee /etc/newsyslog.d/$PLIST_LABEL.conf > /dev/null <<EOF
# logfilename [owner:group] mode count size when flags [/pid_file] [sig_num]
$CEREMONYCLIENT_LOGFILE robbie:staff 644 3 1024 * JG
EOF
return
}
SYSTEMCTL_SERVICE_FILE_ARGS_func() {
# If cluster, update the ceremonyclient_start_cluster.sh file with the right details
# so it can be used in the systemctl service file
if [[ $CLUSTER == 1 ]]; then
SYSTEMCTL_SERVICE_FILE_ARGS="ExecStart=$SCRIPT_ROOT_DIR/ceremonyclient_start_cluster.sh --core-index-start $CLUSTER_CORE_INDEX_START --data-worker-count $CLUSTER_DATA_WORKER_COUNT"
else
SYSTEMCTL_SERVICE_FILE_ARGS="ExecStart=$CEREMONYCLIENT_NODE_DIR/$NODE_BINARY
Environment='GOMAXPROCS=$GOMAXPROCS'"
fi
return
}
BUILD_LINUX_SYSTEMCTL_SERVICE_FILE_func() {
# Calculate GOMAXPROCS based on the number of threads
GOMAXPROCS=$(nproc)
if [[ $CLUSTER == 1 ]]; then
UPDATE_CLUSTER_FILE_func
fi
# Generate the systemctl service file arguments that change depending on whether this is a cluster node or not
SYSTEMCTL_SERVICE_FILE_ARGS_func
tee /lib/systemd/system/ceremonyclient.service > /dev/null <<EOF
[Unit]
Description=ceremonyclient service
[Service]
Type=simple
Restart=always
RestartSec=5s
WorkingDirectory=$CEREMONYCLIENT_NODE_DIR
$SYSTEMCTL_SERVICE_FILE_ARGS
KillSignal=SIGINT
TimeoutStopSec=30s
[Install]
WantedBy=multi-user.target
EOF
return
}
ALTER_RELOAD_RESTART_DAEMONS_func() {
NEW_LATEST_NODE_FILE_INSTALLED_PATH=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -latest-version 'node-installed-files-quiet')
NEW_LATEST_NODE_FILE_INSTALLED_FILENAME=$(echo "$NEW_LATEST_NODE_FILE_INSTALLED_PATH" | awk -F'/' '{print $NF}' | xargs)
# If macOS, then update launchctl plist file and restart service
# Using launchctl commands 'bootout' and 'bootstrap' instead of the deprecated 'load' and 'unload' commands
if [[ "$RELEASE_OS" == "darwin" ]]; then
BUILD_MAC_LAUNCHCTL_PLIST_FILE_func
# Enable, load and start service
sudo launchctl enable system/local.ceremonyclient
sleep 2
sudo launchctl bootstrap system /Library/LaunchDaemons/local.ceremonyclient.plist
sleep 2
# Use kickstart with the -k flag to kill any currently running ceremonyclient services,
# and -p flag to print the PID of the service that starts up
# This ensures only one ceremonyclient service running
launchctl kickstart -kp system/local.ceremonyclient
# Let service sit for 60s, then print out the logfile
echo "ceremonyclient daemon updated and restarted. Waiting 2 minutes before printing from the logfile ceremonyclient."
sleep 120
tail -200 "$CEREMONYCLIENT_LOGFILE"
echo "---- End of logs print ----"
echo ""
# If Linux, then update systemctl service file and restart service
elif [[ "$RELEASE_OS" == "linux" ]]; then
BUILD_LINUX_SYSTEMCTL_SERVICE_FILE_func
# Enable, load and start service
systemctl daemon-reload
sleep 2
systemctl start ceremonyclient
# Let service sit for 60s, then print out the logfile
echo "ceremonyclient service updated and reloaded. Waiting 2 minutes before printing from the logfile ceremonyclient."
sleep 120
journalctl --unit=ceremonyclient.service -n 200
echo "---- End of logs print ----"
echo ""
fi
return
}
# Set to 1 by using the -q flag; quietens unnecessary output
QUIET=0
# Set to 1 by using the -c flag; indicates that this node is running as part of a cluster
# This simply means that when it comes to updating daemon/service files, this script will update the
# ceremonyclient_start_cluster.sh script with the new node filename, and not the daemon/service file.
CLUSTER=0
# Filled with data by using -C and -D; for setting up node as part of cluster
CLUSTER_CORE_INDEX_START=0
CLUSTER_DATA_WORKER_COUNT=0
# Supply a node directory using the -d flag
DIRECTORY=0
while getopts "xhqcC:D:d" opt; do
case "$opt" in
x) set -x;;
h) USAGE_func; exit 0;;
q) QUIET=1;;
c) CLUSTER=1;;
C) CLUSTER_CORE_INDEX_START="$OPTARG";;
D) CLUSTER_DATA_WORKER_COUNT="$OPTARG";;
d) DIRECTORY="$OPTARG";;
*) USAGE_func; exit 0;;
esac
done
shift $((OPTIND -1))
# Make sure that if -c is used, -C and -D are also supplied
if [[ "$CLUSTER" == 1 ]]; then
if [[ "$CLUSTER_CORE_INDEX_START" == 0 || "$CLUSTER_DATA_WORKER_COUNT" == 0 ]]; then
echo "Error: when using -c to indicate that this node is being set up as part of a cluster,"
echo "please also use the [-C core index] and [-D number of data workers] flags."
exit 1
fi
:
else
:
fi
# Make sure .localenv is in order; if not, exit
if $(bash $SCRIPT_DIR/tools/ceremonyclient_check_localenv.sh -q); then
:
else
bash $SCRIPT_DIR/tools/ceremonyclient_check_localenv.sh
exit 1
fi
# The OS of the machine running this script
RELEASE_OS=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -os)
# The release line ('os-arch') of the machine running this script
RELEASE_LINE=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -release-line)
# For the ceremonyclient node directory
# If a directory was supplied via the -d option, use it
# Otherwise, use the directory in the .localenv
if [[ $DIRECTORY == 0 ]]; then
CEREMONYCLIENT_NODE_DIR=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -key "ceremonyclient_node_dir")
else
CEREMONYCLIENT_NODE_DIR="$DIRECTORY"
fi
# Logfile location
CEREMONYCLIENT_LOGFILE="$HOME/ceremonyclient.log"
# Get the latest version of the main node and qclient binaries,
# both installed and available in release
LATEST_NODE_INSTALLED=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -latest-version 'node-installed-files-quiet')
LATEST_NODE_RELEASE=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -latest-version 'node-release-files-quiet')
LATEST_QCLIENT_INSTALLED=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -latest-version 'qclient-installed-files-quiet')
LATEST_QCLIENT_RELEASE=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -latest-version 'qclient-release-files-quiet')
COMPARE_VERSIONS_func "$LATEST_NODE_INSTALLED" "$LATEST_NODE_RELEASE"
COMPARE_VERSIONS_func "$LATEST_QCLIENT_INSTALLED" "$LATEST_QCLIENT_RELEASE"
ALTER_RELOAD_RESTART_DAEMONS_func
exit