Skip to content

Commit 293e6d5

Browse files
authored
Improve version handling to prevent inadvertent downgrades (#226)
* Improve version handling to prevent inadvertent downgrades * Add verbose output for version checking * Improved git branch handling for testing * Stay on current branch unless BRANCHNAME is set * Add verbose output for in-use check
1 parent 17cac38 commit 293e6d5

File tree

2 files changed

+65
-21
lines changed

2 files changed

+65
-21
lines changed

plexupdate-core

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ running() {
198198
# Get a total count of active media (MediaContainer size), then deduct one for every paused stream.
199199
# If all streams are paused, we consider the server to not be active.
200200
local mediacount="$(awk -F'"' '/<MediaContainer size="[0-9]+">/ {count+=$2}; /<Player[^>]* state="paused"/ {count--}; END {print count}' <<< "${DATA}")"
201+
[ "$VERBOSE" = "yes" ] && printf 'Activity check reports a count of %i, based on return data of:\n%s\n\n' "${mediacount}" "${DATA}" >&2
201202
[ $mediacount -gt 0 ] && return 0
202203
fi
203204

@@ -209,6 +210,53 @@ verifyToken() {
209210
wget -qO /dev/null "https://plex.tv/api/resources?X-Plex-Token=${TOKEN}"
210211
}
211212

213+
isNewerVersion() {
214+
# Returns true ONLY if 1 > 2.
215+
[ -z "$1" ] && return 1
216+
[ -z "$2" ] && return
217+
[ "$1" = "$2" ] && return 1
218+
if hash dpkg 2>/dev/null; then
219+
dpkg --compare-versions "$1" gt "$2"
220+
elif sort -V --version &>/dev/null; then
221+
[ "$(printf "$1\n$2" | sort -Vr | head -n1)" = "$1" ]
222+
else
223+
# sort has had -V since at least 2009, so nobody should ever see this
224+
warn "Unable to compare version numbers, assuming '$1' is newer."
225+
fi
226+
}
227+
228+
parseVersion() {
229+
if [ "${REDHAT}" = "yes" ]; then
230+
cut -f2- -d- <<< "$1" | cut -f1-4 -d.
231+
else
232+
cut -f2 -d_ <<< "$1"
233+
fi
234+
}
235+
236+
getPlexVersion() {
237+
if [ "${REDHAT}" != "yes" ]; then
238+
dpkg-query --showformat='${Version}' --show plexmediaserver 2>/dev/null
239+
elif hash rpm 2>/dev/null; then
240+
local rpmtemp
241+
if rpmtemp=$(rpm -q plexmediaserver); then
242+
parseVersion "$rpmtemp"
243+
else
244+
return 1
245+
fi
246+
else
247+
error "Unknown OS, exiting."
248+
exit 1
249+
fi
250+
}
251+
252+
verboseOutput() {
253+
if [ "$VERBOSE" = "yes" ]; then
254+
for i in $@; do
255+
info "$i=${!i}"
256+
done
257+
fi
258+
}
259+
212260
# Shared functions
213261

214262
# SHARED

plexupdate.sh

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,13 @@ if [ "${AUTOUPDATE}" = "yes" ]; then
228228
elif ! git diff --quiet; then
229229
warn "You have made changes to the plexupdate files, cannot auto update"
230230
else
231+
if [ -z "${BRANCHNAME}" ]; then
232+
BRANCHNAME="$(git symbolic-ref -q --short HEAD)"
233+
elif [ "${BRANCHNAME}" != "$(git symbolic-ref -q --short HEAD)" ]; then
234+
git checkout "${BRANCHNAME}"
235+
fi
231236
# Force FETCH_HEAD to point to the correct branch (for older versions of git which don't default to current branch)
232-
if git fetch origin ${BRANCHNAME:-master} --quiet && ! git diff --quiet FETCH_HEAD; then
237+
if git fetch origin ${BRANCHNAME} --quiet && ! git diff --quiet FETCH_HEAD; then
233238
info "Auto-updating..."
234239

235240
# Use an associative array to store permissions. If you're running bash < 4, the declare will fail and we'll
@@ -385,11 +390,7 @@ RELEASE=$(grep -ioe '"label"[^}]*' <<<"${wgetresults}" | grep -i "\"distro\":\"$
385390
DOWNLOAD=$(echo ${RELEASE} | grep -m1 -ioe 'https://[^\"]*')
386391
CHECKSUM=$(echo ${RELEASE} | grep -ioe '\"checksum\"\:\"[^\"]*' | sed 's/\"checksum\"\:\"//')
387392

388-
if [ "$VERBOSE" = "yes" ]; then
389-
for i in RELEASE DOWNLOAD CHECKSUM; do
390-
info "$i=${!i}"
391-
done
392-
fi
393+
verboseOutput RELEASE DOWNLOAD CHECKSUM
393394

394395
if [ -z "${DOWNLOAD}" ]; then
395396
if [ "$DISTRO" = "ubuntu" -a "$BUILD" = "linux-ubuntu-armv7l" ]; then
@@ -423,30 +424,25 @@ fi
423424
# By default, try downloading
424425
SKIP_DOWNLOAD="no"
425426

426-
# Installed version detection
427-
if [ "${REDHAT}" != "yes" ]; then
428-
INSTALLED_VERSION=$(dpkg-query -s plexmediaserver 2>/dev/null | grep -Po 'Version: \K.*')
429-
else
430-
if [ "${AUTOINSTALL}" = "yes" -a "${AUTOSTART}" = "no" ]; then
431-
warn "Your distribution may require the use of the AUTOSTART [-s] option for the service to start after the upgrade completes."
432-
fi
433-
INSTALLED_VERSION=$(rpm -qv plexmediaserver 2>/dev/null)
427+
INSTALLED_VERSION="$(getPlexVersion)" || warn "Unable to detect installed version, first time?"
428+
FILE_VERSION="$(parseVersion "${FILENAME}")"
429+
verboseOutput INSTALLED_VERSION FILE_VERSION
430+
431+
if [ "${REDHAT}" = "yes" -a "${AUTOINSTALL}" = "yes" -a "${AUTOSTART}" = "no" ]; then
432+
warn "Your distribution may require the use of the AUTOSTART [-s] option for the service to start after the upgrade completes."
434433
fi
435434

436435
if [ "${CHECKONLY}" = "yes" ]; then
437-
if [ -z "${INSTALLED_VERSION}" ]; then
438-
warn "Unable to detect installed version, first time?"
439-
elif [[ $FILENAME != *$INSTALLED_VERSION* ]]; then
440-
AVAIL="$(echo "${FILENAME}" | sed -nr 's/^[^0-9]+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\-[^_]+).*/\1/pg')"
441-
info "Your OS reports Plex $INSTALLED_VERSION installed, newer version is available (${AVAIL})"
436+
if [ -n "${INSTALLED_VERSION}" ] && isNewerVersion "$FILE_VERSION" "$INSTALLED_VERSION"; then
437+
info "Your OS reports Plex $INSTALLED_VERSION installed, newer version is available (${FILE_VERSION})"
442438
exit 7
443-
else
439+
elif [ -n "${INSTALLED_VERSION}" ]; then
444440
info "You are running the latest version of Plex (${INSTALLED_VERSION})"
445441
fi
446442
exit 0
447443
fi
448444

449-
if [[ $FILENAME == *$INSTALLED_VERSION* ]] && [ "${FORCE}" != "yes" ] && [ ! -z "${INSTALLED_VERSION}" ]; then
445+
if ! isNewerVersion "$FILE_VERSION" "$INSTALLED_VERSION" && [ "${FORCE}" != "yes" ]; then
450446
info "Your OS reports the latest version of Plex ($INSTALLED_VERSION) is already installed. Use -f to force download."
451447
exit 0
452448
fi

0 commit comments

Comments
 (0)