-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshardeum_node.sh
516 lines (413 loc) · 14.6 KB
/
shardeum_node.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
#!/usr/bin/env bash
clear
while true
do
# Logo
echo " "
echo " _____ _ _ ___ ____________ _____ _ ____ ___ "
echo " / ___| | | | / _ \ | ___ \ _ \ ___| | | | \/ | "
echo " \ \`--.| |_| |/ /_\ \| |_/ / | | | |__ | | | | . . | "
echo " \`--. \ _ || _ || /| | | | __|| | | | |\/| | "
echo " /\__/ / | | || | | || |\ \| |/ /| |___| |_| | | | | "
echo " \____/\_| |_/\_| |_/\_| \_|___/ \____/ \___/\_| |_| "
echo " "
echo " "
PS3='Select an action: '
options=(
"Prepare the server for installation"
"Install Shardeum Node"
"Update Node"
"Dashboard addres"
"Stop and delite node"
"Exit")
select opt in "${options[@]}"
do
case $opt in
"Prepare the server for installation")
cat << EOF
PREPARATION HAS BEGUN
EOF
sleep 3
# Step 1: Install required packages
echo "Step 1/2: Installing required packages..."
sudo apt-get upgrade -y > /dev/null 2>&1
sudo apt-get install curl wget jq libpq-dev libssl-dev build-essential pkg-config openssl ocl-icd-opencl-dev libopencl-clang-dev libgomp1 -y > /dev/null 2>&1
# Step 2: Install Docker Compose
echo "Step 2/2: Installing Docker Compose..."
sudo apt install docker-compose -y > /dev/null 2>&1
echo "Installation complete."
#INSTALL DOCKER
sleep 3
cat << EOF
INSTALL DOCKER AND DOCKER-COMPOSE
EOF
echo "Checking if Docker is already installed..."
if command -v docker >/dev/null 2>&1; then
echo "Docker is already installed on this machine."
else
echo "Installing Docker..."
echo "Step 1/6: Installing dependencies..."
sudo apt-get install curl gnupg apt-transport-https ca-certificates lsb-release -y >/dev/null 2>&1
echo "Step 2/6: Adding Docker GPG key to keyring..."
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg >/dev/null 2>&1
echo "Step 3/6: Adding Docker repository to sources list..."
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null 2>&1
echo "Step 4/6: Updating package list..."
sudo apt-get update >/dev/null 2>&1
echo "Step 5/6: Installing Docker..."
sudo apt-get install docker-ce docker-ce-cli containerd.io -y >/dev/null 2>&1
echo "Step 6/6: Docker installation completed."
echo "Docker has been successfully installed."
fi
#Check if Docker Compose is installed
if command -v docker-compose >/dev/null 2>&1; then
echo "Docker Compose is already installed on this machine."
else
#Installing Docker Compose
#Download the Docker Compose binary
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose >/dev/null 2>&1
#Apply executable permissions to the Docker Compose binary
sudo chmod +x /usr/local/bin/docker-compose
fi
#Verify the installation of Docker and Docker Compose
echo " "
echo "####################################################################################"
docker --version
docker-compose --versio
echo "The server is ready!"
break
;;
"Install Shardeum Node")
cat << EOF
INSTALLATION HAS STARTED
EOF
read -p "During this early stage of Betanet the Shardeum team will be collecting some performance and debugging info from your node to help improve future versions of the software.
This is only temporary and will be discontinued as we get closer to mainnet.
Thanks for running a node and helping to make Shardeum better.
By running this installer, you agree to allow the Shardeum team to collect this data. (y/n)?: " WARNING_AGREE
WARNING_AGREE=${WARNING_AGREE:-y}
if [ $WARNING_AGREE != "y" ];
then
echo "Diagnostic data collection agreement not accepted. Exiting installer."
exit
fi
# Check all things that will be needed for this script to succeed like access to docker and docker-compose
# If any check fails exit with a message on what the user needs to do to fix the problem
command -v git >/dev/null 2>&1 || { echo >&2 "'git' is required but not installed."; exit 1; }
command -v docker >/dev/null 2>&1 || { echo >&2 "'docker' is required but not installed. See https://gitlab.com/shardeum/validator/dashboard/-/tree/dashboard-gui-nextjs#how-to for details."; exit 1; }
if command -v docker-compose &>/dev/null; then
echo "docker-compose is installed on this machine"
elif docker --help | grep -q "compose"; then
echo "docker compose subcommand is installed on this machine"
else
echo "docker-compose or docker compose is not installed on this machine"
exit 1
fi
export DOCKER_DEFAULT_PLATFORM=linux/amd64
docker-safe() {
if ! command -v docker &>/dev/null; then
echo "docker is not installed on this machine"
exit 1
fi
if ! docker $@; then
echo "Trying again with sudo..."
sudo docker $@
fi
}
docker-compose-safe() {
if command -v docker-compose &>/dev/null; then
cmd="docker-compose"
elif docker --help | grep -q "compose"; then
cmd="docker compose"
else
echo "docker-compose or docker compose is not installed on this machine"
exit 1
fi
if ! $cmd $@; then
echo "Trying again with sudo..."
sudo $cmd $@
fi
}
get_ip() {
local ip
if command -v ip >/dev/null; then
ip=$(ip addr show $(ip route | awk '/default/ {print $5}') | awk '/inet/ {print $2}' | cut -d/ -f1)
elif command -v ifconfig >/dev/null; then
ip=$(ifconfig | awk '/inet addr/{print substr($2,6)}')
else
echo "Error: neither 'ip' nor 'ifconfig' command found"
return 1
fi
echo $ip
}
get_external_ip() {
external_ip=''
external_ip=$(curl -s https://api.ipify.org)
if [[ -z "$external_ip" ]]; then
external_ip=$(curl -s http://checkip.dyndns.org | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
fi
if [[ -z "$external_ip" ]]; then
external_ip=$(curl -s http://ipecho.net/plain)
fi
if [[ -z "$external_ip" ]]; then
external_ip=$(curl -s https://icanhazip.com/)
fi
if [[ -z "$external_ip" ]]; then
external_ip=$(get_ip)
if [ $? -eq 0 ]; then
echo "The IP address is: $IP"
else
external_ip="localhost"
fi
fi
echo $external_ip
}
if [[ $(docker-safe info 2>&1) == *"Cannot connect to the Docker daemon"* ]]; then
echo "Docker daemon is not running"
exit 1
else
echo "Docker daemon is running"
fi
cat << EOF
1. Get inf from user
EOF
read -p "Do you want to run the web based Dashboard? (y/n): " RUNDASHBOARD
RUNDASHBOARD=${RUNDASHBOARD:-y}
while true; do
read -p "Set the password to access the Dashboard: " -s input
echo
if [[ -n "$input" ]] && [[ ! "$input" =~ \ ]]; then
DASHPASS=$input
break
else
echo "Invalid input, try again."
fi
done
while :; do
read -p "Enter the port (1025-65536) to access the web based Dashboard (default 8080): " DASHPORT
DASHPORT=${DASHPORT:-8080}
[[ $DASHPORT =~ ^[0-9]+$ ]] || { echo "Enter a valid port"; continue; }
if ((DASHPORT >= 1025 && DASHPORT <= 65536)); then
DASHPORT=${DASHPORT:-8080}
break
else
echo "Port out of range, try again"
fi
done
while :; do
echo "To run a validator on the Sphinx network, you will need to open two ports in your firewall."
read -p "This allows p2p commnication between nodes. Enter the first port (1025-65536) for p2p comminucation (default 9001): " SHMEXT
SHMEXT=${SHMEXT:-9001}
[[ $SHMEXT =~ ^[0-9]+$ ]] || { echo "Enter a valid port"; continue; }
if ((SHMEXT >= 1025 && SHMEXT <= 65536)); then
SHMEXT=${SHMEXT:-9001}
else
echo "Port out of range, try again"
fi
read -p "Enter the second port (1025-65536) for p2p comminucation (default 10001): " SHMINT
SHMINT=${SHMINT:-10001}
[[ $SHMINT =~ ^[0-9]+$ ]] || { echo "Enter a valid port"; continue; }
if ((SHMINT >= 1025 && SHMINT <= 65536)); then
SHMINT=${SHMINT:-10001}
break
else
echo "Port out of range, try again"
fi
done
read -p "What base directory should the node use (defaults to ~/.shardeum): " NODEHOME
NODEHOME=${NODEHOME:-~/.shardeum}
APPSEEDLIST="archiver-sphinx.shardeum.org"
APPMONITOR="monitor-sphinx.shardeum.org"
cat <<EOF
2. Pull Compose Project
EOF
if [ -d "$NODEHOME" ]; then
echo "Removing existing directory $NODEHOME..."
rm -rf "$NODEHOME" >/dev/null 2>&1
fi
echo "Step 1/2: Cloning the repository..."
git clone https://gitlab.com/shardeum/validator/dashboard.git ${NODEHOME} >/dev/null 2>&1 &&
echo "Step 1/2: Entering and setting permissions..."
cd ${NODEHOME} >/dev/null 2>&1 &&
chmod a+x ./*.sh >/dev/null 2>&1
echo "100%. The repository has been successfully cloned and the scripts have been given execute permissions."
cat <<EOF
3. Create and Set .env File
EOF
echo "Writing configuration data to .env file..."
SERVERIP=$(get_external_ip)
echo "IP address: $SERVERIP"
echo "Step 1/3:Entering the directory $NODEHOME..."
cd ${NODEHOME} &&
echo "Step 2/3:Creating .env file..."
touch ./.env
echo "Step 3/3:Writing data to .env file..."
cat >./.env <<EOL
APP_IP=auto
APP_SEEDLIST=${APPSEEDLIST}
APP_MONITOR=${APPMONITOR}
DASHPASS=${DASHPASS}
DASHPORT=${DASHPORT}
SERVERIP=${SERVERIP}
SHMEXT=${SHMEXT}
SHMINT=${SHMINT}
EOL
echo "The .env file has been successfully created and the data has been written to it."
cat <<EOF
4. Building base image
EOF
echo "Building Docker image..."
cd ${NODEHOME}
echo "Step 1/3: Entering the working directory."
echo "Step 2/3: Building the Docker image."
docker-safe build --no-cache -t test-dashboard -f Dockerfile --build-arg RUNDASHBOARD=${RUNDASHBOARD} . >/dev/null 2>&1
echo "Step 3/3: Docker image build complete."
cat <<EOF
5. Start Compose Project
EOF
cd ${NODEHOME}
echo "Step 1/3: Updating ports in the docker-compose file..."
if [[ "$(uname)" == "Darwin" ]]; then
sed -i '' "s/- '8080:8080'/- '$DASHPORT:$DASHPORT'/" docker-compose.yml >/dev/null 2>&1
sed -i '' "s/- '9001-9010:9001-9010'/- '$SHMEXT:$SHMEXT'/" docker-compose.yml >/dev/null 2>&1
sed -i '' "s/- '10001-10010:10001-10010'/- '$SHMINT:$SHMINT'/" docker-compose.yml >/dev/null 2>&1
else
sed -i "s/- '8080:8080'/- '$DASHPORT:$DASHPORT'/" docker-compose.yml >/dev/null 2>&1
sed -i "s/- '9001-9010:9001-9010'/- '$SHMEXT:$SHMEXT'/" docker-compose.yml >/dev/null 2>&1
sed -i "s/- '10001-10010:10001-10010'/- '$SHMINT:$SHMINT'/" docker-compose.yml >/dev/null 2>&1
fi
echo "Step 2/3: Starting the Docker containers..."
docker-compose-safe() {
if command -v docker-compose &>/dev/null; then
cmd="docker-compose"
elif docker --help | grep -q "compose"; then
cmd="docker compose"
else
echo "docker-compose or docker compose is not installed on this machine"
exit 1
fi
if ! $cmd $@ >/dev/null 2>&1; then
echo "Trying again with sudo..."
sudo $cmd $@ >/dev/null 2>&1
fi
}
docker-compose-safe -f docker-compose.yml up -d >/dev/null 2>&1
echo "Step 3/3: Checking the logs to verify that the image has started..."
start_time=$(date +%s)
counter=0
while true; do
if docker-compose logs | grep -q "done" &>/dev/null; then
echo "Signature is okay, Shardeum node installed successfully. Continuing..."
cd $HOME
break
fi
current_time=$(date +%s)
elapsed_time=$((current_time-start_time))
if [ $elapsed_time -gt 600 ]; then
echo "Signature not okay. Exiting..."
exit 1
fi
counter=$((counter+1))
sleep 1
done
cd $HOME
#Do not indent
if [ $RUNDASHBOARD = "y" ]
then
DASHBORDADDRESS=$(wget -qO- eth0.me):8080
cat <<EOF
To use the Web Dashboard:
1. Note the IP address that you used to connect to the node. This could be an external IP, LAN IP or localhost.
2. Open a web browser and navigate to the web dashboard at:
EOF
echo https://$DASHBORDADDRESS
cat <<EOF
3. Go to the Settings tab and connect a wallet.
4. Go to the Maintenance tab and click the Start Node button.
If this validator is on the cloud and you need to reach the dashboard over the internet,
please set a strong password and use the external IP instead of localhost.
EOF
fi
cat <<EOF
To use the Command Line Interface:
1. Navigate to the Shardeum home directory ($NODEHOME).
2. Enter the validator container with ./shell.sh.
3. Run "operator-cli --help" for commands
EOF
break
;;
"Update Node")
command -v docker >/dev/null 2>&1 || { echo >&2 "'docker' is required but not installed. See https://gitlab.com/shardeum/validator/dashboard/-/tree/dashboard-gui-nextjs#how-to for details."; exit 1; }
if command -v docker-compose &>/dev/null; then
echo "docker-compose is installed on this machine"
elif docker --help | grep -q "compose"; then
echo "docker compose subcommand is installed on this machine"
else
echo "docker-compose or docker compose is not installed on this machine"
exit 1
fi
docker-safe() {
if ! command -v docker &>/dev/null; then
echo "docker is not installed on this machine"
exit 1
fi
if ! docker $@; then
echo "Trying again with sudo..."
sudo docker $@
fi
}
echo "Updating validator image and rebuilding docker..."
./docker-down.sh
echo "Clearing validator images..."
./cleanup.sh
echo "Updating local repo..."
git pull origin main
echo "Rebuilding local validator image..."
docker-safe build --no-cache -t local-dashboard -f Dockerfile --build-arg RUNDASHBOARD=y .
./docker-up.sh
echo "Starting image. This could take a while..."
(docker-safe logs -f shardeum-dashboard &) | grep -q 'done'
break
;;
"Dashboard addres")
echo "Copy this address and paste it in the browser window. The dashboard will open where you need to start the node and perform a stake"
DASHBORDADDRESS=$(wget -qO- eth0.me):8080
echo https://$DASHBORDADDRESS
break
;;
"Stop and delite node")
docker-compose-safe() {
if command -v docker-compose &>/dev/null; then
cmd="docker-compose"
elif docker --help | grep -q "compose"; then
cmd="docker compose"
else
echo "docker-compose or docker compose is not installed on this machine"
exit 1
fi
if ! $cmd $@ &>/dev/null; then
echo "Trying again with sudo..."
sudo $cmd $@ &>/dev/null
fi
}
cd $HOME/.shardeum
echo "Step 1: Checking the status of the Docker Compose project..."
if docker-compose logs | grep -q "done" &>/dev/null; then
echo "Step 2: Docker Compose project is up, tearing it down..."
docker-compose-safe -f docker-compose.yml down &>/dev/null
echo "Step 3: Removing the .shardeum directory in the home folder..."
echo "Node deleted successfully."
cd &>/dev/null
rm -rf $HOME/.shardeum &>/dev/null
else
echo "Step 2: Docker Compose project is not up"
fi
break
;;
"Exit")
exit
;;
*) echo "invalid option $REPLY";;
esac
done
done