From 1b57528f14b851493d1270de3a261c863f04e660 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Thu, 11 Aug 2016 18:51:58 -0700 Subject: [PATCH 01/13] Use bash built-in hash to check if programs exist in path --- plexupdate.sh | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index 4aac5b5..319eb51 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -66,9 +66,8 @@ DEBIAN_INSTALL="dpkg -i" DISTRO_INSTALL="" # Sanity, make sure wget is in our path... -wget >/dev/null 2>/dev/null -if [ $? -eq 127 ]; then - echo "Error: This script requires wget in the path. It could also signify that you don't have the tool installed." >&2 +if hash wget 2>/dev/null; then + echo "ERROR: This script requires wget in the path. It could also signify that you don't have the tool installed." >&2 exit 1 fi @@ -209,9 +208,8 @@ if [ "${QUIET}" = "yes" ]; then fi if [ "${AUTOUPDATE}" == "yes" ]; then - git >/dev/null 2>/dev/null - if [ $? -eq 127 ]; then - echo "Error: You need to have git installed for this to work" >&2 + if hash git 2>/dev/null; then + echo "ERROR: You need to have git installed for this to work" >&2 cronexit 1 fi pushd "$(dirname "$0")" >/dev/null From fcdcf1037a47dd43ec8847622a91e1ffc29dbaaa Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Thu, 11 Aug 2016 18:54:11 -0700 Subject: [PATCH 02/13] Clean up error messages to use consistent style --- plexupdate.sh | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index 319eb51..6ebc79c 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -118,12 +118,6 @@ else fi fi -if [ ! "${RELEASE}" = "" ]; then - echo "ERROR: RELEASE keyword is deprecated and should be removed from .plexupdate" >&2 - echo " Use DISTRO and BUILD instead to manually select what to install (check README.md)" >&2 - exit 255 -fi - # Current pages we need - Do not change unless Plex.tv changes again URL_LOGIN=https://plex.tv/users/sign_in.json URL_DOWNLOAD=https://plex.tv/api/downloads/1.json?channel=plexpass @@ -174,7 +168,7 @@ do (-h) usage;; (-a) AUTOINSTALL=yes;; (-c) CRON=yes;; - (-C) echo "ERROR: CRON option has changed, please review README.md" 1>&2; cronexit 255;; + (-C) echo "ERROR: CRON option has changed, please review README.md" >&2; cronexit 255;; (-d) AUTODELETE=yes;; (-f) FORCE=yes;; (-l) LISTOPTS=yes;; @@ -182,29 +176,31 @@ do (-q) QUIET=yes;; (-r) PRINT_URL=yes;; (-s) AUTOSTART=yes;; - (-S) echo "ERROR: SILENT option has been removed, please use QUIET (-q) instead" 1>&2; cronexit 1;; + (-S) echo "ERROR: SILENT option has been removed, please use QUIET (-q) instead" >&2; cronexit 255;; (-u) AUTOUPDATE=yes;; (-U) AUTOUPDATE=no;; (--) ;; - (-*) echo "Error: unrecognized option $1" 1>&2; usage; cronexit 1;; + (-*) echo "ERROR: unrecognized option $1" >&2; usage; cronexit 1;; (*) break;; esac shift done if [ "${KEEP}" = "yes" ]; then - echo "ERROR: KEEP is deprecated" >&2 - cronexit 1 + echo "ERROR: KEEP is deprecated and should be removed from .plexupdate" >&2 + cronexit 255 fi if [ "${SILENT}" = "yes" ]; then - echo "ERROR: SILENT option has been removed, please use QUIET instead" >&2 - cronexit 1 + echo "ERROR: SILENT option has been removed and should be removed from .plexupdate" >&2 + echo " Use QUIET or -q instead" >&2 + cronexit 255 fi -# send all stdout to /dev/null -if [ "${QUIET}" = "yes" ]; then - exec 5>&1 >/dev/null +if [ ! -z "${RELEASE}" ]; then + echo "ERROR: RELEASE keyword is deprecated and should be removed from .plexupdate" >&2 + echo " Use DISTRO and BUILD instead to manually select what to install (check README.md)" >&2 + cronexit 255 fi if [ "${AUTOUPDATE}" == "yes" ]; then @@ -214,18 +210,18 @@ if [ "${AUTOUPDATE}" == "yes" ]; then fi pushd "$(dirname "$0")" >/dev/null if [ ! -d .git ]; then - echo "Error: This is not a git repository, auto update only works if you've done a git clone" >&2 + echo "ERROR: This is not a git repository, auto update only works if you've done a git clone" >&2 cronexit 1 fi git status | grep "git commit -a" >/dev/null 2>/dev/null if [ $? -eq 0 ]; then - echo "Error: You have made changes to the script, cannot auto update" >&2 + echo "ERROR: You have made changes to the script, cannot auto update" >&2 cronexit 1 fi echo -n "Auto updating..." git pull >/dev/null if [ $? -ne 0 ]; then - echo 'Error: Unable to update git, try running "git pull" manually to see what is wrong' >&2 + echo 'ERROR: Unable to update git, try running "git pull" manually to see what is wrong' >&2 cronexit 1 fi echo "OK" @@ -243,7 +239,7 @@ if [ "${AUTOUPDATE}" == "yes" ]; then if [ -f "$0" ]; then /bin/bash "$0" -U ${ALLARGS[@]} else - echo "Error: Unable to relaunch, couldn't find $0" >&2 + echo "ERROR: Unable to relaunch, couldn't find $0" >&2 cronexit 1 fi else @@ -254,14 +250,14 @@ fi # Sanity check if [ "${EMAIL}" == "" -o "${PASS}" == "" ] && [ "${PUBLIC}" == "no" ] && [ ! -f /tmp/kaka ]; then - echo "Error: Need username & password to download PlexPass version. Otherwise run with -p to download public version." >&2 + echo "ERROR: Need username & password to download PlexPass version. Otherwise run with -p to download public version." >&2 cronexit 1 fi if [ "${AUTOINSTALL}" == "yes" -o "${AUTOSTART}" == "yes" ]; then id | grep -i 'uid=0(' 2>&1 >/dev/null if [ $? -ne 0 ]; then - echo "Error: You need to be root to use autoinstall/autostart option." >&2 + echo "ERROR: You need to be root to use autoinstall/autostart option." >&2 cronexit 1 fi fi @@ -270,7 +266,7 @@ fi # Remove any ~ or other oddness in the path we're given DOWNLOADDIR="$(eval cd ${DOWNLOADDIR// /\\ } ; if [ $? -eq 0 ]; then pwd; fi)" if [ -z "${DOWNLOADDIR}" ]; then - echo "Error: Download directory does not exist or is not a directory" >&2 + echo "ERROR: Download directory does not exist or is not a directory" >&2 cronexit 1 fi From ca4320db8e79a7aff595f21fb04655671fdfa571 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Thu, 11 Aug 2016 18:55:14 -0700 Subject: [PATCH 03/13] Simplify DOWNLOADDIR check to remove dangerous eval call --- plexupdate.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plexupdate.sh b/plexupdate.sh index 6ebc79c..882e201 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -264,7 +264,7 @@ fi # Remove any ~ or other oddness in the path we're given -DOWNLOADDIR="$(eval cd ${DOWNLOADDIR// /\\ } ; if [ $? -eq 0 ]; then pwd; fi)" +DOWNLOADDIR="$(cd ${DOWNLOADDIR// /\\ } && pwd)" if [ -z "${DOWNLOADDIR}" ]; then echo "ERROR: Download directory does not exist or is not a directory" >&2 cronexit 1 From faec4b0111792be57d8a8204cff62be7407a7357 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Thu, 11 Aug 2016 19:02:37 -0700 Subject: [PATCH 04/13] Code cleanup to standardize on conditional format * Replace all checks for an empty string with `-z` * Replace all instances of `==` inside single brackets with `=` These are all intentionally in a single commit so they can easily be undone if anyone has a problem with the changes. --- plexupdate.sh | 53 +++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index 882e201..51ac819 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -73,7 +73,7 @@ fi # Allow manual control of configfile HASCFG="${@: -1}" -if [ ! "${HASCFG}" = "" -a ! "${HASCFG:0:1}" = "-" ]; then +if [ ! -z "${HASCFG}" -a ! "${HASCFG:0:1}" = "-" ]; then if [ -f "${HASCFG}" ]; then source "${HASCFG}" else @@ -83,7 +83,7 @@ if [ ! "${HASCFG}" = "" -a ! "${HASCFG:0:1}" = "-" ]; then else # Load settings from config file if it exists # Also, respect SUDO_USER and try that first - if [ "${SUDO_USER}" != "" ]; then + if [ ! -z "${SUDO_USER}" ]; then # Make sure nothing bad comes from this (since we use eval) ERROR=0 if [[ $SUDO_USER == *";"* ]]; then ERROR=1 ; # Allows more commands @@ -101,11 +101,11 @@ else # Try using original user's config CONFIGDIR="$( eval cd ~${SUDO_USER} 2>/dev/null && pwd )" - if [ "${CONFIGDIR}" == "" ]; then + if [ -z "${CONFIGDIR}" ]; then echo "WARNING: SUDO_USER \"${SUDO_USER}\" does not have a valid home directory, ignoring." >&2 fi - if [ "${CONFIGDIR}" != "" -a -f "${CONFIGDIR}/.plexupdate" ]; then + if [ ! -z "${CONFIGDIR}" -a -f "${CONFIGDIR}/.plexupdate" ]; then #echo "INFO: Using \"${SUDO_USER}\" configuration: ${CONFIGDIR}/.plexupdate" source "${CONFIGDIR}/.plexupdate" elif [ -f ~/.plexupdate ]; then @@ -203,7 +203,7 @@ if [ ! -z "${RELEASE}" ]; then cronexit 255 fi -if [ "${AUTOUPDATE}" == "yes" ]; then +if [ "${AUTOUPDATE}" = "yes" ]; then if hash git 2>/dev/null; then echo "ERROR: You need to have git installed for this to work" >&2 cronexit 1 @@ -249,12 +249,12 @@ if [ "${AUTOUPDATE}" == "yes" ]; then fi # Sanity check -if [ "${EMAIL}" == "" -o "${PASS}" == "" ] && [ "${PUBLIC}" == "no" ] && [ ! -f /tmp/kaka ]; then +if [ -z "${EMAIL}" -o -z "${PASS}" ] && [ "${PUBLIC}" = "no" ] && [ ! -f /tmp/kaka ]; then echo "ERROR: Need username & password to download PlexPass version. Otherwise run with -p to download public version." >&2 cronexit 1 fi -if [ "${AUTOINSTALL}" == "yes" -o "${AUTOSTART}" == "yes" ]; then +if [ "${AUTOINSTALL}" = "yes" -o "${AUTOSTART}" = "yes" ]; then id | grep -i 'uid=0(' 2>&1 >/dev/null if [ $? -ne 0 ]; then echo "ERROR: You need to be root to use autoinstall/autostart option." >&2 @@ -270,8 +270,8 @@ if [ -z "${DOWNLOADDIR}" ]; then cronexit 1 fi -if [ "${DISTRO_INSTALL}" == "" ]; then - if [ "${DISTRO}" == "" -a "${BUILD}" == "" ]; then +if [ -z "${DISTRO_INSTALL}" ]; then + if [ -z "${DISTRO}" -a -z "${BUILD}" ]; then # Detect if we're running on redhat instead of ubuntu if [ -f /etc/redhat-release ]; then REDHAT=yes @@ -284,12 +284,12 @@ if [ "${DISTRO_INSTALL}" == "" ]; then DISTRO="ubuntu" DISTRO_INSTALL="${DEBIAN_INSTALL}" fi - elif [ "${DISTRO}" == "" -o "${BUILD}" == "" ]; then + elif [ -z "${DISTRO}" -o -z "${BUILD}" ]; then echo "ERROR: You must define both DISTRO and BUILD" >&2 cronexit 255 fi else - if [ "${DISTRO}" == "" -o "${BUILD}" == "" ]; then + if [ -z "${DISTRO}" -o -z "${BUILD}" ]; then echo "Using custom DISTRO_INSTALL requires custom DISTRO and BUILD too" >&2 cronexit 255 fi @@ -343,8 +343,7 @@ if [ -f /tmp/kaka_token ]; then TOKEN=$(cat /tmp/kaka_token) fi -# If user wants, we skip authentication, but only if previous auth exists -if [ "${PUBLIC}" == "no" ]; then +if [ "${PUBLIC}" = "no" ]; then echo -n "Authenticating..." # Clean old session @@ -386,7 +385,7 @@ elif [ "$PUBLIC" != "no" ]; then URL_DOWNLOAD=${URL_DOWNLOAD_PUBLIC} fi -if [ "${LISTOPTS}" == "yes" ]; then +if [ "${LISTOPTS}" = "yes" ]; then opts="$(wget --load-cookies /tmp/kaka --save-cookies /tmp/kaka --keep-session-cookies "${URL_DOWNLOAD}" -O - 2>/dev/null | grep -oe '"label"[^}]*' | grep -v Download | sed 's/"label":"\([^"]*\)","build":"\([^"]*\)","distro":"\([^"]*\)".*/"\3" "\2" "\1"/' | uniq | sort)" eval opts=( "DISTRO" "BUILD" "DESCRIPTION" "======" "=====" "==============================================" $opts ) @@ -394,9 +393,9 @@ if [ "${LISTOPTS}" == "yes" ]; then DISTRO= for X in "${opts[@]}" ; do - if [ "$DISTRO" == "" ]; then + if [ -z "$DISTRO" ]; then DISTRO="$X" - elif [ "$BUILD" == "" ]; then + elif [ -z "$BUILD" ]; then BUILD="$X" else printf "%-12s %-30s %s\n" "$DISTRO" "$BUILD" "$X" @@ -413,9 +412,9 @@ fi # Set "X-Plex-Token" to the auth token, if no token is specified or it is invalid, the list will return public downloads by default DOWNLOAD=$(wget --header "X-Plex-Token:"${TOKEN}"" --load-cookies /tmp/kaka --save-cookies /tmp/kaka --keep-session-cookies "${URL_DOWNLOAD}" -O - 2>/dev/null | grep -ioe '"label"[^}]*' | grep -i "\"distro\":\"${DISTRO}\"" | grep -i "\"build\":\"${BUILD}\"" | grep -m1 -ioe 'https://[^\"]*' ) -echo -e "OK" +echo "OK" -if [ "${DOWNLOAD}" == "" ]; then +if [ -z "${DOWNLOAD}" ]; then echo "ERROR: Unable to retrieve the URL needed for download (Query DISTRO: $DISTRO, BUILD: $BUILD)" >&2 cronexit 3 fi @@ -426,7 +425,7 @@ if [ $? -ne 0 ]; then cronexit 3 fi -if [ "${PRINT_URL}" == "yes" ]; then +if [ "${PRINT_URL}" = "yes" ]; then if [ "${QUIET}" = "yes" ]; then echo "${DOWNLOAD}" >&5 else @@ -442,7 +441,7 @@ SKIP_DOWNLOAD="no" if [ "${REDHAT}" != "yes" ]; then INSTALLED_VERSION=$(dpkg-query -s plexmediaserver 2>/dev/null | grep -Po 'Version: \K.*') else - if [ "${AUTOSTART}" == "no" ]; then + if [ "${AUTOSTART}" = "no" ]; then echo "WARNING: Your distribution may require the use of the AUTOSTART [-s] option for the service to start after the upgrade completes." fi INSTALLED_VERSION=$(rpm -qv plexmediaserver 2>/dev/null) @@ -460,7 +459,7 @@ if [ -f "${DOWNLOADDIR}/${FILENAME}" -a "${FORCE}" != "yes" ]; then SKIP_DOWNLOAD="yes" fi -if [ "${SKIP_DOWNLOAD}" == "no" ]; then +if [ "${SKIP_DOWNLOAD}" = "no" ]; then if [ -f "${DOWNLOADDIR}/${FILENAME}" ]; then echo "Note! File exists, but asked to overwrite with new copy" fi @@ -475,7 +474,7 @@ if [ "${SKIP_DOWNLOAD}" == "no" ]; then echo "OK" fi -if [ ! "${PLEXSERVER}" = "" -a "${AUTOINSTALL}" == "yes" ]; then +if [ ! -z "${PLEXSERVER}" -a "${AUTOINSTALL}" = "yes" ]; then # Check if server is in-use before continuing (thanks @AltonV, @hakong and @sufr3ak)... if ! wget --no-check-certificate -q -O - https://${PLEXSERVER}:32400/status/sessions | grep -q '' ; then echo "Server ${PLEXSERVER} is currently being used by one or more users, skipping installation. Please run again later" @@ -483,12 +482,12 @@ if [ ! "${PLEXSERVER}" = "" -a "${AUTOINSTALL}" == "yes" ]; then fi fi -if [ "${AUTOINSTALL}" == "yes" ]; then +if [ "${AUTOINSTALL}" = "yes" ]; then sudo ${DISTRO_INSTALL} "${DOWNLOADDIR}/${FILENAME}" fi -if [ "${AUTODELETE}" == "yes" ]; then - if [ "${AUTOINSTALL}" == "yes" ]; then +if [ "${AUTODELETE}" = "yes" ]; then + if [ "${AUTOINSTALL}" = "yes" ]; then rm -rf "${DOWNLOADDIR}/${FILENAME}" echo "Deleted \"${FILENAME}\"" else @@ -496,8 +495,8 @@ if [ "${AUTODELETE}" == "yes" ]; then fi fi -if [ "${AUTOSTART}" == "yes" ]; then - if [ "${REDHAT}" == "no" ]; then +if [ "${AUTOSTART}" = "yes" ]; then + if [ "${REDHAT}" = "no" ]; then echo "The AUTOSTART [-s] option may not be needed on your distribution." fi # Check for systemd From 18f673e96a78c966fafb4a2e107a3158a39e18d3 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Thu, 11 Aug 2016 21:01:13 -0700 Subject: [PATCH 05/13] Fix for hash check to make it actually work --- plexupdate.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index 51ac819..f7171ac 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -66,7 +66,7 @@ DEBIAN_INSTALL="dpkg -i" DISTRO_INSTALL="" # Sanity, make sure wget is in our path... -if hash wget 2>/dev/null; then +if ! hash wget 2>/dev/null; then echo "ERROR: This script requires wget in the path. It could also signify that you don't have the tool installed." >&2 exit 1 fi @@ -204,7 +204,7 @@ if [ ! -z "${RELEASE}" ]; then fi if [ "${AUTOUPDATE}" = "yes" ]; then - if hash git 2>/dev/null; then + if ! hash git 2>/dev/null; then echo "ERROR: You need to have git installed for this to work" >&2 cronexit 1 fi @@ -500,7 +500,7 @@ if [ "${AUTOSTART}" = "yes" ]; then echo "The AUTOSTART [-s] option may not be needed on your distribution." fi # Check for systemd - if [ -f "/bin/systemctl" ]; then + if ! hash systemctl 2>/dev/null; then systemctl start plexmediaserver.service else /sbin/service plexmediaserver start From dab2cf564d048c6cc19e9f12ff92e30d6c3a0744 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Thu, 11 Aug 2016 21:03:05 -0700 Subject: [PATCH 06/13] Clean up bash error and fix -f description --- plexupdate.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index f7171ac..fa48d92 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -31,7 +31,7 @@ #################################################################### # Quick-check before we allow bad things to happen if [ -z "${BASH_VERSINFO}" ]; then - echo "ERROR: You must execute this script with BASH" + echo "ERROR: You must execute this script with BASH" >&2 exit 255 fi #################################################################### @@ -141,7 +141,7 @@ usage() { echo " -c Cron mode, only fatal errors return non-zero cronexit code" echo " -d Auto delete after auto install" echo " -f Force download even if it's the same version or file" - echo " already exists (WILL NOT OVERWRITE)" + echo " already exists" echo " -h This help" echo " -l List available builds and distros" echo " -p Public Plex Media Server version" From 439286ba2ead2688c0c774954da561eb11718339 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Thu, 11 Aug 2016 21:04:35 -0700 Subject: [PATCH 07/13] Make -c only produce output if something is actually done --- plexupdate.sh | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index fa48d92..77c0e1a 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -125,8 +125,9 @@ URL_DOWNLOAD_PUBLIC=https://plex.tv/api/downloads/1.json cronexit() { # Don't give anything but true error codes if in CRON mode + rawexit=$1 if [ "${CRON}" = "yes" -a $1 -gt 1 -a $1 -lt 255 ]; then - cronexit 0 + exit 0 fi exit $1 } @@ -203,6 +204,15 @@ if [ ! -z "${RELEASE}" ]; then cronexit 255 fi +if [ "${CRON}" = "yes" -a "${QUIET}" = "no" ]; then + # If running in cron mode, redirect STDOUT to temporary file + stdoutlog="$(mktemp)" + exec 3>&1 >"${stdoutlog}" +elif [ "${QUIET}" = "yes" ]; then + # Redirect STDOUT to dev null. Use >&3 if you really, really, REALLY need to print to STDOUT + exec 3>&1 > /dev/null +fi + if [ "${AUTOUPDATE}" = "yes" ]; then if ! hash git 2>/dev/null; then echo "ERROR: You need to have git installed for this to work" >&2 @@ -321,6 +331,11 @@ keypair() { # Setup an cronexit handler so we cleanup function cleanup { + if [ "${CRON}" = yes -a "${rawexit}" -ne 5 -a -f "${stdoutlog}" ]; then + exec 1>&3 + cat "${stdoutlog}" + fi + rm "${stdoutlog}" 2>/dev/null >/dev/null rm /tmp/postdata 2>/dev/null >/dev/null rm /tmp/raw 2>/dev/null >/dev/null rm /tmp/failcause 2>/dev/null >/dev/null @@ -398,7 +413,11 @@ if [ "${LISTOPTS}" = "yes" ]; then elif [ -z "$BUILD" ]; then BUILD="$X" else - printf "%-12s %-30s %s\n" "$DISTRO" "$BUILD" "$X" + if [ "${QUIET}" = "yes" ]; then + printf "%-12s %-30s %s\n" "$DISTRO" "$BUILD" "$X" >&3 + else + printf "%-12s %-30s %s\n" "$DISTRO" "$BUILD" "$X" + fi BUILD= DISTRO= fi @@ -427,7 +446,7 @@ fi if [ "${PRINT_URL}" = "yes" ]; then if [ "${QUIET}" = "yes" ]; then - echo "${DOWNLOAD}" >&5 + echo "${DOWNLOAD}" >&3 else echo "${DOWNLOAD}" fi From 610fb596a8e783871fbe7f293d3a1be29e68a592 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Thu, 11 Aug 2016 21:28:30 -0700 Subject: [PATCH 08/13] Update README with a few fixes and more cron info --- README.md | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 94a6ce2..58d5dad 100644 --- a/README.md +++ b/README.md @@ -13,26 +13,26 @@ It's fairly easy, but let's take it step by step (if it seems too much, skip to ####Using wget and unzip -You can either download it as a [zip file](https://github.com/mrworf/plexupdate/archive/master.zip) and unzip it on your server. +Download it as a [zip file](https://github.com/mrworf/plexupdate/archive/master.zip) and unzip it on your server. ``` wget https://github.com/mrworf/plexupdate/archive/master.zip && unzip master.zip && mv plexupdate-master plexupdate && rm master.zip ``` Note that unzip is requered (`sudo apt-get install unzip`). ####Using git to clone (Recommended) -Using git is way eaiser and recommended, if you ask me. +Using git is way easier and recommended, if you ask me. ``` git clone https://github.com/mrworf/plexupdate.git ``` Note that git is requered (`sudo apt-get install git-all`) -The main benefit with git clone is that you can update to latest version very easily. *Said in another way*: Auto update only works, if you've done a git clone. +The main benefit with git clone is that you can update to latest version very easily. If you want to use the auto update feature, you must be using a git clone. ## 2. Setting it up plexupdate.sh looks for a file named `.plexupdate` located in your home directory. Please note that I'm referring to the home directory of the user who is running the plexupdate.sh ... If that user is someone else (root for instance) you'll need to make sure that user has the config file set up properly as well. -The contents of this file usually is +The contents of this file are usually: ``` EMAIL="my.email@plex-server.com" @@ -40,16 +40,16 @@ PASS="my-secret-plex-password" DOWNLOADDIR="/a/folder/to/save/the/files/in" ``` -Obviously you need to change these three so they match your account information. And if you don't put anything as a `DOWNLOADDIR`, the tool will use the folder you're executing the script from. So take care. +Obviously you need to change these so they match your account information. And if you don't put anything as a `DOWNLOADDIR`, the tool will use the folder you're executing the script from. So take care. ## 3. Advanced options -You can point out a different file than ```.plexupdate``` by providing it as the last argument to the script. It HAS to be the LAST argument, or it will be ignored. Any options set by the config file can be overriden with commandline options. +You can point out a different file than ```.plexupdate``` by providing it as the last argument to the script. It HAS to be the LAST argument, or it will be ignored. Any options set by the config file can be overriden with command-line options. There are also a few additional options for the more enterprising user. Setting any of these to `yes` will enable the function. - PLEXSERVER - If set, and combined with AUTOINSTALL, the script will automatically check if server is in-use and deferr the update. Great for crontab users. PLEXSERVER should be set to the IP/DNS of your Plex Media Server, which typically is 127.0.0.1 + If set, and combined with AUTOINSTALL, the script will automatically check if the server is in-use and deferr the update. Great for crontab users. PLEXSERVER should be set to the IP/DNS of your Plex Media Server, which typically is 127.0.0.1 - AUTOUPDATE Makes plexupdate.sh automatically update itself using git. Note! This will fail if git isn't available on the command line. - AUTOINSTALL @@ -59,11 +59,11 @@ There are also a few additional options for the more enterprising user. Setting - PUBLIC The default behavior of plexupdate.sh is to download the PlexPass edition of Plex Media Center. Setting this option to `yes` will make it download the public version instead. If this is yes, then `EMAIL` and `PASS` is no longer needed. - FORCE - Normally plexupdate.sh will avoid downloading a file it already has or if it's the same as the installed version, but this allows you to override. + Normally plexupdate.sh will avoid downloading a file it already has or if it's the same as the installed version. Using this option will force it to download again and, if you have AUTOINSTALL set, reinstall it. - PRINT_URL Authenticate, fetch the download URL, print it, and then exit. - DISTRO_INSTALL - The commandline used to install packages, only change if you need special options. Natively supports Debian and Redhat, so need to set this for these systems. + The command used to install packages, only change if you need special options. Natively supports Debian and Redhat, so you don't need to set this for these systems. NOTE! If you define this, you MUST define DISTRO and BUILD - DISTRO and BUILD Override which version to download, use -l option to see what you can select. @@ -72,15 +72,9 @@ Most of these options can be specified on the command-line as well, this is just ### Using it from CRON -It seems quite popular to run this via crontab, which is fine. But the behavior of the script has been somewhat, shall we say, annoying. +If you want to use plexupdate as either a cron job or as a [systemd job](https://github.com/mrworf/plexupdate/wiki/Running-plexupdate-daily-as-a-systemd-timer), the -c option should do what you want. All non-error exit codes will be set to 0 and no output will be printed to stdout unless something has actually been done. (a new version was downloaded, installed, etc) -Starting today, the ```-C``` option is deprecated and will give an error to check the docs. The new version is ```-c``` and will make sure that only fatal errors are reported back via the exit code. No more 2, 3, 4 or 5 exitcodes. They are converted into 0. Combining this option with ```-q``` will hide any and all non-essential output from the script as well. Only error messages are emitted, so if it fails, you'll know why. - -Take a look at the Wiki, and find the article called [Running plexupdate daily as a systemd timer](https://github.com/mrworf/plexupdate/wiki/Running-plexupdate-daily-as-a-systemd-timer). - -## 4. Command-line - -I'm going to be lazy, just run the tool with `-h` and you'll find out what you can do. It will basically be a mirror of what section 3 just stated :-) +If you don't even want to know when something has been done, you can combine this with the -q option and you will only receive output in the event of an error. Everything else will just silenty finish without producing any output. # Running it @@ -112,6 +106,6 @@ The username and password for http://plex.tv ## My password is rejected even though correct -If you use certain characters, such as dollar sign, in your password, bash will interpret that as a reference to a variable. To resolve this, enclose your password with single quotes instead of the normal quotes. +If you use certain characters (such as `$`) in your password, bash will interpret that as a reference to a variable. To resolve this, enclose your password within single quotes (`'`) instead of the normal quotes (`"`). -Ie, `PASS="MyP4$$w0rD"` will not work, but changing to it to `PASS='MyP4$$w0rD'` will +i.e. `PASS="MyP4$$w0rD"` will not work, but changing to it to `PASS='MyP4$$w0rD'` will From 7470576cd6d0dc22eb3f1a04afe8f10b7e380836 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Tue, 16 Aug 2016 12:12:56 -0700 Subject: [PATCH 09/13] Standardize on all uppercase variables --- plexupdate.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index 77c0e1a..41c2245 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -125,7 +125,7 @@ URL_DOWNLOAD_PUBLIC=https://plex.tv/api/downloads/1.json cronexit() { # Don't give anything but true error codes if in CRON mode - rawexit=$1 + RAWEXIT=$1 if [ "${CRON}" = "yes" -a $1 -gt 1 -a $1 -lt 255 ]; then exit 0 fi @@ -206,8 +206,8 @@ fi if [ "${CRON}" = "yes" -a "${QUIET}" = "no" ]; then # If running in cron mode, redirect STDOUT to temporary file - stdoutlog="$(mktemp)" - exec 3>&1 >"${stdoutlog}" + STDOUTLOG="$(mktemp)" + exec 3>&1 >"${STDOUTLOG}" elif [ "${QUIET}" = "yes" ]; then # Redirect STDOUT to dev null. Use >&3 if you really, really, REALLY need to print to STDOUT exec 3>&1 > /dev/null @@ -331,11 +331,11 @@ keypair() { # Setup an cronexit handler so we cleanup function cleanup { - if [ "${CRON}" = yes -a "${rawexit}" -ne 5 -a -f "${stdoutlog}" ]; then + if [ "${CRON}" = yes -a "${RAWEXIT}" -ne 5 -a -f "${STDOUTLOG}" ]; then exec 1>&3 - cat "${stdoutlog}" + cat "${STDOUTLOG}" fi - rm "${stdoutlog}" 2>/dev/null >/dev/null + rm "${STDOUTLOG}" 2>/dev/null >/dev/null rm /tmp/postdata 2>/dev/null >/dev/null rm /tmp/raw 2>/dev/null >/dev/null rm /tmp/failcause 2>/dev/null >/dev/null From 6c18a2b14703c75366ea558d211dee5163b4802b Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Tue, 16 Aug 2016 12:18:38 -0700 Subject: [PATCH 10/13] Print diagnostic message for autostart to stderr --- plexupdate.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plexupdate.sh b/plexupdate.sh index 41c2245..dd464a7 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -516,7 +516,7 @@ fi if [ "${AUTOSTART}" = "yes" ]; then if [ "${REDHAT}" = "no" ]; then - echo "The AUTOSTART [-s] option may not be needed on your distribution." + echo "The AUTOSTART [-s] option may not be needed on your distribution." >&2 fi # Check for systemd if ! hash systemctl 2>/dev/null; then From d2ccacb3e47baa2b738acc0afa517258b3366ba9 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Tue, 16 Aug 2016 12:26:09 -0700 Subject: [PATCH 11/13] Fix service checking for autostart and add fallback error --- plexupdate.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index dd464a7..cca3eee 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -519,10 +519,15 @@ if [ "${AUTOSTART}" = "yes" ]; then echo "The AUTOSTART [-s] option may not be needed on your distribution." >&2 fi # Check for systemd - if ! hash systemctl 2>/dev/null; then + if hash systemctl 2>/dev/null; then systemctl start plexmediaserver.service + elif hash service 2>/dev/null; then + service plexmediaserver start + elif [ -x /etc/init.d/plexmediaserver ]; then + /etc/init.d/plexmediaserver start else - /sbin/service plexmediaserver start + echo "ERROR: AUTOSTART was specified but no startup scripts were found for 'plexmediaserver'." >&2 + cronexit 1 fi fi From 9d5158cbd99b971bef46fd1180b359ae319e251e Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Tue, 16 Aug 2016 14:35:48 -0700 Subject: [PATCH 12/13] Check for existence of downloaddir instead of empty string --- plexupdate.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plexupdate.sh b/plexupdate.sh index cca3eee..27ecefb 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -275,7 +275,7 @@ fi # Remove any ~ or other oddness in the path we're given DOWNLOADDIR="$(cd ${DOWNLOADDIR// /\\ } && pwd)" -if [ -z "${DOWNLOADDIR}" ]; then +if [ ! -d "${DOWNLOADDIR}" ]; then echo "ERROR: Download directory does not exist or is not a directory" >&2 cronexit 1 fi From 2ef53f85884d706f46bd571dfb478f39b5ed2d89 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Tue, 16 Aug 2016 14:36:40 -0700 Subject: [PATCH 13/13] Go back to using eval for downloaddir --- plexupdate.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plexupdate.sh b/plexupdate.sh index 27ecefb..ce73b85 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -274,7 +274,7 @@ fi # Remove any ~ or other oddness in the path we're given -DOWNLOADDIR="$(cd ${DOWNLOADDIR// /\\ } && pwd)" +DOWNLOADDIR="$(eval cd ${DOWNLOADDIR// /\\ } && pwd)" if [ ! -d "${DOWNLOADDIR}" ]; then echo "ERROR: Download directory does not exist or is not a directory" >&2 cronexit 1