forked from Evolve-Blockchain/Core-Blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-setup.sh
executable file
·530 lines (428 loc) · 14.7 KB
/
node-setup.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
#!/bin/bash
set -e
GREEN='\033[0;32m'
RED='\033[0;31m'
ORANGE='\033[0;33m'
NC='\033[0m' # No Color
#########################################################################
totalRpc=0
totalValidator=0
totalNodes=$(($totalRpc + $totalValidator))
#########################################################################
#+-----------------------------------------------------------------------------------------------+
#| |
#| FUNCTIONS |
#| |
#+------------------------------------------------------------------------------------------------+
task1(){
# update and upgrade the server TASK 1
echo -e "\n\n${ORANGE}TASK: ${GREEN}[Setting up environment]${NC}\n"
apt update && apt upgrade -y
echo -e "\n${GREEN}[TASK 1 PASSED]${NC}\n"
}
task2(){
# installing build-essential TASK 2
echo -e "\n${ORANGE}TASK: ${GREEN}[Setting up environment]${NC}\n"
apt -y install build-essential tree
echo -e "\n${GREEN}[TASK 2 PASSED]${NC}\n"
}
task3(){
# getting golang TASK 3
echo -e "\n${ORANGE}TASK: ${GREEN}[Getting GO]${NC}\n"
cd ./tmp && wget -4 "https://go.dev/dl/go1.17.3.linux-amd64.tar.gz"
echo -e "\n${GREEN}[TASK 3 PASSED]${NC}\n"
}
task4(){
# setting up golang TASK 4
echo -e "\n${ORANGE}TASK: ${GREEN}[Setting GO]${NC}\n"
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.17.3.linux-amd64.tar.gz
LINE='PATH=$PATH:/usr/local/go/bin'
if grep -Fxq "$LINE" /etc/profile
then
# code if found
echo -e "${ORANGE}golang path is already added"
else
# code if not found
echo -e '\nPATH=$PATH:/usr/local/go/bin' >>/etc/profile
fi
echo -e '\nsource ~/.bashrc' >>/etc/profile
if [[ $totalValidator -gt 0 ]]; then
LINE='cd /root/Core-Blockchain/'
if grep -Fxq "$LINE" /etc/profile
then
# code if found
echo -e "${ORANGE}path is already added"
else
# code if not found
echo -e '\ncd /root/Core-Blockchain/' >>/etc/profile
fi
LINE='bash /root/Core-Blockchain/node-start.sh --validator'
if grep -Fxq "$LINE" /etc/profile
then
# code if found
echo -e "${ORANGE}autostart is already added"
else
# code if not found
echo -e '\nbash /root/Core-Blockchain/node-start.sh --validator' >>/etc/profile
fi
fi
if [[ $totalRpc -gt 0 ]]; then
LINE='cd /root/Core-Blockchain/'
if grep -Fxq "$LINE" /etc/profile
then
# code if found
echo -e "${ORANGE}path is already added"
else
# code if not found
echo -e '\ncd /root/Core-Blockchain/' >>/etc/profile
fi
LINE='bash /root/Core-Blockchain/node-start.sh --rpc'
if grep -Fxq "$LINE" /etc/profile
then
# code if found
echo -e "${ORANGE}autostart is already added"
else
# code if not found
echo -e '\nbash /root/Core-Blockchain/node-start.sh --rpc' >>/etc/profile
fi
fi
export PATH=$PATH:/usr/local/go/bin
go env -w GO111MODULE=off
echo -e "\n${GREEN}[TASK 4 PASSED]${NC}\n"
}
task5(){
# set proper group and permissions TASK 5
echo -e "\n${ORANGE}TASK: ${GREEN}[Setting up Permissions]${NC}\n"
ls -all
cd ../
ls -all
chown -R root:root ./
chmod a+x ./node-start.sh
echo -e "\n${GREEN}[TASK 5 PASSED]${NC}\n"
}
task6(){
# do make all TASK 6
echo -e "\n${ORANGE}TASK: ${GREEN}[Building Backend]${NC}\n"
cd node_src
make all
echo -e "\n${GREEN}[TASK 6 PASSED]${NC}\n"
}
task7(){
# setting up directories and structure for node/s TASK 7
echo -e "\n${ORANGE}TASK: ${GREEN}[Building Backend]${NC}\n"
cd ../
i=1
while [[ $i -le $totalNodes ]]; do
mkdir ./chaindata/node$i
((i += 1))
done
tree ./chaindata
echo -e "\n${GREEN}[TASK 7 PASSED]${NC}\n"
}
# task8(){
# #TASK 8
# echo -e "\n${ORANGE}TASK: ${GREEN}[Setting up Accounts]${NC}\n"
# echo -e "\n${ORANGE}This step is very important. Input a password that will be used for a newly created validator account. In next step you will recieve a public key for your validator account"
# echo -e "${ORANGE}Once a validator account is created using your given password, You'll be able to see where the keystore file is located so you can import it in metamask\n\n${NC}"
# i=1
# while [[ $i -le $totalValidator ]]; do
# echo -e "\n\n${GREEN}+-----------------------------------------------------------------------------------------------------+\n"
# read -p "Enter password for validator $i: " password
# echo $password >./chaindata/node$i/pass.txt
# ./node_src/build/bin/geth --datadir ./chaindata/node$i account new --password ./chaindata/node$i/pass.txt
# ((i += 1))
# done
# echo -e "\n${GREEN}[TASK 8 PASSED]${NC}\n"
# }
task8(){
#TASK 8
echo -e "\n${ORANGE}TASK: ${GREEN}[Setting up Accounts]${NC}\n"
echo -e "\n${ORANGE}This step is very important. Input a password that will be used for a newly created validator account. In the next step you will receive a public key for your validator account."
echo -e "${ORANGE}Once a validator account is created using your given password, you'll be able to see where the keystore file is located so you can import it in MetaMask\n\n${NC}"
i=1
while [[ $i -le $totalValidator ]]; do
echo -e "\n\n${GREEN}+-----------------------------------------------------------------------------------------------------+\n"
while true; do
read -p "Do you want to import your existing wallet for validator $i? (y/n): " choice
case $choice in
y|Y)
read -p "Enter the private key for validator $i: " private_key
read -p "Enter the password for your imported wallet: " password
echo $private_key >./chaindata/node$i/pk.txt
echo $password >./chaindata/node$i/pass.txt
./node_src/build/bin/geth --datadir ./chaindata/node$i account import ./chaindata/node$i/pk.txt --password ./chaindata/node$i/pass.txt
echo -e "\n${GREEN}Imported wallet for validator $i with the given private key.\n${NC}"
break
;;
n|N)
read -p "Enter password for validator $i: " password
echo $password >./chaindata/node$i/pass.txt
./node_src/build/bin/geth --datadir ./chaindata/node$i account new --password ./chaindata/node$i/pass.txt
break
;;
*)
echo -e "${RED}Invalid input. Please enter 'y' for yes or 'n' for no.${NC}"
;;
esac
done
((i += 1))
done
echo -e "\n${GREEN}[TASK 8 PASSED]${NC}\n"
}
labelNodes(){
i=1
while [[ $i -le $totalValidator ]]; do
touch ./chaindata/node$i/.validator
((i += 1))
done
i=$((totalValidator + 1))
while [[ $i -le $totalNodes ]]; do
touch ./chaindata/node$i/.rpc
((i += 1))
done
}
displayStatus(){
# start the node
echo -e "\n${ORANGE}STATUS: ${GREEN}ALL TASK PASSED!\n This program will now exit\n Now run ./node-start.sh${NC}\n"
}
displayWelcome(){
# display welcome message
echo -e "\n\n\t${ORANGE}Total RPC to be created: $totalRpc"
echo -e "\t${ORANGE}Total Validators to be created: $totalValidator"
echo -e "\t${ORANGE}Total nodes to be created: $totalNodes"
echo -e "${GREEN}
\t+------------------------------------------------+
\t+ DPos node installation Wizard
\t+ Target OS: Ubuntu 20.04 LTS (Focal Fossa)
\t+ Your OS: $(. /etc/os-release && printf '%s\n' "${PRETTY_NAME}")
\t+------------------------------------------------+
${NC}\n"
echo -e "${ORANGE}
\t+------------------------------------------------+
\t+------------------------------------------------+
${NC}"
}
doUpdate(){
echo -e "${GREEN}
\t+------------------------------------------------+
\t+ UPDATING TO LATEST
\t+------------------------------------------------+
${NC}"
git pull
}
createRpc(){
task1
task2
task3
task4
task5
task6
task7
i=$((totalValidator + 1))
while [[ $i -le $totalNodes ]]; do
read -p "Enter Virtual Host(example: rpc.yourdomain.tld) without https/http: " vhost
echo -e "\nVHOST=$vhost" >> ./.env
./node_src/build/bin/geth --datadir ./chaindata/node$i init ./genesis.json
((i += 1))
done
}
createValidator(){
if [[ $totalValidator -gt 0 ]]; then
task8
fi
i=1
while [[ $i -le $totalValidator ]]; do
./node_src/build/bin/geth --datadir ./chaindata/node$i init ./genesis.json
((i += 1))
done
}
# get external IP of this server
fetchNsetIP(){
echo -e "\nIP=$(curl http://checkip.amazonaws.com)" >> ./.env
}
install_nvm() {
# Check if nvm is installed
if ! command -v nvm &> /dev/null; then
echo "Installing NVM..."
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
# Source NVM scripts for the current session
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
# Add NVM initialization to shell startup file
if [ -n "$BASH_VERSION" ]; then
SHELL_PROFILE="$HOME/.bashrc"
elif [ -n "$ZSH_VERSION" ]; then
SHELL_PROFILE="$HOME/.zshrc"
fi
if ! grep -q 'export NVM_DIR="$HOME/.nvm"' "$SHELL_PROFILE"; then
echo 'export NVM_DIR="$HOME/.nvm"' >> "$SHELL_PROFILE"
echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> "$SHELL_PROFILE"
echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> "$SHELL_PROFILE"
fi
else
echo "NVM is already installed."
fi
# Source NVM scripts (if not sourced already)
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
# Install Node.js version 21.7.1 using nvm
echo "Installing Node.js version 21.7.1..."
nvm install 21.7.1
# Use the installed Node.js version
nvm use 21.7.1
# Verify the installation
node_version=$(node --version)
if [[ $node_version == v21.7.1 ]]; then
echo "Node.js version 21.7.1 installed successfully: $node_version"
else
echo "There was an issue installing Node.js version 21.7.1."
fi
source ~/.bashrc
npm install --global yarn
npm install --global pm2
source ~/.bashrc
}
# Function to delete a folder or file if it exists
delete_if_exists() {
local target="$1"
if [ -d "$target" ]; then
echo "Deleting folder: $target"
rm -rf "$target"
elif [ -f "$target" ]; then
echo "Deleting file: $target"
rm -f "$target"
else
echo "No such file or folder: $target"
fi
}
finalize(){
displayWelcome
createRpc
createValidator
labelNodes
# resource paths
nodePath=/root/Core-Blockchain
ipcPath=$nodePath/chaindata/node1/geth.ipc
chaindataPath=$nodePath/chaindata/node1/geth
snapshotName=$nodePath/chaindata.tar.gz
# echo -e "\n\n\t${ORANGE}Removing existing chaindata, if any${NC}"
# rm -rf $chaindataPath/chaindata
# echo -e "\n\n\t${GREEN}Now importing the snapshot"
# wget https://snapshots.evolveblockchain.io/chaindata.tar.gz
# # Create the directory if it does not exist
# if [ ! -d "$chaindataPath" ]; then
# mkdir -p $chaindataPath
# fi
# # Extract archive to the correct directory
# tar -xvf $snapshotName -C $chaindataPath --strip-components=1
# Set proper permissions
echo -e "\n\n\t${GREEN}Setting directory permissions${NC}"
chown -R root:root /root/Core-Blockchain/chaindata
chmod -R 755 /root/Core-Blockchain/chaindata
echo -e "\n\n\tImport is done, now configuring sync-helper${NC}"
sleep 3
cd $nodePath
install_nvm
cd plugins/sync-helper
yarn
cd ../../
displayStatus
}
#########################################################################
#+-----------------------------------------------------------------------------------------------+
#| |
#| |
#| UTILITY |
#| |
#| |
#+-----------------------------------------------------------------------------------------------+
# Default variable values
verbose_mode=false
output_file=""
# Function to display script usage
usage() {
echo -e "\nUsage: $0 [OPTIONS]"
echo "Options:"
echo -e "\t\t -h, --help Display this help message"
echo -e " \t\t -v, --verbose Enable verbose mode"
echo -e "\t\t --rpc Specify to create RPC node"
echo -e "\t\t --validator <whole number> Specify number of validator node to create"
}
has_argument() {
[[ ("$1" == *=* && -n ${1#*=}) || (! -z "$2" && "$2" != -*) ]]
}
extract_argument() {
echo "${2:-${1#*=}}"
}
# Function to handle options and arguments
handle_options() {
while [ $# -gt 0 ]; do
case $1 in
# display help
-h | --help)
usage
exit 0
;;
# toggle verbose
-v | --verbose)
verbose_mode=true
;;
# take file input
-f | --file*)
if ! has_argument $@; then
echo "File not specified." >&2
usage
exit 1
fi
output_file=$(extract_argument $@)
shift
;;
# take ROC count
--rpc)
totalRpc=1
totalNodes=$(($totalRpc + $totalValidator))
;;
# take validator count
--validator*)
if ! has_argument $@; then
echo "No number given" >&2
usage
exit 1
fi
totalValidator=$(extract_argument $@)
totalNodes=$(($totalRpc + $totalValidator))
shift
;;
# check for update and do update
--update)
doUpdate
exit 0
;;
*)
echo "Invalid option: $1" >&2
usage
exit 1
;;
esac
shift
done
}
# Main script execution
handle_options "$@"
# Perform the desired actions based on the provided flags and arguments
if [ "$verbose_mode" = true ]; then
echo "Verbose mode enabled."
fi
if [ -n "$output_file" ]; then
echo "Output file specified: $output_file"
fi
if [ $# -eq 0 ]
then
echo "No arguments supplied"
usage
exit 1
fi
# bootstraping
finalize