forked from Unimatrix0/update_ombi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_ombi.sh
364 lines (340 loc) · 11.2 KB
/
update_ombi.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
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
#!/bin/bash
####################################
## update_ombi systemd script ##
## GitHub: Unimatrix0/update_ombi ##
####################################
####################################
## Override default settings by ##
## creating update_ombi.conf in ##
## the same directory that the ##
## script is running in and set ##
## the required variables there ##
####################################
## The systemd unit for Ombi ##
ombiservicename="ombi"
## The update_ombi log file ##
logfile="/var/log/ombiupdater.log"
####################################
## The remaining variables only ##
## need to be set or overridden ##
## if the script is unable to ##
## parse the ombi.service file ##
## for the correct settings ##
####################################
## !! This should never happen !! ##
####################################
## The service file's full path ##
ombiservicefile="/etc/systemd/system/$ombiservicename.service"
## Ombi install directory ##
defaultinstalldir="/opt/Ombi"
## User and Group Ombi runs as ##
defaultuser="ombi"
defaultgroup="nogroup"
defaultip="127.0.0.1"
defaultport="5000"
## Level of verbosity ##
## By default, none ##
declare -i verbosity=-1
############################################
## Do not modify anything below this line ##
## unless you know what you are doing ##
############################################
name="update_ombi"
version="1.3.00"
SECONDS=0
arch=$(uname -m)
archshort=${arch:0:3}
while [ $# -gt 0 ]; do
case "$1" in
--verbosity|-v=*)
shift
if [[ ${1#*=} =~ ^-?[0-8]$ ]]; then
verbosity="${1#*=}"
else
printf "****************************\n"
printf "* Error: Invalid verbosity.*\n"
printf "****************************\n"
exit 1
fi
;;
--force|-f=*)
shift
if [[ ${1#*=} =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
force="${1#*=}"
else
printf "****************************\n"
printf "* Error: Invalid version. *\n"
printf "****************************\n"
exit 1
fi
;;
*)
printf "****************************\n"
printf "* Error: Invalid argument. *\n"
printf "****************************\n"
exit 1
esac
shift
done
declare -A LOG_LEVELS=([-1]="none" [0]="emerg" [1]="alert" [2]="crit" [3]="err" [4]="warning" [5]="notice" [6]="info" [7]="debug" [8]="trace")
function .log () {
local LEVEL=${1}
shift
if [[ $verbosity =~ ^-?[0-8]$ ]]; then
if [ $verbosity -ge $LEVEL ]; then
echo "[$(date '+%H:%M:%S')] [${LOG_LEVELS[$LEVEL]}]" "$@"
fi
fi
if [ $verbosity -eq 8 ] || [ $LEVEL -ne 8 ]; then
echo "[$(date '+%Y-%m-%d %H:%M:%S %Z' -u)] [${LOG_LEVELS[$LEVEL]}]" "$@" >> $logfile
fi
}
unzip-strip() (
local zip=$1
local dest=${2:-.}
local temp=$(mktemp -d) && tar -zxf "$zip" -C "$temp" && mkdir -p "$dest" &&
shopt -s dotglob && local f=("$temp"/*) &&
if (( ${#f[@]} == 1 )) && [[ -d "${f[0]}" ]] ; then
cp -r "$temp"/*/* "$dest"
else
cp -r "$temp"/* "$dest"
fi && rm -rf "${temp:?}"/* "$temp"
)
# Import any custom config to override the defaults, if necessary
configfile="$(dirname $0)/update_ombi.conf"
if [ -e $configfile ]; then
source $configfile > /dev/null 2>&1
.log 6 "Script config file found...parsing..."
if [ $? -ne 0 ] ; then
.log 3 "Unable to use config file...using defaults..."
else
.log 6 "Parsed config file"
fi
else
.log 6 "No config file found...using defaults..."
fi
.log 6 "$name v$version"
.log 6 "Verbosity level: [${LOG_LEVELS[$verbosity]}]"
scriptuser=$(whoami)
.log 7 "Update script running as: $scriptuser"
if [ -e $ombiservicefile ]; then
.log 6 "Ombi service file for systemd found...parsing..."
parseresults="Parsing complete: "
ombiservice=$(<$ombiservicefile)
installdir=$(grep -Po '(?<=WorkingDirectory=)(\S|(?<=\\)\s)+' <<< "$ombiservice")
if [ -n "${installdir}" ]; then
parseresults+="InstallDir: $installdir, "
fi
user=$(grep -Po '(?<=User=)(\w+)' <<< "$ombiservice")
if [ -n "${user}" ]; then
parseresults+="User: $user, "
fi
group=$(grep -Po '(?<=Group=)(\w+)' <<< "$ombiservice")
if [ -n "${group}" ]; then
parseresults+="Group: $group, "
fi
url=$(grep -Po '(?<=\-\-host )(http://.+)$' <<< "$ombiservice")
ip=$(grep -Po '(?<=http://)((\d{1,3}\.){3}\d{1,3})(?=:)' <<< "$url")
if [ -n "${ip}" ]; then
parseresults+="IP: $ip, "
fi
port=$(grep -Po '(?<=:)(\d+)$' <<< "$url")
if [ -n "${port}" ]; then
parseresults+="Port: $port "
fi
parseresults="${parseresults// / }"
parseresults="${parseresults/%, /}"
.log 6 "$parseresults"
fi
if [ -z "${installdir}" ]; then
.log 5 "InstallDir not parsed...setting to default: $defaultinstalldir"
installdir="$defaultinstalldir"
fi
if [ -z "${user}" ]; then
.log 5 "User not parsed...setting to default: $defaultuser"
user="$defaultuser"
fi
if [ -z "${group}" ]; then
.log 5 "Group not parsed...setting to default: $defaultgroup"
group="$defaultgroup"
fi
if [ -z "${ip}" ]; then
.log 5 "IP not parsed or set as \"*\"...setting to default: $defaultip"
ip="$defaultip"
fi
if [ -z "${port}" ]; then
.log 5 "Port not parsed...setting to default: $defaultport"
port="$defaultport"
fi
.log 6 "Downloading Ombi update..."
if [ "$archshort" = 'arm' ]; then
filename='linux-arm.tar.gz'
else
filename='linux.tar.gz'
fi
declare -i i=1
declare -i j=5
while [ $i -le $j ]
do
.log 6 "Checking for latest version"
json=$(curl -sL http://$ip:$port/api/v1/Update/develop)
.log 8 "json: $json"
latestversion=$(grep -Po '(?<="updateVersionString":")([^"]+)' <<< "$json")
.log 7 "latestversion: $latestversion"
if [ -n "$force" ]; then
latestversion=$force
.log 7 "forcing version: $latestversion"
fi
json=$(curl -sL https://ci.appveyor.com/api/projects/tidusjar/requestplex/build/$latestversion)
.log 8 "json: $json"
jobId=$(grep -Po '(?<="jobId":")([^"]+)' <<< "$json")
.log 7 "jobId: $jobId"
version=$(grep -Po '(?<="version":")([^"]+)' <<< "$json")
.log 7 "version: $version"
if [ $latestversion != $version ]; then
.log 2 "Build version does not match expected version"
exit 1
fi
.log 6 "Latest version: $version...determining expected file size..."
size=$(curl -sL https://ci.appveyor.com/api/buildjobs/$jobId/artifacts | grep -Po '(?<="'$filename'","type":"File","size":)(\d+)')
.log 7 "size: $size"
if [ -e $size ]; then
if [ $i -lt $j ]; then
.log 3 "Unable to determine update file size...[attempt $i of $j]"
else
.log 2 "Unable to determine update file size...[attempt $i of $j]...Bailing!"
exit 2
fi
i+=1
continue
elif [[ $size =~ ^-?[0-9]+$ ]]; then
.log 6 "Expected file size: $size...downloading..."
break
else
.log 1 "Invalid file size value...bailing!"
exit 99
fi
done
tempdir=$(mktemp -d)
file="$tempdir/ombi_$version.tar.gz"
wget --quiet --show-progress -O $file "https://ci.appveyor.com/api/buildjobs/$jobId/artifacts/$filename"
.log 6 "Version $version downloaded...checking file size..."
if [ "$(wc -c < $file)" != $size ]; then
.log 3 "Downloaded file size does not match expected file size...bailing!"
exit 2
fi
.log 6 "File size validated...checking Ombi service status..."
declare -i running=0
if [ "`systemctl is-active $ombiservicename`" == "active" ]; then
running=1
.log 6 "Ombi is active...attempting to stop..."
declare -i i=1
declare -i j=5
while [ $i -le $j ]
do
if [ $scriptuser = "root" ]; then
systemctl stop $ombiservicename.service > /dev/null 2>&1
else
sudo systemctl stop $ombiservicename.service > /dev/null 2>&1
fi
if [ $? -ne 0 ] || [ "`systemctl is-active $ombiservicename`" == "active" ] ; then
if [ $i -lt $j ]; then
.log 3 "Failed to stop Ombi...[attempt $i of $j]"
sleep 1
else
.log 2 "Failed to stop Ombi...[attempt $i of $j]...Bailing!"
exit 2
fi
i+=1
continue
elif [ "`systemctl is-active $ombiservicename`" == "inactive" ]; then
.log 6 "Ombi stopped...installing update..."
break
else
.log 1 "Unknown error...bailing!"
exit 99
fi
done
else
.log 6 "Ombi is not active...installing update..."
fi
declare -i timestamp=$(date +%s)
.log 6 "Backing up Ombi.db to Ombi.db.$timestamp"
cp ${installdir}Ombi.db ${installdir}Ombi.db.$timestamp
unzip-strip $file $installdir
.log 6 "Update installed...setting ownership..."
chown -R $user:$group $installdir
if [ $running -eq 1 ]; then
.log 6 "Ownership set...starting Ombi..."
declare -i i=1
declare -i j=5
while [ $i -le $j ]
do
if [ $scriptuser = "root" ]; then
systemctl start $ombiservicename.service > /dev/null 2>&1
else
sudo systemctl start $ombiservicename.service > /dev/null 2>&1
fi
if [ $? -ne 0 ] || [ "`systemctl is-active $ombiservicename`" != "active" ] ; then
if [ $i -lt $j ]; then
.log 3 "Failed to start Ombi...[attempt $i of $j]"
sleep 1
else
.log 2 "Failed to start Ombi...[attempt $i of $j]...Bailing!"
exit 3
fi
i+=1
continue
elif [ "`systemctl is-active $ombiservicename`" == "active" ]; then
.log 6 "Ombi started...waiting for confirmation..."
declare -i k=1
declare -i l=5
while [ $k -le $l ]
do
sleep 5
curl -sIL $ip:$port > /dev/null 2>&1
if [ $? -ne 0 ]; then
if [ $k -lt $l ]; then
.log 4 "Ombi startup not confirmed...waiting 5 seconds...[attempt $k of $l]"
else
.log 2 "Ombi startup not confirmed...[attempt $k of $l]...bailing!"
exit 4
fi
k+=1
continue
else
.log 6 "Ombi startup confirmed...cleaning up..."
break
fi
done
break
else
.log 1 "Unknown error...bailing!"
exit 99
fi
done
else
.log 6 "Ownership set...not starting Ombi"
fi
.log 6 "Cleaning up..."
rm -rf "${tempdir:?}"/* "$tempdir"
declare -i elapsedtime=$SECONDS
declare -i minutes=0
declare -i seconds=0
if [ $elapsedtime -ge 60 ]; then
minutes=$(($elapsedtime / 60))
fi
seconds=$(($elapsedtime % 60))
if [ $minutes -ge 2 ]; then
duration="$minutes minutes"
elif [ $minutes -eq 1 ]; then
duration="$minutes minute"
fi
if [ $seconds -ge 2 ]; then
duration+=" $seconds seconds"
elif [ $seconds -eq 1 ]; then
duration+=" $seconds second"
fi
durationmsg="Update complete...elapsed time $duration..."
durationmsg="${durationmsg// / }"
.log 6 "$durationmsg"