-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbootstrap
executable file
·589 lines (543 loc) · 26.8 KB
/
bootstrap
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
#!/bin/bash -x
##
## © verus.io 2018-2024, released under MIT license
## Script written in 2021 by Oink.vrsc@
## Script maintained by Oink.vrsc@
##
## This script creates bootstrap archives from the locally running chain in
## tar.gz and zip format.
## After the acrves are made, the MD5, SHA256 and SHA512 checksums are
## calculated.
## Of the archives a Verus Signature will be created if that option is enabled
## in the <COINTICKER>.json
## It creates a basic page, based on the customizable
## `bootstrap.html.template` with this data:
## - Cointicker and coin name
## - Basic links to the project
## - Bootstrap data like creation timestamp, blockheight and best hash
## - The bootstrap archives, their checksums and signature
## After staging the page, it will move the current bootstrap webfolder to
## `last.<cointicker>-bootstrap` and the staging folder will be renamed
## to `<cointicker>-bootstrap` in the web root location supplied in the
## json for the coin.
##
## Basic options in the json:
## - "sign": true|false (sign with VerusID)
## - "archive": true|false (tar the webfolder for external deployment)
## - "reindex": true|false (reindex the chain after creating bootstrap)
## - "links": list of tags and links to display on the webpage
##Check for a complete description of all options.
##
## Usage: `./bootstrap <Cointicker>`
## the `<CoinTicker>.json` must be present in the same folder as the
## `bootstrap` script. Configure your own `<CoinTicker>.json` and fill with
## the required information. The entered values are case sensitive and paths
## are absolute paths.
## Supply a `<CoinTicker>.png` in the `img` folder for a logo on the page.
## if no command line argument is entered, the script will exit.
## check for command line input
FILE=$1
if [ -z "$FILE" ]; then
echo "No coin supplied to create Bootstrap for."
exit 1
fi
## needed binaries
# Dependencies: jq, zip, curl, bc and tr.
if ! command -v jq &>/dev/null ; then
echo "jq not found. please install using your package manager."
exit 1
fi
if ! command -v zip &>/dev/null ; then
echo "zip not found. please install using your package manager."
exit 1
fi
if ! command -v curl &>/dev/null ; then
echo "curl not found. please install using your package manager."
exit 1
fi
if ! command -v bc &>/dev/null ; then
echo "bc not found. please install using your package manager."
exit 1
fi
if ! command -v tr &>/dev/null ; then
echo "tr not found. please install using your package manager."
exit 1
fi
ZIP=$(which zip)
JQ=$(which jq)
CURL=$(which curl)
BC=$(which bc)
TR=$(which tr)
## Set script folder
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
## Variables declaration
COIN=$(${JQ} -r .coin "${SCRIPT_DIR}/${FILE}.json")
COIN_NAME=$(${JQ} -r .coin_name "${SCRIPT_DIR}/${FILE}.json")
DAEMON_USER=$(${JQ} -r .daemon_user "${SCRIPT_DIR}/${FILE}.json")
DAEMON=$(${JQ} -r .daemon "${SCRIPT_DIR}/${FILE}.json")
RPC_CLIENT=$(${JQ} -r .rpc_client "${SCRIPT_DIR}/${FILE}.json")
CUSTOM_CHAIN_FOLDER=$(${JQ} -r .custom_chain_folder "${SCRIPT_DIR}/${FILE}.json")
TEMPLATE_FOLDER=$(${JQ} -r .template_folder "${SCRIPT_DIR}/${FILE}.json")
STAGING_FOLDER=$(${JQ} -r .staging_folder "${SCRIPT_DIR}/${FILE}.json")
WEB_ROOT=$(${JQ} -r .web_root "${SCRIPT_DIR}/${FILE}.json")
SIGNEE=$(${JQ} .signee "${SCRIPT_DIR}/${FILE}.json")
REINDEX=$(${JQ} .reindex "${SCRIPT_DIR}/${FILE}.json")
SIGN=$(${JQ} .sign "${SCRIPT_DIR}/${FILE}.json")
DISTRIBUTION_TYPE=$(${JQ} -r .distribution_type "${SCRIPT_DIR}/${FILE}.json")
ARCHIVE=$(${JQ} .archive "${SCRIPT_DIR}/${FILE}.json")
FORK_CHECK=$(${JQ} .fork_check "${SCRIPT_DIR}/${FILE}.json")
DEPLOY_EXTERNALLY=$(${JQ} .deploy_externally "${SCRIPT_DIR}/${FILE}.json")
if [ "$FORK_CHECK" == "true" ]; then
EXPLORER=$(${JQ} -r .explorer_url "${SCRIPT_DIR}/${FILE}.json")
fi
RESYNC=$(${JQ} .resync "${SCRIPT_DIR}/${FILE}.json")
if [ "$RESYNC" == "true" ]; then
DOW=$(${JQ} -r .resync_day "${SCRIPT_DIR}/${FILE}.json")
if [ "$DOW" == "$(date +%u)" ] || [ "$DOW" == "daily" ] ; then
REINDEX="false"
else
RESYNC="false"
fi
fi
if [[ "$DISTRIBUTION_TYPE" == "tar" || "$DISTRIBUTION_TYPE" == "zip" || "$DISTRIBUTION_TYPE" == "both" ]]
then
echo "Distribution type $DISTRIBUTION_TYPE accepted."
else
DISTRIBUTION_TYPE="tar"
fi
## Sanity checks
# Does the user in the json exist?
if id -u "${DAEMON_USER}" >/dev/null 2>&1; then
echo "\"daemon_user\": \"${DAEMON_USER}\" specified in ${SCRIPT_DIR}/${FILE}.json exists."
else
echo "\"daemon_user\": \"${DAEMON_USER}\" specified in ${SCRIPT_DIR}/${FILE}.json DOES NOT EXIST."
exit 1
fi
# Does the daemon in the json exist?
if [[ -f "${DAEMON}" ]]; then
echo "\"daemon\": \"${DAEMON}\" specified in ${SCRIPT_DIR}/${FILE}.json exists."
else
echo "\"daemon\": \"${DAEMON}\" specified in ${SCRIPT_DIR}/${FILE}.json DOES NOT EXIST."
exit 1
fi
# Does the RPC-client in the json exist?
if [[ -f "${RPC_CLIENT}" ]]; then
echo "\"rpc_client\": \"${RPC_CLIENT}\" specified in ${SCRIPT_DIR}/${FILE}.json exists."
else
echo "\"rpc_client\": \"${RPC_CLIENT}\" specified in ${SCRIPT_DIR}/${FILE}.json DOES NOT EXIST."
exit 1
fi
# Does the template folder in the json exist?
if [[ -d "${TEMPLATE_FOLDER}" ]]; then
echo "\"template_folder\": \"${TEMPLATE_FOLDER}\" specified in ${SCRIPT_DIR}/${FILE}.json exists."
else
echo "\"template_folder\": \"${TEMPLATE_FOLDER}\" specified in ${SCRIPT_DIR}/${FILE}.json DOES NOT EXIST."
exit 1
fi
# Does the staging folder in the json already exit? (delete if it exists)
if [[ -d "${STAGING_FOLDER}" ]]
then
rm -rf "${STAGING_FOLDER}"
fi
# Does the webroot folder in the json exist?
if [[ -d "${WEB_ROOT}" ]]; then
echo "\"web_root\": \"${WEB_ROOT}\" specified in ${SCRIPT_DIR}/${FILE}.json exists."
else
echo "\"web_root\": \"${WEB_ROOT}\" specified in ${SCRIPT_DIR}/${FILE}.json DOES NOT EXIST."
exit 1
fi
## end of sanity checks
## Check if the daemon is running. If not exit, otherwise continue
## This is the only place where no chain location is specified, since no location is set yet.
## Chicken and egg problem. Needs more thought.
dstat=0
count=$(su "${DAEMON_USER}" -c "${RPC_CLIENT} -chain=${COIN} getconnectioncount")
case $count in
''|*[!0-9]*) dstat=0 ;;
*) dstat=1 ;;
esac
if [[ "$dstat" == "0" ]]; then
exit 1
fi
## Determine standard blockchain locations
# First: set standard locations and web names
if [ "$COIN" == "VRSC" ] || [ "$COIN" == "vrsctest" ]; then
CHAIN_FOLDER=/home/${DAEMON_USER}/.komodo/${COIN}
WEB_NAME=${COIN}
else
SYSTEMTYPE='{\"systemtype\":\"pbaas\"}'
COIN_HEX=$(su ${DAEMON_USER} -c "${RPC_CLIENT} -chain=${COIN} listcurrencies ${SYSTEMTYPE} | ${JQ} -r --arg COIN "${COIN}" '.[] | select (.currencydefinition.name == \"$COIN\") | .currencydefinition.currencyidhex'")
CHAIN_FOLDER=/home/${DAEMON_USER}/.verus/pbaas/${COIN_HEX}
WEB_NAME=${COIN_HEX}
fi
# second: change to custom location if required
if [ "$CUSTOM_CHAIN_FOLDER" == "true" ]; then
CHAIN_FOLDER=$(${JQ} -r .chain_folder "${SCRIPT_DIR}/${FILE}.json")
fi
## Read the last resync/reindex settings from web folder json
if [[ -f "${WEB_ROOT}/${WEB_NAME}-bootstrap/${WEB_NAME}.json" ]]; then
CURRENT_RESYNC=$(${JQ} -r .bootstrap_next.resync ${WEB_ROOT}/${WEB_NAME}-bootstrap/${WEB_NAME}.json)
CURRENT_REINDEX=$(${JQ} -r .bootstrap_next.reindex ${WEB_ROOT}/${WEB_NAME}-bootstrap/${WEB_NAME}.json)
else
CURRENT_RESYNC="null"
CURRENT_REINDEX="null"
fi
## Check if the chain is completely synchronized and wait for full synchronization if needed.
## Currently only supported for the main VRSC chain, if any other chain is specified
## These tests will be bypassed. Checking requires public block explorers available.
if [ "$FORK_CHECK" == "true" ]; then
CHECK_STATUS="UNKNOWN"
while [[ ! "$CHECK_STATUS" == "OK" ]]; do
# collect data
HEIGHT_LOCAL=$(su "${DAEMON_USER}" -c "${RPC_CLIENT} -chain=${COIN} -datadir=${CHAIN_FOLDER} getinfo | ${JQ} .blocks")
HEIGHT_REMOTE=$(${CURL} --silent ${EXPLORER}/api/getblockcount)
HEIGHT_DISTANCE=$(echo "${HEIGHT_LOCAL}-${HEIGHT_REMOTE}" | ${BC} | ${TR} -d -)
# determine status
# either output empty = unknown
if [ -z "${HEIGHT_LOCAL}" ] || [ -z "${HEIGHT_REMOTE}" ]; then
CHECK_STATUS="UNKNOWN"
# equal output = OK
elif [ "${HEIGHT_LOCAL}" -eq "${HEIGHT_REMOTE}" ]; then
CHECK_STATUS="OK"
else
# distance < 3 = warning
if [ "${HEIGHT_DISTANCE}" -lt "3" ]; then
CHECK_STATUS="WARN"
# distance > 3 = critical
else
CHECK_STATUS="CRIT"
fi
fi
if [ ! "${CHECK_STATUS}" -eq "OK" ]; then
sleep 15s
fi
done
## Check if the node is forked, exit if forking is detected
## Currently only supported for the main VRSC chain.
CHECK_STATUS="UNKNOWN"
while [[ ! "$CHECK_STATUS" == "OK" ]]; do
# collect data
HASH_LOCAL=$(su "${DAEMON_USER}" -c "${RPC_CLIENT} -chain=${COIN} -datadir=${CHAIN_FOLDER} getbestblockhash")
HEIGHT_LOCAL=$(su "${DAEMON_USER}" -c "${RPC_CLIENT} -chain=${COIN} -datadir=${CHAIN_FOLDER} getblock ${HASH_LOCAL} 1 | ${JQ} -r .height")
HASH_REMOTE=$(${CURL} --silent "${EXPLORER}/api/getblockhash?index=${HEIGHT_LOCAL}" | ${JQ} -r .)
# determine status
# either output empty = unknown
if [ -z "${HASH_LOCAL}" ] || [ -z "${HASH_REMOTE}" ]; then
CHECK_STATUS="UNKNOWN"
# equal output = OK
elif [ "${HASH_LOCAL}" == "${HASH_REMOTE}" ]; then
CHECK_STATUS="OK"
# nonequal output = critical
else
CHECK_STATUS="CRIT"
exit 1
fi
sleep 5s
done
fi
## Check if the wallet can sign the bootstrap if signing is enabled.
if [[ ( "${SIGN}" == "true" )]]
then
CAN_SIGN=$(su "${DAEMON_USER}" -c "${RPC_CLIENT} -chain=${COIN} -datadir=${CHAIN_FOLDER} getidentity ${SIGNEE} | ${JQ} .cansignfor")
if [[ ! "$CAN_SIGN" == "true" ]]
then
echo "The specified ID cannot sign from this wallet"
exit 1
fi
fi
## Record Bootstrap snapshot data
cp -r "${TEMPLATE_FOLDER}" "${STAGING_FOLDER}"
echo "{" | tee $STAGING_FOLDER/$WEB_NAME.json
echo " \"coin\": \"${COIN}\"," | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
BOOTSTRAP_BLOCKCOUNT=$(su ${DAEMON_USER} -c "${RPC_CLIENT} -chain=${COIN} getblockcount")
BOOTSTRAP_TIME=$(su ${DAEMON_USER} -c "${RPC_CLIENT} -chain=${COIN} getblock ${BOOTSTRAP_BLOCKCOUNT} 1 | ${JQ} -r .time")
BOOTSTRAP_BLOCKHASH=$(su ${DAEMON_USER} -c "${RPC_CLIENT} -chain=${COIN} -datadir=${CHAIN_FOLDER} getblockhash ${BOOTSTRAP_BLOCKCOUNT}")
echo " \"bootstrap_timestamp\": \"$BOOTSTRAP_TIME\"," | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
echo " \"bootstrap_height\": \"${BOOTSTRAP_BLOCKCOUNT}\"," | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
echo " \"bootstrap_bestblockhash\": \"${BOOTSTRAP_BLOCKHASH}\"," | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
if [[ "${DISTRIBUTION_TYPE}" == "tar" ]]
then
echo " \"bootstrap_types\": [\"tar.gz\"]," | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
elif [[ "${DISTRIBUTION_TYPE}" == "zip" ]]
then
echo " \"bootstrap_types\": [\"zip\"]," | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
elif [[ "${DISTRIBUTION_TYPE}" == "both" ]]
then
echo " \"bootstrap_types\": [\"tar.gz\",\"zip\"]," | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
fi
echo " \"bootstrap_current\": {" | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
echo " \"resync\": \"${CURRENT_RESYNC}\"," | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
echo " \"reindex\": \"${CURRENT_REINDEX}\"" | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
echo " }," | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
echo " \"bootstrap_next\": {" | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
echo " \"resync\": \"${RESYNC}\"," | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
echo " \"reindex\": \"${REINDEX}\"" | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
echo " }" | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
echo '}' | tee -a "${STAGING_FOLDER}/${WEB_NAME}.json"
## Recording PBaaS chain definitions for determining notarization heights.
CHAIN_DEFINITIONS=$(su ${DAEMON_USER} -c "${RPC_CLIENT} -chain=${COIN} listcurrencies '{\"systemtype\":\"pbaas\"}'")
## Determining notarization heights
PARENT_ID=$(echo $CHAIN_DEFINITIONS | ${JQ} -r --arg COIN "${COIN}" '.[] | select (.currencydefinition.name == $COIN) | .currencydefinition.parent')
if [[ "$PARENT_ID" == "null" || "$PARENT_ID" == "" ]]
then
sleep 0.1s
else
PARENT_NAME=$(echo $CHAIN_DEFINITIONS | ${JQ} -r --arg PARENT_ID "${PARENT_ID}" '.[] | select (.currencydefinition.currencyid == $PARENT_ID) | .currencydefinition.name')
CHAIN_NOTARIZATION_HEIGHT=$(su "${DAEMON_USER}" -c "${RPC_CLIENT} -chain=${PARENT_NAME} getnotarizationdata \"${COIN}\" | ${JQ} '.notarizations | .[0].notarization.notarizationheight'")
PARENT_NOTARIZATION_HEIGHT=$(su "${DAEMON_USER}" -c "${RPC_CLIENT} -chain=${COIN} getnotarizationdata \"${PARENT_NAME}\" | ${JQ} '.notarizations | .[0].notarization.notarizationheight'")
fi
## Stopping daemon
su "${DAEMON_USER}" -c "${RPC_CLIENT} -chain=${COIN} -datadir=${CHAIN_FOLDER} stop"
## Monitoring if the daemon is still running, before continuing
while ps -u "${DAEMON_USER}" x | grep "${DAEMON}" | grep "${CHAIN_FOLDER}"; do
sleep 2s
done
## Creating bootstrap tar.gz archive, check integrity, move to staging folder and determine the size
cd "${CHAIN_FOLDER}"
if [[ "${DISTRIBUTION_TYPE}" == "tar" || "${DISTRIBUTION_TYPE}" == "both" ]]
then
tar -I 'pigz' -cf "${WEB_NAME}-bootstrap.tar.gz" blocks chainstate
## checking integrity of the created archive. Delete and exit if integrity is compromised (leave daemon switched off)
if ! tar tf "${WEB_NAME}-bootstrap.tar.gz" &> /dev/null; then
rm "${WEB_NAME}-bootstrap.tar.gz"
exit 1
fi
mv "${CHAIN_FOLDER}/${WEB_NAME}-bootstrap.tar.gz" "${STAGING_FOLDER}"
TGZ_SIZE=$(ls -lah "${STAGING_FOLDER}/${WEB_NAME}-bootstrap.tar.gz" | awk '{ print $5}')
fi
## Creating bootstrap zip archive, check integrity, move to staging folder and determine the size
if [[ "${DISTRIBUTION_TYPE}" == "zip" || "${DISTRIBUTION_TYPE}" == "both" ]]
then
${ZIP} -r "${WEB_NAME}-bootstrap.zip" blocks chainstate
## checking integrity of the created archive. Delete and exit if integrity is compromised (leave daemon switched off)
if ! unzip -t "${WEB_NAME}-bootstrap.zip" &> /dev/null; then
rm "${WEB_NAME}-bootstrap.zip"
exit 1
fi
mv "${CHAIN_FOLDER}/${WEB_NAME}-bootstrap.zip" "${STAGING_FOLDER}"
ZIP_SIZE=$(ls -lah "${STAGING_FOLDER}/${WEB_NAME}-bootstrap.zip" | awk '{ print $5}')
fi
## initial archives have been validated. Remove oldest bootstrap (backup) archive folder to make room for the new one.
rm -rf "${WEB_ROOT}/last.${WEB_NAME}-bootstrap"
## Starting daemon back up if signing is required or reindex or rescan is false
if [[ ( "${SIGN}" == "true" ) ]]
then
## the `-fastload` startup switch is available since release v1.2.3-1
su "${DAEMON_USER}" -c "cd ${CHAIN_FOLDER} && ${DAEMON} -chain=${COIN} -datadir=${CHAIN_FOLDER} -fastload -daemon 1>/dev/null 2>&1"
elif [[ (( "${REINDEX}" == "false" ) && ( "${RESYNC}" == "false" )) ]]
then
su "${DAEMON_USER}" -c "cd ${CHAIN_FOLDER} && ${DAEMON} -chain=${COIN} -datadir=${CHAIN_FOLDER} -daemon 1>/dev/null 2>&1"
fi
## Creating checksum files
cd "${STAGING_FOLDER}"
if [[ "${DISTRIBUTION_TYPE}" == "tar" || "${DISTRIBUTION_TYPE}" == "both" ]]
then
md5sum -b "${WEB_NAME}-bootstrap.tar.gz" >"${WEB_NAME}-bootstrap.tar.gz.md5sum"
sha256sum -b "${WEB_NAME}-bootstrap.tar.gz" >"${WEB_NAME}-bootstrap.tar.gz.sha256sum"
sha512sum -b "${WEB_NAME}-bootstrap.tar.gz" >"${WEB_NAME}-bootstrap.tar.gz.sha512sum"
fi
if [[ "${DISTRIBUTION_TYPE}" == "zip" || "${DISTRIBUTION_TYPE}" == "both" ]]
then
md5sum -b "${WEB_NAME}-bootstrap.zip" >"${WEB_NAME}-bootstrap.zip.md5sum"
sha256sum -b "${WEB_NAME}-bootstrap.zip" >"${WEB_NAME}-bootstrap.zip.sha256sum"
sha512sum -b "${WEB_NAME}-bootstrap.zip" >"${WEB_NAME}-bootstrap.zip.sha512sum"
fi
## check if signature creating is enabled and create VerusID signatures
if "${SIGN}"
then
## Check if the daemon is started in a loop
## If false wait 15 seconds and repeat loop
## If true proceed to signing the archives
dstat=0
until [ $dstat == 1 ]; do
count=$(su "${DAEMON_USER}" -c "${RPC_CLIENT} -chain=${COIN} -datadir=${CHAIN_FOLDER} getconnectioncount")
case $count in
''|*[!0-9]*) dstat=0
sleep 15s ;;
*) dstat=1 ;;
esac
done
## sign the tar.gz release
if [[ "${DISTRIBUTION_TYPE}" == "tar" || "${DISTRIBUTION_TYPE}" == "both" ]]
then
su "${DAEMON_USER}" -c "${RPC_CLIENT} -chain=${COIN} -datadir=${CHAIN_FOLDER} signfile ${SIGNEE} ${STAGING_FOLDER}/${WEB_NAME}-bootstrap.tar.gz" > "${STAGING_FOLDER}/${WEB_NAME}-bootstrap.tar.gz.tmp"
if [[ -f "${STAGING_FOLDER}/${WEB_NAME}-bootstrap.tar.gz.tmp" ]]
then
{ head -n 1 "${STAGING_FOLDER}/${WEB_NAME}-bootstrap.tar.gz.tmp"; echo " \"signee\": ${SIGNEE},"; tail -n +2 "${STAGING_FOLDER}/${WEB_NAME}-bootstrap.tar.gz.tmp"; } > "${STAGING_FOLDER}/${WEB_NAME}-bootstrap.tar.gz.verusid"
fi
rm "${STAGING_FOLDER}/${WEB_NAME}-bootstrap.tar.gz.tmp"
fi
## sig the zip release
if [[ "${DISTRIBUTION_TYPE}" == "zip" || "${DISTRIBUTION_TYPE}" == "both" ]]
then
su "${DAEMON_USER}" -c "${RPC_CLIENT} -chain=${COIN} -datadir=${CHAIN_FOLDER} signfile ${SIGNEE} ${STAGING_FOLDER}/${WEB_NAME}-bootstrap.zip"> "${STAGING_FOLDER}/${WEB_NAME}-bootstrap.zip.tmp"
if [[ -f "${STAGING_FOLDER}/${WEB_NAME}-bootstrap.zip.tmp" ]]
then
{ head -n 1 "${STAGING_FOLDER}/${WEB_NAME}-bootstrap.zip.tmp"; echo " \"signee\": ${SIGNEE},"; tail -n +2 "${STAGING_FOLDER}/${WEB_NAME}-bootstrap.zip.tmp"; } > "${STAGING_FOLDER}/${WEB_NAME}-bootstrap.zip.verusid"
fi
rm "${STAGING_FOLDER}/${WEB_NAME}-bootstrap.zip.tmp"
fi
fi
## depending on the value of $REINDEX, stop the chain and start it with the `-reindex` parameters
if [[ "${REINDEX}" == "false" ]]
then
## Reindex is disabled
sleep 0.1s
else
## Stopping daemon
su "${DAEMON_USER}" -c "${RPC_CLIENT} -chain=${COIN} -datadir=${CHAIN_FOLDER} stop"
## Monitoring if the daemon is still running, before continuing
while ps -u "${DAEMON_USER}" x | grep "${DAEMON}" | grep "${CHAIN_FOLDER}\ "; do
sleep 2s
done
## Starting daemon back up with -reindex
su "${DAEMON_USER}" -c "cd ${CHAIN_FOLDER} && ${DAEMON} -chain=${COIN} -datadir=${CHAIN_FOLDER} -reindex -daemon 1>/dev/null 2>&1"
fi
## depending on the value of $RESYNC, stop the chain, clear current chain folder and start it.
if [[ "${RESYNC}" == "false" ]]; then
## Resync is not active
sleep 01.s
else
## Stopping daemon
su "${DAEMON_USER}" -c "${RPC_CLIENT} -chain=${COIN} -datadir=${CHAIN_FOLDER} stop"
## Monitoring if the daemon is still running, before continuing
while ps -u "${DAEMON_USER}" x | grep "${DAEMON}" | grep "${CHAIN_FOLDER}\ "; do
sleep 2s
done
## Clear files out of chain folder
if [[ -d "${CHAIN_FOLDER}/blocks" ]]; then rm -rf "${CHAIN_FOLDER}/blocks"; fi
if [[ -d "${CHAIN_FOLDER}/chainstate" ]]; then rm -rf "${CHAIN_FOLDER}/chainstate"; fi
if [[ -d "${CHAIN_FOLDER}/database" ]]; then rm -rf "${CHAIN_FOLDER}/database"; fi
if [[ -d "${CHAIN_FOLDER}/notarisations" ]]; then rm -rf "${CHAIN_FOLDER}/notarisations"; fi
if [[ -f "${CHAIN_FOLDER}/db.log" ]]; then rm -f "${CHAIN_FOLDER}/db.log"; fi
if [[ -f "${CHAIN_FOLDER}/fee_estimates.dat" ]]; then rm -f "${CHAIN_FOLDER}/fee_estimates.dat"; fi
if [[ -f "${CHAIN_FOLDER}/komodostate" ]]; then rm -f "${CHAIN_FOLDER}/komodostate"; fi
if [[ -f "${CHAIN_FOLDER}/komodostate.ind" ]]; then rm -f "${CHAIN_FOLDER}/komodostate.ind"; fi
if [[ -f "${CHAIN_FOLDER}/.lock" ]]; then rm -f "${CHAIN_FOLDER}/.lock"; fi
if [[ -f "${CHAIN_FOLDER}/peers.dat" ]]; then rm -f "${CHAIN_FOLDER}/peers.dat"; fi
if [[ -f "${CHAIN_FOLDER}/signedmasks" ]]; then rm -f "${CHAIN_FOLDER}/signedmasks"; fi
if [[ -f "${CHAIN_FOLDER}/verus.pid" ]]; then rm -f "${CHAIN_FOLDER}/verus.pid"; fi
## Starting daemon back up
su "${DAEMON_USER}" -c "cd ${CHAIN_FOLDER} && ${DAEMON} -chain=${COIN} -datadir=${CHAIN_FOLDER} -daemon 1>/dev/null 2>&1"
fi
## Collect data about the bootstrap (recorded at daemon shutdown, filesizes read directly from files)
COIN=$(${JQ} -r .coin "${STAGING_FOLDER}/${WEB_NAME}.json")
BOOTSTRAP_TIMESTAMP=$(date -d @$(${JQ} -r .bootstrap_timestamp "${STAGING_FOLDER}/${WEB_NAME}.json"))
BOOTSTRAP_HEIGHT=$(${JQ} -r .bootstrap_height "${STAGING_FOLDER}/${WEB_NAME}.json")
BOOTSTRAP_BESTBLOCKHASH=$(${JQ} -r .bootstrap_bestblockhash "${STAGING_FOLDER}/${WEB_NAME}.json")
## Create populated $WEB_NAME.html from local template file
mv "${STAGING_FOLDER}/bootstrap.html.template" "${STAGING_FOLDER}/${WEB_NAME}.html"
## Remove unused distribution options
if [[ "${DISTRIBUTION_TYPE}" == "tar" ]]
then
mv "${STAGING_FOLDER}/${WEB_NAME}.html" "${STAGING_FOLDER}/${WEB_NAME}.tmp"
{ head -n 54 "${STAGING_FOLDER}/${WEB_NAME}.tmp"; tail -n +65 "${STAGING_FOLDER}/${WEB_NAME}.tmp"; } > "${STAGING_FOLDER}/${WEB_NAME}.html"
rm -f "${STAGING_FOLDER}/${WEB_NAME}.tmp"
fi
if [[ "${DISTRIBUTION_TYPE}" == "zip" ]]
then
mv "${STAGING_FOLDER}/${WEB_NAME}.html" "${STAGING_FOLDER}/${WEB_NAME}.tmp"
{ head -n 46 "${STAGING_FOLDER}/${WEB_NAME}.tmp"; tail -n +54 "${STAGING_FOLDER}/${WEB_NAME}.tmp"; } > "${STAGING_FOLDER}/${WEB_NAME}.html"
rm -f "${STAGING_FOLDER}/${WEB_NAME}.tmp"
fi
## Remove signature lines from template if signing is false.
if ! ${SIGN}
then
sed -i '/<li>Signatures:/d' "${STAGING_FOLDER}/${WEB_NAME}.html"
fi
if [[ "$PARENT_ID" == "null" || "$PARENT_ID" == "" ]]
then
## Remove notarization lines from template if chain has no parent, or PBaaS is not (yet) enabled on the chain.
sed -i '/notarized/d' "${STAGING_FOLDER}/${WEB_NAME}.html"
else
## replace placeholders with the parent name & notarization heights
sed -i "s~%%PARENT_NAME%%~${PARENT_NAME}~gip" "${STAGING_FOLDER}/${WEB_NAME}.html"
sed -i "s~%%CHAIN_NOTARIZATION_HEIGHT%%~${CHAIN_NOTARIZATION_HEIGHT}~gip" "${STAGING_FOLDER}/${WEB_NAME}.html"
sed -i "s~%%PARENT_NOTARIZATION_HEIGHT%%~${PARENT_NOTARIZATION_HEIGHT}~gip" "${STAGING_FOLDER}/${WEB_NAME}.html"
fi
## replace placeholders for values with recorded values
sed -i "s~%%EXPLORER_URL%%~${EXPLORER}~gip" "${STAGING_FOLDER}/${WEB_NAME}.html"
sed -i "s/%%BOOTSTRAP_TIMESTAMP%%/${BOOTSTRAP_TIMESTAMP}/gip" "${STAGING_FOLDER}/${WEB_NAME}.html"
sed -i "s/%%BOOTSTRAP_BESTBLOCKHASH%%/${BOOTSTRAP_BESTBLOCKHASH}/gip" "${STAGING_FOLDER}/${WEB_NAME}.html"
sed -i "s/%%BOOTSTRAP_HEIGHT%%/${BOOTSTRAP_HEIGHT}/gip" "${STAGING_FOLDER}/${WEB_NAME}.html"
if [[ "${DISTRIBUTION_TYPE}" == "zip" || "${DISTRIBUTION_TYPE}" == "both" ]]
then
sed -i "s/%%ZIP_SIZE%%/${ZIP_SIZE}/gip" "${STAGING_FOLDER}/${WEB_NAME}.html"
fi
if [[ "${DISTRIBUTION_TYPE}" == "tar" || "${DISTRIBUTION_TYPE}" == "both" ]]
then
sed -i "s/%%TGZ_SIZE%%/${TGZ_SIZE}/gip" "${STAGING_FOLDER}/${WEB_NAME}.html"
fi
sed -i "s/%%COIN%%/${COIN}/gip" "${STAGING_FOLDER}/${WEB_NAME}.html"
sed -i "s/%%WEB_NAME%%/${WEB_NAME}/gip" "${STAGING_FOLDER}/${WEB_NAME}.html"
sed -i "s/%%COIN_NAME%%/${COIN_NAME}/gip" "${STAGING_FOLDER}/${WEB_NAME}.html"
sed -i '$!N; /^\(.*\)\n\1$/!P; D' "${STAGING_FOLDER}/${WEB_NAME}.html"
## Insert Synchronization method into $WEB_NAME.html
if ! [[ "${CURRENT_RESYNC}" == "null" || "${CURRENT_REINDEX}" == "null" ]]; then
if [[ "${CURRENT_RESYNC}" == "true" ]]; then
mv "${STAGING_FOLDER}/${WEB_NAME}.html" "${STAGING_FOLDER}/${WEB_NAME}.tmp"
{ head -n 37 "${STAGING_FOLDER}/${WEB_NAME}.tmp"; echo -e "\t\t\t\t\t<li>Before bootstrapping, the chain was <b>synchronized from genesis</b>.</li>"; tail -n +38 "${STAGING_FOLDER}/${WEB_NAME}.tmp"; } > "${STAGING_FOLDER}/${WEB_NAME}.html"
rm -f "${STAGING_FOLDER}/${WEB_NAME}.tmp"
elif [[ "${CURRENT_REINDEX}" == "true" ]]; then
mv "${STAGING_FOLDER}/${WEB_NAME}.html" "${STAGING_FOLDER}/${WEB_NAME}.tmp"
{ head -n 37 "${STAGING_FOLDER}/${WEB_NAME}.tmp"; echo -e "\t\t\t\t\t<li>Before bootstrapping, the chain was <b>reindexed</b>.</li>"; tail -n +38 "${STAGING_FOLDER}/${WEB_NAME}.tmp"; } > "${STAGING_FOLDER}/${WEB_NAME}.html"
rm -f "${STAGING_FOLDER}/${WEB_NAME}.tmp"
else
mv "${STAGING_FOLDER}/${WEB_NAME}.html" "${STAGING_FOLDER}/${WEB_NAME}.tmp"
{ head -n 37 "${STAGING_FOLDER}/${WEB_NAME}.tmp"; echo -e "\t\t\t\t\t<li>Before bootstrapping, the chain continued after the previous bootstrap.</li>"; tail -n +38 "${STAGING_FOLDER}/${WEB_NAME}.tmp"; } > "${STAGING_FOLDER}/${WEB_NAME}.html"
rm -f "${STAGING_FOLDER}/${WEB_NAME}.tmp"
fi
fi
## Fill $WEB_NAME.html with links from $WEB_NAME.json
m=$(${JQ} -r ".links | length" "${SCRIPT_DIR}/${FILE}.json")
for (( n=m-1; n>=0; n-- ))
do
tag=$(${JQ} -r ".links[$n].tag" "${SCRIPT_DIR}/${FILE}.json")
url=$(${JQ} -r ".links[$n].URL" "${SCRIPT_DIR}/${FILE}.json")
mv "${STAGING_FOLDER}/${WEB_NAME}.html" "${STAGING_FOLDER}/${WEB_NAME}.tmp"
{ head -n 30 "${STAGING_FOLDER}/${WEB_NAME}.tmp"; echo -e "\t\t\t\t\t<li>$tag: <a href=\"$url\" target=\"_blank\">$url</a></li>"; tail -n +31 "${STAGING_FOLDER}/${WEB_NAME}.tmp"; } > "${STAGING_FOLDER}/${WEB_NAME}.html"
rm -f "${STAGING_FOLDER}/${WEB_NAME}.tmp"
done
## Copy to external staging locations
if [[ "${DEPLOY_EXTERNALLY}" == "true" ]]; then
m=$(${JQ} -r ".deploy_locations | length" "${SCRIPT_DIR}/${FILE}.json")
for (( n=m-1; n>=0; n-- ))
do
REMOTE_HOST=$(${JQ} -r ".deploy_locations[$n].host" "${SCRIPT_DIR}/${FILE}.json")
REMOTE_WEB_ROOT=$(${JQ} -r ".deploy_locations[$n].web_root" "${SCRIPT_DIR}/${FILE}.json")
ssh -q -t $REMOTE_HOST "rm -rf $REMOTE_WEB_ROOT/last.$WEB_NAME-bootstrap; mkdir $REMOTE_WEB_ROOT/stage.$WEB_NAME-bootstrap"
scp -r $STAGING_FOLDER/* $REMOTE_HOST:$REMOTE_WEB_ROOT/stage.$WEB_NAME-bootstrap
done
fi
## Moving folders for deployment
mv "${WEB_ROOT}/${WEB_NAME}-bootstrap" "${WEB_ROOT}/last.${WEB_NAME}-bootstrap"
mv "${STAGING_FOLDER}" "${WEB_ROOT}/${WEB_NAME}-bootstrap"
## Deploying externally staged locations
if [[ "${DEPLOY_EXTERNALLY}" == "true" ]]; then
m=$(${JQ} -r ".deploy_locations | length" "${SCRIPT_DIR}/${FILE}.json")
for (( n=m-1; n>=0; n-- ))
do
REMOTE_HOST=$(${JQ} -r ".deploy_locations[$n].host" "${SCRIPT_DIR}/${FILE}.json")
REMOTE_WEB_ROOT=$(${JQ} -r ".deploy_locations[$n].web_root" "${SCRIPT_DIR}/${FILE}.json")
ssh -q -t $REMOTE_HOST "mv $REMOTE_WEB_ROOT/$WEB_NAME-bootstrap $REMOTE_WEB_ROOT/last.$WEB_NAME-bootstrap; mv $REMOTE_WEB_ROOT/stage.$WEB_NAME-bootstrap $REMOTE_WEB_ROOT/$WEB_NAME-bootstrap && cd $REMOTE_WEB_ROOT/$WEB_NAME-bootstrap && ln -s $WEB_NAME.html index.html && chown -R www-data:www-data $REMOTE_WEB_ROOT/$WEB_NAME-bootstrap"
done
fi
## Create symlink
cd "${WEB_ROOT}/${WEB_NAME}-bootstrap"
ln -s "${WEB_NAME}.html" index.html
cd /
## Set priviliges
chown -R www-data:www-data "${WEB_ROOT}/${WEB_NAME}-bootstrap"
## create an archive for the entire website, for manual distribution
if "${ARCHIVE}"
then
## remove web archive from the last.$WEB_NAME-bootstrap to conserve drivespace
if [ ! -f "${WEB_ROOT}/last.${WEB_NAME}-bootstrap/${WEB_NAME}-bootstrap.tar" ]
then
rm -f "${WEB_ROOT}/last.${WEB_NAME}-bootstrap/${WEB_NAME}-bootstrap.tar"
fi
## create archive
cd "${WEB_ROOT}"
tar -cvf "${WEB_NAME}-bootstrap.tar" "${WEB_NAME}-bootstrap"
mv "${WEB_ROOT}/${WEB_NAME}-bootstrap.tar" "${WEB_ROOT}/${WEB_NAME}-bootstrap"
fi