-
Notifications
You must be signed in to change notification settings - Fork 5
/
BU_auto_node.sh
293 lines (252 loc) · 7.29 KB
/
BU_auto_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
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
#!/bin/bash
#
# v0.0.1
# Installing BU on a new machine, configuring it, make it persistan to reboot
# Tested on Ubuntu 14.04 and 16.04 x86_64
# inspired by https://raw.githubusercontent.com/XertroV/BitcoinAutoNode/master/bitcoinAutoNode.sh
#
# WARN if run by sudo we need to input the password from stdin on disable password prompt for sudoers
#
# -d download instead of compiling
# -n avoid to create swap partition
# -r reboot at the end
# -s enable the sync of the blockchain from trusted source
# -p disable prune mode
#
# TODO
# - add a conf file instead of using all these variables
# - fetch a list of BU node to connect to
# - check if there's enough space to store the blockchain
set -eu
#TODO read these pars from a conf file, ideally the JSON used in release signature.
BU_BRANCH="0.12.1bu"
BU_TAG="bu0.12.1c"
USR="testme"
BU_REPO="https://github.com/BitcoinUnlimited/BitcoinUnlimited.git"
BU_URL64="https://www.bitcoinunlimited.info/downloads/bitcoinUnlimited-0.12.1-linux64.tar.gz"
BU_URL32="https://www.bitcoinunlimited.info/downloads/bitcoinUnlimited-0.12.1-linux32.tar.gz"
BU_SUM64="34de171ac1b48b0780d68f3844c9fd2e8bfe6a7780b55e1f012067c2440ebd8a"
BU_SUM32="984111483981bbfa5d33f40014336d74cbb263a51cb42a87e5d1871f88c14a7c"
BU_HOME="/home/$USR"
SSH_KEY_PATH=/home/$USR/.ssh/id_rsa
REMOTE_USR=auser
REMOTE_HOST=trusted_host
REMOTE_BLOCKCHAIN_PATH=path_to_the_blockchain
sync_chain() {
echo "Start syncing the chain ..."
sudo -u $USR bash <<-EOH
rsync --progress -a -e "ssh -i $SSH_KEY_PATH" $REMOTE_USR@$REMOTE_HOST:$REMOTE_BLOCKCHAIN_PATH/blocks /home/$USR/.bitcoin
rsync --progress -a -e "ssh -i $SSH_KEY_PATH" $REMOTE_USR@$REMOTE_HOST:$REMOTE_BLOCKCHAIN_PATH/chainstate /home/$USR/.bitcoin
EOH
chown $USR.$USR -R /home/$USR/.bitcoin
echo "Synced!"
}
# default value for variuois mode
DW_MODE=0
CR_SWAP=1
REBOOT=0
SYNC=0
PRUNE=1
# add getopts parsing
while getopts "dnrsp" Opts; do
case $Opts in
d)
#Download mode activated
DW_MODE=1
;;
n)
#Disable swap creation
CR_SWAP=0
;;
r)
#reboot the machien at the end
REBOOT=1
;;
s)
#sync the chain
SYNC=1
;;
p)
#toggle prune mode
PRUNE=0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
### helper functions
check_root_sudo () {
if [ "$(whoami)" != "root" ]; then
echo "Please run as root, or use sudo"
exit 1
fi;
}
check_tmux_screen () {
if [[ -v TERM && "$TERM" != "screen" ]]; then
echo "Please run the script inside a terminal multiplexer (e.g. tmux, screen, ...)";
echo "To install tmux use sudo apt-get install tmux"
exit 1;
else
echo "Tmux/Screen detected"
fi
}
check_user_exists() {
user_exists=$(id -u $USR > /dev/null 2>&1; echo $?)
if [ "$user_exists" == "1" ]; then
echo "User $USR does not exist, create the account via adduser(8) before proceeding";
exit 1;
fi
}
#### main functions
install_prereq_comp () {
echo -n "Updating Ubuntu ... "
# removed ppa bitcon rep since wei don't need berkeley db
apt-get -y -qq update
echo "done."
echo -n "Installing BU prereq via apt-get ..."
echo -en " done.\nInstalling git ..."
sudo apt-get -qq -y install git > /dev/null
echo -en " done.\nInstalling build-essentials and friends ..."
sudo apt-get -qq -y install build-essential libtool autotools-dev automake pkg-config > /dev/null
echo -en " done.\nInstalling helper libs ..."
sudo apt-get -qq -y install libssl-dev libevent-dev bsdmainutils libboost-all-dev > /dev/null
echo -n " done."
}
install_prereq_down () {
echo -n "Updating Ubuntu ... "
# removed ppa bitcon rep since wei don't need berkeley db
apt-get -y -qq update
echo "done."
echo -n "Installing BU prereq via apt-get ..."
sudo apt-get -qq -y install curl > /dev/null
echo " done."
}
create_swap () {
echo -n "Creating Swap ... "
SWAP_SIZE=`free -m | grep Mem | awk '{print $2}'`
rm -f /swapfile
fallocate -l ${SWAP_SIZE}M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
echo "done."
}
cloning () {
echo "Cloning Bitcoin BU ... "
sudo -u $USR bash <<-EOH
cd /home/$USR
mkdir -p ./src && cd ./src
git clone $BU_REPO BU > /dev/null
cd BU
git checkout $BU_BRANCH
EOH
echo "done."
}
compiling () {
echo -n "Compiling ... "
sudo -u $USR bash <<-EOH
cd /home/$USR/src/BU
./autogen.sh
./configure --without-gui --without-upnp --disable-tests --disable-wallet --disable-zmq
make -j$(grep -c '^processor' /proc/cpuinfo)
EOH
cd /home/$USR/src/BU
make install
echo " done."
}
downloading () {
echo -n "Downloading BU ... "
tmp_dir="/tmp/`< /dev/urandom tr -dc A-Za-z0-9 | head -c10`"
mkdir $tmp_dir
cd $tmp_dir
# TODO use a switch to explictly list all support arch
if [ "$(uname -m)" == "x86_64" ]; then
bu_name="bu64.tar.gz"
bu_url=$BU_URL64
bu_sum=$BU_SUM64
else
bu_name="bu32.tar.gz"
bu_url=$BU_URL32
bu_sum=$BU_SUM32
fi;
curl -s -L --max-redirs 2 -o $bu_name $bu_url
echo " done."
echo -n "Verify check sum ... "
bu_real_sum=`sha256sum $bu_name | awk '{print $1}'`
if [ "$bu_real_sum" != "$bu_sum" ]; then
echo "SHA256 mismatch! Aborting!"
echo "Expected archive sha256 checksum is $bu_sum, whereas this what we got: $bu_real_sum"
fi;
echo " done."
echo -n "Installing in /usr/local/bin ... "
tmp_path=`tar tf $bu_name | head -n1 | awk -F '/' '{print $1}'`
tar xf $bu_name
cp $tmp_path/bin/* /usr/local/bin
rm -rf $tmp_path
echo " done."
}
setting_up () {
echo -n "Setting up bitcoind datadir and conf file ..."
echo " done."
echo -n "Creating config ... "
su $USR -c "mkdir -p ${BU_HOME}/.bitcoin"
config="${BU_HOME}/.bitcoin/bitcoin.conf"
su $USR -c "touch $config"
echo "server=1" > $config
echo "daemon=1" >> $config
echo "checkblocks=7" >> $config
echo "logtimemicros=1" >> $config
echo "connections=40" >> $config
echo "dbcache=200" >> $config
# rpc credentials
randUser=`< /dev/urandom tr -dc A-Za-z0-9 | head -c30`
randPass=`< /dev/urandom tr -dc A-Za-z0-9 | head -c30`
echo "rpcuser=$randUser" >> $config
echo "rpcpassword=$randPass" >> $config
# set prune amount to size of `/` 70% free space (and then by /1000 to turn KB to MB) => ~ /1429
if [ "$PRUNE" == "1" ]; then
echo "prune="$(expr $(df | grep '/$' | tr -s ' ' | cut -d ' ' -f 4) / 1429) >> $config # safe enough for now
fi;
chown $USR.$USR -R $BU_HOME/.bitcoin
echo "done."
}
start_at_boot () {
echo -n "Setting bitcoind to start at boot ... "
LN=`wc -l /etc/rc.local | awk '{print $1}'`
BU_INIT="su ${USR} -c '/usr/local/bin/bitcoind -datadir=${BU_HOME}/.bitcoin -daemon'"
sed -i "${LN}i $BU_INIT" /etc/rc.local
echo "done."
}
### main
# mandatory checks
check_root_sudo
check_tmux_screen
check_user_exists
# main
if [ "$CR_SWAP" == "1" ]; then
create_swap
fi;
if [ "$DW_MODE" == "0" ]; then
install_prereq_comp
cloning
compiling
else
install_prereq_down
downloading
fi;
# starting box configuration
setting_up
start_at_boot
# sync the blockchain this will take a long while
if [ "$SYNC" == "1" ]; then
sync_chain
fi;
echo "Finished."
if [ "$REBOOT" == "1" ]; then
echo "Rebooting now ..."
reboot
else
echo "To start your bitcoind: sudo su ${USR} -c '/usr/local/bin/bitcoind -datadir=${BU_HOME}/.bitcoin -daemon'"
fi