-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdate-plex-in-docker.bash
executable file
·679 lines (635 loc) · 26.4 KB
/
update-plex-in-docker.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#!/usr/bin/env bash
#############################
## Issues ##
#############################
# If you experience any issues, please let me know here:
# https://github.com/goose-ws/bash-scripts
# These scripts are purely a passion project of convenience for myself, so pull requests welcome :)
#############################
## About ##
#############################
# This script serves to update instaces of Plex running in docker containers. If you are using the official Plex image
# then updates are downloaded and installed within the container on each start, rather than by updating the image.
# This script works around that by using API calls, curl, and jq to parse the current version of a Plex server and
# compare it to the latest available versions offered on Plex's website.
#############################
## Changelog ##
#############################
# 2025-01-06
# Added Discord messaging (See updated .env file)
# Updated printOutput verbosity levels (See updated .env file)
# Updated some verbiage
# 2024-07-29
# Improved some wording
# 2024-01-27
# Remove codecs from inside the container (Removed from .env)
# Added support for ChuckPA's database repair tool
# Added support for when a container has multiple networks attached (Multiple IP addresses)
# Updated the logic for sending Telegram messages to make sure the bot can authenticate to each channel
# Added support for super groups, silent messages (See updated .env file)
# Added support for sending error messages via telegram (See updated .env file)
# Added a changelog message for when users update
# 2023-10-15
# Fixed the lockfile logic
# 2023-10-13
# Updated some spacing, and modified telegram send message command to allow for multiple channels
# Added a disclaimer for where to file issues above the "About" section
# 2023-05-25
# Added functionality to self determine container IP address
# Added config options for verbosity
# Both initiated via PR from @ndoty
# 2023-03-16
# Rewrite of old script, removal of old script, and initial commit of new script
#############################
## Installation ##
#############################
# 1. Download the script .bash file somewhere safe
# 2. Download the script .env file somewhere safe
# 3. Edit the .env file to your liking
# 4. Set the script to run on an hourly cron job, or whatever your preference is
#############################
## Sanity checks ##
#############################
if ! [ -e "/bin/bash" ]; then
echo "This script requires Bash"
exit 255
fi
if [[ -z "${BASH_VERSINFO[0]}" || "${BASH_VERSINFO[0]}" -lt "4" ]]; then
echo "This script requires Bash version 4 or greater"
exit 255
fi
depArr=("awk" "curl" "docker" "jq" "md5sum" "printf" "rm" "xmllint")
depFail="0"
for i in "${depArr[@]}"; do
if [[ "${i:0:1}" == "/" ]]; then
if ! [[ -e "${i}" ]]; then
echo "${i}\\tnot found"
depFail="1"
fi
else
if ! command -v "${i}" > /dev/null 2>&1; then
echo "${i}\\tnot found"
depFail="1"
fi
fi
done
if [[ "${depFail}" -eq "1" ]]; then
echo "Dependency check failed"
exit 255
fi
realPath="$(realpath "${0}")"
scriptName="$(basename "${0}")"
lockFile="${realPath%/*}/.${scriptName}.lock"
# URL of where the most updated version of the script is
updateURL="https://raw.githubusercontent.com/goose-ws/bash-scripts/main/update-plex-in-docker.bash"
# For ease of printing messages
lineBreak=$'\n\n'
#############################
## Lockfile ##
#############################
if [[ -e "${lockFile}" ]]; then
if kill -s 0 "$(<"${lockFile}")" > /dev/null 2>&1; then
echo "${0##*/} :: $(date "+%Y-%m-%d %H:%M:%S") :: [1] Lockfile present, exiting"
exit 0
else
echo "${0##*/} :: $(date "+%Y-%m-%d %H:%M:%S") :: [1] Removing stale lockfile for PID $(<"${lockFile}")"
fi
fi
echo "${$}" > "${lockFile}"
#############################
## Standard Functions ##
#############################
function printOutput {
case "${1}" in
0) logLevel="[reqrd]";; # Required
1) logLevel="[error]";; # Errors
2) logLevel="[warn] ";; # Warnings
3) logLevel="[info] ";; # Informational
4) logLevel="[verb] ";; # Verbose
5) logLevel="[DEBUG]";; # Super Secret Debug Mode
esac
if [[ "${1}" -le "${outputVerbosity}" ]]; then
echo "${0##*/} :: $(date "+%Y-%m-%d %H:%M:%S") :: ${logLevel} ${2}"
fi
if [[ "${1}" -le "1" ]]; then
errorArr+=("${2}")
fi
}
function removeLock {
if rm -f "${lockFile}"; then
printOutput "4" "Lockfile removed"
else
printOutput "1" "Unable to remove lockfile"
fi
}
function badExit {
removeLock
if [[ -z "${2}" ]]; then
printOutput "0" "Received signal: ${1}"
exit "255"
else
if [[ "${telegramErrorMessages,,}" =~ ^(yes|true)$ ]]; then
sendTelegramMessage "<b>${0##*/}</b>${lineBreak}${lineBreak}Error Code ${1}:${lineBreak}${2}" "${telegramErrorChannel}"
fi
printOutput "1" "${2}"
exit "${1}"
fi
}
function cleanExit {
removeLock
exit 0
}
function sendDiscordMessage {
# Message to send should be passed as functional positional parameter #1
if [[ -z "${discordWebhook}" ]]; then
printOutput "5" "No Discord Webhook URL provided, unable to send Discord message"
return 0
fi
# Make sure our message is not blank
if [[ -z "${1}" ]]; then
printOutput "1" "No message passed to send to Discord"
return 1
fi
# Send the plain text message
# Positional parameter 2 is the URL
# Positional parameter 3 is the text
callCurlPost "${discordWebhook}" "${1}"
}
function callCurlPost {
# URL to call should be ${1}
if [[ -z "${1}" ]]; then
printOutput "1" "No input URL provided for POST"
return 1
fi
printOutput "5" "Issuing curl command [curl -skL -X POST \"${1}\"]"
curlOutput="$(curl -skL -X POST "${1}" 2>&1)"
curlExitCode="${?}"
if [[ "${curlExitCode}" -ne "0" ]]; then
printOutput "1" "Curl returned non-zero exit code ${curlExitCode}"
while read -r i; do
printOutput "1" "Output: ${i}"
done <<<"${curlOutput}"
return 1
fi
}
function sendTelegramMessage {
# Message to send should be passed as function positional parameter #1
# We can pass an "Admin channel" as positional parameter #2 for the case of sending error messages
# Let's check to make sure our messaging credentials are valid
skipTelegram="0"
telegramOutput="$(curl -skL "https://api.telegram.org/bot${telegramBotId}/getMe" 2>&1)"
curlExitCode="${?}"
if [[ "${curlExitCode}" -ne "0" ]]; then
printOutput "1" "Curl to Telegram to check Bot ID returned a non-zero exit code: ${curlExitCode}"
skipTelegram="1"
elif [[ -z "${telegramOutput}" ]]; then
printOutput "1" "Curl to Telegram to check Bot ID returned an empty string"
skipTelegram="1"
else
printOutput "3" "Curl exit code and null output checks passed"
fi
if [[ "${skipTelegram}" -eq "0" ]]; then
if ! [[ "$(jq -M -r ".ok" <<<"${telegramOutput,,}")" == "true" ]]; then
printOutput "1" "Telegram bot API check failed"
else
printOutput "4" "Telegram bot API key authenticated: $(jq -M -r ".result.username" <<<"${telegramOutput}")"
for chanId in "${telegramChannelId[@]}"; do
if [[ -n "${2}" ]]; then
chanId="${2}"
fi
telegramOutput="$(curl -skL "https://api.telegram.org/bot${telegramBotId}/getChat?chat_id=${telegramChannelId}")"
curlExitCode="${?}"
if [[ "${curlExitCode}" -ne "0" ]]; then
printOutput "1" "Curl to Telegram to check channel returned a non-zero exit code: ${curlExitCode}"
elif [[ -z "${telegramOutput}" ]]; then
printOutput "1" "Curl to Telegram to check channel returned an empty string"
elif [[ "$(jq -M -r ".ok" <<<"${telegramOutput,,}")" == "true" ]]; then
printOutput "4" "Telegram channel authenticated: $(jq -M -r ".result.title" <<<"${telegramOutput}")"
telegramOutput="$(curl -skL --data-urlencode "text=${1}" "https://api.telegram.org/bot${telegramBotId}/sendMessage?chat_id=${chanId}&parse_mode=html" 2>&1)"
curlExitCode="${?}"
if [[ "${curlExitCode}" -ne "0" ]]; then
printOutput "1" "Curl to Telegram returned a non-zero exit code: ${curlExitCode}"
elif [[ -z "${telegramOutput}" ]]; then
printOutput "1" "Curl to Telegram to send message returned an empty string"
else
printOutput "5" "Curl exit code and null output checks passed"
# Check to make sure Telegram returned a true value for ok
if ! [[ "$(jq -M -r ".ok" <<<"${telegramOutput}")" == "true" ]]; then
printOutput "1" "Failed to send Telegram message:"
printOutput "1" ""
while read -r i; do
printOutput "1" "${i}"
done < <(jq . <<<"${telegramOutput}")
printOutput "1" ""
else
printOutput "3" "Telegram message sent successfully"
fi
fi
else
printOutput "1" "Telegram channel check failed"
fi
if [[ -n "${2}" ]]; then
break
fi
done
fi
fi
}
function callCurl {
# URL to call should be $1
curlOutput="$(curl -skL -H "Authorization: Bearer ${apiKey}" "${1}" 2>&1)"
curlExitCode="${?}"
if [[ "${curlExitCode}" -ne "0" ]]; then
printOutput "1" "Curl returned non-zero exit code ${curlExitCode}"
while read -r i; do
printOutput "1" "Output: ${i}"
done <<<"${curlOutput}"
badExit "1" "Bad curl output"
fi
}
#############################
## Unique Functions ##
#############################
function getNowPlaying {
# TODO: Replace this with a 'yq' function
nowPlaying="$(curl -skL -m 15 "${plexAdd}/status/sessions?X-Plex-Token=${plexAccessToken}" | grep -Eo "size=\"[[:digit:]]+\"")"
nowPlaying="${nowPlaying#*size=\"}"
nowPlaying="${nowPlaying%%\"*}"
printOutput "3" "Now playing count: ${nowPlaying}"
}
#############################
## Signal Traps ##
#############################
trap "badExit SIGINT" INT
trap "badExit SIGQUIT" QUIT
trap "badExit SIGKILL" KILL
#############################
## Positional parameters ##
#############################
# We can run the positional parameter options without worrying about lockFile
case "${1,,}" in
"-h"|"--help")
echo "-h --help Displays this help message"
echo ""
echo "-u --update Self update to the most recent version"
exit 0
;;
"-u"|"--Update")
oldStartLine="0"
while read -r i; do
if [[ "${i}" == "## Changelog ##" ]]; then
oldStartLine="1"
elif [[ "${oldStartLine}" -eq "1" ]]; then
oldStartLine="2"
elif [[ "${oldStartLine}" -eq "2" ]]; then
oldStartLine="${i}"
break
fi
done < "${0}"
if curl -skL "${updateURL}" -o "${0}"; then
if chmod +x "${0}"; then
printOutput "0" "Update complete"
newStartLine="0"
while read -r i; do
if [[ "${newStartLine}" -eq "2" ]]; then
if [[ "${i}" == "${oldStartLine}" ]]; then
break
fi
if [[ "${i:2:1}" =~ ^[0-9]$ ]]; then
changelogArr+=(" ${i#\#}")
else
changelogArr+=(" - ${i#\#}")
fi
elif [[ "${newStartLine}" -eq "1" ]]; then
newStartLine="2"
elif [[ "${i}" == "## Changelog ##" ]]; then
newStartLine="1"
fi
done < <(curl -skL "${updateURL}")
printOutput "0" "Changelog:"
for i in "${changelogArr[@]}"; do
printOutput "0" "${i}"
done
cleanExit
else
badExit "2" "Update downloaded, but unable to \`chmod +x\`"
fi
else
badExit "3" "Unable to download Update"
fi
;;
esac
#############################
## Initiate .env file ##
#############################
if [[ -e "${realPath%/*}/${scriptName%.bash}.env" ]]; then
source "${realPath%/*}/${scriptName%.bash}.env"
else
badExit "4" "Error: \"${realPath%/*}/${scriptName%.bash}.env\" does not appear to exist"
fi
varFail="0"
# Standard checks
if ! [[ "${updateCheck,,}" =~ ^(yes|no|true|false)$ ]]; then
echo "Option to check for updates not valid. Assuming no."
updateCheck="No"
fi
if ! [[ "${outputVerbosity}" =~ ^[1-5]$ ]]; then
echo "Invalid output verbosity defined. Assuming level 1 (Errors only)"
outputVerbosity="1"
fi
# Config specific checks
if ! [[ "${repairDatabase,,}" =~ ^(yes|no|true|false)$ ]]; then
echo "Option to run database repair tool not valid. Assuming no."
repairDatabase="No"
fi
if [[ -z "${plexAccessToken}" ]]; then
echo "Please specify a 'plexAccessToken=\"\"'"
varFail="1"
fi
if [[ -z "${plexPort}" ]]; then
echo "Please specify a 'plexPort=\"\"'"
varFail="1"
elif ! [[ "${plexPort}" =~ ^[0-9]+$ ]]; then
echo "Please specify a numerical 'plexPort=\"\"'"
varFail="1"
elif ! [[ "${plexPort}" -ge "0" && "${plexPort}" -le "65535" ]]; then
echo "Please specify a valid 'plexPort=\"\"'"
varFail="1"
fi
if [[ -z "${plexScheme}" ]]; then
echo "Please specify a 'plexScheme=\"\"'"
varFail="1"
elif ! [[ "${plexScheme}" =~ ^https?$ ]]; then
echo "Please specify a valid 'plexScheme=\"\"'"
varFail="1"
fi
if [[ -z "${containerName}" ]]; then
echo "Please specify a 'containerName=\"\"'"
varFail="1"
fi
if [[ -z "${plexVersion}" ]]; then
echo "Please specify a 'plexVersion=\"\"'"
varFail="1"
else
if ! [[ "${plexVersion}" =~ (plexpass|beta|public) ]]; then
echo "Please specify a valid 'plexVersion=\"\"'"
varFail="1"
fi
fi
# TODO: Automate this variable via 'uname'
if [[ -z "${hostOS}" ]]; then
echo "Please specify a 'hostOS=\"\"'"
varFail="1"
else
case "${hostOS}" in
"1") hostOS="Windows";;
"2") hostOS="MacOS";;
"3") hostOS="Linux";;
"4") hostOS="FreeBSD";;
"5") hostOS="nas";;
"6") hostOS="Netgear";;
"7") hostOS="QNAP";;
"8") hostOS="unRAID";;
"9") hostOS="Drobo";;
"10") hostOS=" ASUSTOR";;
"11") hostOS=" Seagate";;
"12") hostOS=" Western Digital";;
"13") hostOS=" Western Digital (OS 3)";;
*) echo "Please specify a valid 'hostOS=\"\"'"; varFail="1";;
esac
fi
# Quit if failures
if [[ "${varFail}" -eq "1" ]]; then
badExit "5" "Please fix above errors"
fi
#############################
## Update check ##
#############################
if [[ "${updateCheck,,}" =~ ^(yes|true)$ ]]; then
newest="$(curl -skL "${updateURL}" | md5sum | awk '{print $1}')"
current="$(md5sum "${0}" | awk '{print $1}')"
if ! [[ "${newest}" == "${current}" ]]; then
printOutput "0" "A newer version is available"
# If our ${TERM} is dumb, we're probably running via cron, and should push a message to Telegram, if allowed
if [[ "${TERM,,}" == "dumb" ]]; then
if [[ "${telegramErrorMessages}" =~ ^(yes|true)$ ]]; then
sendTelegramMessage "[${0##*/}] An update is available" "${telegramErrorChannel}"
fi
fi
else
printOutput "4" "No new updates available"
fi
fi
#############################
## Payload ##
#############################
# If we're being invoked by cron, sleep for up to ${cronSleep}
if [[ -t 1 ]]; then
printOutput "5" "Script spawned by interactive terminal"
else
printOutput "4" "Script spawned by cron with PID [${$}]"
if [[ "${cronSleep}" =~ ^[0-9]+$ ]]; then
sleepTime="$(( RANDOM % cronSleep ))"
printOutput "3" "Sleeping for [${sleepTime}] seconds before continuing"
sleep "${sleepTime}"
fi
fi
# If using docker, we should ensure we have permissions to do so
if ! docker version > /dev/null 2>&1; then
badExit "6" "Do not appear to have permission to run on the docker socket (\`docker version\` returned non-zero exit code)"
fi
# Get the IP address of the Plex container
if [[ -z "${containerIp}" ]]; then
printOutput "3" "Attempting to automatically determine container IP address"
# Find the type of networking the container is using
unset containerNetworking
while read i; do
if [[ -n "${i}" ]]; then
containerNetworking+=("${i}")
fi
done < <(docker inspect -f '{{range $k, $v := .NetworkSettings.Networks}}{{println $k}}{{end}}' "${containerName}")
printOutput "4" "Container is utilizing ${#containerNetworking[@]} network type(s): ${containerNetworking[*]}"
for i in "${containerNetworking[@]}"; do
if [[ -z "${i}" ]]; then
printOutput "3" "No network type defined. Checking to see if networking is through another container."
# IP address returned blank. Is it being networked through another container?
containerIp="$(docker inspect "${containerName}" | jq -M -r ".[].HostConfig.NetworkMode")"
containerIp="${containerIp#\"}"
containerIp="${containerIp%\"}"
printOutput "4" "Network mode: ${containerIp%%:*}"
if [[ "${containerIp%%:*}" == "container" ]]; then
# Networking is being run through another container. So we need that container's IP address.
printOutput "4" "Networking routed through another container. Retrieving IP address."
containerIp="$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "${containerIp#container:}")"
else
printOutput "1" "Unable to determine networking type"
unset containerIp
fi
elif [[ "${i}" == "host" ]]; then
# Host networking, so we can probably use localhost
printOutput "4" "Networking type: ${i}"
containerIp="127.0.0.1"
else
# Something else. Let's see if we can get it via inspect.
printOutput "4" "Other networking type: ${i}"
containerIp="$(docker inspect "${containerName}" | jq -M -r ".[] | .NetworkSettings.Networks.${i}.IPAddress")"
fi
if [[ -z "${containerIp}" ]] || ! [[ "${containerIp}" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.([0-9]{1,3}|[0-9]/[0-9]{1,2})$ ]]; then
printOutput "1" "Unable to determine IP address via networking mode: ${i}"
else
printOutput "3" "Container IP address: ${containerIp}"
break
fi
done
if [[ -z "${containerIp}" ]] || ! [[ "${containerIp}" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.([0-9]{1,3}|[0-9]/[0-9]{1,2})$ ]]; then
badExit "7" "Unable to determine IP address"
fi
fi
# Build our address
plexAdd="${plexScheme}://${containerIp}:${plexPort}"
printOutput "5" "Server address: ${plexAdd}"
# Make sure our server is reachable, and we can check our version
callCurl "${plexAdd}/servers?X-Plex-Token=${plexAccessToken}"
myVer="$(grep -Ev "^<\?xml" <<<"${curlOutput}" | grep -Eo "version=\"([[:alnum:]]|\.|-)+\"")"
myVer="${myVer#*version=\"}"
myVer="${myVer%%\"*}"
if [[ "${myVer}" == "null" ]] || [[ -z "${myVer}" ]]; then
badExit "8" "Unable to parse local version"
else
printOutput "3" "Detected local version: ${myVer}"
fi
# Make sure we can check the latest version
callCurl "https://plex.tv/api/downloads/1.json?channel=${plexVersion}"
currVer="$(jq ".computer.${hostOS}.version" <<<"${curlOutput}")"
currVer="${currVer#\"}"
currVer="${currVer%\"}"
if [[ "${currVer}" == "null" ]] || [[ -z "${currVer}" ]]; then
badExit "9" "Unable to parse latest version"
else
printOutput "3" "Detected current version: ${currVer}"
fi
if [[ "${myVer}" == "${currVer}" ]]; then
printOutput "3" "Versions match, no update needed"
cleanExit;
fi
# If we've gotten this far, version strings do not match
myVer2="${myVer%-*}"
myVer2="${myVer2//./}"
currVer2="${currVer%-*}"
currVer2="${currVer2//./}"
if [[ "${myVer2}" -gt "${currVer2}" ]]; then
# We already have a version more recent than the current, probably a beta/Plex Pass version
printOutput "3" "Local version newer than current stable version."
cleanExit;
fi
# Is anyone playing anything?
getNowPlaying;
if [[ "${nowPlaying}" -ne "0" ]]; then
# At least one person is watching something
# We'll try again at the next cron run
printOutput "3" "Detected ${nowPlaying} users currently using Plex"
cleanExit;
fi
# Nobody is watching anything. Maybe someone was between episodes? Let's wait 1 minute and check.
printOutput "4" "Sleeping for 60 seconds before re-checking play status"
sleep 60
# Is anyone playing anything?
getNowPlaying;
if [[ "${nowPlaying}" -ne "0" ]]; then
# At least one person is watching something
# We'll try again at the next cron run
printOutput "3" "Detected ${nowPlaying} users currently using Plex"
cleanExit;
fi
# Nice, nobody's watching anything.
# Are we allowed to run the DB repair tool?
if [[ "${repairDatabase,,}" =~ ^(yes|true)$ ]]; then
printOutput "3" "Pulling newest copy of DB Repair Tool into container"
if docker exec "${containerName}" curl -skL "https://raw.githubusercontent.com/ChuckPa/PlexDBRepair/master/DBRepair.sh" -o "/root/db_repair_new.sh" > /dev/null 2>&1; then
toolVersion="$(docker exec "${containerName}" grep -m 1 "# Version:" "/root/db_repair_new.sh" 2>/dev/null | awk '{print $3}')"
toolDate="$(docker exec "${containerName}" grep -m 1 "# Date:" "/root/db_repair_new.sh" 2>/dev/null | awk '{print $3}')"
if [[ -n "${toolDate}" && -n "${toolVersion}" ]]; then
printOutput "4" "Newest copy of repair tool pulled"
printOutput "5" "Tool Version: ${toolVersion} | Tool Date: ${toolDate}"
if docker exec "${containerName}" chmod +x "/root/db_repair_new.sh" > /dev/null 2>&1; then
printOutput "5" "Tool set as executable successfully"
if [[ -n "${telegramBotId}" && -n "${telegramChannelId[0]}" ]]; then
dockerHost="$(</etc/hostname)"
printOutput "4" "Got hostname: ${dockerHost}"
eventText="<b>Plex Server Update for ${dockerHost%%.*}</b>${lineBreak}Stopping Plex Media Server for database maintenance and repair, and server upgrade"
printOutput "4" "Telegram messaging enabled -- Passing message to function"
sendTelegramMessage "${eventText}"
fi
if [[ -n "${discordWebhook}" ]]; then
dockerHost="$(</etc/hostname)"
printOutput "4" "Got hostname: ${dockerHost}"
eventText="**Plex Server Update for ${dockerHost%%.*}**${lineBreak}Stopping Plex Media Server for database maintenance and repair, and server upgrade"
printOutput "4" "Discord messaging enabled -- Passing message to function"
sendDiscordMessage "${eventText}"
fi
printOutput "3" "Initiating database repair -- This may take some time"
printOutput "4" "Begin repair tool output"
printOutput "4" "============================"
while read -r i; do
printOutput "4" "${i}"
done < <(docker exec "${containerName}" /root/db_repair_new.sh stop check auto exit 2>&1)
printOutput "4" "============================"
printOutput "4" "End of repair tool output"
else
printOutput "1" "Unable to set tool as executable -- Skipping database repair"
fi
else
printOutput "1" "Unable to validate tool version/date -- Skipping database repair"
fi
else
printOutput "1" "Unable to pull newest copy of repair tool -- Skipping database repair"
fi
else
if [[ -n "${telegramBotId}" && -n "${telegramChannelId[0]}" ]]; then
dockerHost="$(</etc/hostname)"
printOutput "4" "Got hostname: ${dockerHost}"
eventText="<b>Plex Server Update for ${dockerHost%%.*}</b>${lineBreak}Stopping Plex Media Server for server upgrade"
printOutput "4" "Telegram messaging enabled -- Passing message to function"
sendTelegramMessage "${eventText}"
fi
if [[ -n "${discordWebhook}" ]]; then
dockerHost="$(</etc/hostname)"
printOutput "4" "Got hostname: ${dockerHost}"
eventText="**Plex Server Update for ${dockerHost%%.*}**${lineBreak}Stopping Plex Media Server for server upgrade"
printOutput "4" "Discord messaging enabled -- Passing message to function"
sendDiscordMessage "${eventText}"
fi
fi
# Clean out the Codecs folder, because apparently that sometimes breaks things between upgrades if you don't
# https://old.reddit.com/r/PleX/comments/lzwkyc/eae_timeout/gq4xcat/
printOutput "2" "Cleaning out Codecs directory"
if docker exec "${containerName}" rm -rf "/config/Library/Application Support/Plex Media Server/Codecs" > /dev/null 2>&1; then
printOutput "3" "Codecs directory cleared successfully"
else
badExit "10" "Unable to clear Codecs directory"
fi
# Restart the Docker container.
printOutput "3" "Restarting container"
if docker restart "${containerName}" > /dev/null 2>&1; then
printOutput "3" "Container restarted successfully"
else
badExit "11" "Unable restart the container"
fi
if [[ -n "${telegramBotId}" && -n "${telegramChannelId[0]}" ]]; then
dockerHost="$(</etc/hostname)"
eventText="<b>Plex Server Update for ${dockerHost%%.*}</b>${lineBreak}Plex Media Server restarted for update from version <i>${myVer}</i> to version <i>${currVer}</i>"
printOutput "4" "Got hostname: ${dockerHost}"
printOutput "4" "Telegram messaging enabled -- Passing message to function"
sendTelegramMessage "${eventText}"
fi
if [[ -n "${discordWebhook}" ]]; then
dockerHost="$(</etc/hostname)"
eventText="**Plex Server Update for ${dockerHost%%.*}**${lineBreak}Plex Media Server restarted for update from version *${myVer}* to version *${currVer}*"
printOutput "4" "Got hostname: ${dockerHost}"
printOutput "4" "Discord messaging enabled -- Passing message to function"
sendDiscordMessage "${eventText}"
fi
#############################
## End of file ##
#############################
cleanExit