-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinstall.sh
More file actions
359 lines (327 loc) · 10.1 KB
/
install.sh
File metadata and controls
359 lines (327 loc) · 10.1 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
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
#!/bin/bash
CONFIG_FILE='nextcoin.conf'
COIN_DAEMON='nextd'
COIN_CLI='next-cli'
COIN_REPO='https://github.com/ImTheDeveloper/next_mn/raw/master/next_linux_bin.zip'
COIN_NAME='NEXT'
COIN_PORT=7077
RPC_PORT=10001
SENTINEL_REPO='https://github.com/NextExchange/sentinel.git'
OS_VERSION='U18'
NODEIP=$(curl -s4 icanhazip.com)
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
progressfilt () {
local flag=false c count cr=$'\r' nl=$'\n'
while IFS='' read -d '' -rn 1 c
do
if $flag
then
printf '%c' "$c"
else
if [[ $c != $cr && $c != $nl ]]
then
count=0
else
((count++))
if ((count > 1))
then
flag=true
fi
fi
fi
done
}
function compile_node() {
echo -e "Preparing to download and compile $COIN_NAME${NC} node wallet"
TMP_FOLDER=$(mktemp -d)
cd $TMP_FOLDER
wget -q $COIN_REPO >/dev/null 2>&1
compile_error
COIN_ZIP=$(echo $COIN_REPO | awk -F'/' '{print $NF}')
unzip $COIN_ZIP >/dev/null 2>&1
compile_error
rm -f $COIN_ZIP >/dev/null 2>&1
chmod +x $COIN_DAEMON $COIN_CLI
cp $COIN_DAEMON $COIN_CLI $NEXTCOINFOLDER
cd - >/dev/null 2>&1
rm -rf $TMP_FOLDER >/dev/null 2>&1
echo -e "Node files downloaded and setup.${NC}"
clear
}
function configure_systemd() {
cat << EOF > /etc/systemd/system/$COIN_NAME.service
[Unit]
Description=$COIN_NAME service
After=network.target
[Service]
User=$NEXTCOINUSER
Group=$NEXTCOINUSER
Type=forking
#PIDFile=$NEXTCOINFOLDER/$COIN_NAME.pid
ExecStart=$NEXTCOINFOLDER/$COIN_DAEMON -daemon -conf=$NEXTCOINFOLDER/$CONFIG_FILE -datadir=$NEXTCOINFOLDER
ExecStop=$NEXTCOINFOLDER/$COIN_CLI -conf=$NEXTCOINFOLDER/$CONFIG_FILE -datadir=$NEXTCOINFOLDER stop
Restart=always
PrivateTmp=true
TimeoutStopSec=60s
TimeoutStartSec=10s
StartLimitInterval=120s
StartLimitBurst=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
sleep 3
systemctl start $COIN_NAME.service >/dev/null 2>&1
systemctl enable $COIN_NAME.service >/dev/null 2>&1
if [[ -z "$(ps axo cmd:100 | egrep $COIN_DAEMON)" ]]; then
echo -e "${RED}$COIN_NAME is not running${NC}, please investigate. You should start by running the following commands as root:"
echo -e "${GREEN}systemctl start $COIN_NAME.service"
echo -e "systemctl status $COIN_NAME.service"
echo -e "less /var/log/syslog${NC}"
exit 1
fi
}
function configure_startup() {
cat << EOF > /etc/init.d/$COIN_NAME
#! /bin/bash
### BEGIN INIT INFO
# Provides: $COIN_NAME
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: $COIN_NAME
# Description: This file starts and stops $COIN_NAME MN server
#
### END INIT INFO
case "\$1" in
start)
$NEXTCOINFOLDER/$COIN_DAEMON -daemon
sleep 5
;;
stop)
$NEXTCOINFOLDER/$COIN_CLI stop
;;
restart)
$NEXTCOINFOLDER/$COIN_CLI stop
sleep 10
$NEXTCOINFOLDER/$COIN_DAEMON -daemon
;;
*)
echo "Usage: $COIN_NAME {start|stop|restart}" >&2
exit 3
;;
esac
EOF
chmod +x /etc/init.d/$COIN_NAME >/dev/null 2>&1
update-rc.d $COIN_NAME defaults >/dev/null 2>&1
/etc/init.d/$COIN_NAME start
if [ "$?" -gt "0" ]; then
sleep 5
/etc/init.d/$COIN_NAME start
fi
}
function create_config() {
RPCUSER=$(tr -cd '[:alnum:]' < /dev/urandom | fold -w10 | head -n1)
RPCPASSWORD=$(tr -cd '[:alnum:]' < /dev/urandom | fold -w22 | head -n1)
cat << EOF > $NEXTCOINFOLDER/$CONFIG_FILE
rpcuser=$RPCUSER
rpcpassword=$RPCPASSWORD
rpcport=$RPC_PORT
#rpcallowip=127.0.0.1
listen=1
server=1
daemon=1
port=$COIN_PORT
EOF
}
function create_key() {
echo -e "Please provide your ${RED}$COIN_NAME Masternode Private Key generated from your local wallet${NC}.\nLeave blank and hit [ENTER] if you wish for the script to generate a new ${RED}$COIN_NAME Masternode Private Key${NC} for you:"
read -e COINKEY
if [[ -z "$COINKEY" ]]; then
echo -e "Please wait whilst we generate your masternode private key. It will be displayed once installation has completed."
$NEXTCOINFOLDER/$COIN_DAEMON -daemon >/dev/null 2>&1
sleep 30
if [ -z "$(ps axo cmd:100 | grep $COIN_DAEMON)" ]; then
echo -e "${RED}$COIN_NAME server couldn not start. Check /var/log/syslog for errors.${NC}"
exit 1
fi
COINKEY=$($NEXTCOINFOLDER/$COIN_CLI masternode genkey)
if [ "$?" -gt "0" ];
then
echo -e "${RED}Wallet not fully loaded. Let us wait and try again to generate the Private Key${NC}"
sleep 30
COINKEY=$($NEXTCOINFOLDER/$COIN_CLI masternode genkey)
fi
$NEXTCOINFOLDER/$COIN_CLI stop >/dev/null 2>&1
fi
clear
}
function update_config() {
sed -i 's/daemon=1/daemon=0/' $NEXTCOINFOLDER/$CONFIG_FILE
cat << EOF >> $NEXTCOINFOLDER/$CONFIG_FILE
logintimestamps=1
maxconnections=64
testnet=0
debug=0
masternode=1
externalip=$NODEIP
port=$COIN_PORT
masternodeprivkey=$COINKEY
EOF
}
function enable_firewall() {
echo -e "Installing and setting up firewall to allow ingress on port ${GREEN}$COIN_PORT${NC}"
ufw allow ssh >/dev/null 2>&1
ufw allow $COIN_PORT >/dev/null 2>&1
ufw default allow outgoing >/dev/null 2>&1
echo "y" | ufw enable >/dev/null 2>&1
}
function get_ip() {
declare -a NODE_IPS
for ips in $(netstat -i | awk '!/Kernel|Iface|lo/ {print $1," "}')
do
NODE_IPS+=($(curl --interface $ips --connect-timeout 2 -s4 icanhazip.com))
done
if [ ${#NODE_IPS[@]} -gt 1 ]
then
echo -e "${GREEN}More than one IP. Please type 0 to use the first IP, 1 for the second and so on...${NC}"
INDEX=0
for ip in "${NODE_IPS[@]}"
do
echo ${INDEX} $ip
let INDEX=${INDEX}+1
done
read -e choose_ip
NODEIP=${NODE_IPS[$choose_ip]}
else
NODEIP=${NODE_IPS[0]}
fi
}
function compile_error() {
if [ "$?" -gt "0" ];
then
echo -e "${RED}Failed to compile $COIN_NAME. Please investigate.${NC}"
exit 1
fi
}
function detect_os() {
if lsb_release -d | grep -q 'Ubuntu 18.04'; then
OS_VERSION='U18'
elif lsb_release -d | grep -q 'Ubuntu 18.10'; then
OS_VERSION='U18'
elif lsb_release -d | grep -q 'Ubuntu 16.04'; then
OS_VERSION='U16'
elif lsb_release -d | grep -q 'Ubuntu 14.04'; then
OS_VERSION='U14'
elif lsb_release -d | grep -q 'Debian'; then
OS_VERSION='D'
else
echo -e "${RED}Script currently only supports Ubuntu 14.04, 16.04, 18.04 or Debian. Installation will need to be carried out manually.${NC}"
exit 1
fi
}
function checks() {
detect_os
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}$0 must be run as root.${NC}"
exit 1
fi
if [ -n "$(pidof $COIN_DAEMON)" ] || [ -e "$COIN_DAEMOM" ] ; then
echo -e "${RED}$COIN_NAME is already installed.${NC}"
exit 1
fi
}
function prepare_system() {
echo -e "Prepare the system to install ${GREEN}$COIN_NAME${NC} master node."
apt-get update >/dev/null 2>&1
echo -e "Installing required packages.${NC}"
apt-get install -y wget curl ufw binutils net-tools unzip git >/dev/null 2>&1
echo -e "Checking if swap space is needed."
PHYMEM=$(free -g|awk '/^Mem:/{print $2}')
SWAP=$(swapon -s)
if [[ "$PHYMEM" -lt "2" && -z "$SWAP" ]];
then
echo -e "${GREEN}Server is running with less than 2G of RAM, creating 2G swap file.${NC}"
dd if=/dev/zero of=/swapfile bs=1024 count=2M >/dev/null 2>&1
chmod 600 /swapfile >/dev/null 2>&1
mkswap /swapfile >/dev/null 2>&1
swapon -a /swapfile >/dev/null 2>&1
else
echo -e "${GREEN}The server running with at least 2G of RAM, or SWAP exists.${NC}"
fi
clear
}
function important_information() {
echo
echo -e "================================================================================"
echo -e "${GREEN}$COIN_NAME${NC} Masternode is up and running listening on port ${RED}$COIN_PORT${NC}."
echo -e "Configuration file is: ${RED}$NEXTCOINFOLDER/$CONFIG_FILE${NC}"
echo -e "VPS_IP:PORT ${RED}$NODEIP:$COIN_PORT${NC}"
echo -e "MASTERNODE PRIVATEKEY is: ${RED}$COINKEY${NC}"
if [[ -n $SENTINEL_REPO ]]; then
echo -e "Sentinel is installed in ${RED}$NEXTCOINFOLDER/sentinel${NC}"
echo -e "Sentinel logs is: ${RED}$NEXTCOINFOLDER/sentinel.log${NC}"
fi
echo -e "Useful commands for checking your node can be found in the ${GREEN}README${NC} here:"
echo -e "${RED}https://github.com/ImTheDeveloper/next_mn/blob/master/README.md${NC}"
echo -e "Produced by @munkee - any questions catch me on telegram!${NC}"
echo -e "================================================================================"
}
function install_sentinel() {
echo -e "Installing sentinel.${NC}"
apt-get -y install python-virtualenv virtualenv >/dev/null 2>&1
git clone $SENTINEL_REPO $NEXTCOINFOLDER/sentinel >/dev/null 2>&1
cd $NEXTCOINFOLDER/sentinel
virtualenv ./venv >/dev/null 2>&1
./venv/bin/pip install -r requirements.txt >/dev/null 2>&1
cat << EOF > $NEXTCOINFOLDER/sentinel/sentinel.conf
next_conf=$NEXTCOINFOLDER/$CONFIG_FILE
EOF
echo "*/2 * * * * cd $NEXTCOINFOLDER/sentinel && ./venv/bin/python bin/sentinel.py >> $NEXTCOINFOLDER/sentinel.log 2>&1" > $NEXTCOINFOLDER/$COIN_NAME.cron
crontab $NEXTCOINFOLDER/$COIN_NAME.cron
rm $NEXTCOINFOLDER/$COIN_NAME.cron >/dev/null 2>&1
clear
}
function ask_user() {
DEFAULTNEXTCOINUSER=$COIN_NAME
read -p "Which user do you wish to use/create to run this masternode as?: " -i $DEFAULTNEXTCOINUSER -e NEXTCOINUSER
: ${NEXTCOINUSER:=$DEFAULTNEXTCOINUSER}
if [ -z "$(getent passwd $NEXTCOINUSER)" ]; then
useradd -m $NEXTCOINUSER
USERPASS=$(tr -cd '[:alnum:]' < /dev/urandom | fold -w12 | head -n1)
echo "$NEXTCOINUSER:$USERPASS" | chpasswd
fi
if (($OS_VERSION == 'D')); then
NEXTCOINHOME=$(su - $NEXTCOINUSER bash -c 'echo $HOME')
else
NEXTCOINHOME=$(sudo -H -u $NEXTCOINUSER bash -c 'echo $HOME')
fi
DEFAULTNEXTCOINFOLDER="$NEXTCOINHOME/.next"
read -p "Configuration folder: " -i $DEFAULTNEXTCOINFOLDER -e NEXTCOINFOLDER
: ${NEXTCOINFOLDER:=$DEFAULTNEXTCOINFOLDER}
mkdir -p $NEXTCOINFOLDER
chown -R $NEXTCOINUSER: $NEXTCOINFOLDER >/dev/null
}
function setup_node() {
ask_user
get_ip
compile_node
create_config
create_key
update_config
enable_firewall
install_sentinel
important_information
if (( $OS_VERSION == 'U16' || $OS_VERSION == 'U18' || $OS_VERSION == 'D' )); then
configure_systemd
else
configure_startup
fi
}
##### Main #####
checks
prepare_system
setup_node