-
Notifications
You must be signed in to change notification settings - Fork 3
/
lwf_manager.bash
executable file
·409 lines (348 loc) · 12.1 KB
/
lwf_manager.bash
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
#!/usr/bin/env bash
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
version="0.9.0"
cd "$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
root_path=$(pwd)
mkdir -p $root_path/logs
logfile=$root_path/logs/lwfcoin.log
set_branch() {
if [ "$(grep "704f232786a9bff25d0630c06abbc34957448ba6309d6dcef949cf9a6f43954a" $LWF_CONFIG )" ];then
GIT_BRANCH="master"
elif [ "$(grep "c16656e85880df9a41abed0aa13b2987b0d853adadc91cbc7e5c8332ea37ccc9" $LWF_CONFIG )" ];then
GIT_BRANCH="testnet"
else
GIT_BRANCH="unknown"
fi
}
LWF_CONFIG="config.json"
DB_NAME="$(grep "database" $LWF_CONFIG | cut -f 4 -d '"' | head -1)"
DB_UNAME="$(grep "user" $LWF_CONFIG | cut -f 4 -d '"' | head -1)"
DB_PASSWD="$(grep "password" $LWF_CONFIG | cut -f 4 -d '"' | head -1)"
GIT_ROOT="https://github.com/lwfcoin"
GIT_BRANCH=""
set_branch
BLOCKCHAIN_URL="http://downloads.lwf.io/snapshots/$GIT_BRANCH/latest"
DB_SNAPSHOT="blockchain.db.gz"
install_prereq() {
if [[ ! -f /usr/bin/sudo ]]; then
echo "Install sudo before continuing. Issue: apt-get install sudo as root user."
echo "Also make sure that your user has sudo access."
fi
sudo id &> /dev/null || { exit 1; };
echo ""
echo "-------------------------------------------------------"
echo "LWF installer script. Version: $version"
echo "-------------------------------------------------------"
echo -n "Running: apt-get update... ";
sudo apt-get update &> /dev/null || \
{ echo "Could not update apt repositories. Run apt-get update manually. Exiting." && exit 1; };
echo -e "done.\n"
echo -n "Running: apt-get install curl build-essential python lsb-release wget openssl autoconf libtool automake libsodium-dev... ";
sudo apt-get install -y -qq curl build-essential python lsb-release wget openssl autoconf libtool automake libsodium-dev &>> $logfile || \
{ echo "Could not install packages prerequisites. Exiting." && exit 1; };
echo -e "done.\n"
echo -n "Updating apt repository sources for postgresql.. ";
sudo bash -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main" > /etc/apt/sources.list.d/pgdg.list' &>> $logfile || \
{ echo "Could not add postgresql repo to apt." && exit 1; }
echo -e "done.\n"
echo -n "Adding postgresql repo key... "
sudo wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add - &>> $logfile || \
{ echo "Could not add postgresql repo key. Exiting." && exit 1; }
echo -e "done.\n"
echo -n "Installing postgresql... "
sudo apt-get update -qq &> /dev/null && sudo apt-get install -y -qq postgresql-9.6 postgresql-contrib-9.6 libpq-dev &>> $logfile || \
{ echo "Could not install postgresql. Exiting." && exit 1; }
echo -e "done.\n"
echo -n "Enable postgresql... "
sudo update-rc.d postgresql enable
echo -e "done.\n"
return 0;
}
ntp_checks() {
# Install NTP or Chrony for Time Management - Physical Machines only
if [[ ! -f "/proc/user_beancounters" ]]; then
if ! sudo pgrep -x "ntpd" > /dev/null; then
echo -n "\nInstalling NTP... "
sudo apt-get install ntp -yyq &>> $logfile
sudo service ntp stop &>> $logfile
sudo ntpdate pool.ntp.org &>> $logfile
sudo service ntp start &>> $logfile
if ! sudo pgrep -x "ntpd" > /dev/null; then
echo "LWF requires NTP running. Please check /etc/ntp.conf and correct any issues. Exiting."
exit 1
echo -e "done.\n"
fi # if sudo pgrep
fi # if [[ ! -f "/proc/user_beancounters" ]]
elif [[ -f "/proc/user_beancounters" ]]; then
echo -e "Running OpenVZ or LXC VM, NTP is not required, done.\n"
fi
}
create_database() {
sudo -u postgres dropdb --if-exists "$DB_NAME" 2> /dev/null
if [[ $? -ne 0 ]]; then
echo "X Failed to drop Postgresql database."
exit 1
fi
sudo -u postgres createdb -O "$DB_UNAME" "$DB_NAME" 2> /dev/null
if [[ $? -ne 0 ]]; then
echo "X Failed to create Postgresql database."
exit 1
fi
}
download_blockchain() {
echo -n "Download a recent, verified snapshot? ([y]/n): "
read downloadornot
if [ "$downloadornot" == "y" ] || [ -z "$downloadornot" ]; then
rm -f $DB_SNAPSHOT
echo "√ Downloading $DB_SNAPSHOT from $BLOCKCHAIN_URL"
curl --progress-bar -o $DB_SNAPSHOT "$BLOCKCHAIN_URL"
if [ $? != 0 ]; then
rm -f $DB_SNAPSHOT
echo "X Failed to download blockchain snapshot."
exit 1
else
echo "√ Blockchain snapshot downloaded successfully."
fi
else
echo "√ Using this Awesome Snapshot."
fi
}
restore_blockchain() {
export PGPASSWORD=$DB_PASSWD
echo "Restoring blockchain with $DB_SNAPSHOT"
# Be sure that all tables exist
gunzip -fcq "$DB_SNAPSHOT" | psql -q -h 127.0.0.1 -U "$DB_UNAME" -d "$DB_NAME" &> $logfile
if [ $? != 0 ]; then
echo "X Failed to restore blockchain."
exit 1
else
if [[ -f $DB_SNAPSHOT ]]; then
echo "√ Using Local Snapshot."
else
echo "X No local snapshot found. Exiting."
exit 1;
fi
fi
}
add_pg_user_database() {
if start_postgres; then
user_exists=$(grep postgres /etc/passwd |wc -l);
if [[ $user_exists == 1 ]]; then
res=$(sudo -u postgres psql -tAc "SELECT 1 FROM pg_roles WHERE rolname='$DB_UNAME'" 2> /dev/null)
if [[ $res -ne 1 ]]; then
echo -n "Creating database user... "
res=$(sudo -u postgres psql -c "CREATE USER $DB_UNAME WITH PASSWORD '$DB_PASSWD';" 2> /dev/null)
res=$(sudo -u postgres psql -tAc "SELECT 1 FROM pg_roles WHERE rolname='$DB_UNAME'" 2> /dev/null)
if [[ $res -eq 1 ]]; then
echo -e "done.\n"
fi
fi
echo -n "Creating database... "
res=$(sudo -u postgres dropdb --if-exists "$DB_NAME" 2> /dev/null)
res=$(sudo -u postgres createdb -O "$DB_UNAME" "$DB_NAME" 2> /dev/null)
res=$(sudo -u postgres psql -t -c "SELECT count(*) FROM pg_database where datname='$DB_NAME'" 2> /dev/null)
if [[ $res -eq 1 ]]; then
echo -e "done.\n"
fi
fi
return 0
fi
return 1;
}
start_postgres() {
installed=$(dpkg -l | grep postgresql | grep ii | head -n1 | wc -l);
running=$(ps aux | grep "bin\/postgres" | wc -l);
if [[ $installed -ne 1 ]]; then
echo "Postgres is not installed. Install postgres manually before continuing. Exiting."
exit 1;
fi
if [[ $running -ne 1 ]]; then
sudo /etc/init.d/postgresql start &>> $logfile || { echo -n "Could not start postgresql, try to start it manually. Exiting." && exit 1; }
fi
return 0
}
install_node_npm() {
echo -n "Installing nodejs and npm..."
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - &>> $logfile
sudo apt-get install -y -qq nodejs &>> $logfile || { echo "Could not install nodejs and npm. Exiting." && exit 1; }
echo -e "done.\n" && echo -n "Installing grunt-cli... "
sudo npm install grunt-cli -g &>> $logfile || { echo "Could not install grunt-cli. Exiting." && exit 1; }
echo -e "done.\n" && echo -n "Installing bower... "
sudo npm install bower -g &>> $logfile || { echo "Could not install bower. Exiting." && exit 1; }
echo -e "done.\n" && echo -n "Installing process management software... "
sudo npm install forever -g &>> $logfile || { echo "Could not install process management software(forever). Exiting." && exit 1; }
echo -e "done.\n"
return 0;
}
install_lwf() {
echo -n "Installing LWF core..."
npm install --production &>> $logfile || { echo "Could not install LWF, please check the log directory. Exiting." && exit 1; }
echo -e "done.\n"
return 0;
}
# (re)Install wallet
# ---------------------
function install_wallet {
cd $HOME
## Check if directory exists
if [ -d "$root_path" ]; then
cd $root_path
echo -n "Installing wallet... "
if [[ -d "public" ]]; then
echo "Found an existing public folder. Will remove it now.."
rm -rf public/
fi
git clone -b $GIT_BRANCH $GIT_ROOT/lwf-wallet public &>> $logfile || { echo -n "Could not clone git wallet source. Exiting." && exit 1; }
cd public && npm install &>> $logfile || { echo -n "Could not install web wallet node modules. Exiting." && exit 1; }
npm run grunt-release &>> $logfile || { echo -e "\n\nCould not build web wallet release. Exiting." && exit 1; }
echo "Done."
echo "---- PLEASE RELOAD YOUR NODE ----"
else
echo "Directory $root_path does not exist! Nothing to install.."
exit 1;
fi
}
update_client() {
echo "Updating LWF client..."
if [[ -f config.json ]]; then
cp -i $root_path/config.json $root_path/config.json.bak
fi
cd $root_path
git checkout . &>> $logfile || { echo "Failed to checkout latest status. See $logfile for more information." && exit 1; }
git pull &>> $logfile || { echo "Failed to fetch new files from git. See $logfile for more information. Exiting." && exit 1; }
npm install --production &>> $logfile || { echo "Could not install node modules. See $logfile for more information. Exiting." && exit 1; }
if [[ -f config.json.bak ]]; then
echo "Take over config.json entries from previous installation... "
node $root_path/updateConfig.js -o $root_path/config.json.bak -n $root_path/config.json && \
rm -f $root_path/config.json.bak
fi
return 0;
}
stop_lwf() {
echo -n "Stopping LWF... "
forever_exists=$(whereis forever | awk {'print $2'})
if [[ ! -z $forever_exists ]]; then
$forever_exists stop $root_path/app.js &>> $logfile
fi
echo "√"
if ! running; then
return 0
fi
return 1
}
start_lwf() {
echo -n "Starting LWF... "
forever_exists=$(whereis forever | awk {'print $2'})
if [[ ! -z $forever_exists ]]; then
$forever_exists start -o $root_path/logs/lwfcoin.log -e $root_path/logs/lwfcoin-err.log app.js &>> $logfile || \
{ echo -e "\nCould not start LWF." && exit 1; }
fi
sleep 1
echo "√"
if running; then
return 0
fi
return 1
}
running() {
process=$(forever list |grep app.js |awk {'print $9'})
if [[ -z $process ]] || [[ "$process" == "STOPPED" ]]; then
return 1
fi
return 0
}
show_blockHeight(){
export PGPASSWORD=$DB_PASSWD
blockHeight=$(psql -d $DB_NAME -U $DB_UNAME -h localhost -p 5432 -t -c "select height from blocks order by height desc limit 1")
echo "Block height = $blockHeight"
}
parse_option() {
OPTIND=2
while getopts d:r:n opt
do
case $opt in
s) install_with_ssl=true ;;
esac
done
}
rebuild_lwf() {
download_blockchain
create_database
restore_blockchain
}
start_log() {
echo "Starting $0... " > $logfile
echo -n "Date: " >> $logfile
date >> $logfile
echo "" >> $logfile
}
case $1 in
"install")
parse_option $@
start_log
install_prereq
ntp_checks
add_pg_user_database
install_node_npm
install_lwf
echo ""
echo ""
echo "LWF successfully installed"
;;
"install_wallet")
install_wallet
sleep 2
;;
"update_client")
start_log
stop_lwf
sleep 1
update_client
sleep 1
start_lwf
show_blockHeight
;;
"reload")
stop_lwf
sleep 1
start_lwf
show_blockHeight
;;
"rebuild")
stop_lwf
sleep 1
start_postgres
sleep 1
rebuild_lwf
start_lwf
show_blockHeight
;;
"clean_start")
stop_lwf
create_database
start_lwf
;;
"status")
if running; then
echo "√ LWF is running."
show_blockHeight
else
echo "X LWF is NOT running."
fi
;;
"start")
start_lwf
show_blockHeight
;;
"stop")
stop_lwf
;;
*)
echo 'Available options: install, install_wallet, reload (stop/start), rebuild (official snapshot), clean_start (drop database), start, stop, update_client'
echo 'Usage: ./lwf_manager.bash install'
exit 1
;;
esac
exit 0;