Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Autoupdate" feature. Update S6. Shellcheck. #81

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ uid=1001(myuser) gid=1001(myuser) groups=1001(myuser)
In the above case, if you set the `PLEX_UID` and `PLEX_GID` to `1001`, then the permissions will match that of your own user.

## Tags
In addition to the standard version and `latest` tags, there is a special `autoupdate` tag. This container behave differently than your typical containers. The `autoupdate` container does **not** have any Plex Media Server binary installed. Instead, every time this container is run, it will fetch the latest version, install it and then start the Plex Media Server.
In addition to the standard version and `latest` tags, there is a special `autoupdate` tag. This container behaves differently than typical containers. The `autoupdate` container does **not** have any Plex Media Server binary installed. Instead, every time this container is run, it checks to see if the server is installed. If it is (because you've already started this container before) it starts the server. If it wasn't installed (because this is the first time the server is starting) it will install it, then start the server.

Since containers lose their state every time they are restarted, the binary is cached in `/config/install` so it won't need to be downloaded again, but it will need to be installed every time this container starts.

This container will automatically check for new updates every day between 4:00am - 4:30am (according to timezone in `TZ`).
The `autoupdate` container will automatically check for new updates every day between 4:00am - 4:30am (according to timezone set in `TZ`).

- **AUTO_UPDATE_CHANNEL** This variable can only be `public` or `beta` (default). The `public` value restricts this check to public versions only whereas `beta` value will fetch beta versions. If the server is not logged in or you do not have Plex Pass on your account, the `beta` tagged images will be restricted to publicly available versions only.
- **FORCE_UPDATE** Set this variable to `true` in order to ignore previously cached binaries in `/config/install` upon startup. This is generally not required, but provided just-in-case. You can also manually force an update by simply deleting the `/config/install` directory.

### Forcing an update/install on startup
- Create an empty file called `_force_` in the `/config` directory. (i.e. `touch /config/_force_`) You can also manually force an update by simply recreating the image. Just restarting the container (or stopping and starting) will not normally update the existing installation within the container.

To view the Docker images head over to [https://hub.docker.com/r/plexinc/pms-docker/tags/](https://hub.docker.com/r/plexinc/pms-docker/tags/)

Expand Down
37 changes: 27 additions & 10 deletions dev/dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ IFS=$'\n\t'
# Assumes this is running on Linux (any distro) using (intel/amd).

# This bakes the specific binary into the image, you can override these vars on command line
# DOCKERHUB_IMAGE=myorg/mycontainer ./dev/sh bake
# prompt> DOCKERHUB_IMAGE=myorg/mycontainer ./dev/sh bake
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prompt>?

VERSION_TO_BUILD=${VERSION_TO_BUILD:-"1.32.4.7195-7c8f9d3b6"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it should default to a particular version as this script is likely not to be updated often as the server advances in version.

DOCKERHUB_IMAGE=${DOCKERHUB_IMAGE:-"plexinc/pms-docker"}
# launch with KEEP=true to keep the container after starting/running it - manual cleanup required when done
# prompt> KEEP=true ./dev.sh debug arm64v8 autoupdate
KEEP=${KEEP:-}

setup() {
# Create a multi-arch buildx builder named PlexBuilder (if it doesn't exist)
Expand Down Expand Up @@ -49,19 +52,33 @@ debug() {
platform=$1
name=$2
autoupdate=$3
if [ "$autoupdate" = "autoupdate" ]; then
name=$name:autoupdate
else
name=$name:latest
fi
[ "$autoupdate" = "autoupdate" ] && name=$name:autoupdate || name=$name:latest
if [[ $platform == linux/arm* ]]; then
# shellcheck disable=SC2064
trap "trap - SIGTERM && docker stop debug-plex" SIGINT SIGTERM EXIT
docker run --rm --name debug-plex --platform "$platform" -e DEBUG=true "$name" &
trap "trap - SIGTERM && docker stop debug-${name/:/_}" SIGINT SIGTERM EXIT
if [ "${KEEP,,}" = "true" ]; then
if docker start "debug-${name/:/_}"; then
docker attach "debug-${name/:/_}" &
else
docker run --name "debug-${name/:/_}" --platform "$platform" -e DEBUG=true "$name" &
fi
else
docker run -rm --name "debug-${name/:/_}" --platform "$platform" -e DEBUG=true "$name" &
fi
sleep 5
docker exec -it debug-plex bash
docker exec -it "debug-${name/:/_}" bash
else
docker run --rm --name debug-plex --platform "$platform" -e DEBUG=true -it "$name" bash
if [ "${KEEP,,}" = "true" ]; then
if docker start "debug-${name/:/_}"; then
# shellcheck disable=SC2064
trap "trap - SIGTERM && docker stop debug-${name/:/_}" SIGINT SIGTERM EXIT
docker attach "debug-${name/:/_}"
else
docker run --name "debug-${name/:/_}" --platform "$platform" -e DEBUG=true -it "$name" bash
fi
else
docker run -rm --name "debug-${name/:/_}" --platform "$platform" -e DEBUG=true -it "$name" bash
fi
fi
}

Expand Down
10 changes: 1 addition & 9 deletions root/etc/plex/plex-startup
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@ IFS=$'\n\t'
# This script is called by the autoupdate, beta, and public tags only

DEBUG=${DEBUG:-}
FORCE_UPDATE=${FORCE_UPDATE:-}

# If we are debugging, enable trace
if [ "${DEBUG,,}" = "true" ]; then
set -x
fi

# shellcheck source=../../plex-common.sh
. /plex-common.sh

CACHED_URL=
[[ -r /config/install/plexmediaserver.url ]] && CACHED_URL=$(< /config/install/plexmediaserver.url)
if [ ! "${FORCE_UPDATE,,}" = "true" ] && [ -n "$CACHED_URL" ] && [ -f /config/install/plexmediaserver.deb ]; then
echo "Installing previously downloaded plex version..."
installFromRawUrl "$CACHED_URL"
else
if [ -f /config/_force_ ] || ! dpkg --get-selections plexmediaserver 2> /dev/null | grep -wq "install"; then
echo "Downloading latest plex version..."
exec /etc/plex/plex-update
fi
7 changes: 3 additions & 4 deletions root/installBinary.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ else
# This pre-installs the specified version in TAG into this docker image.
remoteVersion=
remoteFile=
getVersionInfo "${TAG}" "" remoteVersion remoteFile
remoteFileHashSha256=
getVersionInfo "${TAG}" "" remoteVersion remoteFile remoteFileHashSha256

if [ -z "${remoteVersion}" ] || [ -z "${remoteFile}" ]; then
echo "Could not get install version"
exit 1
fi

echo "Attempting to install: ${remoteVersion}"
installFromUrl "${remoteFile}"
# delete unnecessary installer
rm -rf /config/install
installFromUrl "${remoteFile}" "${remoteFileHashSha256}"
fi
59 changes: 31 additions & 28 deletions root/plex-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ set -euo pipefail
IFS=$'\n\t'

PLEX_UPDATE_CHANNEL=${PLEX_UPDATE_CHANNEL:-}
FORCE_UPDATE=${FORCE_UPDATE:-}

CONT_CONF_FILE="/version.txt"

Expand Down Expand Up @@ -31,6 +30,7 @@ function getVersionInfo {
local token="$2"
local -n getVersionInfo_remoteVersion=$3
local -n getVersionInfo_remoteFile=$4
local -n getVersionInfo_remoteFileHashSha256=$5

local channel=
local tokenNeeded=1
Expand Down Expand Up @@ -77,37 +77,40 @@ function installFromRawUrl {
local remoteFile="$1"
local expectedSha256="${2:-}"

# if download url matches and download is cached, then install it without download
[[ -r /config/install/plexmediaserver.url ]] && oldurl=$(< /config/install/plexmediaserver.url)
if [ ! "${FORCE_UPDATE,,}" = "true" ] && [ "$remoteFile" = "${oldurl:-}" ] && [ -f /config/install/plexmediaserver.deb ]; then
install "$remoteFile"
return $?
fi
rm -rf /tmp/plexmediaserver.deb
if curl --create-dirs -J -L -o /tmp/plexmediaserver.deb "${remoteFile}" ; then
if [ -n "$expectedSha256" ]; then
sha256=$(sha256sum /tmp/plexmediaserver.deb | awk '{ print $1 }')
# compare sha256, if provided
if [ ! "$expectedSha256" = "$sha256" ]; then
cleanup "Download failed: sha256sum does not match: expected=$expectedSha256 actual=$sha256"
fi
else
# no sha256, check if size appears ok
if [[ $(stat -c %s /tmp/plexmediaserver.deb) -lt 10000 ]]; then
# shellcheck disable=SC2119
cleanup "Download failed: size appears wrong"
fi
fi
# looks good, move tmp into position

curl --create-dirs -J -L -o /config/install/tmp/plexmediaserver.deb "${remoteFile}"
local last=$?
local sha256;
sha256=$(sha256sum /config/install/tmp/plexmediaserver.deb | awk '{ print $1 }')
echo "$sha256" > /config/install/tmp/plexmediaserver.sha256
echo "$remoteFile" > /config/install/tmp/plexmediaserver.url
# test if deb file size is ok, or if download failed
if [[ "$last" -gt "0" ]] || [[ $(stat -c %s /config/install/tmp/plexmediaserver.deb) -lt 10000 ]]; then
rm -rf /config/install/tmp
echo "Failed to fetch update: curl returned $last"
exit 1
fi
# compare sha256, if provided
if [ -n "$expectedSha256" ] && [ ! "$expectedSha256" = "$sha256" ]; then
rm -rf /config/install/tmp
echo "Failed to fetch update: sha256sum does not match: expected=$expectedSha256 actual=$sha256"
exit 1
install "$remoteFile"
else
# shellcheck disable=SC2119
cleanup
fi
}

# looks good, move tmp into position
mv /config/install/tmp/* /config/install && rm -rf /config/install/tmp
install "$remoteFile"
function cleanup {
local msg="${1:-"Download failed"}"
rm -rf /tmp/plexmediaserver.deb
echo "$msg"
exit 1
}

function install {
dpkg -i --force-confold /config/install/plexmediaserver.deb
dpkg -i --force-confold /tmp/plexmediaserver.deb
rm -rf /tmp/plexmediaserver.deb
# clean up _force_ flag, if exists
rm -rf /config/_force_
}