forked from terracoin/terracoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trc-updater
executable file
·252 lines (221 loc) · 5.87 KB
/
trc-updater
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
#!/bin/bash
install_vers=0.12.2.5
downloaded=0
tmpdir=$(mktemp -d)
if [ $(uname -s) != "Linux" ]; then
echo "This script is only for Linux"
exit
fi
usecurl=1
if ! which curl > /dev/null; then
usecurl=0
if ! which wget > /dev/null; then
echo "Could not find curl or wget!"
exit
fi
fi
if ! which sudo > /dev/null; then
echo "Could not find sudo!"
exit
fi
arch=$(uname -m)
if [ $arch != "x86_64" ]; then
arch="i686-pc";
fi
vers_major=$(echo ${install_vers} | cut -d. -f1)
vers_minor=$(echo ${install_vers} | cut -d. -f2)
vers_micro=$(echo ${install_vers} | cut -d. -f3)
vers_build=$(echo ${install_vers} | cut -d. -f4)
printf -v current_vers "%02d%02d%02d" $vers_minor $vers_micro $vers_build
vers="${vers_major}.${vers_minor}.${vers_micro}"
url=https://terracoin.io/bin/terracoin-core-${install_vers}/terracoin-${vers}-${arch}-linux-gnu.tar.gz
myuid=$(id -u)
pwd=$(pwd)
pids=$(ps auxwww | grep terracoind | grep -v grep | grep -v testnet | awk {'print $2'})
if [ $(echo ${pids} | wc -w) -lt 1 ]; then
echo "terracoind not found running!"
exit
fi
force=0
yes=0
for param in "$@"; do
case $param in
"-f")
echo "Enabling downgrading"
force=1
;;
"-y")
echo "Enabling assume yes"
yes=1
;;
esac
done
for pid in ${pids}; do
tmpforce=${force}
terracoincli_found=0
terracoind_uid=0
terracoincli_uid=0
runas=0
runasun="root"
if [ ! -d /proc/${pid} ]; then
echo "terracoind process not found!"
continue
fi
runas=$(stat -c "%u" /proc/${pid}/cmdline)
runasun=$(id -nu ${runas})
usesudo=0
if [ ${myuid} -ne ${runas} ]; then
usesudo=1
fi
if [ ${usesudo} -eq 1 ] && [ ${myuid} -ne 0 ]; then
echo "You do not have the required permissions, please run with sudo"
continue
fi
cmdprefix=""
if [ ${usesudo} -eq 1 ]; then
cmdprefix="sudo -u #${runas} "
fi
newstart=0
cmd=$(cat /proc/${pid}/cmdline | tr '\000' ' ')
cleancmd=$(echo ${cmd} | sed -e 's/-reindex//g')
reindexcmd="${cleancmd} -reindex"
exe=$(${cmdprefix}realpath /proc/${pid}/exe)
cwd=$(${cmdprefix}realpath /proc/${pid}/cwd)
if ! [[ ${exe} =~ terracoind$ ]]; then
continue;
fi
terracoind_dir=$(dirname ${exe})
if [ ! -d ${terracoind_dir} ]; then
echo "terracoind Install directory not found!"
continue
fi
echo "terracoind install directory... ${terracoind_dir}"
read -p "Replace terracoin in ${terracoind_dir}? " -n 1 -r
echo
if ! [[ $REPLY =~ ^[yY]$ ]]; then
continue
fi
echo -n "Looking for terracoin-cli... "
if [ -f ${terracoind_dir}/terracoin-cli ]; then
terracoincli_found=1
terracoincli_dir=${terracoind_dir}
elif [ -f $(which terracoin-cli) ]; then
terracoincli_found=1
terracoincli_dir=$(dirname $(which terracoin-cli))
fi
if [ ${terracoincli_found} -eq 1 ]; then
echo "OK"
else
echo "FAILED"
fi
terracoind_vers=0
terracoindprefix=""
terracoincliprefix=""
terracoind_uid=$(stat -c "%u" ${terracoind_dir}/terracoind)
if [ ${terracoind_uid} -ne ${myuid} ]; then
terracoindprefix="sudo -u #${terracoind_uid} "
fi
if [ ${terracoincli_found} -eq 1 ]; then
terracoincli_uid=$(stat -c "%u" ${terracoincli_dir}/terracoin-cli)
if [ ${terracoincli_uid} -ne ${myuid} ]; then
terracoincliprefix="sudo -u #${terracoincli_uid} "
fi
terracoind_vers=$(${cmdprefix}${terracoincli_dir}/terracoin-cli getinfo 2>/dev/null | grep '"version"' | cut -d: -f2 | tr -d " " | tr -d ",")
if [ ${terracoind_vers} -eq ${current_vers} ]; then
echo "terracoind already at current version!"
continue
elif [ ${terracoind_vers} -gt ${current_vers} ] && [ ${force} -ne 1 ]; then
echo "terracoind already at newer then requested, to force please rerun with -f"
continue
elif [ ${terracoind_vers} -gt ${current_vers} ] && [ ${force} -eq 1 ]; then
echo "terracoind is newer, downgrading"
fi
fi
if [ ${terracoind_vers} -eq 0 ]; then
read -p "Could not detect current running version, continue?" -n 1 -r
echo
if ! [[ $REPLY =~ ^[yY]$ ]]; then
continue
fi
read -p "Add reindex to be safe?" -n 1 -r
echo
if [[ $REPLY =~ ^[yY]$ ]]; then
tmpforce=1
fi
elif [ ${terracoind_vers} -lt 120108 ]; then
tmpforce=1
fi
if [ ${downloaded} -eq 0 ]; then
chmod 777 ${tmpdir}
echo -n "Downloading new version... "
if [ $usecurl -eq 1 ]; then
curl ${url} --output ${tmpdir}/terracoin.tar.gz --silent
else
wget ${url} -O ${tmpdir}/terracoin.tar.gz --quiet
fi
if [ $? -ne 0 ]; then
echo "FAILED ($?)"
rm -rf ${tmpdir}
exit
fi
echo "OK"
echo -n "Extracting files... "
tar xzf ${tmpdir}/terracoin.tar.gz -C ${tmpdir}/ --strip-components=1
if [ $? -ne 0 ]; then
echo "FAILED ($?)"
rm -rf ${tmpdir}
exit
fi
echo "OK"
chmod 777 ${tmpdir}/bin
downloaded=1
fi
echo -n "Stopping terracoind... "
if [ ${terracoincli_found} -eq 1 ]; then
kill -INT ${pid} > /dev/null
else
${cmdprefix}${terracoincli_dir}/terracoin-cli stop > /dev/null
fi
if [ $? -ne 0 ]; then
echo "FAILED ($?)"
continue
fi
while [ -d /proc/${pid} ]; do
sleep 1
done
echo "OK"
echo -n "Updating terracoind... "
${terracoindprefix}cp ${tmpdir}/bin/terracoind ${terracoind_dir} 2>/dev/null
if [ $? -ne 0 ]; then
echo "FAILED ($?), this is likely do to having 2 copies running on the same system, close one and rerun!"
else
newstart=1
echo "OK"
if [ ${terracoincli_found} -eq 1 ]; then
echo -n "Updating terracoin-cli... "
${terracoincliprefix}cp ${tmpdir}/bin/terracoin-cli ${terracoincli_dir}
if [ $? -ne 0 ]; then
echo "FAILED ($?), you should do this manually"
else
echo "OK"
fi
fi
fi
echo -n "Restarting terracoind... "
if [ ${newstart} -eq 1 ]; then
if [ ${tmpforce} -eq 1 ] && [[ ${cmd} != *"-reindex"* ]]; then
cmd=${reindexcmd}
echo -n "(will take longer as reindex is enabled)... "
else
cmd=${cleancmd}
fi
fi
su - ${runasun} -c "cd ${cwd} && ${cmd} &" > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "FAILED ($?), Please restart it by hand"
continue
fi
echo "OK"
done
rm -rf ${tmpdir}
echo "Done"