diff --git a/Dockerfile b/Dockerfile index 5465787..8b65d7b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -147,8 +147,9 @@ RUN find -exec sh -c 'file "{}" | grep -q ELF && strip --strip-debug "{}"' \; \ && mv usr/bin/curl /output/usr/bin \ && mv usr/bin/xml /output/usr/bin/xmlstarlet -ADD start_pms /output/usr/sbin/start_pms -RUN chmod +x /output/usr/sbin/start_pms +ADD entrypoint /output/usr/local/bin/ +ADD *.sh /output/usr/local/bin/ +RUN chmod +x /output/usr/local/bin/* #========================= @@ -172,16 +173,26 @@ LABEL maintainer="Spritsail " \ io.spritsail.version.libstdcpp=${LIBSTDCPP_VER} \ io.spritsail.version.xmlstarlet=${XMLSTAR_VER} -ENV SUID=900 SGID=900 +WORKDIR /usr/lib/plexmediaserver COPY --from=builder /output/ / +ENV SUID=900 SGID=900 \ + PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS="6" \ + PLEX_MEDIA_SERVER_MAX_STACK_SIZE="3000" \ + PLEX_MEDIA_SERVER_TMPDIR="/tmp" \ + PLEX_MEDIA_SERVER_HOME="/usr/lib/plexmediaserver" \ + PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR="/var/lib/plexmediaserver" + HEALTHCHECK --interval=10s --timeout=5s \ CMD [ "wget", "-O", "/dev/null", "-T", "10", "-q", "localhost:32400/identity" ] -WORKDIR /usr/lib/plexmediaserver - EXPOSE 32400 +VOLUME ["/config", "/transcode"] + +RUN mkdir -p "$PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR" \ + && ln -sfv /config "$PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR/Plex Media Server" + ENTRYPOINT ["/sbin/tini", "--"] -CMD ["/usr/sbin/start_pms"] +CMD ["/usr/local/bin/entrypoint"] diff --git a/entrypoint b/entrypoint new file mode 100755 index 0000000..37175de --- /dev/null +++ b/entrypoint @@ -0,0 +1,28 @@ +#!/bin/sh +set -e + +if su-exec -e [ ! -w "/config" ]; then + 2>&1 echo "Warning: No permission to write in '/config' directory." + 2>&1 echo " Correcting permissions to prevent a crash" + 2>&1 echo + ( + set -x + chown $SUID:$SGID /config + chmod o+rw /config + ) +fi + +exec su-exec -e sh </dev/null & + +# Use a random pidfile +export PLEX_MEDIA_SERVER_PIDFILE="\$(mktemp -ut pms-pid.XXXXXX)" + +exec "\$PLEX_MEDIA_SERVER_HOME/Plex Media Server" +EOF diff --git a/gen-config.sh b/gen-config.sh new file mode 100755 index 0000000..48046ae --- /dev/null +++ b/gen-config.sh @@ -0,0 +1,64 @@ +#!/bin/sh +set -e + +PREF_FILE="${1:-/config/Preferences.xml}" + +getPref() { + xmlstarlet sel -T -t -m "/Preferences" -v "@$1" -n "${PREF_FILE}" +} +setPref() { + count="$(xmlstarlet sel -t -v "count(/Preferences/@$1)" "${PREF_FILE}")" + if [ $(($count + 0)) -gt 0 ]; then + xmlstarlet ed --inplace --update "/Preferences/@$1" -v "$2" "${PREF_FILE}" 2>/dev/null + else + xmlstarlet ed --inplace --insert "/Preferences" --type attr -n "$1" -v "$2" "${PREF_FILE}" 2>/dev/null + fi +} + +# Create a default config file allowing external access +echo -e $'\n' > "/config/Preferences.xml" + +# Enforced defaults. These can be changed manually afterwards. +setPref "AcceptedEULA" "1" +setPref "TranscoderTempDirectory" "/transcode" + +# The following below is ripped directly from the official (inferior) Plex container: +# https://github.com/plexinc/pms-docker/blob/155f00c71b50f94c73ffea0aae16cc61ef957df7/root/etc/cont-init.d/40-plex-first-run + +# 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 [ ! -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 + +test -n "${ADVERTISE_IP}" && setPref "customConnections" "${ADVERTISE_IP}" +test -n "${ALLOWED_NETWORKS}" && setPref "allowedNetworks" "${ALLOWED_NETWORKS}" +test -n "${DISABLE_REMOTE_SEC}" && setPref "disableRemoteSecurity" "1" diff --git a/start_pms b/start_pms deleted file mode 100755 index 720b17c..0000000 --- a/start_pms +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/sh - -# Default values -export PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS="${PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS:-6}" -export PLEX_MEDIA_SERVER_HOME="${PLEX_MEDIA_SERVER_HOME:-/usr/lib/plexmediaserver}" -export PLEX_MEDIA_SERVER_MAX_STACK_SIZE="${PLEX_MEDIA_SERVER_MAX_STACK_SIZE:-3000}" -export PLEX_MEDIA_SERVER_TMPDIR="${PLEX_MEDIA_SERVER_TMPDIR:-/tmp}" -export PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR="${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR:-/config}" -export PLEX_MEDIA_SERVER_CONFIG_DIR="${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}/Plex Media Server" -export TMPDIR="${PLEX_MEDIA_SERVER_TMPDIR}" - -if [ ! -d "$PLEX_MEDIA_SERVER_CONFIG_DIR" ]; then - mkdir -p "$PLEX_MEDIA_SERVER_CONFIG_DIR" && \ - chown $SUID:$SGID "$PLEX_MEDIA_SERVER_CONFIG_DIR" - if [ ! $? -eq 0 ]; then - echo "WARNING COULDN'T CREATE $PLEX_MEDIA_SERVER_CONFIG_DIR, MAKE SURE I HAVE PERMISSON TO DO THAT!" - exit 1 - fi -fi - -if su-exec -e [ ! -w "$PLEX_MEDIA_SERVER_CONFIG_DIR" ]; then - echo "ERROR: CANNOT WRITE IN $PLEX_MEDIA_SERVER_CONFIG_DIR, MAKE SURE I HAVE PERMISSION TO DO THAT!" - exit 2 -fi - -if [ ! -e "$PLEX_MEDIA_SERVER_CONFIG_DIR/Preferences.xml" ]; then - # Create a default config file allowing external access - echo -e '\n' \ - >> "$PLEX_MEDIA_SERVER_CONFIG_DIR/Preferences.xml" -fi - -# Allow Plex group to write to tmpdir -chgrp $SGID "$TMPDIR" -chmod g+rwx "$TMPDIR" - -PIDFILE="${PLEX_MEDIA_SERVER_CONFIG_DIR}/plexmediaserver.pid" -if [ -f "$PIDFILE" ]; then - rm -f "$PIDFILE" -fi - -tail -F "$PLEX_MEDIA_SERVER_CONFIG_DIR/Logs/Plex Media Server.log" >/proc/1/fd/1 2>/dev/null & - -exec su-exec -e "$PLEX_MEDIA_SERVER_HOME/Plex Media Server"