-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
install_node.sh
205 lines (180 loc) · 7.38 KB
/
install_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
#!/usr/bin/env bash
branch="master"
network="mainnet"
username="adamant"
databasename="adamant_main"
configfile="config.json"
processname="adamant"
port="36666"
nodejs="hydrogen"
while getopts 'b:n:j:' OPTION; do
OPTARG=$(echo "$OPTARG" | xargs)
case "$OPTION" in
b)
branch="$OPTARG"
;;
n)
if [ "$OPTARG" == "testnet" ]
then
network="$OPTARG"
username="adamanttest"
databasename="adamant_test"
configfile="test/config.json"
processname="adamanttest"
port="36667"
elif [ "$OPTARG" != "mainnet" ]
then
printf "\nNetwork should be 'mainnet' or 'testnet'.\n\n"
exit 1
fi
;;
j)
if [ "$OPTARG" == "16" ] || [ "$OPTARG" == "gallium" ]
then
nodejs="gallium"
elif [ "$OPTARG" != "18" ] && [ "$OPTARG" != "hydrogen" ]
then
printf "\nNodejs should be 'gallium' = '16', or 'hydrogen' = '18'.\n\n"
exit 1
fi
;;
*)
printf "\nWrong parameters. Use '-b' for branch, '-t' for network, '-j' for Nodejs version.\n\n"
exit 1
;;
esac
done
printf "\n"
printf "Welcome to the ADAMANT node installer v2.1.1 for Ubuntu 18, 20, 22. Make sure you got this file from adamant.im website or GitHub.\n"
printf "This installer is the easiest way to run ADAMANT node. We still recommend to consult IT specialist if you are not familiar with Linux systems.\n"
printf "You can see full installation instructions on https://medium.com/adamant-im/how-to-run-your-adamant-node-on-ubuntu-990e391e8fcc\n"
printf "The installer will ask you to set database and user passwords during the installation.\n"
printf "Also, the system may ask to choose some parameters, like encoding, keyboard, and grub. Generally, you can leave them by default.\n\n"
printf "Note: You've choosed '%s' network.\n" "$network"
printf "Note: You've choosed '%s' branch.\n" "$branch"
printf "Note: You've choosed '%s' Nodejs version.\n" "$nodejs"
printf "\n"
read -r -p "WARNING! Running this script is recommended for new droplets. Existing data MAY BE DAMAGED. If you agree to continue, type \"yes\": " agreement
if [[ $agreement != "yes" ]]
then
printf "\nInstallation cancelled.\n\n"
exit 1
fi
IMAGE=false
if [[ $network == "mainnet" ]]
then
printf "\nBlockchain image saves time on node sync but you must completely trust the image.\n"
printf "If you skip this step, your node will check every single transaction, which takes time (up for several days).\n"
read -r -p "Do you want to use the ADAMANT blockchain image to bootstrap a node? [Y/n]: " useimage
case $useimage in
[yY][eE][sS]|[yY]|[jJ]|'')
IMAGE=true
printf "\nI'll download blockchain image and your node will be on the actual height in a few minutes.\n\n"
;;
*)
printf "\nI'll sync your node from the beginning. It may take several days to raise up to the actual blockchain height.\n\n"
;;
esac
fi
hostname=$(cat "/etc/hostname")
if grep -q "$hostname" "/etc/hosts"
then
printf "Hostname /etc/hosts seems to be good.\n\n"
else
printf "File /etc/hosts has no hostname record. I'll fix it.\n\n"
sh -c -e "echo '\n127.0.1.1 $hostname' >> /etc/hosts";
fi
get_database_password () {
read -r -sp "Set the database password: $(echo $'\n> ')" postgrespwd
read -r -sp "$(echo $'\n')Confirm password: $(echo $'\n> ')" postgrespwdconfirmation
if [[ $postgrespwd = "$postgrespwdconfirmation" ]]
then
echo "$postgrespwd"
else
printf "\nPassword mismatch. Try again.\n\n"
get_database_password
fi
}
DB_PASSWORD="$(get_database_password)"
#User
printf "\n\nChecking if user '%s' exists…\n\n" "$username"
if [[ $(id -u "$username" > /dev/null 2>&1; echo $?) = 1 ]]
then
printf "Creating system user named '%s'…\n" "$username"
adduser --gecos "" "$username"
printf "User '%s' has been created.\n\n" "$username"
fi
#Packages
printf "Updating system packages…\n\n"
sudo apt update && sudo apt upgrade -y
printf "\n\nInstalling postgresql and other prerequisites…\n\n"
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" > /etc/apt/sources.list.d/pgdg.list' && wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -
sudo apt update && sudo DEBIAN_FRONTEND=noninteractive apt-get -yq upgrade
sudo apt install -y build-essential curl automake autoconf libtool rpl mc git postgresql postgresql-contrib libpq-dev redis-server
#Start postgres. This step is necessary for Windows Subsystem for Linux machines
sudo service postgresql start
#Postgres
printf "\n\nCreating database '%s' and database user '%s'…\n\n" "$databasename" "$username"
cd /tmp || echo "/tmp: No such directory"
sudo -u postgres psql -c "CREATE ROLE ${username} LOGIN PASSWORD '${DB_PASSWORD}';"
sudo -u postgres psql -c "CREATE DATABASE ${databasename};"
sudo -u postgres psql -c "ALTER DATABASE ${databasename} OWNER TO ${username};"
#Run next commands as user
su - "$username" <<EOSU
#NodeJS
printf "\n\nInstalling nvm & node.js…\n\n"
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
source ~/.nvm/nvm.sh
source ~/.profile
source ~/.bashrc
nvm i --lts=$nodejs
npm i -g pm2
#ADAMANT
printf "\n\nInstalling ADAMANT '%s' node. Cloning project repository from GitHub ('%s' branch)…\n\n" "$network" "$branch"
git clone https://github.com/Adamant-im/adamant --branch $branch
cd adamant || { printf "\n\nUnable to enter node's directory 'adamant'. Something is wrong, halting.\n\n"; exit 1; }
npm i
#Setup node: set DB password in config.json
printf "\n\nSetting node's config…\n\n"
if [[ $configfile == "config.json" ]]
then
cp config.default.json config.json
elif [ "$configfile" == "test/config.json" ]
then
cp test/config.default.json test/config.json
fi
rpl -i -q '"password": "password",' "\"password\": \"${DB_PASSWORD}\"," "$configfile"
#By default, node's API is available only from localhost
#rpl -i -q '"public": false,' '"public": true,' "$configfile"
# Download actual blockchain image for 'mainnet' network
if [[ $IMAGE = true ]]
then
printf "\n\nDownloading actual blockchain image…\n\n"
wget https://explorer.adamant.im/db_backup.sql.gz
printf "\nUnzipping the blockchain image, it can take a few minutes…\n\n"
gunzip db_backup.sql.gz
printf "\nLoading the blockchain image…\n\n"
psql adamant_main < db_backup.sql
printf "\nDeleting temporary blockchain image file…\n"
rm db_backup.sql
fi
printf "\n\nAdding ADAMANT '%s' node to crontab for autostart after system reboot…\n\n" "$network"
if [[ $network == "mainnet" ]]
then
crontab -l | { cat; echo "@reboot cd /home/adamant/adamant && pm2 start --name adamant app.js"; } | crontab -
else
crontab -l | { cat; echo "@reboot cd /home/adamanttest/adamant && pm2 start --name adamanttest app.js -- --config test/config.json --genesis test/genesisBlock.json"; } | crontab -
fi
printf "\n\nRunning ADAMANT '%s' node…\n\n" "$network"
if [[ $network == "mainnet" ]]
then
pm2 start --name adamant app.js
else
pm2 start --name adamanttest app.js -- --config test/config.json --genesis test/genesisBlock.json
fi
EOSU
printf "\n\nFinished ADAMANT '%s' node installation script. Executed in %s seconds.\n" "$network" "$SECONDS"
printf "Check your node status with 'pm2 show %s' command.\n" "$processname"
printf "Current node's height: 'curl http://localhost:%s/api/blocks/getHeight'\n" "$port"
printf "Thank you for supporting true decentralized ADAMANT Messenger.\n\n"
su - "$username"