-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-stop.sh
More file actions
executable file
·201 lines (163 loc) · 4.72 KB
/
node-stop.sh
File metadata and controls
executable file
·201 lines (163 loc) · 4.72 KB
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
#!/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))
isRPC=false
isValidator=false
#########################################################################
source ./.env
#########################################################################
#+-----------------------------------------------------------------------------------------------+
#| |
#| |
#| FUNCTIONS |
#| |
#| |
#+-----------------------------------------------------------------------------------------------+
countNodes(){
local i=1
totalNodes=$(ls -l ./chaindata/ | grep -c ^d)
while [[ $i -le $totalNodes ]]; do
if [ -f "./chaindata/node$i/.rpc" ]; then
((totalRpc += 1))
else
if [ -f "./chaindata/node$i/.validator" ]; then
((totalValidator += 1))
fi
fi
((i += 1))
done
}
stopNode(){
local session=$1
local nodePath="/root/Core-Blockchain/chaindata/$session"
if tmux has-session -t "$session" 2>/dev/null; then
tmux send-keys -t "$session" "exit" Enter
sleep 2
# Check if the geth.ipc file exists after stopping the node
if [ -e "$nodePath/geth.ipc" ]; then
echo -e "${ORANGE}Node $session did not stop gracefully. Retrying...${NC}"
tmux send-keys -t "$session" "exit" Enter
sleep 2
fi
# Final check
if [ -e "$nodePath/geth.ipc" ]; then
echo -e "${RED}Node $session is still running. Killing tmux session...${NC}"
tmux kill-session -t "$session"
else
echo -e "${GREEN}Node $session stopped successfully.${NC}"
tmux kill-session -t "$session"
fi
else
echo -e "${RED}Session $session does not exist.${NC}"
fi
}
stopRpc(){
i=$((totalValidator + 1))
while [[ $i -le $totalNodes ]]; do
stopNode "node$i"
((i += 1))
done
}
stopValidator(){
i=1
while [[ $i -le $totalValidator ]]; do
stopNode "node$i"
((i += 1))
done
}
finalize(){
pm2 stop all
countNodes
if [ "$isRPC" = true ]; then
echo -e "\n${GREEN}+------------------- Stopping RPC -------------------+"
stopRpc
fi
if [ "$isValidator" = true ]; then
echo -e "\n${GREEN}+------------------- Stopping Validator -------------------+"
stopValidator
fi
echo -e "\n${GREEN}+------------------ Active Nodes -------------------+"
tmux ls || echo -e "${RED}No active tmux sessions found.${NC}"
echo -e "\n${GREEN}+------------------ Active Nodes -------------------+${NC}"
}
# 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 Stop all the RPC nodes installed"
echo -e "\t\t --validator Stop all the Validator nodes installed"
}
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)
isRPC=true
;;
# take validator count
--validator)
isValidator=true
;;
*)
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
finalize