-
Notifications
You must be signed in to change notification settings - Fork 28
/
start_tracker_incremental_gcp.sh
117 lines (97 loc) · 3.54 KB
/
start_tracker_incremental_gcp.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
#!/bin/bash
if [[ "$#" -gt 2 ]]; then
printf "Usage: bash start_tracker_incremental_gcp.sh <GCP Username> [--keep-code|--no-keep-code]\n"
printf "Example: bash start_tracker_incremental_gcp.sh gcp_user --keep-code\n"
printf "\n"
exit
fi
printf "\n[[[[[ start_tracker_incremental_gcp.sh ]]]]]\n\n"
# 1. Configure env vars
printf "\n#### [Step 1] Configure env vars ####\n\n"
GCP_USER="$1"
KEEP_CODE_OPTION="--keep-code"
if [[ $# = 2 ]]; then
if [[ $2 = '--keep-code' ]]; then
KEEP_CODE_OPTION=$2
elif [[ $2 = '--no-keep-code' ]]; then
KEEP_CODE_OPTION=$2
else
printf "Invalid option: $2\n"
exit
fi
fi
printf "GCP_USER=$GCP_USER\n"
printf "KEEP_CODE_OPTION=$KEEP_CODE_OPTION\n"
# 2. Get currently used directory & new directory
printf "\n#### [Step 2] Get currently used directory & new directory ####\n\n"
OLD_DIR_PATH=$(find /home/ain-blockchain* -maxdepth 0 -type d)
printf "OLD_DIR_PATH=$OLD_DIR_PATH\n"
date=$(date '+%Y-%m-%dT%H:%M')
printf "date=$date\n"
NEW_DIR_NAME="ain-blockchain-$date"
printf "NEW_DIR_NAME=$NEW_DIR_NAME\n"
NEW_DIR_PATH="/home/$NEW_DIR_NAME"
printf "NEW_DIR_PATH=$NEW_DIR_PATH\n"
# 3. Set up working directory & install modules
printf "\n#### [Step 3] Set up working directory & install modules ####\n\n"
if [[ $KEEP_CODE_OPTION = "--no-keep-code" ]]; then
printf '\n'
printf 'Setting up new data directory..\n'
CODE_CMD="cd ~; sudo mv ain-blockchain $NEW_DIR_NAME; sudo mv $NEW_DIR_NAME /home; sudo chmod -R 777 $NEW_DIR_PATH; sudo chown -R $GCP_USER:$GCP_USER $NEW_DIR_PATH"
printf "\nCODE_CMD=$CODE_CMD\n"
eval $CODE_CMD
printf '\n'
printf 'Installing node modules..\n'
cd $NEW_DIR_PATH
INSTALL_CMD="yarn install --ignore-engines"
printf "\nINSTALL_CMD=$INSTALL_CMD\n"
eval $INSTALL_CMD
else
printf '\n'
printf 'Reusing existing working directory..\n'
CODE_CMD="sudo chmod -R 777 $OLD_DIR_PATH; sudo chown -R $GCP_USER:$GCP_USER $OLD_DIR_PATH"
printf "\nCODE_CMD=$CODE_CMD\n"
eval $CODE_CMD
fi
# 4. Kill old tracker server
printf "\n#### [Step 4] Kill old tracker server ####\n\n"
KILL_CMD="sudo killall node"
printf "KILL_CMD=$KILL_CMD\n\n"
eval $KILL_CMD
sleep 10
# 5. Remove old working directory
printf "\n#### [Step 5] Remove old working directory ####\n\n"
if [[ $KEEP_CODE_OPTION = "--no-keep-code" ]]; then
printf '\n'
printf 'Removing old working directory..\n'
RM_CMD="sudo rm -rf $OLD_DIR_PATH"
printf "\nRM_CMD=$RM_CMD\n"
eval $RM_CMD
else
printf '\n'
printf 'Keeping existing working directory..\n'
fi
# 6. Start new tracker server
printf "\n#### [Step 6] Start new tracker server ####\n\n"
START_CMD="nohup node --async-stack-traces tracker-server/index.js >/dev/null 2>error_logs.txt &"
printf "\nSTART_CMD=$START_CMD\n"
printf "START_CMD=$START_CMD\n" >> start_commands.txt
eval $START_CMD
# 7. Wait until the new tracker server catches up
printf "\n#### [Step 7] Wait until the new tracker server catches up ####\n\n"
SECONDS=0
loopCount=0
while :
do
numNodesAlive=$(curl -m 20 -X GET -H "Content-Type: application/json" "http://localhost:8080/network_status" | jq -r '.numNodesAlive')
printf "\nnumNodesAlive = ${numNodesAlive}\n"
if [[ $numNodesAlive -gt 0 ]]; then
printf "\nBlockchain Tracker server is running!\n"
printf "\nTime it took to sync in seconds: $SECONDS\n"
break
fi
((loopCount++))
printf "\nLoop count: ${loopCount}\n"
sleep 20
done
printf "\n* << Tracker server successfully deployed! ************************************\n\n"