This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
lisk.sh
executable file
Β·463 lines (406 loc) Β· 11.1 KB
/
lisk.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
#!/bin/bash
#
# LiskHQ/lisk-scripts/lisk.sh
# Copyright (C) 2017 Lisk Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
######################################################################
# shellcheck disable=SC2129
cd "$(cd -P -- "$(dirname -- "$0")" && pwd -P)" || exit 2
if [ ! -f "$(pwd)/app.js" ]; then
echo "Error: Lisk installation was not found. Exiting."
exit 1
fi
if [ "$USER" == "root" ]; then
echo "Error: Lisk should not be run be as root. Exiting."
exit 1
fi
# shellcheck disable=SC1090
. "$(pwd)/shared.sh"
# shellcheck disable=SC1090
. "$(pwd)/env.sh"
PM2_CONFIG="$(pwd)/etc/pm2-lisk.json"
PM2_APP="$(grep "name" "$PM2_CONFIG" | cut -d'"' -f4)" >> /dev/null
LISK_CONFIG="$(grep "config" "$PM2_CONFIG" | cut -d'"' -f4 | cut -d' ' -f2)" >> /dev/null
LISK_LOGS="$(jq -r '.logFileName' "$LISK_CONFIG")"
LOGS_DIR="$(pwd)/logs"
# Allocates variables for use later, reusable for changing pm2 config.
config() {
DB_NAME="$(jq -r '.db.database' "$LISK_CONFIG")"
DB_PORT="$(jq -r '.db.port' "$LISK_CONFIG")"
DB_USER="$USER"
DB_PASS="password"
DB_DATA="$(pwd)/pgsql/data"
DB_LOG_FILE="$LOGS_DIR/pgsql.log"
DB_SNAPSHOT="blockchain.db.gz"
DB_DOWNLOAD=Y
REDIS_CONFIG="$(pwd)/etc/redis.conf"
REDIS_BIN="$(pwd)/bin/redis-server"
REDIS_CLI="$(pwd)/bin/redis-cli"
REDIS_ENABLED="$(jq -r '.cacheEnabled' "$LISK_CONFIG")"
REDIS_PORT="$(jq -r '.redis.port' "$LISK_CONFIG")"
REDIS_PASSWORD="$(jq -r '.redis.password' "$LISK_CONFIG")"
REDIS_PID="$(pwd)/redis/redis_6380.pid"
}
# Sets all of the variables
config
# Setup logging
SH_LOG_FILE="$LOGS_DIR/lisk.out"
exec > >(tee -ia "$SH_LOG_FILE")
exec 2>&1
################################################################################
blockheight() {
DB_HEIGHT="$(psql -d "$DB_NAME" -t -p "$DB_PORT" -c 'select height from blocks order by height desc limit 1;')"
HEIGHT="${DB_HEIGHT:- Unavailable}"
echo -e "Current Block Height:" "$HEIGHT"
}
network() {
# shellcheck disable=SC2143
if [ "$(grep "da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba" "$LISK_CONFIG" )" ];then
NETWORK="test"
elif [ "$(grep "ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511" "$LISK_CONFIG")" ];then
NETWORK="main"
elif [ "$(grep "ef3844327d1fd0fc5785291806150c937797bdb34a748c9cd932b7e859e9ca0c" "$LISK_CONFIG")" ];then
NETWORK="beta"
else
NETWORK="local"
fi
echo -e 'Lisk configured for '"$NETWORK"' network\n' >> "$SH_LOG_FILE" 2>&1
}
create_user() {
dropuser --if-exists "$DB_USER" >> "$SH_LOG_FILE" 2>&1
createuser --createdb "$DB_USER" >> "$SH_LOG_FILE" 2>&1
if ! psql -qd postgres -c "ALTER USER $DB_USER WITH PASSWORD '$DB_PASS';" >> "$SH_LOG_FILE" 2>&1; then
echo "X Failed to create Postgresql user."
exit 1
else
echo "β Postgresql user created successfully."
fi
}
create_database() {
dropdb --if-exists "$DB_NAME" >> "$SH_LOG_FILE" 2>&1
if ! createdb "$DB_NAME" >> "$SH_LOG_FILE" 2>&1; then
echo "X Failed to create Postgresql database."
exit 1
else
echo "β Postgresql database created successfully."
fi
}
populate_database() {
if psql -ltAq | grep -q "^$DB_NAME|" >> "$SH_LOG_FILE" 2>&1; then
download_blockchain
restore_blockchain
fi
}
download_blockchain() {
if [ "$DB_DOWNLOAD" = "Y" ]; then
rm -f "$DB_SNAPSHOT"
if [ "$BLOCKCHAIN_URL" = "" ]; then
BLOCKCHAIN_URL="https://downloads.lisk.io/lisk/$NETWORK"
fi
echo 'β Downloading '"$DB_SNAPSHOT"' from '"$BLOCKCHAIN_URL"
if ! curl --progress-bar -o "$DB_SNAPSHOT" "$BLOCKCHAIN_URL/$DB_SNAPSHOT"; then
rm -f "$DB_SNAPSHOT"
echo "X Failed to download blockchain snapshot."
exit 1
else
# Required to clean up ugly curl output in the logs
sed -i -e '/[#]/d' "$SH_LOG_FILE"
echo "β Blockchain snapshot downloaded successfully."
fi
else
echo -e "β Using Local Snapshot."
fi
}
restore_blockchain() {
echo 'Restoring blockchain with '"$DB_SNAPSHOT"
if ! gunzip -fcq "$DB_SNAPSHOT" | psql -q -U "$DB_USER" -d "$DB_NAME" >> "$SH_LOG_FILE" 2>&1; then
echo "X Failed to restore blockchain."
exit 1
else
echo "β Blockchain restored successfully."
fi
}
autostart_cron() {
local cmd="crontab"
if ! command -v "$cmd" > /dev/null 2>&1; then
echo "X Failed to execute crontab."
return 1
fi
crontab=$($cmd -l 2> /dev/null | sed '/lisk\.sh start/d' 2> /dev/null)
crontab=$(cat <<-EOF
$crontab
@reboot $(command -v "bash") $(pwd)/lisk.sh start > $(pwd)/cron.log 2>&1
EOF
)
if ! printf "%s\\n" "$crontab" | $cmd - >> "$SH_LOG_FILE" 2>&1; then
echo "X Failed to update crontab."
return 1
else
echo "β Crontab updated successfully."
return 0
fi
}
coldstart_lisk() {
stop_lisk >> "$SH_LOG_FILE" 2>&1
stop_postgresql >> "$SH_LOG_FILE" 2>&1
rm -rf "$DB_DATA"
pg_ctl initdb -D "$DB_DATA" >> "$SH_LOG_FILE" 2>&1
sleep 2
start_postgresql
sleep 1
create_user
create_database
populate_database
autostart_cron
start_lisk
}
start_postgresql() {
if pgrep -x "postgres" > /dev/null 2>&1; then
echo "β Postgresql is running."
else
if ! pg_ctl -D "$DB_DATA" -l "$DB_LOG_FILE" start >> "$SH_LOG_FILE" 2>&1; then
echo "X Failed to start Postgresql."
exit 1
else
echo "β Postgresql started successfully."
fi
fi
}
stop_postgresql() {
if ! pgrep -x "postgres" > /dev/null 2>&1; then
echo "β Postgresql is not running."
else
if pg_ctl -D "$DB_DATA" -l "$DB_LOG_FILE" stop >> "$SH_LOG_FILE" 2>&1; then
echo "β Postgresql stopped successfully."
else
echo "X Postgresql failed to stop."
fi
if pgrep -x "postgres" >> "$SH_LOG_FILE" 2>&1; then
pkill -x postgres -9 >> "$SH_LOG_FILE" 2>&1;
echo "β Postgresql Killed."
fi
fi
}
start_redis() {
if [[ "$REDIS_ENABLED" == 'true' ]]; then
if [[ "$REDIS_PORT" == '6379' ]]; then
echo "β Using OS Redis-Server, skipping startup"
elif [[ ! -f "$REDIS_PID" ]]; then
if "$REDIS_BIN" "$REDIS_CONFIG"; then
echo "β Redis-Server started successfully."
else
echo "X Failed to start Redis-Server."
exit 1
fi
else
echo "β Redis-Server is already running"
fi
fi
}
stop_redis() {
if [[ "$REDIS_ENABLED" == 'true' ]]; then
if [[ "$REDIS_PORT" == '6379' ]]; then
echo "β OS Redis-Server detected, skipping shutdown"
elif [[ -f "$REDIS_PID" ]]; then
if stop_redis_cmd; then
echo "β Redis-Server stopped successfully."
else
echo "X Failed to stop Redis-Server."
REDIS_PID="$(tail -n1 "$REDIS_PID")"
pkill -9 "$REDIS_PID"
echo "β Redis-Server killed"
fi
else
echo "β Redis-Server already stopped"
fi
fi
}
stop_redis_cmd(){
# Necessary to pass the right password string to redis
if [[ "$REDIS_PASSWORD" != null ]]; then
"$REDIS_CLI" -p "$REDIS_PORT" "-a $REDIS_PASSWORD" shutdown
else
"$REDIS_CLI" -p "$REDIS_PORT" shutdown
fi
}
start_lisk() {
start_redis
if pm2 start "$PM2_CONFIG" >> "$SH_LOG_FILE"; then
echo "β Lisk started successfully."
sleep 3
check_status
else
echo "X Failed to start Lisk."
fi
}
stop_lisk() {
pm2 delete "$PM2_CONFIG" >> "$SH_LOG_FILE"
echo "β Lisk stopped successfully."
stop_redis
}
reload_lisk() {
echo "Stopping Lisk to reload PM2 config"
stop_lisk
start_lisk
}
rebuild_lisk() {
create_database
download_blockchain
restore_blockchain
}
pm2_cleanup() {
pm2 delete all
pm2 kill
}
check_status() {
PM2_PID="$( pm2 jlist |jq -r ".[] | select(.name == \"$PM2_APP\").pm2_env.pm_pid_path" )"
pm2 describe "$PM2_APP" >> "$SH_LOG_FILE"
check_pid
if [ "$STATUS" -eq 0 ]; then
echo "β Lisk is running as PID: $PID"
blockheight
else
echo "X Lisk is not running"
exit 1
fi
}
check_pid() {
if [ -f "$PM2_PID" ]; then
read -r PID < "$PM2_PID" 2>&1 > /dev/null
fi
if [ ! -z "$PID" ]; then
ps -p "$PID" > /dev/null 2>&1
STATUS=$?
else
STATUS=1
fi
}
lisky() {
node "$(pwd)/bin/lisky"
}
tail_logs() {
tail -f "$LISK_LOGS"
}
help() {
echo -e "\\nCommand Options for Lisk.sh"
echo -e "\\nAll options may be passed [-p <PM2-config.json>]"
echo -e "\\nstart_node Starts a Nodejs process for Lisk"
echo -e "start Starts the Nodejs process and PostgreSQL Database for Lisk"
echo -e "stop_node Stops a Nodejs process for Lisk"
echo -e "stop Stop the Nodejs process and PostgreSQL Database for Lisk"
echo -e "reload Restarts the Nodejs process for Lisk"
echo -e "rebuild [-u URL] [-f file.db.gz] [-0] Rebuilds the PostgreSQL database"
echo -e "start_db Starts the PostgreSQL database"
echo -e "stop_db Stops the PostgreSQL database"
echo -e "coldstart Creates the PostgreSQL database and configures config.json for Lisk"
echo -e "lisky Launches Lisky"
echo -e "logs Displays and tails logs for Lisk"
echo -e "status Displays the status of the PID associated with Lisk"
echo -e "help Displays this message"
}
parse_option() {
OPTIND=2
while getopts ":p:f:u:l:0" OPT; do
case "$OPT" in
p)
if [ -f "$OPTARG" ]; then
PM2_CONFIG="$OPTARG"
PM2_APP="$( jq .apps[0].name -r "$PM2_CONFIG" )"
LISK_CONFIG="$( jq .apps[0].args -r "$PM2_CONFIG" |cut -d' ' -f2 )"
# Resets all of the variables
config
else
echo "PM2-config.json not found. Please verify the file exists and try again."
exit 1
fi ;;
u)
BLOCKCHAIN_URL="$OPTARG"
;;
f)
DB_SNAPSHOT="$OPTARG"
if [ -f "$OPTARG" ]; then
DB_DOWNLOAD=N
fi ;;
0)
DB_SNAPSHOT="$(pwd)/var/db/blockchain.db.gz"
DB_DOWNLOAD=N
;;
:) echo 'Missing option argument for -'"$OPTARG" >&2; exit 1;;
*) echo 'Unimplemented option: -'"$OPTARG" >&2; exit 1;;
esac
done
}
parse_option "$@"
network
case $1 in
"coldstart")
coldstart_lisk
;;
"start_node")
start_lisk
;;
"start")
start_postgresql
sleep 2
start_lisk
;;
"stop_node")
stop_lisk
;;
"stop")
stop_lisk
stop_postgresql
;;
"reload")
reload_lisk
;;
"rebuild")
stop_lisk
sleep 1
start_postgresql
sleep 1
rebuild_lisk
start_lisk
;;
"start_db")
start_postgresql
;;
"stop_db")
stop_postgresql
;;
"cleanup")
pm2_cleanup
;;
"status")
check_status
;;
"logs")
tail_logs
;;
"lisky")
lisky
;;
"help")
help
;;
*)
echo "Error: Unrecognized command."
echo ""
echo "Available commands are: start stop start_node stop_node start_db stop_db reload rebuild coldstart logs lisky status help"
help
;;
esac
# Required to clean up colour characters that don't translate well from tee
sed -i -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' "$SH_LOG_FILE"