forked from GoPlugin/plugin-deployment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_pli_node_deploy.sh
438 lines (337 loc) · 12.5 KB
/
_pli_node_deploy.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
#!/bin/bash
# Authenticate sudo perms before script execution to avoid timeouts or errors
sudo -l > /dev/null 2>&1
# Set Colour Vars
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
FUNC_VARS(){
## VARIABLE / PARAMETER DEFINITIONS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Get current user id and store as var
USER_ID=$(getent passwd $EUID | cut -d: -f1)
PLI_VARS_FILE="plinode_$(hostname -f)".vars
if [ ! -e ~/$PLI_VARS_FILE ]; then
clear
echo
echo
echo -e "${RED} #### ERROR: No VARIABLES file found. ####${NC}"
echo
echo -e "${RED} ..creating local vars file '$HOME/$PLI_VARS_FILE' ${NC}"
cp sample.vars ~/$PLI_VARS_FILE
echo
echo -e "${GREEN} please update the vars file with your specific values.. ${NC}"
echo -e "${GREEN} copy command to edit: ${NC}"
echo
echo -e "${GREEN}nano ~/$PLI_VARS_FILE ${NC}"
echo
echo
#sleep 2s
exit 1
fi
source ~/$PLI_VARS_FILE
}
FUNC_VALUE_CHECK(){
echo -e "${GREEN}#########################################################################"
echo -e "${GREEN}#########################################################################"
echo -e "${GREEN}"
echo -e "${GREEN} Script Deployment menthod"
echo -e "${GREEN}"
echo -e "${GREEN}#########################################################################"
echo -e "${GREEN}#########################################################################${NC}"
echo -e "${GREEN}#########################################################################"
echo
echo -e "${GREEN}## CONFIRM SCRIPTS VARIABLES FILE HAS BEEN UPDATED...${NC}"
echo
# Ask the user acc for login details (comment out to disable)
while true; do
read -t7 -r -p "please confirm that you have updated the vars file with your values ? (Y/n) " _input
if [ $? -gt 128 ]; then
clear
echo
echo "timed out waiting for user response - proceeding as normal..."
FUNC_NODE_DEPLOY;
fi
case $_input in
[Yy][Ee][Ss]|[Yy]* )
FUNC_NODE_DEPLOY
break
;;
[Nn][Oo]|[Nn]* )
FUNC_EXIT
;;
* ) echo "Please answer (y)es or (n)o.";;
esac
done
}
FUNC_NODE_DEPLOY(){
FUNC_VARS;
echo -e "${GREEN}#########################################################################"
echo -e "${GREEN}#########################################################################"
echo -e "${GREEN}"
echo -e "${GREEN} Script Deployment menthod"
echo -e "${GREEN}"
echo -e "${GREEN}#########################################################################"
echo -e "${GREEN}#########################################################################${NC}"
echo
echo
echo -e "${GREEN}#########################################################################"
echo
echo -e "${GREEN}## Install: Credentials files prep...${NC}"
echo
touch {$FILE_KEYSTORE,$FILE_API}
chmod 666 {$FILE_KEYSTORE,$FILE_API}
echo $API_EMAIL > $FILE_API
echo $API_PASS >> $FILE_API
echo $PASS_KEYSTORE > $FILE_KEYSTORE
chmod 600 {$FILE_KEYSTORE,$FILE_API}
# Remove the file if necessary; sudo rm -f {.env.apicred,.env.password}
echo
echo
echo -e "${GREEN}#########################################################################"
echo
echo -e "${GREEN}## Install: UPDATE bash file $BASH_FILE1 with user values...${NC}"
echo
sed -i.bak "s/$DB_PWD_FIND/'$DB_PWD_NEW'/g" $BASH_FILE1
#cat $BASH_FILE1 | grep 'postgres PASSWORD'
#sleep 1s
echo
echo
echo -e "${GREEN}#########################################################################"
echo
echo -e "${GREEN}## Install: PRE-CHECKS for bash file $BASH_FILE1...${NC}"
echo
sudo apt remove --autoremove golang -y
sudo rm -rf /usr/local/go
echo
echo
echo -e "${GREEN}#########################################################################"
echo
echo -e "${GREEN}## Install: EXECUTE bash file $BASH_FILE1...${NC}"
echo
bash $BASH_FILE1
echo
echo
echo -e "${GREEN}#########################################################################"
echo
echo -e "${GREEN}## Install: Update bash file $BASH_FILE2 with user CREDENTIALS values...${NC}"
echo
sed -i.bak "s/password.txt/$FILE_KEYSTORE/g" $BASH_FILE2
sed -i.bak "s/apicredentials.txt/$FILE_API/g" $BASH_FILE2
sed -i.bak "s/:postgres/:$DB_PWD_NEW/g" $BASH_FILE2
sed -i.bak '/SECURE_COOKIES=false/d' $BASH_FILE2
cat $BASH_FILE2 | grep node
sleep 1s
echo
echo
echo -e "${GREEN}## Install: Update bash file $BASH_FILE2 with user TLS values...${NC}"
echo
sed -i.bak "s/PLUGIN_TLS_PORT=0/PLUGIN_TLS_PORT=$PLI_HTTPS_PORT/g" $BASH_FILE2
sed -i.bak "/^export PLUGIN_TLS_PORT=.*/a export TLS_CERT_PATH=$TLS_CERT_PATH/server.crt\nexport TLS_KEY_PATH=$TLS_CERT_PATH/server.key" $BASH_FILE2
cat $BASH_FILE2 | grep TLS
sleep 1s
echo
echo
echo -e "${GREEN}## Install: Create loca Certificate Authority / TLS Certificate & files / folders...${NC}"
echo
mkdir $TLS_CERT_PATH && cd $TLS_CERT_PATH
openssl req -x509 -out server.crt -keyout server.key -newkey rsa:4096 \
-sha256 -days 3650 -nodes -extensions EXT -config \
<(echo "[dn]"; echo CN=localhost; echo "[req]"; echo distinguished_name=dn; echo "[EXT]"; echo subjectAltName=DNS:localhost; echo keyUsage=digitalSignature; echo \
extendedKeyUsage=serverAuth) -subj "/CN=localhost"
sleep 1s
echo
echo
echo -e "${GREEN}## Install: Update bash file $BASH_FILE2 with INITIATORS values...${NC}"
echo
sed -i.bak "/^ export DATABASE_TIMEOUT=.*/a export FEATURE_EXTERNAL_INITIATORS=true" $BASH_FILE2
cat $BASH_FILE2 | grep INITIATORS
sleep 1s
echo -e "${GREEN}## Install: Check Golang version & bash profile path...${NC}"
echo
source ~/.profile
GO_VER=$(go version)
go version; GO_EC=$?
case $GO_EC in
0) echo -e "${GREEN}## Command exited with NO error...${NC}"
echo $GO_VER
echo
echo -e "${GREEN}## Install proceeding as normal...${NC}"
;;
1) echo -e "${RED}## Command exited with ERROR - updating bash profile...${NC}"
echo
source ~/.profile;
sudo sh -c 'echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile'
echo "cat "export PATH=$PATH:/usr/local/go/bin" >> ~/.profile"
echo
echo -e "${RED}## Check GO Version manually...${NC}"
sleep 2s
#FUNC_EXIT_ERROR
#exit 1
;;
*) echo -e "${RED}## Command exited with OTHER ERROR...${NC}"
echo -e "${RED}## 'go version' returned : $GO_EC ${NC}"
echo
FUNC_EXIT_ERROR
#exit 1
;;
esac
sleep 1s
echo -e "${GREEN}## Install: Start PM2 $BASH_FILE2 & set auto start on reboot...${NC}"
echo
cd /$PLI_DEPLOY_PATH
pm2 start $BASH_FILE2
sleep 1s
pm2 list
sleep 2s
pm2 list
pm2 startup systemd
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u $USER_ID --hp /home/$USER_ID
pm2 save
# INTERACTIVE: Calls function to check if user wants to proceed to next stage of setup.
#FUNC_DO_INIT_CHECK;
# NON-INTERACTIVE: Proceed with next stage of setup.
FUNC_INITIATOR;
}
FUNC_INITIATOR(){
FUNC_VARS;
echo
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
echo -e "${GREEN}## CLONE & INSTALL LOCAL INITIATOR...${NC}"
echo
# Added to resolve error running 'plugin help'
source ~/.profile
cd /$PLI_DEPLOY_PATH
git clone https://github.com/GoPlugin/external-Initiator
cd $PLI_INITOR_DIR
git checkout main
go install
echo
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
echo -e "${GREEN}## CREATE LOCAL INITIATOR...${NC}"
echo
export FEATURE_EXTERNAL_INITIATORS=true
plugin admin login -f "../$FILE_API"
sleep 0.5s
plugin initiators create $PLI_L_INIT_NAME http://localhost:8080/jobs > $PLI_INIT_RAWFILE
# plugin initiators create xdc http://localhost:8080/jobs
# plugin initiators destroy xdc http://localhost:8080/jobs
# plugin initiators create xdc http://localhost:8080/jobs > $PLI_INIT_RAWFILE
echo
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
echo -e "${GREEN}## CAPTURE INITIATOR CREDENTIALS & FILE MANIPULATION...${NC}"
echo
sed -i 's/ ║ /,/g;s/╬//g;s/═//g;s/║//g' $PLI_INIT_RAWFILE
sed -n '/'"$PLI_L_INIT_NAME"'/,//p' $PLI_INIT_RAWFILE > $PLI_INIT_DATFILE
sed -i 's/,/\n/g;s/^.'"$PLI_L_INIT_NAME"'//g' $PLI_INIT_DATFILE
sed -i 's/^http.*//g' $PLI_INIT_DATFILE
sed -i.bak '/^$/d;/^\s*$/d;s/[ \t]\+$//' $PLI_INIT_DATFILE
cp $PLI_INIT_DATFILE ~/$PLI_INIT_DATFILE.bak && chmod 600 ~/$PLI_INIT_DATFILE.bak
cat $PLI_INIT_DATFILE
sleep 1s
echo
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
echo -e "${GREEN}## READ INITIATOR CREDENTIALS AS VARIABLES...${NC}"
echo
read -r -d '' EXT_ACCESSKEY EXT_SECRET EXT_OUTGOINGTOKEN EXT_OUTGOINGSECRET <$PLI_INIT_DATFILE
echo
#echo "$EXT_ACCESSKEY"
#echo "$EXT_SECRET"
#echo "$EXT_OUTGOINGTOKEN"
#echo "$EXT_OUTGOINGSECRET"
sleep 1s
echo
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
echo -e "${GREEN}## CREATE INITIATOR PM2 SERVICE FILE: $BASH_FILE3 & file perms ${NC}"
echo
cd /$PLI_DEPLOY_PATH
cat <<EOF > $BASH_FILE3
#!/bin/bash
export EI_DATABASEURL=postgresql://postgres:${DB_PWD_NEW}@127.0.0.1:5432/plugin_mainnet_db?sslmode=disable
export EI_CHAINLINKURL=http://localhost:6688
export EI_IC_ACCESSKEY=${EXT_ACCESSKEY}
export EI_IC_SECRET=${EXT_SECRET}
export EI_CI_ACCESSKEY=${EXT_OUTGOINGTOKEN}
export EI_CI_SECRET=${EXT_OUTGOINGSECRET}
echo *** Starting EXTERNAL INITIATOR ***
external-initiator "{\"name\":\"$PLI_E_INIT_NAME\",\"type\":\"xinfin\",\"url\":\"https://pluginrpc.blocksscan.io\"}" --chainlinkurl "http://localhost:6688/"
EOF
sleep 1s
cat $BASH_FILE3
chmod u+x $BASH_FILE3
echo
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
echo -e "${GREEN}## START INITIATOR PM2 SERVICE $BASH_FILE3 ${NC}"
echo
pm2 start $BASH_FILE3
sleep 1s
pm2 status
sleep 3s
pm2 startup systemd
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u $USER_ID --hp /home/$USER_ID
pm2 save
FUNC_EXIT;
}
FUNC_DO_INIT_CHECK(){
echo -e "${GREEN}#########################################################################"
echo
echo -e "${GREEN}## CONFIRM SCRIPTS EXPORT VALUES HAVE BEEN UPDATED...${NC}"
echo
while true; do
read -t10 -r -p "Do you wish to proceed to INITIATOR SETUP ? (Y/n) " _input
if [ $? -gt 128 ]; then
#clear
echo
echo "timed out waiting for user response - proceeding as normal..."
FUNC_INITIATOR;
fi
case $_input in
[Yy][Ee][Ss]|[Yy]* )
FUNC_INITIATOR
break
;;
[Nn][Oo]|[Nn]* )
FUNC_EXIT
;;
* ) echo "Please answer (y)es or (n)o."
echo "NOTE: timeout value is (y)es";;
esac
done
}
FUNC_EXIT(){
exit 0
}
FUNC_EXIT_ERROR(){
exit 1
}
#FUNC_VALUE_CHECK;
clear
case "$1" in
fullnode)
FUNC_VALUE_CHECK
;;
initiator)
FUNC_INITIATOR
;;
*)
echo
echo
echo "Usage: $0 {fullnode|initiator}"
echo
echo "please provide one of the above values to run the scripts"
echo " example: " $0 fullnode""
echo
echo
esac