· 6 years ago · Dec 13, 2019, 08:30 PM
1#!/bin/bash
2
3###############################
4# Script by Thanos and Fosskers
5###############################
6
7LOG_FILE=/tmp/install.log
8
9decho () {
10 echo `date +"%H:%M:%S"` $1
11 echo `date +"%H:%M:%S"` $1 >> $LOG_FILE
12}
13
14error() {
15 local parent_lineno="$1"
16 local message="$2"
17 local code="${3:-1}"
18 echo "Error on or near line ${parent_lineno}; exiting with status ${code}"
19 exit "${code}"
20}
21trap 'error ${LINENO}' ERR
22
23clear
24
25cat <<'FIG'
26__/\\\________/\\\__/\\\\\\\\\\\\________/\\\\\\\\\_____________________________
27__\/\\\_____/\\\//__\/\\\////////\\\____/\\\\\\\\\\\\\__________________________
28___\/\\\__/\\\//_____\/\\\______\//\\\__/\\\/////////\\\________________________
29____\/\\\\\\//\\\_____\/\\\_______\/\\\_\/\\\_______\/\\\_______________________
30_____\/\\\//_\//\\\____\/\\\_______\/\\\_\/\\\\\\\\\\\\\\\______________________
31______\/\\\____\//\\\___\/\\\_______\/\\\_\/\\\/////////\\\_____________________
32_______\/\\\_____\//\\\__\/\\\_______/\\\__\/\\\_______\/\\\____________________
33________\/\\\______\//\\\_\/\\\\\\\\\\\\/___\/\\\_______\/\\\___________________
34_________\///________\///__\////////////_____\///________\///___________________
35__/\\\\\_____/\\\_______/\\\\\_______/\\\\\\\\\\\\_____/\\\\\\\\\\\\\\\_________
36__\/\\\\\\___\/\\\_____/\\\///\\\____\/\\\////////\\\__\/\\\///////////_________
37___\/\\\/\\\__\/\\\___/\\\/__\///\\\__\/\\\______\//\\\_\/\\\___________________
38____\/\\\//\\\_\/\\\__/\\\______\//\\\_\/\\\_______\/\\\_\/\\\\\\\\\\\__________
39_____\/\\\\//\\\\/\\\_\/\\\_______\/\\\_\/\\\_______\/\\\_\/\\\///////__________
40______\/\\\_\//\\\/\\\_\//\\\______/\\\__\/\\\_______\/\\\_\/\\\________________
41_______\/\\\__\//\\\\\\__\///\\\__/\\\____\/\\\_______/\\\__\/\\\_______________
42________\/\\\___\//\\\\\____\///\\\\\/_____\/\\\\\\\\\\\\/___\/\\\\\\\\\\\\\\\__
43_________\///_____\/////_______\/////_______\////////////_____\///////////////__
44FIG
45
46# Check if executed as root user
47if [[ $EUID -ne 0 ]]; then
48 echo -e "This script has to be run as \033[1mroot\033[0m user."
49 exit 1
50fi
51
52# Print variable on a screen
53decho "Make sure you double check information before hitting enter!"
54
55# --- USER INPUT --- #
56read -e -p "Please enter your node's Domain Name: " whereami
57if [[ $whereami == "" ]]; then
58 decho "WARNING: No domain given, exiting!"
59 exit 3
60fi
61
62read -e -p "Please enter your Public Key: " publickey
63if [[ $publickey == "" ]]; then
64 decho "WARNING: No public key given, exiting!"
65 exit 3
66fi
67
68read -e -p "Please enter your Email Address: " email
69if [[ $email == "" ]]; then
70 decho "WARNING: No email address given, exiting!"
71 exit 3
72fi
73
74# --- SYSTEM SETUP --- #
75
76# Check for systemd.
77systemctl --version >/dev/null 2>&1 || { decho "systemd is required. Are you using Ubuntu 18.04?" >&2; exit 1; }
78
79# Update packages.
80decho "Updating system..."
81
82apt-get update -y >> $LOG_FILE 2>&1
83
84# Install required packages
85decho "Installing base packages and dependencies..."
86decho "This may take a while..."
87
88apt-get install -y certbot >> $LOG_FILE 2>&1
89apt-get install -y librocksdb-dev >> $LOG_FILE 2>&1
90apt-get install -y curl >> $LOG_FILE 2>&1
91
92# --- NODE BINARY SETUP --- #
93
94NODE=https://github.com/kadena-io/chainweb-node/releases/download/1.3.1/chainweb.8.6.5.ubuntu-18.04.1e6c76b2.tar.gz
95MINER=https://github.com/kadena-io/chainweb-miner/releases/download/v1.0.3/chainweb-miner-1.0.3-ubuntu-18.04.tar.gz
96
97decho 'Downloading Node...'
98mkdir -p /root/kda
99cd /root/kda/
100wget --no-check-certificate $NODE >> $LOG_FILE 2>&1
101tar -xvf chainweb.8.6.5.ubuntu-18.04.1e6c76b2.tar.gz >> $LOG_FILE 2>&1
102wget --no-check-certificate $MINER >> $LOG_FILE 2>&1
103tar -xvf chainweb-miner-1.0.3-ubuntu-18.04.tar.gz >> $LOG_FILE 2>&1
104
105# Create config.yaml
106decho "Creating config files and Health check..."
107
108touch /root/kda/config.yaml
109cat << EOF > /root/kda/config.yaml
110chainweb:
111 # The defining value of the network. To change this means being on a
112 # completely independent Chainweb.
113 chainwebVersion: mainnet01
114 # The number of requests allowed per second per client to certain endpoints.
115 # If these limits are crossed, you will receive a 429 HTTP error.
116 throttling:
117 local: 0.1
118 mining: 5
119 global: 50
120 putPeer: 11
121 mining:
122 # Settings for how a Node can provide work for remote miners.
123 coordination:
124 enabled: true
125 # "public" or "private".
126 mode: private
127 # The number of "/mining/work" calls that can be made in total over a 5
128 # minute period.
129 limit: 1200
130 # When "mode: private", this is a list of miner account names who are
131 # allowed to have work generated for them.
132 miners:
133 - account: $publickey
134 predicate: keys-all
135 public-keys:
136 - $publickey
137 - account: 1bb4fabd9b88310135677d0e51b48e48a8db6a32e3a0aeb544d64a6ab35f066f
138 predicate: keys-all
139 public-keys:
140 - 1bb4fabd9b88310135677d0e51b48e48a8db6a32e3a0aeb544d64a6ab35f066f
141 - account: 730f3ee4dc399aa5b343ad82cf4f9d7052f85911825abad40850bb9d92a3d5fd
142 predicate: keys-all
143 public-keys:
144 - 730f3ee4dc399aa5b343ad82cf4f9d7052f85911825abad40850bb9d92a3d5fd
145 p2p:
146 # Your node's network identity.
147 peer:
148 # Filepath to the "fullchain.pem" of the certificate of your domain.
149 # If "null", this will be auto-generated.
150 certificateChainFile: /etc/letsencrypt/live/$whereami/fullchain.pem
151 # Filepath to the "privkey.pem" of the certificate of your domain.
152 # If "null", this will be auto-generated.
153 keyFile: /etc/letsencrypt/live/$whereami/privkey.pem
154 # You.
155 hostaddress:
156 # This should be your public IP or domain name.
157 hostname: $whereami
158 # The port you'd like to run the Node on. 443 is a safe default.
159 port: 443
160 # Initial peers to connect to in order to join the network for the first time.
161 # These will share more peers and block data to your Node.
162 peers:
163 - address:
164 hostname: us-w1.chainweb.com
165 port: 443
166 id: null
167 - address:
168 hostname: us-w2.chainweb.com
169 port: 443
170 id: null
171 - address:
172 hostname: us-w3.chainweb.com
173 port: 443
174 id: null
175 - address:
176 hostname: us-e1.chainweb.com
177 port: 443
178 id: null
179 - address:
180 hostname: us-e2.chainweb.com
181 port: 443
182 id: null
183 - address:
184 hostname: us-e3.chainweb.com
185 port: 443
186 id: null
187 - address:
188 hostname: fr1.chainweb.com
189 port: 443
190 id: null
191 - address:
192 hostname: fr2.chainweb.com
193 port: 443
194 id: null
195 - address:
196 hostname: fr3.chainweb.com
197 port: 443
198 id: null
199 - address:
200 hostname: jp1.chainweb.com
201 port: 443
202 id: null
203 - address:
204 hostname: jp2.chainweb.com
205 port: 443
206 id: null
207 - address:
208 hostname: jp3.chainweb.com
209 port: 443
210 id: null
211logging:
212 # All structural (JSON, etc.) logs.
213 telemetryBackend:
214 enabled: true
215 configuration:
216 handle: stdout
217 color: auto
218 # "text" or "json"
219 format: text
220 # Simple text logs.
221 backend:
222 handle: stdout
223 color: auto
224 # "text" or "json"
225 format: text
226 logger:
227 log_level: warn
228 filter:
229 rules:
230 - key: component
231 value: cut-monitor
232 level: info
233 - key: component
234 value: pact-tx-replay
235 level: info
236 - key: component
237 value: connection-manager
238 level: info
239 - key: component
240 value: miner
241 level: info
242 - key: component
243 value: local-handler
244 level: info
245 default: error
246EOF
247
248# --- SYSTEMD SETUP FOR NODE --- #
249touch /etc/systemd/system/kadena-node.service
250cat <<EOF > /etc/systemd/system/kadena-node.service
251[Unit]
252Description=Kadena Node
253[Service]
254User=root
255KillMode=process
256KillSignal=SIGINT
257WorkingDirectory=/root/kda
258ExecStart=/root/kda/chainweb-node --config-file=/root/kda/config.yaml
259Restart=always
260RestartSec=5
261LimitNOFILE=65536
262[Install]
263WantedBy=multi-user.target
264EOF
265
266# --- HEALTH CHECK --- #
267touch /root/kda/health.sh
268chmod +x /root/kda/health.sh
269cat <<EOF > /root/kda/health.sh
270#!/bin/bash
271status_code=\$(timeout 5s curl --write-out %{http_code} https://$whereami:443/chainweb/0.0/mainnet01/cut/ --silent --output /dev/null)
272echo \$status_code
273if [[ "\$status_code" -ne 200 ]]; then
274 echo "No response from API: Restarting the Node"
275 systemctl daemon-reload
276 systemctl restart kadena-node
277fi
278EOF
279
280# --- HEALTH CHECK CRONTAB --- #
281echo "*/5 * * * * /root/kda/health.sh >/root/kda/health.out 2>/root/kda/health.err" >> newCrontab
282crontab -u root newCrontab >> $LOG_FILE 2>&1
283rm newCrontab >> $LOG_FILE 2>&1
284
285# --- DOMAIN-SPECIFIC CERTIFICATE CREATION --- #
286certbot certonly --non-interactive --agree-tos -m $email --standalone --cert-name $whereami -d $whereami >> $LOG_FILE 2>&1
287
288# --- ENABLE THE NODE --- #
289systemctl daemon-reload
290systemctl enable kadena-node
291
292# --- DOWNLOAD A DATABASE SNAPSHOT --- #
293echo "Downloading recent database snapshot..."
294echo "This may take a while..."
295
296# Send a stop message, just in case.
297systemctl stop kadena-node
298# No-op if it already exists.
299mkdir -p /root/.local/share/chainweb-node/mainnet01/0/
300cd /root/.local/share/chainweb-node/mainnet01/0/
301# Remove these, in case they were already there.
302rm -rf rocksDb sqlite
303# Fetch the snapshot.
304wget http://node-dbs.chainweb.com/db-chainweb-node-ubuntu.18.04-latest.tar.gz
305tar xvfz db-chainweb-node-ubuntu.18.04-latest.tar.gz >> $LOG_FILE 2>&1
306systemctl start kadena-node
307clear
308
309# Installation Completed
310echo 'Installation completed!'
311echo 'Health checks are in place, and everything is automated from now on.'
312echo 'Type "nano /root/kda/config.yaml" to edit your config if necessary.'
313echo 'CTRL+x to save, Y to confirm, then "systemctl restart kadena-node".'
314echo 'Type "journalctl -fu kadena-node" to see the node log.'