Deployment scripts for the mining node
1
#!/bin/bash
2
set -e
3
set -u
4
set -x
5
6
EXT_IP="$(curl ifconfig.co)"
7
8
# Install logentries daemon /*
9
start_logentries() {
10
echo "=====> start_logentries"
11
sudo bash -c "echo 'deb http://rep.logentries.com/ trusty main' > /etc/apt/sources.list.d/logentries.list"
12
sudo bash -c "gpg --keyserver pgp.mit.edu --recv-keys C43C79AD && gpg -a --export C43C79AD | apt-key add -"
13
sudo apt-get update
14
sudo apt-get install -y logentries
15
sudo le reinit --user-key=0665901a-e843-41c5-82c1-2cc4b39f0b21 --pull-server-side-config=False
16
17
mkdir -p /home/${ADMIN_USERNAME}/logs
18
touch /home/${ADMIN_USERNAME}/logs/netstats_daemon.err
19
touch /home/${ADMIN_USERNAME}/logs/netstats_daemon.out
20
touch /home/${ADMIN_USERNAME}/logs/parity.err
21
touch /home/${ADMIN_USERNAME}/logs/parity.out
22
touch /home/${ADMIN_USERNAME}/logs/parity.log
23
touch /home/${ADMIN_USERNAME}/logs/transferRewardToPayoutKey.out
24
touch /home/${ADMIN_USERNAME}/logs/transferRewardToPayoutKey.err
25
26
sudo bash -c "cat >> /etc/le/config << EOF
27
[install_err]
28
path = /var/lib/waagent/custom-script/download/0/stderr
29
destination = AlphaTestTestNet/${EXT_IP}
30
[install_out]
31
path = /var/lib/waagent/custom-script/download/0/stdout
32
destination = AlphaTestTestNet/${EXT_IP}
33
[netstats_daemon_err]
34
path = /home/${ADMIN_USERNAME}/logs/netstats_daemon.err
35
destination = AlphaTestTestNet/${EXT_IP}
36
[netstats_daemon_out]
37
path = /home/${ADMIN_USERNAME}/logs/netstats_daemon.out
38
destination = AlphaTestTestNet/${EXT_IP}
39
[parity_err]
40
path = /home/${ADMIN_USERNAME}/logs/parity.err
41
destination = AlphaTestTestNet/${EXT_IP}
42
[parity_out]
43
path = /home/${ADMIN_USERNAME}/logs/parity.out
44
destination = AlphaTestTestNet/${EXT_IP}
45
[parity_log]
46
path = /home/${ADMIN_USERNAME}/logs/parity.log
47
destination = AlphaTestTestNet/${EXT_IP}
48
[transferReward_out]
49
path = /home/${ADMIN_USERNAME}/logs/transferRewardToPayoutKey.out
50
destination = AlphaTestTestNet/${EXT_IP}
51
[transferReward_err]
52
path = /home/${ADMIN_USERNAME}/logs/transferRewardToPayoutKey.err
53
destination = AlphaTestTestNet/${EXT_IP}
54
EOF"
55
sudo apt-get install -y logentries-daemon
56
sudo service logentries start
57
echo "<===== start_logentries"
58
}
59
60
start_logentries
61
62
# */
63
64
echo "========== AlphaTestTestNet/mining-node/install.sh starting =========="
65
echo "===== current time: $(date)"
66
echo "===== username: $(whoami)"
67
echo "===== working directory: $(pwd)"
68
echo "===== operating system info:"
69
lsb_release -a
70
echo "===== memory usage info:"
71
free -m
72
echo "===== external ip: ${EXT_IP}"
73
echo "===== environmental variables:"
74
printenv
75
76
# script parameters
77
#INSTALL_DOCKER_VERSION="17.03.1~ce-0~ubuntu-xenial"
78
#INSTALL_DOCKER_IMAGE="parity/parity:v1.6.8"
79
INSTALL_CONFIG_REPO="https://raw.githubusercontent.com/poanetwork/test-templates/AlphaTestTestNet/TestTestNet/mining-node"
80
GENESIS_REPO_LOC="https://raw.githubusercontent.com/poanetwork/oracles-scripts/alphadevtestnet/spec.json"
81
GENESIS_JSON="spec.json"
82
NODE_TOML="node.toml"
83
NODE_PWD="node.pwd"
84
85
export HOME="${HOME:-/home/${ADMIN_USERNAME}}"
86
87
#echo "===== will use docker version: ${INSTALL_DOCKER_VERSION}"
88
#echo "===== will use parity docker image: ${INSTALL_DOCKER_IMAGE}"
89
echo "===== repo base path: ${INSTALL_CONFIG_REPO}"
90
91
# this should be provided through env by azure template
92
NETSTATS_SERVER="${NETSTATS_SERVER}"
93
NETSTATS_SECRET="${NETSTATS_SECRET}"
94
MINING_KEYFILE="${MINING_KEYFILE}"
95
MINING_ADDRESS="${MINING_ADDRESS}"
96
MINING_KEYPASS="${MINING_KEYPASS}"
97
NODE_FULLNAME="${NODE_FULLNAME:-Anonymous}"
98
NODE_ADMIN_EMAIL="${NODE_ADMIN_EMAIL:-[email protected]}"
99
ADMIN_USERNAME="${ADMIN_USERNAME}"
100
101
prepare_homedir() {
102
echo "=====> prepare_homedir"
103
#ln -s "$(pwd)" "/home/${ADMIN_USERNAME}/script-dir"
104
cd "/home/${ADMIN_USERNAME}"
105
mkdir -p logs
106
mkdir -p logs/old
107
echo "<===== prepare_homedir"
108
}
109
110
add_user_to_docker_group() {
111
# based on https://askubuntu.com/questions/477551/how-can-i-use-docker-without-sudo
112
echo "=====> add_user_to_docker_group"
113
sudo groupadd docker
114
sudo gpasswd -a "${ADMIN_USERNAME}" docker
115
newgrp docker
116
echo "===== Groups: "
117
groups
118
echo "<===== add_user_to_docker_group"
119
}
120
121
install_ntpd() {
122
echo "=====> install_ntpd"
123
sudo timedatectl set-ntp no
124
sudo apt-get -y install ntp
125
126
sudo bash -c "cat > /etc/cron.hourly/ntpdate << EOF
127
#!/bin/sh
128
sudo service ntp stop
129
sudo ntpdate -s ntp.ubuntu.com
130
sudo service ntp start
131
EOF"
132
sudo chmod 755 /etc/cron.hourly/ntpdate
133
echo "<===== install_ntpd"
134
}
135
136
install_haveged() {
137
echo "=====> install_haveged"
138
sudo apt-get -y install haveged
139
sudo update-rc.d haveged defaults
140
echo "<===== install_haveged"
141
}
142
143
allocate_swap() {
144
echo "=====> allocate_swap"
145
sudo apt-get -y install bc
146
#sudo fallocate -l $(echo "$(free -b | awk '/Mem/{ print $2 }')*2" | bc -l) /swapfile
147
sudo fallocate -l 1G /swapfile
148
sudo chmod 600 /swapfile
149
sudo mkswap /swapfile
150
sudo swapon /swapfile
151
sudo sh -c "printf '/swapfile none swap sw 0 0\n' >> /etc/fstab"
152
sudo sh -c "printf 'vm.swappiness=10\n' >> /etc/sysctl.conf"
153
sudo sysctl vm.vfs_cache_pressure=50
154
sudo sh -c "printf 'vm.vfs_cache_pressure = 50\n' >> /etc/sysctl.conf"
155
echo "<===== allocate_swap"
156
}
157
158
install_nodejs() {
159
echo "=====> install_nodejs"
160
# curl -sL https://deb.nodesource.com/setup_0.12 | bash -
161
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
162
sudo apt-get update
163
sudo apt-get install -y build-essential git unzip wget nodejs ntp cloud-utils
164
165
# add symlink if it doesn't exist
166
[[ ! -f /usr/bin/node ]] && sudo ln -s /usr/bin/nodejs /usr/bin/node
167
echo "<===== install_nodejs"
168
}
169
170
install_docker_ce() {
171
echo "=====> install_docker_ce"
172
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
173
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
174
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
175
sudo apt-get update
176
sudo apt-get -y install docker-ce=${INSTALL_DOCKER_VERSION}
177
sudo docker pull ${INSTALL_DOCKER_IMAGE}
178
echo "<===== install_docker_ce"
179
}
180
181
pull_image_and_configs() {
182
echo "=====> pull_image_and_configs"
183
184
# curl -s -O "${INSTALL_CONFIG_REPO}/../${GENESIS_JSON}"
185
curl -s -o "${GENESIS_JSON}" "${GENESIS_REPO_LOC}"
186
curl -s -O "${INSTALL_CONFIG_REPO}/${NODE_TOML}"
187
sed -i "/\[network\]/a nat=\"extip:${EXT_IP}\"" ${NODE_TOML}
188
cat >> ${NODE_TOML} <<EOF
189
[misc]
190
logging="engine=trace,network=trace,discovery=trace"
191
log_file = "/home/${ADMIN_USERNAME}/logs/parity.log"
192
[account]
193
password = ["${NODE_PWD}"]
194
unlock = ["${MINING_ADDRESS}"]
195
[mining]
196
force_sealing = true
197
engine_signer = "${MINING_ADDRESS}"
198
reseal_on_txs = "none"
199
EOF
200
echo "${MINING_KEYPASS}" > "${NODE_PWD}"
201
mkdir -p parity/keys/OraclesPoA
202
echo ${MINING_KEYFILE} | base64 -d > parity/keys/OraclesPoA/mining.key.${MINING_ADDRESS}
203
echo "<===== pull_image_and_configs"
204
}
205
206
# based on https://get.parity.io
207
install_netstats() {
208
echo "=====> install_netstats"
209
git clone https://github.com/poanetwork/eth-net-intelligence-api
210
cd eth-net-intelligence-api
211
#sed -i '/"web3"/c "web3": "0.19.x",' package.json
212
npm install
213
sudo npm install pm2 -g
214
215
cat > app.json << EOL
216
[
217
{
218
"name" : "netstats_daemon",
219
"script" : "app.js",
220
"log_date_format" : "YYYY-MM-DD HH:mm:SS Z",
221
"error_file" : "/home/${ADMIN_USERNAME}/logs/netstats_daemon.err",
222
"out_file" : "/home/${ADMIN_USERNAME}/logs/netstats_daemon.out",
223
"merge_logs" : false,
224
"watch" : false,
225
"max_restarts" : 100,
226
"exec_interpreter" : "node",
227
"exec_mode" : "fork_mode",
228
"env":
229
{
230
"NODE_ENV" : "production",
231
"RPC_HOST" : "localhost",
232
"RPC_PORT" : "8545",
233
"LISTENING_PORT" : "30300",
234
"INSTANCE_NAME" : "${NODE_FULLNAME}",
235
"CONTACT_DETAILS" : "${NODE_ADMIN_EMAIL}",
236
"WS_SERVER" : "http://${NETSTATS_SERVER}:3000",
237
"WS_SECRET" : "${NETSTATS_SECRET}",
238
"VERBOSITY" : 2
239
}
240
}
241
]
242
EOL
243
cd ..
244
cat > netstats.start <<EOF
245
cd eth-net-intelligence-api
246
pm2 startOrRestart app.json
247
cd ..
248
EOF
249
chmod +x netstats.start
250
sudo -u root -E -H ./netstats.start
251
echo "<===== install_netstats"
252
}
253
254
install_netstats_via_systemd() {
255
echo "=====> install_netstats_via_systemd"
256
git clone https://github.com/poanetwork/eth-net-intelligence-api
257
cd eth-net-intelligence-api
258
#sed -i '/"web3"/c "web3": "0.19.x",' package.json
259
npm install
260
sudo npm install pm2 -g
261
262
cat > app.json << EOL
263
[
264
{
265
"name" : "netstats_daemon",
266
"script" : "app.js",
267
"log_date_format" : "YYYY-MM-DD HH:mm:SS Z",
268
"error_file" : "/home/${ADMIN_USERNAME}/logs/netstats_daemon.err",
269
"out_file" : "/home/${ADMIN_USERNAME}/logs/netstats_daemon.out",
270
"merge_logs" : false,
271
"watch" : false,
272
"max_restarts" : 100,
273
"exec_interpreter" : "node",
274
"exec_mode" : "fork_mode",
275
"env":
276
{
277
"NODE_ENV" : "production",
278
"RPC_HOST" : "localhost",
279
"RPC_PORT" : "8545",
280
"LISTENING_PORT" : "30300",
281
"INSTANCE_NAME" : "${NODE_FULLNAME}",
282
"CONTACT_DETAILS" : "${NODE_ADMIN_EMAIL}",
283
"WS_SERVER" : "http://${NETSTATS_SERVER}:3000",
284
"WS_SECRET" : "${NETSTATS_SECRET}",
285
"VERBOSITY" : 2
286
}
287
}
288
]
289
EOL
290
cd ..
291
sudo bash -c "cat > /etc/systemd/system/oracles-netstats.service <<EOF
292
[Unit]
293
Description=oracles netstats service
294
After=network.target
295
[Service]
296
Type=oneshot
297
RemainAfterExit=true
298
User=${ADMIN_USERNAME}
299
Group=${ADMIN_USERNAME}
300
Environment=MYVAR=myval
301
WorkingDirectory=/home/${ADMIN_USERNAME}/eth-net-intelligence-api
302
ExecStart=/usr/bin/pm2 startOrRestart app.json
303
[Install]
304
WantedBy=multi-user.target
305
EOF"
306
sudo systemctl enable oracles-netstats
307
sudo systemctl start oracles-netstats
308
echo "<===== install_netstats_via_systemd"
309
}
310
311
start_docker() {
312
echo "=====> start_docker"
313
cat > docker.start <<EOF
314
sudo docker run -d \\
315
--name oracles-poa \\
316
-p 30300:30300 \\
317
-p 30300:30300/udp \\
318
-p 8080:8080 \\
319
-p 8180:8180 \\
320
-p 8545:8545 \\
321
-v "$(pwd)/${NODE_PWD}:/build/${NODE_PWD}" \\
322
-v "$(pwd)/parity:/build/parity" \\
323
-v "$(pwd)/${GENESIS_JSON}:/build/${GENESIS_JSON}" \\
324
-v "$(pwd)/${NODE_TOML}:/build/${NODE_TOML}" \\
325
${INSTALL_DOCKER_IMAGE} --config "${NODE_TOML}" > logs/docker.out 2> logs/docker.err
326
container_id="\$(cat logs/docker.out)"
327
sudo ln -sf "/var/lib/docker/containers/\${container_id}/\${container_id}-json.log" logs/parity.log
328
EOF
329
chmod +x docker.start
330
./docker.start
331
echo "<===== start_docker"
332
}
333
334
use_deb() {
335
echo "=====> use_deb"
336
curl -LO 'http://parity-downloads-mirror.parity.io/v1.7.0/x86_64-unknown-linux-gnu/parity_1.7.0_amd64.deb'
337
sudo dpkg -i parity_1.7.0_amd64.deb
338
sudo apt-get install dtach
339
340
cat > parity.start << EOF
341
dtach -n parity.dtach bash -c "parity -l engine=trace,discovery=trace,network=trace --config ${NODE_TOML} >> logs/parity.out 2>> logs/parity.err"
342
EOF
343
chmod +x parity.start
344
./parity.start
345
echo "<===== use_deb"
346
}
347
348
use_deb_via_systemd() {
349
echo "=====> use_deb_via_systemd"
350
curl -LO 'http://parity-downloads-mirror.parity.io/v1.7.0/x86_64-unknown-linux-gnu/parity_1.7.0_amd64.deb'
351
sudo dpkg -i parity_1.7.0_amd64.deb
352
353
sudo bash -c "cat > /etc/systemd/system/oracles-parity.service <<EOF
354
[Unit]
355
Description=oracles parity service
356
After=network.target
357
[Service]
358
User=${ADMIN_USERNAME}
359
Group=${ADMIN_USERNAME}
360
WorkingDirectory=/home/${ADMIN_USERNAME}
361
ExecStart=/usr/bin/parity --config=node.toml
362
Restart=always
363
[Install]
364
WantedBy=multi-user.target
365
EOF"
366
sudo systemctl enable oracles-parity
367
sudo systemctl start oracles-parity
368
echo "<===== use_deb_via_systemd"
369
}
370
371
use_bin() {
372
echo "=====> use_bin"
373
sudo apt-get install -y dtach unzip
374
curl -L -o parity-bin-v1.7.0.zip 'https://gitlab.parity.io/parity/parity/-/jobs/61863/artifacts/download'
375
unzip parity-bin-v1.7.0.zip -d parity-bin-v1.7.0
376
ln -s parity-bin-v1.7.0/target/release/parity parity-v1.7.0
377
378
cat > parity.start << EOF
379
dtach -n parity.dtach bash -c "./parity-v1.7.0 -l discovery=trace,network=trace --config ${NODE_TOML} >> logs/parity.out 2>> logs/parity.err"
380
EOF
381
chmod +x parity.start
382
./parity.start
383
echo "<===== use_bin"
384
}
385
386
compile_source() {
387
echo "=====> compile_source"
388
sudo apt-get -y install gcc g++ libssl-dev libudev-dev pkg-config
389
curl https://sh.rustup.rs -sSf | sh -s -- -y
390
source "/home/${ADMIN_USERNAME}/.cargo/env"
391
rustc --version
392
cargo --version
393
394
git clone -b "v1.7.0" https://github.com/paritytech/parity parity-src-v1.7.0
395
cd parity-src-v1.7.0
396
cargo build --release
397
cd ..
398
ln -s parity-src-v1.7.0/target/release/parity parity-v1.7.0
399
400
cat > parity.start << EOF
401
./parity-v1.7.0 -l discovery=trace,network=trace --config "${NODE_TOML}" >> logs/parity.out 2>> logs/parity.err
402
EOF
403
chmod +x parity.start
404
dtach -n parity.dtach "./parity.start"
405
echo "<===== compile_source"
406
}
407
408
install_scripts() {
409
echo "=====> install_scripts"
410
git clone -b alphadevtestnet --single-branch https://github.com/poanetwork/oracles-scripts
411
ln -s ../node.toml oracles-scripts/node.toml
412
cd oracles-scripts/scripts
413
npm install
414
sudo bash -c "cat > /etc/cron.daily/transferRewardToPayoutKey <<EOF
415
416
#!/bin/bash
417
cd "$(pwd)"
418
echo \"Starting at \\\$(date)\" >> \"/home/${ADMIN_USERNAME}/logs/transferRewardToPayoutKey.out\"
419
echo \"Starting at \\\$(date)\" >> \"/home/${ADMIN_USERNAME}/logs/transferRewardToPayoutKey.err\"
420
node transferRewardToPayoutKey.js >> \"/home/${ADMIN_USERNAME}/logs/transferRewardToPayoutKey.out\" 2>> \"/home/${ADMIN_USERNAME}/logs/transferRewardToPayoutKey.err\"
421
echo \"\" >> \"/home/${ADMIN_USERNAME}/logs/transferRewardToPayoutKey.out\"
422
echo \"\" >> \"/home/${ADMIN_USERNAME}/logs/transferRewardToPayoutKey.err\"
423
EOF"
424
sudo chmod 755 /etc/cron.daily/transferRewardToPayoutKey
425
cd ../..
426
echo "<===== install_scripts"
427
}
428
429
setup_autoupdate() {
430
echo "=====> setup_autoupdate"
431
sudo docker pull poanetwork/docker-run
432
sudo bash -c "cat > /etc/cron.daily/docker-autoupdate << EOF
433
#!/bin/sh
434
outlog='/home/${ADMIN_USERNAME}/logs/docker-autoupdate.out'
435
errlog='/home/${ADMIN_USERNAME}/logs/docker-autoupdate.err'
436
echo \"Starting: \\\$(date)\" >> \"\\\${outlog}\"
437
echo \"Starting: \\\$(date)\" >> \"\\\${errlog}\"
438
sudo docker run --rm -v /var/run/docker.sock:/tmp/docker.sock poanetwork/docker-run update >> \"\\\${outlog}\" 2>> \"\\\${errlog}\"
439
echo \"\" >> \"\\\${outlog}\"
440
echo \"\" >> \"\\\${errlog}\"
441
EOF"
442
sudo chmod 755 /etc/cron.daily/docker-autoupdate
443
echo "<===== setup_autoupdate"
444
}
445
446
configure_logrotate() {
447
echo "=====> configure_logrotate"
448
449
sudo bash -c "cat > /etc/logrotate.d/oracles.conf << EOF
450
/home/${ADMIN_USERNAME}/logs/*.log {
451
rotate 10
452
size 200M
453
missingok
454
compress
455
copytruncate
456
dateext
457
dateformat %Y-%m-%d-%s
458
olddir old
459
}
460
/home/${ADMIN_USERNAME}/.pm2/pm2.log {
461
su ${ADMIN_USERNAME} ${ADMIN_USERNAME}
462
rotate 10
463
size 200M
464
missingok
465
compress
466
copytruncate
467
dateext
468
dateformat %Y-%m-%d-%s
469
}"
470
echo "<===== configure_logrotate"
471
}
472
473
# MAIN
474
main () {
475
sudo apt-get update
476
477
prepare_homedir
478
#add_user_to_docker_group
479
480
install_ntpd
481
install_haveged
482
allocate_swap
483
484
install_nodejs
485
#install_docker_ce
486
pull_image_and_configs
487
488
#start_docker
489
#use_deb
490
use_deb_via_systemd
491
#use_bin
492
493
#setup_autoupdate
494
495
#install_netstats
496
install_netstats_via_systemd
497
install_scripts
498
configure_logrotate
499
}
500
501
main
502
echo "========== AlphaTestTestNet/mining-node/install.sh finished =========="
Copied!
Last modified 1yr ago
Export as PDF
Copy link