-
Notifications
You must be signed in to change notification settings - Fork 1
/
cudos-init-node.sh
executable file
·294 lines (248 loc) · 9.43 KB
/
cudos-init-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
#!/bin/bash
#
# Copyright 2022 Andrew Meredith <[email protected]>
# Copyright 2022 Cudo Ventures - All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##########################################################################
########################################
# Initial safety and environment checks
########################################
#
# Check that this is being run as user cudos
#
if [[ "$( whoami )" != "cudos" ]]
then
echo -ne "Error: $0 must be run as user cudos.\n\n"
exit 1
fi
#
# Set the CUDOS_HOME variable using the profile
#
source /etc/profile.d/cudos-noded.sh
#
# Check that the $CUDOS_HOME variable is set and pointing to a writeable directory
#
if [[ "$CUDOS_HOME" == "" ]]
then
echo -ne "Error: Cudos home directory variable 'CUDOS_HOME' unset.\n\n"
exit 1
fi
if [[ ! -d "$CUDOS_HOME" ]]
then
echo -ne "Error: Directory '$CUDOS_HOME' does not exist\n\n"
exit 1
fi
TCHFILE="${CUDOS_HOME}/touchtest.%%"
if ! touch "$TCHFILE"
then
rm -f "$TCHFILE"
echo -ne "Error: Cannot write to directory '$CUDOS_HOME'\n\n"
ls -ld "${CUDOS_HOME}"
exit 1
fi
rm -f "$TCHFILE"
########################################
# Set up node type and run mode
########################################
#
# Parse command line aruments
#
print_help()
{
cat<<EOF
Usage: cudos-init-node.sh [--help|--init|--reinit|--reconfig|--scan-peers] [node-type]
NB For backward compatibility, if there are no arguments on the command line, the
default behaviour of this script is "--init full-node"
If [node-type] is not given, node type "full-node" is assumed
-h | --help - Prints this help text
-i | --init - Initialises and configures the node
Requires the machine to be unconfigured and will exit with an error if the machine
has any existing configuration. Runs "cudos init" followed by the configurator using
the "node-type" given or "full-node" if no arguments present.
-r | --reinit - Reinitialises and configures an already configured and initialised node
This optioin is comparable to --init only for nodes that have already been configured.
Removes the existing config.toml & app.toml and then runs "cudos init" followed by
the configurator using the "node-type" given or "full-node" if no arguments present.
Intended to "start again" on an existing node
-c | --configure - Rebuilds the .toml files
Reruns the toml file configuration step for that node type.
NB Unlike --init this option is intended to be run on already configured nodes
NB Unlike --init & --reinit this option has no effect on the database, it just updates the toml files
-s | --scan-peers - Checks the connectivity with the declared peers and seeds
Pick out the configured seeds and sentries from the toml files and uses cudos-p2p-scan to check
their connectivity.
NB Does not change the configuration. This is a read only operation.
EOF
}
TEMP=$(getopt -o hircs --long help,init,reinit,configure,scan-peers -n 'cudos-noded-init.sh' -- "$@")
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around '$TEMP': they are essential!
eval set -- "$TEMP"
OPT_INIT=false
OPT_REINIT=false
OPT_CONFIGURE=false
OPT_SCAN_PEERS=false
while true; do
case "$1" in
-h | --help ) RUN_MODE="help"; shift ;;
-i | --init ) RUN_MODE="init"; shift ;;
-r | --reinit ) RUN_MODE="reinit"; shift ;;
-c | --configure ) RUN_MODE="configure"; shift ;;
-s | --scan-peers ) RUN_MODE="scan-peers"; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
# Set type name from the first remaining command line argument
TYPE_NAME="$1"
# Set to "full-node" if no node type given
if [[ "$TYPE_NAME" = "" ]]
then
TYPE_NAME="full-node"
fi
export TYPE_NAME
# Set run mode to "init" if no mode given
if [[ "$RUN_MODE" = "" ]]
then
RUN_MODE="init"
fi
########################################
# Execute the required procedures
########################################
#
# Print the help text and exit if -h or --help given
#
if [[ "$RUN_MODE" = "help" ]]
then
print_help
exit 0
fi
#
# Check to see if there is already a configuration
# which is fatal in run mdoe "init"
#
for FNM in "${CUDOS_HOME}/config/config.toml" "${CUDOS_HOME}/config/app.toml"
do
if [[ -f "${FNM}" && "$RUN_MODE" = "init" ]]
then
echo -ne "Error: Cannot initialise, $FNM already present\nConsider using --reinit\n\n"
exit 1
fi
done
#
# if run mode init or reinit requested, initialise the node using "cudos-noded init"
#
if [[ "$RUN_MODE" = "init" || "$RUN_MODE" = "reinit" ]]
then
echo "Info: Stashing the genesis file"
TMPFN="${CUDOS_HOME}/config/genesis.json-$$"
mv -f "$CUDOS_HOME"/config/genesis.json "$TMPFN"
echo "Info: Clearing node database with 'cudos-noded unsafe-reset-all'"
cudos-noded unsafe-reset-all
echo "Info: Initialising node with 'cudos-noded init $HOSTNAME'"
cudos-noded init "$HOSTNAME" 2>/tmp/genesis.$$.json
if [[ $? -ne 0 ]]
then
echo -ne "Error: cudos-noded init failed\n\n"
cat /tmp/genesis.$$.json
mv -f "$TMPFN" "$CUDOS_HOME"/config/genesis.json
exit 1
fi
echo "Info: Restoring the genesis file"
mv -f "$TMPFN" "$CUDOS_HOME"/config/genesis.json
fi
#
# Configuration step
#
if [[ "$RUN_MODE" = "init" || "$RUN_MODE" = "reinit" || "$RUN_MODE" = "configure" ]]
then
#
# Select behaviour based on the node type name given
#
echo "Info: Configuring node as a $TYPE_NAME"
case $TYPE_NAME in
full-node)
cudos-noded-ctl set seeds "$CUDOS_HOME"/config/seeds.config
cudos-noded-ctl set persistent_peers "$CUDOS_HOME"/config/persistent-peers.config
cudos-noded-ctl set private_peers "$CUDOS_HOME"/config/private-peers.config
cudos-noded-ctl set unconditional_peers "$CUDOS_HOME"/config/unconditional-peers.config
cudos-noded-ctl set pex true
cudos-noded-ctl set unsafe true
cudos-noded-ctl set prometheus true
cudos-noded-ctl set seed_mode false
cudos-noded-ctl set minimum-gas-prices "5000000000000acudos"
;;
clustered-node)
cudos-noded-ctl addrbook_clear
cudos-noded-ctl set seeds ""
cudos-noded-ctl set persistent_peers "$CUDOS_HOME"/config/persistent-peers.config
cudos-noded-ctl set private_peers "$CUDOS_HOME"/config/private-peers.config
cudos-noded-ctl set unconditional_peers "$CUDOS_HOME"/config/unconditional-peers.config
cudos-noded-ctl set pex false
cudos-noded-ctl set unsafe false
cudos-noded-ctl set prometheus true
cudos-noded-ctl set seed_mode false
cudos-noded-ctl set minimum-gas-prices "5000000000000acudos"
;;
seed-node)
cudos-noded-ctl set seeds "$CUDOS_HOME"/config/seeds.config
cudos-noded-ctl set persistent_peers "$CUDOS_HOME"/config/persistent-peers.config
cudos-noded-ctl set private_peers "$CUDOS_HOME"/config/private-peers.config
cudos-noded-ctl set unconditional_peers "$CUDOS_HOME"/config/unconditional-peers.config
cudos-noded-ctl set pex true
cudos-noded-ctl set unsafe true
cudos-noded-ctl set prometheus true
cudos-noded-ctl set seed_mode true
cudos-noded-ctl set minimum-gas-prices "5000000000000acudos"
;;
sentry-node)
cudos-noded-ctl set seeds "$CUDOS_HOME"/config/seeds.config
cudos-noded-ctl set persistent_peers "$CUDOS_HOME"/config/persistent-peers.config
cudos-noded-ctl set private_peers "$CUDOS_HOME"/config/private-peers.config
cudos-noded-ctl set unconditional_peers "$CUDOS_HOME"/config/unconditional-peers.config
cudos-noded-ctl set pex true
cudos-noded-ctl set unsafe true
cudos-noded-ctl set prometheus true
cudos-noded-ctl set seed_mode false
cudos-noded-ctl set minimum-gas-prices "5000000000000acudos"
;;
*)
echo -ne "Error: Unsupported Node Type: $TYPE_NAME\n\n"
exit 1
esac
fi
#
# Run the peer scan
#
if [[ "$RUN_MODE" = "scan-peers" ]]
then
SEED_LIST=` grep "^seeds =" ${CUDOS_HOME}/config/config.toml | sed -e's/^seeds = "//' | sed -e's/"$//' | tr "," " "`
SENTRY_LIST=` grep "^persistent_peers =" ${CUDOS_HOME}/config/config.toml | sed -e's/^persistent_peers = "//' | sed -e's/"$//' | tr "," " "`
echo -e "\nSeeds:"
for HOST_ADDR in $SEED_LIST
do
echo "- Full Address: $HOST_ADDR"
IPPRT="$( echo $HOST_ADDR | sed -e's/^.*@//' )"
cudos-p2p-scan $IPPRT
done
echo -e "\nPersistent Peers:"
for HOST_ADDR in $SENTRY_LIST
do
echo "- Full Address: $HOST_ADDR"
IPPRT="$( echo $HOST_ADDR | sed -e's/^.*@//' )"
cudos-p2p-scan $IPPRT
done
fi