-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceremonyclient_setup_macos.sh
executable file
·658 lines (528 loc) · 20.5 KB
/
ceremonyclient_setup_macos.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
#!/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 "Sets up a Quilibrium node on this machine."
echo ""
echo "USAGE: bash ceremonyclient_setup.sh [-h] [-x] [-c] [-d directory]"
echo ""
echo " -h Display this help dialogue."
echo " -x For debugging the script; sets the x shell builtin, 'set -x'."
echo " -c (Optional) 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 to install to 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/\/*$//')
# .localenv file location
LOCALENV="$SCRIPT_ROOT_DIR/.localenv"
# Initialise a .localenv file
# Install dependancies
# If macOS, install homebrew, then git gmp rsync rclone
# If Linux, install git make build-essential libgmp-dev rsync rclone wget curl sudo
# Install Go, Rust, gRPC
# Set up bashrc/zshrc with Go and Rust
# Download node binary, make it executable
# Download qclient binary, make it executable
# Build the service file, load it up
# If part of cluster, build service using start_cluster
# If macOS, launchctl with plutil test and log rotation
# If Linux, systemctl
# Let run for 3 mins, print output of logs
# Config tips & suggestions instructions
# Config gRPC instructions
# If cluster, config cluster instructions
# Instructions on setting up backups
# Check the .localenv file; if it doesn't exist, initialise one
CHECK_LOCALENV_func() {
if [[ $(./tools/ceremonyclient_check_localenv.sh) ]]; then
echo "pass"
else
echo "not pass"
# bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -env-init
fi
exit
return
}
# Install dependancies
# If macOS, install homebrew, then git gmp rsync rclone
# If Linux, install git make build-essential libgmp-dev rsync rclone wget curl sudo
# Install Go, Rust, gRPC
# Set up bashrc/zshrc with Go and Rust
# ZSH is the default macOS terminal
ALTER_MAC_ZSH_PROFILE_func() {
tee ~/.zshrc > /dev/null <<EOF
# Terminal display preferences
autoload -Uz vcs_info
precmd() { vcs_info }
zstyle ':vcs_info:git:*' formats '%b '
setopt PROMPT_SUBST
PROMPT='%F{green}%n@%m%f %F{green}%*%f %F{blue}%~%f %F{red}${vcs_info_msg_0_}%f$ '
# Quil nodes
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
. "$HOME/.cargo/env"
export GO111MODULE=on
export GOPROXY=https://goproxy.cn,direct
export PATH=$PATH:/usr/local/go/bin
EOF
. ~/.zshrc
return
}
# Bash is MY default Linux terminal - adjust this to preference
ALTER_LINUX_BASHRC_PROFILE_func() {
tee ~/.bashrc > /dev/null <<EOF
# Terminal display preferences
PS1='${debian_chroot:+($debian_chroot)}\[\033[0;32m\]\u@\h\[\033[00m\] \[\033[0;32m\]\D{%H:%M:%S}\[\033[00m\] \[\033[0;34m\]\w\[\033[00m\] $ '
# Quil nodes
GOROOT=/usr/local/go
GOPATH=$HOME/go
GO111MODULE=on
GOPROXY=https://goproxy.cn,direct
PATH=$GOPATH/bin:$GOROOT/bin:$PATH
. "$HOME/.cargo/env"
EOF
. ~/.bashrc
return
}
INSTALL_DEPENDANCIES_ALTER_TERMINAL_PROFILES_func() {
MAC_BREW_DEPENDANCIES="git gmp rsync rclone"
LINUX_APT_DEPENDANCIES="git make build-essential libgmp-dev rsync rclone wget curl sudo"
GOLANG_URL="https://go.dev/dl/go1.22.10.$RELEASE_OS-$RELEASE_ARCH.tar.gz"
# If macOS then
if [[ "$RELEASE_OS" == 'darwin' ]]; then
# Install brew, dependancies, Golang, Rust, gRPC
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install "$MAC_BREW_DEPENDANCIES"
curl -s -S "$GOLANG_URL" --output go.tar.gz
tar -xvf go.tar.gz
mv go /usr/local
rm go.tar.gz
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
cargo install uniffi-bindgen-go --git https://github.com/NordSecurity/uniffi-bindgen-go --tag v0.2.2+v0.25.0
go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest
# Adjust zsh profile
ALTER_MAC_ZSH_PROFILE_func
# If Linux then
elif [[ "$RELEASE_OS" == 'linux' ]]; then
# Install dependancies
apt install "$LINUX_APT_DEPENDANCIES"
# Adjust bash profile
ALTER_LINUX_BASH_PROFILE_func
fi
return
}
# Download node binary, make it executable
# Download qclient binary, make it executable
# Download and make executable the node/qclient binaries
DOWNLOAD_INSTALL_BINARIES_func() {
bash $SCRIPT_DIR/tools/ceremonyclient_download.sh -f "$NODE_BINARY"
bash $SCRIPT_DIR/tools/ceremonyclient_download.sh -f "$QCLIENT_BINARY"
return
}
# Build the service file, load it up
# If part of cluster, build service using start_cluster
# If macOS, launchctl with plutil test and log rotation
# If Linux, systemctl
# 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
}
CONFIG_CHANGES_func() {
# Enable gRPC
go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest
bash $SCRIPT_DIR/tools/ceremonyclient_grpc.sh -q -l
bash $SCRIPT_DIR/tools/ceremonyclient_grpc.sh -q -p
# Set maxFrames (frame truncation) to 1001 frames, to save on disk space
sudo sed -i -E 's|maxFrames: .*|maxFrames: 1001|' "$CEREMONYCLIENT_CONFIG_FILE"
return
}
FINISHING_TIPS_func() {
echo ""
if [[ $CLUSTER == 1 ]]; then
echo "Make sure to configure the 'dataWorkersMultiaddrs' section of 'engine' in $CEREMONYCLIENT_CONFIG,"
echo "and that this section is the same ON ALL MACHINES in order for this node to function as part of the cluster."
fi
echo "To set up backups, "
echo ""
return
}
# Instructions on setting up backups
# Set to 1 by using the -q flag; quietens unnecessary output
QUIET=0
# To indicate to the script that this node is being set up as part of a cluster
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:" 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";;
*) 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
# 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
# (macOS only) The logfile that will be used for the ceremonyclient
CEREMONYCLIENT_LOGFILE="$HOME/ceremonyclient.log"
# Ceremonyclient config file location
CEREMONYCLIENT_CONFIG_FILE=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -key "ceremonyclient_config")
# Plist name and file
PLIST_LABEL="local.ceremonyclient"
PLIST_FILE=/Library/LaunchDaemons/$PLIST_LABEL.plist
# Get the latest version numbers of the node and qclient binaries from release
NODE_VERSION=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -latest-version 'node-release-quiet')
QCLIENT_VERSION=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -latest-version 'qclient-release-quiet')
# Get the latest version files of the node and qclient binaries from release
NODE_BINARY=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -latest-version 'node-release-files-quiet')
QCLIENT_BINARY=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -latest-version 'qclient-release-files-quiet')
RELEASE_ARCH=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -arch)
RELEASE_OS=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -os)
RELEASE_LINE="$RELEASE_OS-$RELEASE_ARCH"
FETCH_FILES_func() {
local FILE_PATTERN="$1"
local TYPE="$(echo $FILE_PATTERN | awk -F'-' '{print $1}')_release_url"
local URL=$(bash $SCRIPT_DIR/tools/ceremonyclient_env.sh -key $TYPE)
# List files in most recent release
RELEASE_FILES_AVAILABLE=$(curl -s -S $URL | grep $FILE_PATTERN)
if [[ -z "$RELEASE_FILES_AVAILABLE" ]]; then
echo "Error: no release files relating to $FILE_PATTERN could be found."
echo "This could be due to network issues."
return 1
fi
for RELEASE_FILE in $RELEASE_FILES_AVAILABLE; do
if curl -s -S "https://releases.quilibrium.com/$RELEASE_FILE" > "$CEREMONYCLIENT_NODE_DIR/$RELEASE_FILE"; then
echo "Downloaded and installed file: $RELEASE_FILE."
fi
done
return
}
### NODE BINARY DOWNLOAD
# Set up environment variables (redundant but solves the command go not found error)
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
echo "Downloading node binaries..."
mkdir -p ~/ceremonyclient/node
cd ~/ceremonyclient/node
# List files in most recent release
NODE_RELEASE_FILES=$(curl -s -S https://releases.quilibrium.com/release | grep $RELEASE_LINE)
if [[ -z "$NODE_RELEASE_FILES" ]]; then
echo "Error: No node files found for $RELEASE_LINE"
echo "This could be due to network issues or no releases for your architecture."
exit 1
fi
# For each file in most recent release, download it
for NODE_FILE in $NODE_RELEASE_FILES; do
version=$(echo "$NODE_FILE" | cut -d '-' -f 2)
if curl -s -S "https://releases.quilibrium.com/$NODE_FILE" > "$NODE_FILE"; then
:
fi
done
# Make node binary executable
latest_node_file=$(echo "$NODE_RELEASE_FILES" | grep "$RELEASE_LINE"$)
chmod +x $latest_node_file
sleep 3
### QCLIENT BINARY DOWNLOAD
echo "Downloading qclient binaries..."
mkdir -p ~/ceremonyclient/client
cd ~/ceremonyclient/client
# List files in most recent release
qclientfiles=$(curl -s -S https://releases.quilibrium.com/qclient-release | grep $RELEASE_LINE)
if [[ -z "$qclientfiles" ]]; then
echo "Error: No qclient files found for $RELEASE_LINE"
echo "This could be due to network issues or no releases for your architecture."
exit 1
fi
# For each file in most recent release, download it
for qclientfile in $qclientfiles; do
version=$(echo "$qclientfile" | cut -d '-' -f 2)
if curl -s -S "https://releases.quilibrium.com/$qclientfile" > "$qclientfile"; then
:
fi
done
# Make qclient binary executable
latest_qclient_file=$(echo "$qclientfiles" | grep "$RELEASE_LINE"$)
chmod +x $latest_qclient_file
echo ""
sleep 3
### BUILDING CEREMONYCLIENT SERVICE
echo "Building ceremonyclient service..."
# Use the home directory in the path
NODE_PATH=$HOME/ceremonyclient/node
NODE_BINARY_PATH=$NODE_PATH/$NODE_BINARY
# Set GOMAXPROCS
# LINUX - Calculate GOMAXPROCS based on the number of threads
#GOMAXPROCS=$(nproc)
# MAC - Calculate GOMAXPROCS based on the number of threads
GOMAXPROCS=$(sysctl -n hw.logicalcpu)
# Setup log file
echo "Creating log file..."
rm -rf $CEREMONYCLIENT_LOGFILE
touch $CEREMONYCLIENT_LOGFILE
chmod 644 $CEREMONYCLIENT_LOGFILE
# Create service file
echo "Building service file..."
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>
<key>ProgramArguments</key>
<array>
<string>$NODE_BINARY_PATH</string>
</array>
<key>UserName</key>
<string>$USER</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<dict>
<key>Crashed</key>
<true/>
</dict>
<key>EnvironmentVariables</key>
<dict>
<key>GOMAXPROCS</key>
<string>$GOMAXPROCS</string>
</dict>
<key>ExitTimeOut</key>
<integer>30</integer>
<key>WorkingDirectory</key>
<string>$NODE_PATH</string>
<key>StandardErrorPath</key>
<string>$CEREMONYCLIENT_LOGFILE</string>
<key>StandardOutPath</key>
<string>$CEREMONYCLIENT_LOGFILE</string>
</dict>
</plist>
EOF
# Test service file
echo "Checking service file..."
PLUTIL_TEST=$(plutil -lint $PLIST_FILE)
if [[ $PLUTIL_TEST == "$PLIST_FILE: OK" ]]; then
:
fi
# Configure log rotation
echo "Configuring log rotation for ceremonyclient logs. This requires sudo, so please provide password if asked."
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
# Start up service
echo "Starting the ceremonyclient service..."
sudo launchctl enable system/$PLIST_LABEL
sleep 3
sudo launchctl bootstrap system $PLIST_FILE
sleep 3
launchctl kickstart -kp system/$PLIST_LABEL
sleep 3
echo ""
echo "Node is now set up and running. Leave it to run for about 20 minutes before stopping it."
echo "Node file: $NODE_BINARY_PATH"
echo "Log file: $CEREMONYCLIENT_LOGFILE"
echo "Plist file: $PLIST_FILE"
echo "Use \`tail -F $CEREMONYCLIENT_LOGFILE\` to view in real-time the output of the node."
echo "The logfile is rotated when it reaches 1GB in filesize, and 2 archives are kept. This will result in a total disk usage of 3GB by the logfile and archives."
echo "If you want to add gRPC, you should wait about 20 minutes for the node to sync up, then run the gRPC script."
exit