forked from koen84/elrond-go-scripts-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.sh
executable file
·331 lines (287 loc) · 11.8 KB
/
script.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
#!/bin/bash
set -e
#Color to the people
RED='\x1B[0;31m'
CYAN='\x1B[0;36m'
GREEN='\x1B[0;32m'
NC='\x1B[0m'
source config/identity
source config/variables.cfg
source config/functions.cfg
case "$1" in
'install')
read -p "How many nodes do you want to run ? : " NUMBEROFNODES
re='^[0-9]+$'
if ! [[ $NUMBEROFNODES =~ $re ]] && [ "$NUMBEROFNODES" -gt 0 ]
then
NUMBEROFNODES = 1
fi
#Check if CUSTOM_HOME exists
if ! [ -d "$CUSTOM_HOME" ]; then echo -e "${RED}Please configure your variables first ! (variables.cfg --> CUSTOM_HOME & CUSTOM_USER)${NC}"; exit; fi
prerequisites
replicant
#Keep track of how many nodes you've started on the machine
echo "$NUMBEROFNODES" > $CUSTOM_HOME/.numberofnodes
paths
go_lang
#If repos are present and you run install again this will clean up for you :D
if [ -d "$GOPATH/src/github.com/ElrondNetwork/elrond-go" ]; then echo -e "${RED}--> Repos present. Either run the upgrade command or cleanup & install again...${NC}"; echo -e; exit; fi
mkdir -p $GOPATH/src/github.com/ElrondNetwork
git_clone
build_node
build_keygen
#Run the install process for each node
for i in $(seq 1 $NUMBEROFNODES);
do
INDEX=$(( $i - 1 ))
WORKDIR="$CUSTOM_HOME/elrond-nodes/node-$INDEX"
install
node_name
keys
systemd
done
echo -e
echo -e "${GREEN}--------------------------------------------------------------------------------${NC}"
echo -e "${GREEN}---> This next section asks if you want to install the ${CYAN}AUTOPUDATER${GREEN}${NC}"
echo -e "${GREEN}--------------------------------------------------------------------------------${NC}"
echo -e
read -p "Do you want to install the auto-update function (Default No) ? (Yy/Nn)" yn
echo -e
case $yn in
[Yy]* )
echo -e "${GREEN}Adding auto-update to crontab !${NC}"
autoupdate
;;
[Nn]* )
echo -e "${GREEN}Fine... let's continue...${NC}"
;;
* )
echo -e "${GREEN}I'll take that as a no then...${NC}"
;;
esac
sudo chown -R $CUSTOM_USER:$CUSTOM_USER $CUSTOM_HOME/elrond-nodes
;;
'install-remote')
deploy_to_host
for HOST in $(cat config/target_ips)
do
echo -e
echo -e "${GREEN}--------------------------------------------------------------------------------${NC}"
echo -e
echo -e "${GREEN}---> Running the install process on the ${CYAN}$HOST${GREEN} machine ...${NC}"
echo -e
echo -e "${GREEN}--------------------------------------------------------------------------------${NC}"
echo -e
ssh -t -o StrictHostKeyChecking=no -p $SSHPORT -i "$PEM" $CUSTOM_USER@$HOST "cd $CUSTOM_HOME/$DIRECTORY_NAME && ./script.sh install"
done
;;
'upgrade')
paths
#Remove previously cloned repos
if [ -d "$GOPATH/src/github.com/ElrondNetwork/elrond-go" ]; then sudo rm -rf $GOPATH/src/github.com/ElrondNetwork/elrond-*; echo -e; echo -e "${RED}--> Repos present. Removing and fetching again...${NC}"; echo -e; fi
git_clone
build_node
INSTALLEDNODES=$(cat $CUSTOM_HOME/.numberofnodes)
#Run the update process for each node
for i in $(seq 1 $INSTALLEDNODES);
do
UPDATEINDEX=$(( $i - 1 ))
UPDATEWORKDIR="$CUSTOM_HOME/elrond-nodes/node-$UPDATEINDEX"
cp $UPDATEWORKDIR/config/prefs.toml $UPDATEWORKDIR/config/prefs.toml.save
read -p "Do you want to remove the current Node DB & Logs for node-$UPDATEINDEX ? (yes/no):" CLEAN
if [ "$CLEAN" != "no" ]
then
sudo systemctl stop elrond-node-$UPDATEINDEX
cleanup
update
mv $UPDATEWORKDIR/config/prefs.toml.save $UPDATEWORKDIR/config/prefs.toml
sudo systemctl start elrond-node-$UPDATEINDEX
else
sudo systemctl stop elrond-node-$UPDATEINDEX
update
mv $UPDATEWORKDIR/config/prefs.toml.save $UPDATEWORKDIR/config/prefs.toml
sudo systemctl start elrond-node-$UPDATEINDEX
fi
done
;;
'auto_upgrade')
paths
#Remove previously cloned repos
if [ -d "$GOPATH/src/github.com/ElrondNetwork/elrond-go" ]; then sudo rm -rf $GOPATH/src/github.com/ElrondNetwork/elrond-*; fi
git_clone
build_node
INSTALLEDNODES=$(cat $CUSTOM_HOME/.numberofnodes)
curl --silent "https://api.github.com/repos/ElrondNetwork/elrond-go/releases/latest" | grep "body" > $HOME/tmp
if grep -q "*This release should start with a new DB*" "$HOME/tmp"
then DBQUERY=1
else DBQUERY=0
fi
if [ "$DBQUERY" -eq "1" ]; then
for i in $(seq 1 $INSTALLEDNODES);
do
UPDATEINDEX=$(( $i - 1 ))
UPDATEWORKDIR="$CUSTOM_HOME/elrond-nodes/node-$UPDATEINDEX"
cp $UPDATEWORKDIR/config/prefs.toml $UPDATEWORKDIR/config/prefs.toml.save
sudo systemctl stop elrond-node-$UPDATEINDEX
echo "Database Cleanup Called ! Erasing DB for elrond-node-$UPDATEINDEX..." >> $HOME/autoupdate.status
cleanup
update
mv $UPDATEWORKDIR/config/prefs.toml.save $UPDATEWORKDIR/config/prefs.toml
sudo systemctl start elrond-node-$UPDATEINDEX
done
else
for i in $(seq 1 $INSTALLEDNODES);
do
UPDATEINDEX=$(( $i - 1 ))
UPDATEWORKDIR="$CUSTOM_HOME/elrond-nodes/node-$UPDATEINDEX"
cp $UPDATEWORKDIR/config/prefs.toml $UPDATEWORKDIR/config/prefs.toml.save
sudo systemctl stop elrond-node-$UPDATEINDEX
echo "Database Cleanup Not Needed for elrond-node-$UPDATEINDEX ! Moving to next step... " >> $HOME/autoupdate.status
update
mv $UPDATEWORKDIR/config/prefs.toml.save $UPDATEWORKDIR/config/prefs.toml
sudo systemctl start elrond-node-$UPDATEINDEX
done
fi
rm $HOME/tmp
;;
'upgrade-remote')
deploy_to_host
for HOST in $(cat config/target_ips)
do
echo -e
echo -e "${GREEN}--------------------------------------------------------------------------------${NC}"
echo -e
echo -e "${GREEN}---> Running the upgrade process on the ${CYAN}$HOST${GREEN} machine ...${NC}"
echo -e
echo -e "${GREEN}--------------------------------------------------------------------------------${NC}"
echo -e
ssh -t -o StrictHostKeyChecking=no -p $SSHPORT -i "$PEM" $CUSTOM_USER@$HOST "cd $CUSTOM_HOME/$DIRECTORY_NAME && ./script.sh upgrade"
done
;;
'start')
NODESTOSTART=$(cat $CUSTOM_HOME/.numberofnodes)
for i in $(seq 1 $NODESTOSTART);
do
STARTINDEX=$(( $i - 1 ))
echo -e
echo -e "${GREEN}Starting Elrond Node-$STARTINDEX binary on host ${CYAN}$HOST${GREEN}...${NC}"
echo -e
sudo systemctl start elrond-node-$STARTINDEX
done
;;
'start-remote')
for HOST in $(cat config/target_ips)
do
echo -e
echo -e "${GREEN}Starting Elrond Node binaries on host ${CYAN}$HOST${GREEN}...${NC}"
echo -e
ssh -t -o StrictHostKeyChecking=no -p $SSHPORT -i "$PEM" $CUSTOM_USER@$HOST "cd $CUSTOM_HOME/$DIRECTORY_NAME && ./script.sh start"
done
;;
'stop')
NODESTOSTOP=$(cat $CUSTOM_HOME/.numberofnodes)
for i in $(seq 1 $NODESTOSTOP);
do
STOPINDEX=$(( $i - 1 ))
echo -e
echo -e "${GREEN}Stopping Elrond Node-$STOPINDEX binary on host ${CYAN}$HOST${GREEN}...${NC}"
echo -e
sudo systemctl stop elrond-node-$STOPINDEX
done
;;
'stop-remote')
for HOST in $(cat config/target_ips)
do
echo -e
echo -e "${GREEN}Stopping Elrond Node binaries on host ${CYAN}$HOST${GREEN}...${NC}"
echo -e
ssh -t -o StrictHostKeyChecking=no -p $SSHPORT -i "$PEM" $CUSTOM_USER@$HOST "cd $CUSTOM_HOME/$DIRECTORY_NAME && ./script.sh stop"
done
;;
'cleanup')
paths
echo -e
read -p "Do you want to delete installed nodes (Default No) ? (Yy/Nn)" yn
echo -e
case $yn in
[Yy]* )
echo -e "${RED}OK ! Cleaning everything !${NC}"
if [[ -f $CUSTOM_HOME/.numberofnodes ]]; then
NODESTODESTROY=$(cat $CUSTOM_HOME/.numberofnodes)
for i in $(seq 1 $NODESTODESTROY);
do
KILLINDEX=$(( $i - 1 ))
echo -e
echo -e "${GREEN}Stopping Elrond Node-$KILLINDEX binary on host ${CYAN}$HOST${GREEN}...${NC}"
echo -e
[ -e /etc/systemd/system/elrond-node-$KILLINDEX.service ] && sudo systemctl stop elrond-node-$KILLINDEX
echo -e "${GREEN}Erasing unit file and node folder for Elrond Node-$KILLINDEX...${NC}"
echo -e
[ -e /etc/systemd/system/elrond-node-$KILLINDEX.service ] && sudo rm /etc/systemd/system/elrond-node-$KILLINDEX.service
if [ -d $CUSTOM_HOME/elrond-nodes/node-$KILLINDEX ]; then sudo rm -rf $CUSTOM_HOME/elrond-nodes/node-$KILLINDEX; fi
done
fi
#Reload systemd after deleting node units
sudo systemctl daemon-reload
echo -e
echo -e "${GREEN}Removing auto-updater crontab from host ${CYAN}$HOST${GREEN}...${NC}"
echo -e
crontab -l | grep -v '/auto-updater.sh' | crontab -
echo -e "${GREEN}Removing cloned elrond-go & elrond-configs repo from host ${CYAN}$HOST${GREEN}...${NC}"
echo -e
if [ -d "$GOPATH/src/github.com/ElrondNetwork/elrond-go" ]; then sudo rm -rf $GOPATH/src/github.com/ElrondNetwork/elrond-*; fi
;;
[Nn]* )
echo -e "${GREEN}Fine ! Skipping cleanup on this machine...${NC}"
;;
* )
echo -e "${GREEN}I'll take that as a no then... moving on...${NC}"
;;
esac
;;
'cleanup-remote')
for HOST in $(cat config/target_ips)
do
echo -e
echo -e "${GREEN}Running cleanup script on host ${CYAN}$HOST${GREEN}...${NC}"
echo -e
ssh -t -o StrictHostKeyChecking=no -p $SSHPORT -i "$PEM" $CUSTOM_USER@$HOST "cd $CUSTOM_HOME/$DIRECTORY_NAME && ./script.sh cleanup"
done
;;
'crontab')
echo -e
echo -e "${GREEN}--------------------------------------------------------------------------------${NC}"
echo -e "${GREEN}---> This next section asks if you want to install the ${CYAN}AUTOPUDATER${GREEN}${NC}"
echo -e "${GREEN}--------------------------------------------------------------------------------${NC}"
echo -e
read -p "Do you want to install the auto-update function (Default No) ? (Yy/Nn)" yn
echo -e
case $yn in
[Yy]* )
echo -e "${GREEN}Adding auto-update to crontab !${NC}"
if (crontab -l 2>/dev/null | grep -q "auto-updater.sh"); then echo "Crontab already installed"; else autoupdate; fi
;;
[Nn]* )
echo -e "${GREEN}Fine... let's continue...${NC}"
;;
* )
echo -e "${GREEN}I'll take that as a no then...${NC}"
;;
esac
;;
'crontab-remote')
for HOST in $(cat config/target_ips)
do
echo -e
echo -e "${GREEN}Running auto-update cronjob install script on host ${CYAN}$HOST${GREEN}...${NC}"
echo -e
ssh -t -o StrictHostKeyChecking=no -p $SSHPORT -i "$PEM" $CUSTOM_USER@$HOST "cd $CUSTOM_HOME/$DIRECTORY_NAME && ./script.sh crontab"
done
;;
'deploy')
deploy_to_host
;;
*)
echo "Usage: Missing parameter ! [install|install-remote|upgrade|upgrade-remote|start|start-remote|stop|stop-remote|cleanup|cleanup-remote]"
;;
esac