From 0f37d36f407f30b4add23dc0d28f1664e44c516d Mon Sep 17 00:00:00 2001 From: Devin Buhl Date: Sun, 11 Feb 2024 21:09:57 -0500 Subject: [PATCH] fix: add plex prefs to entrypoint script (#958) * fix: add plex prefs to entrypoint script Signed-off-by: Devin Buhl * fix: add back PLEX_PREFERENCE_x Signed-off-by: Devin Buhl * fix: update script Signed-off-by: Devin Buhl --------- Signed-off-by: Devin Buhl --- apps/plex/Dockerfile | 1 - apps/plex/entrypoint.sh | 107 +++++++++++++++++++++++++++++++++- apps/plex/plex-preferences.sh | 102 -------------------------------- 3 files changed, 104 insertions(+), 106 deletions(-) delete mode 100755 apps/plex/plex-preferences.sh diff --git a/apps/plex/Dockerfile b/apps/plex/Dockerfile index e5bf5b2b3..b61c8a6e0 100644 --- a/apps/plex/Dockerfile +++ b/apps/plex/Dockerfile @@ -51,6 +51,5 @@ RUN \ WORKDIR "${PLEX_MEDIA_SERVER_HOME}" USER kah -COPY ./apps/plex/plex-preferences.sh /scripts/plex-preferences.sh COPY ./apps/plex/entrypoint.sh /entrypoint.sh CMD ["/entrypoint.sh"] diff --git a/apps/plex/entrypoint.sh b/apps/plex/entrypoint.sh index 25d829d94..0a0d953d9 100755 --- a/apps/plex/entrypoint.sh +++ b/apps/plex/entrypoint.sh @@ -2,15 +2,116 @@ #shellcheck disable=SC1091 test -f "/scripts/umask.sh" && source "/scripts/umask.sh" -test -f "/scripts/plex-preferences.sh" && source "/scripts/plex-preferences.sh" #shellcheck disable=SC2155 export PLEX_MEDIA_SERVER_INFO_MODEL=$(uname -m) #shellcheck disable=SC2155 export PLEX_MEDIA_SERVER_INFO_PLATFORM_VERSION=$(uname -r) -[[ -f "${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}/Plex Media Server/plexmediaserver.pid" ]] && \ - rm -f "${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}/Plex Media Server/plexmediaserver.pid" +function getPref { + local key="$1" + xmlstarlet sel -T -t -m "/Preferences" -v "@${key}" -n "${prefFile}" +} + +function setPref { + local key="$1" + local value="$2" + count="$(xmlstarlet sel -t -v "count(/Preferences/@${key})" "${prefFile}")" + count=$((count + 0)) + if [[ $count -gt 0 ]]; then + xmlstarlet ed --inplace --update "/Preferences/@${key}" -v "${value}" "${prefFile}" + else + xmlstarlet ed --inplace --insert "/Preferences" --type attr -n "${key}" -v "${value}" "${prefFile}" + fi +} + +prefFile="${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}/Plex Media Server/Preferences.xml" + +# Create empty Preferences.xml file if it doesn't exist already +if [ ! -e "${prefFile}" ]; then + echo "Creating pref shell" + mkdir -p "$(dirname "${prefFile}")" + cat > "${prefFile}" <<-EOF + + +EOF +fi + +# Setup Server's client identifier +serial="$(getPref "MachineIdentifier")" +if [[ -z "${serial}" ]]; then + serial="$(cat /proc/sys/kernel/random/uuid)" + setPref "MachineIdentifier" "${serial}" +fi +clientId="$(getPref "ProcessedMachineIdentifier")" +if [[ -z "${clientId}" ]]; then + clientId="$(echo -n "${serial}- Plex Media Server" | sha1sum | cut -b 1-40)" + setPref "ProcessedMachineIdentifier" "${clientId}" +fi + +# Get server token and only turn claim token into server token if we have former but not latter. +token="$(getPref "PlexOnlineToken")" +if [[ -n "${PLEX_CLAIM_TOKEN}" ]] && [[ -z "${token}" ]]; then + echo "Attempting to obtain server token from claim token..." + loginInfo="$(curl -fsSL -X POST \ + -H 'X-Plex-Client-Identifier: '"${clientId}" \ + -H 'X-Plex-Product: Plex Media Server'\ + -H 'X-Plex-Version: 1.1' \ + -H 'X-Plex-Provides: server' \ + -H 'X-Plex-Platform: Linux' \ + -H 'X-Plex-Platform-Version: 1.0' \ + -H 'X-Plex-Device-Name: PlexMediaServer' \ + -H 'X-Plex-Device: Linux' \ + "https://plex.tv/api/claim/exchange?token=${PLEX_CLAIM_TOKEN}")" + token="$(echo "$loginInfo" | sed -n 's/.*\(.*\)<\/authentication-token>.*/\1/p')" + + if [[ "$token" ]]; then + echo "Token obtained successfully!" + setPref "PlexOnlineToken" "${token}" + fi +fi + +# Set other preferences +[[ -n "${ADVERTISE_IP}" ]] && PLEX_ADVERTISE_URL=${ADVERTISE_IP} +if [[ -n "${PLEX_ADVERTISE_URL}" ]]; then + echo "Setting customConnections to: ${PLEX_ADVERTISE_URL}" + setPref "customConnections" "${PLEX_ADVERTISE_URL}" +fi + +[[ -n "${ALLOWED_NETWORKS}" ]] && PLEX_NO_AUTH_NETWORKS=${ALLOWED_NETWORKS} +if [[ -n "${PLEX_NO_AUTH_NETWORKS}" ]]; then + echo "Setting allowedNetworks to: ${PLEX_NO_AUTH_NETWORKS}" + setPref "allowedNetworks" "${PLEX_NO_AUTH_NETWORKS}" +fi + +# Set transcoder directory if not yet set +if [[ -z "$(getPref "TranscoderTempDirectory")" ]]; then + echo "Setting TranscoderTempDirectory to: /transcode" + setPref "TranscoderTempDirectory" "/transcode" +fi + +# Parse list of all exported variables that start with PLEX_PREFERENCE_ +# The format of which is PLEX_PREFERENCE_="Key=Value" +# Where Key is the EXACT key to use in the Plex Preference file +# And Value is the EXACT value to use in the Plex Preference file for that key. +# Please note it looks like many of the key's are camelCase in some fashion. +# Additionally there are likely some preferences where environment variable injection +# doesn't really work for. +for var in "${!PLEX_PREFERENCE_@}"; do + value="${!var}" + PreferenceValue="${value#*=}" + PreferenceKey="${value%=*}" + setPref "${PreferenceKey}" "${PreferenceValue}" +done + +# Remove pid file +rm -f "${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}/Plex Media Server/plexmediaserver.pid" + +# Purge Codecs folder +if [[ "${PLEX_PURGE_CODECS}" == "true" ]]; then + echo "Purging Codecs folder..." + find "${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}/Plex Media Server/Codecs" -mindepth 1 -not -name '.device-id' -print -delete +fi #shellcheck disable=SC2086 exec \ diff --git a/apps/plex/plex-preferences.sh b/apps/plex/plex-preferences.sh deleted file mode 100755 index ca775ad0b..000000000 --- a/apps/plex/plex-preferences.sh +++ /dev/null @@ -1,102 +0,0 @@ -#!/usr/bin/env bash - -# This file is based off of the official 40-plex-first-run -# Here: https://github.com/plexinc/pms-docker/blob/master/root/etc/cont-init.d/40-plex-first-run -# It should live in /etc/cont-init.d/ - -# If we are debugging, enable trace -if [ "${DEBUG,,}" = "true" ]; then - set -x -fi - -function getPref { - local key="${1}" - xmlstarlet sel -T -t -m "/Preferences" -v "@${key}" -n "${prefFile}" -} - -function setPref { - local key="${1}" - local value="${2}" - count="$(xmlstarlet sel -t -v "count(/Preferences/@${key})" "${prefFile}")" - count=$(($count + 0)) - if [[ $count > 0 ]]; then - xmlstarlet ed --inplace --update "/Preferences/@${key}" -v "${value}" "${prefFile}" - else - xmlstarlet ed --inplace --insert "/Preferences" --type attr -n "${key}" -v "${value}" "${prefFile}" - fi -} - -prefFile="${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}/Plex Media Server/Preferences.xml" - -# Create empty shell pref file if it doesn't exist already -if [ ! -e "${prefFile}" ]; then - echo "Creating pref shell" - mkdir -p "$(dirname "${prefFile}")" - cat > "${prefFile}" <<-EOF - - -EOF -fi - -# Setup Server's client identifier -serial="$(getPref "MachineIdentifier")" -if [ -z "${serial}" ]; then - serial="$(uuidgen)" - setPref "MachineIdentifier" "${serial}" -fi -clientId="$(getPref "ProcessedMachineIdentifier")" -if [ -z "${clientId}" ]; then - clientId="$(echo -n "${serial}- Plex Media Server" | sha1sum | cut -b 1-40)" - setPref "ProcessedMachineIdentifier" "${clientId}" -fi - -# Get server token and only turn claim token into server token if we have former but not latter. -token="$(getPref "PlexOnlineToken")" -if [ ! -z "${PLEX_CLAIM}" ] && [ -z "${token}" ]; then - echo "Attempting to obtain server token from claim token" - loginInfo="$(curl -X POST \ - -H 'X-Plex-Client-Identifier: '${clientId} \ - -H 'X-Plex-Product: Plex Media Server'\ - -H 'X-Plex-Version: 1.1' \ - -H 'X-Plex-Provides: server' \ - -H 'X-Plex-Platform: Linux' \ - -H 'X-Plex-Platform-Version: 1.0' \ - -H 'X-Plex-Device-Name: PlexMediaServer' \ - -H 'X-Plex-Device: Linux' \ - "https://plex.tv/api/claim/exchange?token=${PLEX_CLAIM}")" - token="$(echo "$loginInfo" | sed -n 's/.*\(.*\)<\/authentication-token>.*/\1/p')" - - if [ "$token" ]; then - echo "Token obtained successfully" - setPref "PlexOnlineToken" "${token}" - fi -fi - -if [ -n "${ADVERTISE_IP}" ]; then - setPref "customConnections" "${ADVERTISE_IP}" -fi - -if [ -n "${ALLOWED_NETWORKS}" ]; then - setPref "allowedNetworks" "${ALLOWED_NETWORKS}" -fi - -# Set transcoder temp if not yet set -if [ -z "$(getPref "TranscoderTempDirectory")" ]; then - setPref "TranscoderTempDirectory" "/transcode" -fi - -# Parse list of all exported variables that start with PLEX_PREFERENCE_ -# The format of which is PLEX_PREFERENCE_="Key=Value" -# Where Key is the EXACT key to use in the Plex Preference file -# And Value is the EXACT value to use in the Plex Preference file for that key. -# Please note it looks like many of the key's are camelCase in some fashion. -# Additionally there are likely some preferences where environment variable injection -# doesn't really work for. -for var in "${!PLEX_PREFERENCE_@}"; do - value="${!var}" - PreferenceValue="${value#*=}" - PreferenceKey="${value%=*}" - setPref "${PreferenceKey}" "${PreferenceValue}" -done - -echo "Plex Media Server preferences updated"