From e8cd4d7e894351b268b3df8b80e7c62dd901eed6 Mon Sep 17 00:00:00 2001 From: aptalca Date: Wed, 4 Dec 2019 11:41:45 -0500 Subject: [PATCH] Add support for PLEX_CLAIM --- README.md | 6 ++++- readme-vars.yml | 4 ++- root/etc/cont-init.d/45-plex-claim | 43 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 root/etc/cont-init.d/45-plex-claim diff --git a/README.md b/README.md index 4141bc5..6672404 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ docker create \ -e PGID=1000 \ -e VERSION=docker \ -e UMASK_SET=022 `#optional` \ + -e PLEX_CLAIM= `#optional` \ -v :/config \ -v :/tv \ -v :/movies \ @@ -95,6 +96,7 @@ services: - PGID=1000 - VERSION=docker - UMASK_SET=022 #optional + - PLEX_CLAIM= #optional volumes: - :/config - :/tv @@ -114,6 +116,7 @@ Container images are configured using parameters passed at runtime (such as thos | `-e PGID=1000` | for GroupID - see below for explanation | | `-e VERSION=docker` | Set whether to update plex or not - see Application Setup section. | | `-e UMASK_SET=022` | control permissions of files and directories created by Plex | +| `-e PLEX_CLAIM=` | Optionally you can obtain a claim token from https://plex.tv/claim and input here. Keep in mind that the claim tokens expire within 4 minutes. | | `-v /config` | Plex library location. *This can grow very large, 50gb+ is likely for a large collection.* | | `-v /tv` | Media goes here. Add as many as needed e.g. `/movies`, `/tv`, etc. | | `-v /movies` | Media goes here. Add as many as needed e.g. `/movies`, `/tv`, etc. | @@ -133,7 +136,7 @@ Will set the environment variable `PASSWORD` based on the contents of the `/run/ ## Optional Parameters -*Special note* - If you'd like to run Plex without requiring `--net=host` (`NOT recommended`) then you will need the following ports in your `docker create` command: +*Special note* - If you'd like to run Plex without requiring `--net=host` (`NOT recommended`) then you will need the following ports in your `docker create` command (you need to set PLEX_CLAIM to claim a server set up with bridge networking): ``` -p 32400:32400 \ @@ -262,6 +265,7 @@ Once registered you can define the dockerfile to use with `-f Dockerfile.aarch64 ## Versions +* **04.12.19:** - Add variable for setting PLEX_CLAIM. * **06.08.19:** - Add variable for setting UMASK. * **10.07.19:** - Fix permissions for tuner (/dev/dvb) devices. * **20.05.19:** - Bugfix do not allow Root group for Intel QuickSync ownership rules. diff --git a/readme-vars.yml b/readme-vars.yml index 2391000..0b16320 100644 --- a/readme-vars.yml +++ b/readme-vars.yml @@ -34,9 +34,10 @@ param_env_vars: opt_param_usage_include_env: true opt_param_env_vars: - { env_var: "UMASK_SET", env_value: "022", desc: "control permissions of files and directories created by Plex"} + - { env_var: "PLEX_CLAIM", env_value: "", desc: "Optionally you can obtain a claim token from https://plex.tv/claim and input here. Keep in mind that the claim tokens expire within 4 minutes."} optional_parameters: | - *Special note* - If you'd like to run Plex without requiring `--net=host` (`NOT recommended`) then you will need the following ports in your `docker create` command: + *Special note* - If you'd like to run Plex without requiring `--net=host` (`NOT recommended`) then you will need the following ports in your `docker create` command (you need to set PLEX_CLAIM to claim a server set up with bridge networking): ``` -p 32400:32400 \ @@ -86,6 +87,7 @@ app_setup_block: | # changelog changelogs: + - { date: "04.12.19:", desc: "Add variable for setting PLEX_CLAIM." } - { date: "06.08.19:", desc: "Add variable for setting UMASK." } - { date: "10.07.19:", desc: "Fix permissions for tuner (/dev/dvb) devices." } - { date: "20.05.19:", desc: "Bugfix do not allow Root group for Intel QuickSync ownership rules." } diff --git a/root/etc/cont-init.d/45-plex-claim b/root/etc/cont-init.d/45-plex-claim new file mode 100644 index 0000000..4997067 --- /dev/null +++ b/root/etc/cont-init.d/45-plex-claim @@ -0,0 +1,43 @@ +#!/usr/bin/with-contenv bash + +if grep -qs "PlexOnlineToken" "/config/Library/Application Support/Plex Media Server/Preferences.xml" || [ -z "$PLEX_CLAIM" ]; then + exit 0 +fi + +if [ ! -f "/config/Library/Application Support/Plex Media Server/Preferences.xml" ]; then + UMASK_SET=${UMASK_SET:-022} + umask "$UMASK_SET" + echo "Temporarily starting Plex Media Server." + export PLEX_MEDIA_SERVER_INFO_MODEL=$(uname -m) + export PLEX_MEDIA_SERVER_INFO_PLATFORM_VERSION=$(uname -r) + s6-setuidgid abc /bin/bash -c \ + 'LD_LIBRARY_PATH=/usr/lib/plexmediaserver:/usr/lib/plexmediaserver/lib /usr/lib/plexmediaserver/Plex\ Media\ Server' & + echo "Waiting for Plex to generate its config" + until grep -qs "ProcessedMachineIdentifier" "/config/Library/Application Support/Plex Media Server/Preferences.xml"; do + sleep 1 + done + echo "Stopping Plex to claim server" + kill $(cat "/config/Library/Application Support/Plex Media Server/plexmediaserver.pid") + wait + echo "Plex stopped" +fi + +ProcessedMachineIdentifier=$(sed -n "s/^.*ProcessedMachineIdentifier=\"\([^\"]*\)\".*$/\1/p" "/config/Library/Application Support/Plex Media Server/Preferences.xml") +PlexOnlineToken="$(curl -X POST \ + -H 'X-Plex-Client-Identifier: '"${ProcessedMachineIdentifier}" \ + -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}" \ + | sed -n 's/.*\(.*\)<\/authentication-token>.*/\1/p')" + +if [ -n "$PlexOnlineToken" ]; then + echo "Server claimed successfully, navigate to http://serverip:32400/web to complete plex setup." + sed -i "s/\/>/ PlexOnlineToken=\"${PlexOnlineToken}\"\/>/g" "/config/Library/Application Support/Plex Media Server/Preferences.xml" +else + echo "Unable to claim Plex server. Either manually claim by connecting to http://serverip:32400/web from the same network subnet, or recreate container with a new claim token." +fi \ No newline at end of file