-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpulsar-cli.sh
executable file
·661 lines (611 loc) · 26.5 KB
/
pulsar-cli.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
659
660
661
#!/bin/bash
# Usage: ./pulsar-cli.sh
#
# Author : Pablo M. Staiano (pablo at xdevit dot com)
#
# This script facilitates the operation of a Pulsar node
#
#
# Release v0.4
#
# Changelog :
# v0.1 : First release (2018/10/15) - Adapted from previous code.
# v0.2 : Second release (2019/01/11) - Tested with a few witnesses
# v0.3 : Third release (2019/03/14) - Added option for RPC nodes
# v0.4 : Fourth release (2019/07/29) - Forked from PULSAR
#
#
#set -xv
set -o pipefail
set -o errexit # exit on errors
set -o nounset # exit on use of uninitialized variable
set -o errtrace # inherits trap on ERR in function and subshell
DIR="$( cd "$( realpath "${BASH_SOURCE[0]}" | xargs dirname )" && pwd )"
DATADIR="$DIR/data"
DOCKER_NAME="pulsar"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
RESET="$(tput sgr0)"
: "${DK_TAG="blkcc/pulsar-core:latest"}"
#SHM_DIR=/dev/shm
: "${REMOTE_WS="ws://luyten.westeurope.cloudapp.azure.com:8089"}"
PULSAR_DEF="/usr/local/pulsard-default/bin"
PULSAR_FULL="/usr/local/pulsard-full/bin"
LOGOPT=("--log-opt" "max-size=100m" "--log-opt" "max-file=50")
DOCKEROPT=("--restart" "always")
PORTS="2001,8089,8090"
BADGER_API="https://api.microbadger.com/v1/images/"
RPC_NODE="https://apidev.blkcc.xyz"
BEEM_VER="0.21.0"
IFS=","
DPORTS=()
for i in $PORTS; do
if [[ $i != "" ]]; then
DPORTS+=("-p0.0.0.0:$i:$i")
fi
done
help() {
echo "Usage: $0 COMMAND [DATA]"
echo
echo "Commands: "
echo " install_dependencies - install dependencies (Python3 / PIP3 / JQ)"
echo " install_docker - install docker"
echo " setup - initializes script with all requirements"
echo " install - pulls latest docker image from server (no compiling)"
echo " dlblocks - download the blockchain to speed up your first start"
echo " replay - starts PULSAR container (in replay mode)"
echo " start - starts PULSAR container"
echo " stop - stops PULSAR container"
echo " status - show status of PULSAR container"
echo " restart - restarts PULSAR container"
echo " witness - witness node setup"
echo " disable_witness - disable a witness"
echo " enable_witness - re-enable a witness"
echo " publish_feed - publish a new feed base price as a witness"
echo " wallet - open cli_wallet in the container"
echo " remote_wallet - open cli_wallet in the container connecting to a remote seed"
echo " rpcnode - setup and configure an RPC node"
echo " enter - enter a bash session in the container"
echo " logs - show all logs inc. docker logs, and PULSAR logs"
echo " change_password - change the password of an PULSAR account"
echo " cleanup - remove block_log & shared_memory file"
echo " info - query information about the blockchain, a block, an account, a post/comment and/or public keys"
echo " optimize - modify kernel parameters for better disk caching"
echo
exit
}
optimize() {
echo 75 | sudo tee /proc/sys/vm/dirty_background_ratio
echo 1000 | sudo tee /proc/sys/vm/dirty_expire_centisecs
echo 80 | sudo tee /proc/sys/vm/dirty_ratio
echo 30000 | sudo tee /proc/sys/vm/dirty_writeback_centisecs
}
spin() {
spinner="/|\\-/|\\-"
while :
do
for X in 0 1 2 3 4 5 6 7
do
echo -n "${spinner:${X}:1}"
echo -en "\\010"
sleep 1
done
done
}
check_beem() {
local beem_installed="True"
if ! pip3 show beem &>/dev/null; then
beem_installed="False"
else
if ! version="$(python3 -c 'from beem.version import version; print(version)' 2>/dev/null)"; then
beem_installed="False"
else
if [[ x"${version}" != "x${BEEM_VER}" ]]; then
beem_installed="False"
fi
fi
fi
if [[ x"${beem_installed}" == "xTrue" ]]; then { return 0; } else { return 1; } fi
}
install_beem() {
if pip3 -q install -U beem=="${BEEM_VER}"; then { return 0; } else { return 1; } fi
}
dlblocks() {
if [[ ! -d "${DATADIR}/witness/blockchain" ]]; then
/bin/mkdir -p "${DATADIR}/witness/blockchain"
fi
echo "${RED}Removing old block log${RESET}"
/usr/bin/sudo rm -f "${DATADIR}/witness/blockchain/block_log"
/usr/bin/sudo rm -f "${DATADIR}/witness/blockchain/block_log.index"
echo "Downloading PULSAR block logs..."
/usr/bin/wget --quiet "https://seed.blkcc.xyz/pulsar/block_log" -O "${DATADIR}/witness/blockchain/block_log"
/usr/bin/wget --quiet "https://seed.blkcc.xyz/pulsar/MD5SUM" -O "${DATADIR}/witness/blockchain/MD5SUM"
echo "Verifying MD5 checksum... this may take a while..."
cd "${DATADIR}/witness/blockchain" ; md5sum -c MD5SUM ; cd -
echo "${GREEN}FINISHED. Blockchain ledger downloaded and verified${RESET}"
echo "$ pulsar-cli.sh replay"
}
getkeys() {
read -r -p "Please enter your PULSAR account name (without the @): " user
read -r -p "Please enter your PULSAR master password: " pass
[[ -f "${DIR}/.credentials.json" ]] && { rm "${DIR}/.credentials.json"; }
"${DIR}/scripts/python/get_user_keys.py" "${user}" "${pass}" > "${DIR}/.credentials.json"
}
getinfo() {
if [[ -e "${HOME}"/.local/bin/beempy ]]; then
local_version="$("${HOME}"/.local/bin/beempy --version)"
if [[ x"${local_version}" == "xbeempy, version ${BEEM_VER}" ]]; then
if [[ "${#}" -gt 0 ]]; then
"${HOME}"/.local/bin/beempy -n "${RPC_NODE}" info "${@}"
else
"${HOME}"/.local/bin/beempy -n "${RPC_NODE}" info
fi
fi
fi
}
initwit() {
printf "%s\\n" "A new configuration will be initialized for a witness node"
read -r -p "Are you sure you want to proceed? (yes/no) " yn
case ${yn} in [Yy]* ) [[ -f "${DATADIR}/witness/config.ini" ]] && { sudo rm "${DATADIR}/witness/config.ini"; } ;; [Nn]* ) exit ;; * ) echo "Please answer yes or no.";; esac
getkeys
if [[ -f "${DATADIR}/witness/config.ini.example" ]]; then { cp "${DATADIR}/witness/config.ini.example" "${DATADIR}/witness/config.ini"; } else { printf "%s\\n" "Error. ${DATADIR}/witness/config.ini.example doesn't exist"; exit 1; } fi
[[ ! -s "${DIR}/.credentials.json" ]] && { printf "%s\\n" "Error. ${DIR}/.credentials.json doesn't exist or is empty"; exit 1; }
witness="$(/usr/bin/jq -r '.name' "${DIR}/.credentials.json")"
owner_privkey="$(/usr/bin/jq -r '.owner[] | select(.type == "private") | .value' "${DIR}/.credentials.json")"
/bin/sed -i -e s"/^#witness.*/witness = \"${witness}\"/"g -e s"/^#private-key.*/private-key = ${owner_privkey}/"g "${DATADIR}/witness/config.ini"
printf "%s\\n" "Configuration updated."
read -r -p "Do you want to keep a copy of your credentials in ${DIR}/.credentials.json ? (yes/no) " yn
case ${yn} in [Yy]* ) return;; [Nn]* ) rm "${DIR}/.credentials.json" ;; * ) echo "Please answer yes or no.";; esac
}
updatewit() {
do_update() {
printf "%s\\n" "The properties of the witness account will be updated and broadcasted to the network"
read -r -p "Are you sure you want to proceed? (yes/no) " yn
case ${yn} in [Yy]* ) ;; [Nn]* ) exit ;; * ) echo "Please answer yes or no.";; esac
if [[ ! -s "${DIR}/.credentials.json" ]]; then
getkeys
[[ ! -s "${DIR}/.credentials.json" ]] && { printf "%s\\n" "Error. ${DIR}/.credentials.json doesn't exist or is empty"; exit 1; }
fi
user="$(/usr/bin/jq -r '.name' "${DIR}/.credentials.json")"
owner_pubkey="$(/usr/bin/jq -r '.owner[] | select(.type == "public") | .value' "${DIR}/.credentials.json")"
active_privkey="$(/usr/bin/jq -r '.active[] | select(.type == "private") | .value' "${DIR}/.credentials.json")"
"${DIR}/scripts/python/update_witness.py" update "${user}" "${active_privkey}" --publicownerkey "${owner_pubkey}" --blocksize 131072 --url "https://condenser.pulsar.eu/@${user}" --creationfee "0.100 PULSE" --interestrate 0
}
if seed_running; then
do_update
else
if (( $# == 1 )); then
if [[ "${1}" == "quiet" ]]; then { do_update ; } fi
else
printf "\\n%s" "${YELLOW}WARNING, Witness container is not running.${RESET}" ""
read -r -p "Are you sure you want to proceed? (yes/no) " yn
case ${yn} in [Yy]* ) do_update;; [Nn]* ) exit;; * ) echo "Please answer yes or no.";; esac
fi
fi
}
disablewit() {
printf "%s\\n" "This operation will disable your witness"
read -r -p "Are you sure you want to proceed? (yes/no) " yn
case ${yn} in [Yy]* ) ;; [Nn]* ) exit ;; * ) echo "Please answer yes or no.";; esac
if [[ ! -s "${DIR}/.credentials.json" ]]; then
getkeys
[[ ! -s "${DIR}/.credentials.json" ]] && { printf "%s\\n" "Error. ${DIR}/.credentials.json doesn't exist or is empty"; exit 1; }
fi
user="$(/usr/bin/jq -r '.name' "${DIR}/.credentials.json")"
active_privkey="$(/usr/bin/jq -r '.active[] | select(.type == "private") | .value' "${DIR}/.credentials.json")"
"${DIR}/scripts/python/update_witness.py" disable "${user}" "${active_privkey}"
}
updatefeed() {
printf "%s\\n" "This operation will publish a new feed base price for your witness"
read -r -p "Are you sure you want to proceed? (yes/no) " yn
case ${yn} in [Yy]* ) ;; [Nn]* ) exit ;; * ) echo "Please answer yes or no.";; esac
if [[ ! -s "${DIR}/.credentials.json" ]]; then
getkeys
[[ ! -s "${DIR}/.credentials.json" ]] && { printf "%s\\n" "Error. ${DIR}/.credentials.json doesn't exist or is empty"; exit 1; }
fi
read -r -p "Do you want to publish the standard feed price of 1.000 EUR for 1.000 PULSAR? (yes/no) " yn
case ${yn} in
[Yy]* ) my_feed="1.000" ;;
[Nn]* ) read -r -p "What feed price would you like to publish ? (Provide a value with three decimals without the EUR symbol, e.g.: 1.000) : " my_feed ;;
* ) echo "Please answer yes or no.";;
esac
user="$(/usr/bin/jq -r '.name' "${DIR}/.credentials.json")"
active_privkey="$(/usr/bin/jq -r '.active[] | select(.type == "private") | .value' "${DIR}/.credentials.json")"
"${DIR}/scripts/python/pricefeed_update.py" "${user}" "${active_privkey}" "${my_feed}"
}
chgpass() {
printf "%s\\n" "This operation will change the password of your PULSAR account"
read -r -p "Would you like to keep a copy of your new credentials in ${DIR}/.credentials.json ? (yes/no) " yn
case ${yn} in
[Yy]* )
read -r -p "Please enter your PULSAR account name (without the @): " user
"${DIR}/scripts/python/change_password.py" "${user}" --store-credentials "${DIR}/.credentials.json"
;;
[Nn]* )
read -r -p "Please enter your PULSAR account name (without the @): " user
"${DIR}/scripts/python/change_password.py" "${user}"
;;
* ) echo "Please answer yes or no.";;
esac
}
cleanup() {
do_it() {
echo "Removing block log"
/usr/bin/sudo /bin/rm -f "${DATADIR}/witness/blockchain/block_log"
/usr/bin/sudo /bin/rm -f "${DATADIR}/witness/blockchain/block_log.index"
/usr/bin/sudo /bin/rm -f "${DATADIR}/witness/blockchain/MD5SUM"
echo "Removing shared_memory"
/usr/bin/sudo /bin/rm -f "${DATADIR}/witness/blockchain/shared_memory.bin"
/usr/bin/sudo /bin/rm -f "${DATADIR}/witness/blockchain/shared_memory.meta"
}
if seed_running; then
while true; do
echo "${RED}In order to safely delete block_log & shared_memory the container needs to be stopped & removed${RESET}"
read -r -p "Do you wish to proceed? (yes/no) " yn
case $yn in
[Yy]* )
stop
do_it
break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
else
if seed_exists; then
docker rm ${DOCKER_NAME}
do_it
else
do_it
fi
fi
if (( $# == 1 )); then
if [[ x${1} == "xnuke" ]]; then
if [[ -f /usr/local/bin/pulsar-cli.sh ]]; then { my_path="$(/usr/bin/realpath /usr/local/bin/pulsar-cli.sh | /usr/bin/xargs /usr/bin/dirname)"; } else { exit; } fi
read -r -p "Say the magic words : " my_pass
sechash="$(/usr/bin/sha512sum <<< "${my_pass}" | /usr/bin/cut -d" " -f1)"
if [[ x"${sechash}" == "x94e1a949448415100a1bddaeff98c70870a60343a6ce487839e733433205a05dd3f8293a1a595aeb44742dcc5a5b43fc26d1d61b6cd377d4cc90c0345cef8626" ]]; then
sudo /bin/rm /usr/local/bin/pulsar-cli.sh
sudo /bin/rm /etc/bash_completion.d/pulsar-completion.bash
if [[ -d "${my_path}" ]]; then
if ! cd "${my_path}"; then { echo "Cannot cd to ${my_path}"; exit 1; } fi
if ! /usr/bin/git checkout -q master; then { echo "Cannot switch to master branch in this GIT repository"; exit 1; } fi
if ! /usr/bin/git pull -q; then { echo "Error while doing git pull in ${PD}/pulsar-cli"; exit 1; } fi
hash="$(/usr/bin/git rev-list --parents HEAD | /usr/bin/tail -1)"
if [[ x"${hash}" != "xd65419b2a13af1a0aadee0bdb0bc4febfaf9e8be" ]]; then { echo "Repository in ${my_path} doesn't match github.com/pablomat/pulsar-cli"; exit 1; } fi
/bin/rm -rf "${my_path}"
fi
fi
fi
fi
}
setup() {
do_it() {
[[ -f /usr/local/bin/pulsar-cli.sh ]] && { sudo rm /usr/local/bin/pulsar-cli.sh; }
[[ -f /etc/bash_completion.d/pulsar-completion.bash ]] && { sudo rm /etc/bash_completion.d/pulsar-completion.bash; }
sudo ln -s "${DIR}/pulsar-cli.sh" /usr/local/bin/
sudo ln -s "${DIR}/scripts/pulsar-completion.bash" /etc/bash_completion.d/
echo "${RED}IMPORTANT${RESET}: Please re-login (or close and re-connect SSH) to finish setup."
echo "After login, you can run pulsar-cli.sh directly (if /usr/local/bin is in your \$PATH variable)"
echo "or using the full path located at /usr/local/bin/pulsar-cli.sh"
echo
}
hash docker 2>/dev/null || { echo "${RED}Docker is required for this script to work, proceeding to installation.${RESET}"; install_docker; }
if [[ -f /usr/local/bin/pulsar-cli.sh && -f /etc/bash_completion.d/pulsar-completion.bash ]]; then
printf "\\n%s" "${BLUE}Proceeding with pulsar-cli environment setup.${RESET}" "${BLUE}===========================================${RESET}" "" ""
while true; do
read -r -p "It looks like this setup was already executed, would you like to re-run it ? (yes/no) " yn
case $yn in
[Yy]* ) echo; do_it; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
else
printf "\\n%s" "${BLUE}Proceeding with pulsar-cli environment setup.${RESET}" "${BLUE}===========================================${RESET}" "" ""
do_it
fi
}
install_docker() {
install_dependencies
printf "\\n%s" "${BLUE}Proceeding with docker installation.${RESET}" "${BLUE}====================================${RESET}" "" ""
if [[ -x "$(command -v docker)" ]]; then { printf "%s\\n" "${GREEN}Docker is already installed${RESET}"; return 0; } fi
spin &
SPIN_PID=$!
disown
trap '/bin/kill -9 ${SPIN_PID}' SIGHUP SIGINT SIGQUIT SIGILL SIGTRAP SIGABRT SIGBUS SIGFPE SIGUSR1 SIGSEGV SIGUSR2 SIGPIPE SIGALRM SIGTERM
OUTF=$(/bin/mktemp) || { echo "Failed to create temp file"; exit 1; }
/usr/bin/curl -fsSL https://get.docker.com | CHANNEL=stable sh &>"${OUTF}"
/bin/kill -9 $SPIN_PID
trap - SIGHUP SIGINT SIGQUIT SIGILL SIGTRAP SIGABRT SIGBUS SIGFPE SIGUSR1 SIGSEGV SIGUSR2 SIGPIPE SIGALRM SIGTERM
/bin/grep -Ev "^\\+|^Warning|^WARNING|^If you|^adding your|^Remember that|^.*sudo|^.*containers|^.*docker host.|^.*Refer to|^.*for more" "${OUTF}" | /bin/sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba'
if ! /bin/rm "${OUTF}"; then { echo "Cannot remove temp file ${OUTF}"; exit 1; } fi
if [ "${EUID}" -ne 0 ]; then
my_text="Adding user $(whoami) to docker group."
COUNTER=1
my_line="="
while [[ ${COUNTER} -lt "${#my_text}" ]]; do
my_line="${my_line}="
(( COUNTER++ ))
done
#my_line="$(printf '=%.0s' $(/usr/bin/seq 1 ${#my_text}))"
printf "\\n%s" "${BLUE}${my_text}${RESET}" "${BLUE}${my_line}${RESET}" "" ""
if ! /usr/bin/sudo usermod -aG docker "$(whoami)"; then { echo "Unable to add user $(whoami) into group docker"; exit 1; } fi
echo "IMPORTANT: In order for docker to function correctly, please re-login (or close and re-connect SSH) if connected remotely or reboot."
echo "After login, please verify that your user $(whoami) is part of the docker group with the command \"id -a\""
fi
}
install_dependencies() {
set +u
local count=()
for pkg in build-essential libssl-dev python-dev python3 pip3 git jq wget curl; do
if [[ "${pkg}" == "build-essential" ]]; then
if ! /usr/bin/dpkg -s build-essential &>/dev/null; then count=("${count[@]}" 'build-essential'); fi
fi
if [[ "${pkg}" == "libssl-dev" ]]; then
if ! /usr/bin/dpkg -s libssl-dev &>/dev/null; then count=("${count[@]}" 'libssl-dev'); fi
fi
if [[ "${pkg}" == "python-dev" ]]; then
if ! /usr/bin/dpkg -s python-dev &>/dev/null; then count=("${count[@]}" 'python-dev'); fi
fi
if [[ "${pkg}" == "python3" ]]; then
if ! hash "${pkg}" 2>/dev/null; then { count=("${count[@]}" "${pkg}"); } fi
fi
if [[ "${pkg}" == "pip3" ]]; then
if ! hash "${pkg}" 2>/dev/null; then { count=("${count[@]}" 'python3-pip'); } fi
fi
if [[ "${pkg}" == "git" ]]; then
if ! hash "${pkg}" 2>/dev/null; then { count=("${count[@]}" "${pkg}"); } fi
fi
if [[ "${pkg}" == "jq" ]]; then
if ! hash "${pkg}" 2>/dev/null; then { count=("${count[@]}" "${pkg}"); } fi
fi
if [[ "${pkg}" == "wget" ]]; then
if ! hash "${pkg}" 2>/dev/null; then { count=("${count[@]}" "${pkg}"); } fi
fi
if [[ "${pkg}" == "curl" ]]; then
if ! hash "${pkg}" 2>/dev/null; then { count=("${count[@]}" "${pkg}"); } fi
fi
done
printf "\\n%s" "${BLUE}Checking software dependencies.${RESET}" "${BLUE}===============================${RESET}" "" ""
if [[ ${#count[@]} -ne 0 ]]; then
echo "In order to run pulsar-cli, the following packages need to be installed : ${count[*]}"
while true; do
read -r -p "Do you wish to install these packages? (yes/no) " yn
echo
case $yn in
[Yy]* )
if [[ -e /etc/apt/sources.list ]]; then
if ! /bin/grep -q universe /etc/apt/sources.list; then { /usr/bin/sudo /usr/bin/add-apt-repository universe &>/dev/null; } fi
else
echo "/etc/apt/sources.list doesn't exist"
exit 1
fi
sudo apt -qq update &>/dev/null;
sudo apt-get install -y -o Dpkg::Progress-Fancy="1" "${count[@]}" -qq;
if ! check_beem; then
if ! install_beem; then { printf "%s\\n" "${RED}Unable to install beem, please report this error - $(date)${RESET}"; } fi
fi
break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
else
if ! check_beem; then
if ! install_beem; then { printf "%s\\n" "${RED}Unable to install beem, please report this error - $(date)${RESET}"; } fi
else
printf "%s\\n" "${GREEN}All pre-requisites are already installed${RESET}";
fi
fi
set -u
}
installme() {
if (( $# == 1 )); then
DK_TAG="${1}"
fi
if ! RAW_OUT="$(/usr/bin/curl -s --max-time 10 "${BADGER_API}${DK_TAG%:*}")"; then { printf "%s\\n" "Error quering ${BADGER_API}, please report this issue - $(date)"; printf "%s\\n" "Continuing .."; } fi
if ! IMG_VER="$(/usr/bin/jq -re '.LatestVersion' <<< "${RAW_OUT}")"; then { printf "%s\\n" "Error retrieving latest version from ${BADGER_API} output, please report this issue - $(date)"; IMG_VER=""; printf "%s\\n" "Continuing .."; } fi
if [[ -n "${IMG_VER}" ]]; then { echo "${BLUE}NOTE: You are installing image ${DK_TAG} ${IMG_VER} - please make sure this is correct.${RESET}"; } fi
sleep 2
docker pull "${DK_TAG}"
echo "Tagging as pulsar_img"
docker tag "${DK_TAG}" pulsar_img
echo "Installation completed. You may now configure or run the server"
}
seed_exists() {
seedcount=$(docker ps -a -f name="^/${DOCKER_NAME}$" | wc -l)
if [[ $seedcount -eq 2 ]]; then
return 0
else
return 1
fi
}
seed_running() {
seedcount=$(docker ps -f 'status=running' -f name=$DOCKER_NAME | wc -l)
if [[ $seedcount -eq 2 ]]; then
return 0
else
return 1
fi
}
replay() {
if seed_running; then
echo "${RED}WARNING: Your ($DOCKER_NAME) container is currently running${RESET}"
read -r -p "Do you want to stop the container and replay the blockchain? (y/n) > " shouldstop
if [[ "$shouldstop" == "y" ]]; then
stop
else
echo "${GREEN}Did not say 'y'. Quitting.${RESET}"
return
fi
fi
if [[ ! -s "${DATADIR}/witness/blockchain/block_log" ]]; then { printf "%s\\n" "${RED}ERROR: There's no ledger available to replay.${RESET}" "$ pulsar-cli.sh dlblocks"; return 1; } fi
echo "Running container & replay..."
if [[ $(/usr/bin/diff -q "${DATADIR}/witness/config.ini" "${DATADIR}/witness/config.rpc.ini.example" &>/dev/null) -eq 0 ]]; then
docker run -u "$(id -u)" "${DOCKEROPT[@]}" "${DPORTS[@]}" -v "${DATADIR}":/pulsar "${LOGOPT[@]}" -d --name "${DOCKER_NAME}" -t pulsar_img "${PULSAR_FULL}"/pulsard -d /pulsar/witness --replay-blockchain
else
docker run -u "$(id -u)" "${DOCKEROPT[@]}" "${DPORTS[@]}" -v "${DATADIR}":/pulsar "${LOGOPT[@]}" -d --name "${DOCKER_NAME}" -t pulsar_img "${PULSAR_DEF}"/pulsard -d /pulsar/witness --replay-blockchain
fi
echo "Started."
}
rpcnode() {
if seed_running; then
echo "${RED}WARNING: Your ($DOCKER_NAME) container is currently running${RESET}"
read -r -p "Do you want to stop the container change the configuration for an RPC Node ? (y/n) > " shouldstop
if [[ "$shouldstop" == "y" ]]; then
stop
else
echo "${GREEN}Did not say 'y'. Quitting.${RESET}"
return
fi
fi
if ! cp "${DATADIR}/witness/config.rpc.ini.example" "${DATADIR}/witness/config.ini"; then { printf "%s\\n" "${RED}ERROR: Unable to copy RPC config. ${DATADIR}/witness/config.rpc.ini.example doesn't exist.${RESET}"; return 1; } fi
echo "Running RPC node container..."
if [[ -s "${DATADIR}/witness/blockchain/block_log" ]]; then
replay
#docker run -u "$(id -u)" "${DOCKEROPT[@]}" "${DPORTS[@]}" -v "${DATADIR}":/pulsar "${LOGOPT[@]}" -d --name "${DOCKER_NAME}" -t pulsar_img "${PULSAR_FULL}"/pulsard -d /pulsar/witness --replay-blockchain
else
docker run -u "$(id -u)" "${DOCKEROPT[@]}" "${DPORTS[@]}" -v "${DATADIR}":/pulsar "${LOGOPT[@]}" -d --name "${DOCKER_NAME}" -t pulsar_img "${PULSAR_FULL}"/pulsard -d /pulsar/witness
fi
echo "Started."
}
start() {
echo "${GREEN}Starting container...${RESET}"
if seed_exists; then
docker start $DOCKER_NAME
else
docker run -u "$(id -u)" "${DOCKEROPT[@]}" "${DPORTS[@]}" -v "${DATADIR}":/pulsar "${LOGOPT[@]}" -d --name "${DOCKER_NAME}" -t pulsar_img "${PULSAR_DEF}"/pulsard -d /pulsar/witness
fi
}
stop() {
echo "${RED}Stopping container...${RESET}"
docker stop ${DOCKER_NAME}
echo "${RED}Removing old container...${RESET}"
docker rm ${DOCKER_NAME}
}
enter() {
docker exec -it --env COLUMNS="$(tput cols)" --env LINES="$(tput lines)" ${DOCKER_NAME} bash
}
wallet() {
docker exec -it ${DOCKER_NAME} "${PULSAR_DEF}"/cli_wallet -s ws://127.0.0.1:8090 -w /pulsar/wallet.json
}
remote_wallet() {
if (( $# == 1 )); then
REMOTE_WS=$1
fi
docker run -u "$(id -u)" -v "${DATADIR}":/pulsar --rm -it pulsar_img "${PULSAR_DEF}"/cli_wallet -s "$REMOTE_WS" -w /pulsar/wallet.json
}
logs() {
echo "${BLUE}DOCKER LOGS: (press ctrl-c to exit) ${RESET}"
docker logs -f --tail=30 ${DOCKER_NAME}
}
status() {
if seed_exists; then
echo "Container exists?: ${GREEN}YES${RESET}"
else
echo "Container exists?: ${RED}NO (!)${RESET}"
echo "Container doesn't exist, thus it is NOT running. Run '$0 install && $0 start'${RESET}"
return
fi
if seed_running; then
echo "Container running?: ${GREEN}YES${RESET}"
else
echo "Container running?: ${RED}NO (!)${RESET}"
echo "Container isn't running. Start it with '$0 start' or '$0 replay'${RESET}"
return
fi
}
if [[ ! -f "${DATADIR}/witness/config.ini" ]]; then
echo "config.ini not found. copying example (seed)";
cp "${DATADIR}/witness/config.ini.example" "${DATADIR}/witness/config.ini"
fi
if [ "$#" -lt 1 ]; then
help
fi
case $1 in
install_docker)
install_docker
;;
install_dependencies)
install_dependencies
;;
install)
installme "${@:2}"
;;
witness_setup)
initwit
;;
enable_witness)
updatewit
;;
witness)
initwit
echo
updatewit quiet
;;
disable_witness)
disablewit
;;
publish_feed)
updatefeed
;;
start)
start
;;
replay)
replay
;;
stop)
stop
;;
setup)
setup
;;
restart)
stop
sleep 5
start
;;
optimize)
echo "Applying recommended dirty write settings..."
optimize
;;
status)
status
;;
wallet)
wallet
;;
remote_wallet)
remote_wallet "${@:2}"
;;
rpcnode)
rpcnode
;;
dlblocks)
dlblocks
;;
enter)
enter
;;
logs)
logs
;;
info)
getinfo "${@:2}"
;;
change_password)
chgpass
;;
cleanup)
cleanup "${@:2}"
;;
*)
echo "Invalid cmd"
help
;;
esac
# vim: set filetype=sh ts=4 sw=4 tw=0 wrap et: