From 6a00b660a9f598a5d4ebdfce569358bce0bcc032 Mon Sep 17 00:00:00 2001 From: Henric Andersson Date: Wed, 16 Nov 2016 21:20:50 -0800 Subject: [PATCH 01/42] Initial commit after cleaning up log --- README.md | 20 +- extras/cronwrapper | 41 +++ plexupdate.sh | 817 +++++++++++++++++++-------------------------- 3 files changed, 398 insertions(+), 480 deletions(-) mode change 100644 => 100755 README.md create mode 100755 extras/cronwrapper diff --git a/README.md b/README.md old mode 100644 new mode 100755 index b3e0fb6..31faf64 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ wget https://github.com/mrworf/plexupdate/archive/master.zip && unzip master.zip Note that unzip is required (`sudo apt-get install unzip`). ####Using git to clone (Recommended) -Using git is way easier 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 ``` @@ -54,11 +54,11 @@ There are also a few additional options for the more enterprising user. Setting Makes plexupdate.sh automatically update itself using git. Note! This will fail if git isn't available on the command line. - AUTOINSTALL Automatically installs the newly downloaded version. Currently works for Debian based systems as well as rpm based distros. Will fail miserably if you're not root. -- AUTODELETE +- AUTODELETE Once successfully downloaded and installed, it will delete the package (want not, waste not? ;-)) -- PUBLIC +- 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 +- FORCE 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 UNLESS the file already downloaded has the correct checksum. If you have AUTOINSTALL set, plexupdate.sh will then reinstall it. - FORCEALL Using this option will force plexupdate.sh to override the checksum check and will download the file again, and if you have AUTOINSTALL set, will reinstall it. @@ -74,16 +74,16 @@ Most of these options can be specified on the command-line as well, this is just ### Using it from CRON -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) +Generally, you copy the cronwrapper from the extras folder into the ```/etc/cron.weekly``` and edit the script to suite your needs. It will require you to create a configuration file (or at least, it's highly recommended). It also provides logging via syslog if needed. Should the script fail, it will print why and in most cases, this will result in an email to you. -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 silently finish without producing any output. +[Also checkout the newly written wiki for more details] ### Command Line Options Several new command line options are available. They can be specified in any order. - ```--config ``` - Defines the location the script should look for the config file. + Defines the location the script should look for the config file. - ```--email ``` Email to sign in to Plex.tv - ```--pass ``` @@ -95,8 +95,8 @@ Several new command line options are available. They can be specified in any ord - ```--port ``` This is the port that Plex Media Server uses. - ```--saveconfig``` - Saves the configuration as it is currently. This will take whatever is in the config file, plus whatever is specified on the command line and will save the config file with that information. Any information in the config file that plexupdate.sh does not understand or use WILL BE LOST. - + Saves the configuration as it is currently. This will take whatever is in the config file, plus whatever is specified on the command line and will save the config file with that information. Any information in the config file that plexupdate.sh does not understand or use WILL BE LOST. + ### Logs The script now outputs everything to a file (by default `/tmp/plexupdate.log`). This log ***MAY*** contain passwords, so if you post it online, ***USE CAUTION***. @@ -131,7 +131,7 @@ sudo plexupdate/plexupdate.sh -p -u -a ## What username and password are you talking about -The username and password for http://plex.tv +The username and password for http://plex.tv ## My password is rejected even though correct diff --git a/extras/cronwrapper b/extras/cronwrapper new file mode 100755 index 0000000..abcb3e4 --- /dev/null +++ b/extras/cronwrapper @@ -0,0 +1,41 @@ +#!/bin/bash + +CONFIGURED=false + +CONF= +SCRIPT=/home/john.doe/plexupdate/plexupdate.sh +LOGGING=false + +if ! $CONFIGURED; then + echo "ERROR: You have not configured this script" >&2 + exit 255 +fi + +if [ ! -z "$CONF" ]; then + # We have a config file, prefix it with parameter + CONF="--config \"${CONF}\"" +fi + +LOGFILE=$(mktemp /tmp/plexupdate.cron.XXXX) + +RET=0 +if $LOGGING; then + "${SCRIPT}" "${CONF}" 2>&1 | tee ${LOGFILE} | logger -t plexupdate -p daemon.info + if grep -q '^ERROR:' ${LOGFILE}; then + RET=1 + fi +else + "${SCRIPT}" "${CONF}" 2>&1 >${LOGFILE} + RET=$? +fi + +if [ $RET -ne 0 -a $RET -ne 2 -a $RET -ne 5 ]; then + # Make sure user gets an email about this + cat ${LOGFILE} >&2 +else + # Nah, not important + RET=0 +fi + +rm "${LOGFILE}" 2>/dev/null +exit $RET diff --git a/plexupdate.sh b/plexupdate.sh index 7a69840..67f3772 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -40,89 +40,6 @@ if [ -z "${BASH_VERSINFO}" ]; then exit 255 fi -#################################################################### -# Functions for logging -if [ -z ${FILE_STDOUTLOG} ]; then - FILE_STDOUTLOG="/tmp/plexupdate.log" -fi - -# This is used in CRON mode to output only the lines from this run of the script -OUTPUTLINES=0 - -timestamp() { - date +"%F %T" -} - -Log() { - printf "%s - %s: %s\n" "$(timestamp)" "$1" "$2" >>${FILE_STDOUTLOG} - OUTPUTLINES=$((OUTPUTLINES+=1)) -} - -# $1 - Type of message (INFO, WARNING, ERROR, etc) -# $2 - Actual message -# $3 - Should include newline (assumed yes. If this equals "no" will not include it) -LogStdOut() { - if [ ! -z "$1" ]; then - printf "%s: %s" "$1" "$2" - else - printf "%s" "$2" - fi - if [ -z "$3" -o ! "$3" = "no" ]; then - printf "\n" - fi -} - -# $1 - Filename to add to log -# $2 - Type to log file contents as (INFO, ERROR, WARNING) -LogFileContents() { - while read line; - do - Log "$2" "$line" - done <"$1" -} - -# $1 - Message -# $2 - Print newline? defaults to yes -infoLog() { - Log "INFO" "$1" - if [ "${CRON}" = "no" ]; then - LogStdOut "" "$1" "$2" - fi -} - -infoLogNoNewline() { - infoLog "$1" "no" -} - -errorLog() { - Log "ERROR" "$1" >&2 - if [ "${CRON}" = "no" ]; then - LogStdOut "ERROR" "$1" "$2" >&2 - fi -} - -errorLogNoNewline() { - errorLog "$1" "no" -} - -warningLog() { - Log "WARNING" "$1" >&2 - if [ "${CRON}" = "no" ]; then - LogStdOut "WARNING" "$1" "$2" >&2 - fi -} - -warningLogNoNewline() { - warningLog "$1" "no" -} - -# Output log separator to make it easier to see each separate script execution -infoLog "##### Begin PlexUpdate #####" >/dev/null -# Output the arguments for this execution (NOTE: passwords entered on command line will get logged this way) -ALLARGS="$@" -infoLog "Args: ${ALLARGS}" >/dev/null -unset ALLARGS - ################################################################# # Don't change anything below this point, use a .plexupdate file # in your home directory to override this section. @@ -138,12 +55,11 @@ PLEXPORT=32400 # (aka "Advanced" settings, can be overriden with config file) FORCE=no FORCEALL=no -PUBLIC=no +PUBLIC=yes AUTOINSTALL=no AUTODELETE=no AUTOUPDATE=no AUTOSTART=no -CRON=no QUIET=no ARCH=$(uname -m) IGNOREAUTOUPDATE=no @@ -156,31 +72,33 @@ REDHAT_INSTALL="yum -y install" DEBIAN_INSTALL="dpkg -i" DISTRO_INSTALL="" +# 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' +URL_DOWNLOAD_PUBLIC='https://plex.tv/api/downloads/1.json' + FILE_POSTDATA=$(mktemp /tmp/plexupdate.postdata.XXXX) -FILE_RAW=$(mktemp /tmp/plexupdate.raw.XXXX) -FILE_FAILCAUSE=$(mktemp /tmp/plexupdate.failcause.XXXX) +FILE_RAW=$(mktemp /tmp/plexupdate.failcause.XXXX) +FILE_FAILCAUSE=$(mktemp /tmp/plexupdate.raw.XXXX) FILE_KAKA=$(mktemp /tmp/plexupdate.kaka.XXXX) FILE_SHA=$(mktemp /tmp/plexupdate.sha.XXXX) FILE_WGETLOG=$(mktemp /tmp/plexupdate.wget.XXXX) -FILE_CMDLOG=$(mktemp /tmp/plexupdate.cmds.XXXX) -FILE_CMDERR=$(mktemp /tmp/plexupdate.errs.XXXX) FILE_LOCAL=$(mktemp /tmp/plexupdate.local.XXXX) FILE_REMOTE=$(mktemp /tmp/plexupdate.remote.XXXX) -# 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 -URL_DOWNLOAD_PUBLIC=https://plex.tv/api/downloads/1.json - ###################################################################### # Functions for rest of script -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 - exit 0 - fi - exit $1 + +warn() { + echo "WARNING: $@" >&1 +} + +info() { + echo "$@" >&1 +} + +error() { + echo "ERROR: $@" >&2 } usage() { @@ -188,7 +106,6 @@ usage() { echo "" echo "" echo " -a Auto install if download was successful (requires root)" - 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 unless checksum passes" @@ -197,7 +114,7 @@ usage() { echo " -l List available builds and distros" echo " -p Public Plex Media Server version" echo " -P Show progressbar when downloading big files" - echo " -q Quiet mode. No stdout, only stderr and cronexit codes" + echo " -q Quiet mode. No stdout, only stderr and exit codes" echo " -r Print download URL and exit" echo " -s Auto start (needed for some distros)" echo " -u Auto update plexupdate.sh before running it (experimental)" @@ -212,7 +129,7 @@ usage() { echo " --port Port for Plex Server. Used with --server" echo " --saveconfig Save the configuration to config file" echo - cronexit 0 + exit 0 } running() { @@ -234,8 +151,8 @@ running() { return 1 else # We do not know what this means... - warningLog "Unknown response (${RET}) from server >>>" - warningLog "${DATA}" + warn "Unknown response (${RET}) from server >>>" + warn "${DATA}" return 0 fi } @@ -251,285 +168,6 @@ trimQuotes() { echo $__buffer } -if [ ! $# -eq 0 ]; then - HASCFG="${@: -1}" - if [ ! -z "${HASCFG}" -a ! "${HASCFG:0:1}" = "-" -a ! "${@:(-2):1}" = "--config" ]; then - if [ -f "${HASCFG}" ]; then - warningLog "Specifying config file as last argument is deprecated. Use --config instead." - CONFIGFILE=${HASCFG} - fi - fi -fi - -# Parse commandline -ALLARGS=( "$@" ) -optstring="acCdfFhlpPqrSsuU -l config:,dldir:,email:,pass:,server:,port:,saveconfig" -getopt -T >/dev/null -if [ $? -eq 4 ]; then - optstring="-o $optstring" -fi -GETOPTRES=$(getopt $optstring -- "$@") -if [ $? -eq 1 ]; then - cronexit 1 -fi - -set -- ${GETOPTRES} -while true; -do - case "$1" in - (-h) usage;; - (-a) AUTOINSTALL_CL=yes;; - (-c) CRON_CL=yes;; - (-C) errorLog "CRON option has changed, please review README.md"; cronexit 255;; - (-d) AUTODELETE_CL=yes;; - (-f) FORCE_CL=yes;; - (-F) FORCEALL_CL=yes;; - (-l) LISTOPTS=yes;; - (-p) PUBLIC_CL=yes;; - (-P) SHOWPROGRESS=yes;; - (-q) QUIET_CL=yes;; - (-r) PRINT_URL=yes;; - (-s) AUTOSTART_CL=yes;; - (-S) errorLog "SILENT option has been removed, please use QUIET (-q) instead"; cronexit 255;; - (-u) AUTOUPDATE_CL=yes;; - (-U) IGNOREAUTOUPDATE=yes;; - - (--config) shift; CONFIGFILE="$1"; CONFIGFILE=$(trimQuotes ${CONFIGFILE});; - (--dldir) shift; DOWNLOADDIR_CL="$1"; DOWNLOADDIR_CL=$(trimQuotes ${DOWNLOADDIR_CL});; - (--email) shift; EMAIL_CL="$1"; EMAIL_CL=$(trimQuotes ${EMAIL_CL});; - (--pass) shift; PASS_CL="$1"; PASS_CL=$(trimQuotes ${PASS_CL});; - (--server) shift; PLEXSERVER_CL="$1"; PLEXSERVER_CL=$(trimQuotes ${PLEXSERVER_CL});; - (--port) shift; PLEXPORT_CL="$1"; PLEXPORT_CL=$(trimQuotes ${PLEXPORT_CL});; - (--saveconfig) SAVECONFIG=yes;; - - (--) ;; - (-*) errorLog "Unrecognized option $1"; usage; cronexit 1;; - (*) break;; - esac - shift -done - -# Sanity, make sure wget is in our path... -if ! hash wget 2>/dev/null; then - errorLog "This script requires wget in the path. It could also signify that you don't have the tool installed." - exit 1 -fi - -# Allow manual control of configfile -if [ ! -z "${CONFIGFILE}" ]; then - if [ -f "${CONFIGFILE}" ]; then - infoLog "Using configuration: ${CONFIGFILE}" #>/dev/null - source "${CONFIGFILE}" - else - errorLog "Cannot load configuration ${CONFIGFILE}" - exit 1 - fi -else - # Load settings from config file if it exists - # Also, respect SUDO_USER and try that first - 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 - elif [[ $SUDO_USER == *" "* ]]; then ERROR=1 ; # Space is not a good thing - elif [[ $SUDO_USER == *"&"* ]]; then ERROR=1 ; # Spinning off the command is bad - elif [[ $SUDO_USER == *"<"* ]]; then ERROR=1 ; # No redirection - elif [[ $SUDO_USER == *">"* ]]; then ERROR=1 ; # No redirection - elif [[ $SUDO_USER == *"|"* ]]; then ERROR=1 ; # No pipes - elif [[ $SUDO_USER == *"~"* ]]; then ERROR=1 ; # No tilde - fi - if [ ${ERROR} -gt 0 ]; then - errorLog "SUDO_USER variable is COMPROMISED: \"${SUDO_USER}\"" - exit 255 - fi - - # Try using original user's config - CONFIGDIR="$( eval cd ~${SUDO_USER} 2>/dev/null && pwd )" - if [ -z "${CONFIGDIR}" ]; then - warningLog "SUDO_USER \"${SUDO_USER}\" does not have a valid home directory, ignoring." - fi - - if [ ! -z "${CONFIGDIR}" -a -f "${CONFIGDIR}/.plexupdate" ]; then - infoLog "Using \"${SUDO_USER}\" configuration: ${CONFIGDIR}/.plexupdate" #>/dev/null - CONFIGFILE="${CONFIGDIR}/.plexupdate" - source "${CONFIGDIR}/.plexupdate" - elif [ -f ~/.plexupdate ]; then - # Fallback for compatibility - infoLog "Using \"${SUDO_USER}\" configuration: ${HOME}/.plexupdate" #>/dev/null - CONFIGFILE="${HOME}/.plexupdate" # tilde expansion won't happen later. - source ~/.plexupdate - fi - elif [ -f ~/.plexupdate ]; then - # Fallback for compatibility - infoLog "Using configuration: ${HOME}/.plexupdate" #>/dev/null - CONFIGFILE="${HOME}/.plexupdate" - source ~/.plexupdate - fi -fi - -# The way I wrote this, it assumes that whatever we put on the command line is what we want and should override -# any values in the configuration file. As a result, we need to check if they've been set on the command line -# and overwrite the values that may have been loaded with the config file - -for VAR in AUTOINSTALL CRON AUTODELETE DOWNLOADDIR EMAIL PASS FORCE FORCEALL PUBLIC QUIET AUTOSTART AUTOUPDATE PLEXSERVER PLEXPORT -do - VAR2="$VAR""_CL" - if [ ! -z ${!VAR2} ]; then - eval $VAR='${!VAR2}' - fi -done - -# This will destroy and recreate the config file. Any settings that are set in the config file but are no longer -# valid will NOT be saved. -if [ "${SAVECONFIG}" = "yes" ]; then - echo "# Config file for plexupdate" >${CONFIGFILE:="${HOME}/.plexupdate"} - - for VAR in AUTOINSTALL CRON AUTODELETE DOWNLOADDIR EMAIL PASS FORCE FORCEALL PUBLIC QUIET AUTOSTART AUTOUPDATE PLEXSERVER PLEXPORT CHECKUPDATE - do - if [ ! -z ${!VAR} ]; then - - # The following keys have defaults set in this file. We don't want to include these values if they are the default. - if [ ${VAR} = "FORCE" \ - -o ${VAR} = "FORCEALL" \ - -o ${VAR} = "PUBLIC" \ - -o ${VAR} = "AUTOINSTALL" \ - -o ${VAR} = "AUTODELETE" \ - -o ${VAR} = "AUTOUPDATE" \ - -o ${VAR} = "AUTOSTART" \ - -o ${VAR} = "CRON" \ - -o ${VAR} = "QUIET" ]; then - - if [ ${!VAR} = "yes" ]; then - echo "${VAR}='${!VAR}'" >> ${CONFIGFILE} - fi - elif [ ${VAR} = "PLEXPORT" ]; then - if [ ! "${!VAR}" = "32400" ]; then - echo "${VAR}='${!VAR}'" >> ${CONFIGFILE} - fi - else - echo "${VAR}='${!VAR}'" >> ${CONFIGFILE} - fi - fi - done -fi - -if [ "${SHOWPROGRESS}" = "yes" ]; then - WGETOPTIONS="--show-progress" -fi - -if [ "${IGNOREAUTOUPDATE}" = "yes" ]; then - AUTOUPDATE=no -fi - -if [ "${KEEP}" = "yes" ]; then - errorLog "KEEP is deprecated and should be removed from .plexupdate" - cronexit 255 -fi - -if [ "${SILENT}" = "yes" ]; then - errorLog "SILENT option has been removed and should be removed from .plexupdate" - errorLog " Use QUIET or -q instead" - cronexit 255 -fi - -if [ ! -z "${RELEASE}" ]; then - errorLog "RELEASE keyword is deprecated and should be removed from .plexupdate" - errorLog " Use DISTRO and BUILD instead to manually select what to install (check README.md)" - cronexit 255 -fi - -if [ "${CRON}" = "yes" -a "${QUIET}" = "no" ]; then - exec 3>&1 >${FILE_CMDLOG} 2>${FILE_CMDERR} -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 - errorLog "You need to have git installed for this to work" - cronexit 1 - fi - pushd "$(dirname "$0")" >/dev/null - if [ ! -d .git ]; then - errorLog "This is not a git repository, auto update only works if you've done a git clone" - cronexit 1 - fi - git status | grep "git commit -a" >/dev/null 2>/dev/null - if [ $? -eq 0 ]; then - errorLog "You have made changes to the script, cannot auto update" - cronexit 1 - fi - infoLogNoNewline "Auto updating..." - git pull >/dev/null - if [ $? -ne 0 ]; then - errorLog 'Unable to update git, try running "git pull" manually to see what is wrong' - cronexit 1 - fi - infoLog "OK" - popd >/dev/null - - if ! type "$0" 2>/dev/null >/dev/null ; then - if [ -f "$0" ]; then - /bin/bash "$0" -U ${ALLARGS[@]} - else - errorLog "Unable to relaunch, couldn't find $0" - cronexit 1 - fi - else - "$0" -U ${ALLARGS[@]} - fi - cronexit $? -fi - -# Sanity check -if [ -z "${EMAIL}" -o -z "${PASS}" ] && [ "${PUBLIC}" = "no" ] && [ ! -f "${FILE_KAKA}" ]; then - errorLog "Need username & password to download PlexPass version. Otherwise run with -p to download public version." - cronexit 1 -fi - -if [ "${AUTOINSTALL}" = "yes" -o "${AUTOSTART}" = "yes" ]; then - id | grep -i 'uid=0(' 2>&1 >/dev/null - if [ $? -ne 0 ]; then - errorLog "You need to be root to use autoinstall/autostart option." - cronexit 1 - fi -fi - - -# Remove any ~ or other oddness in the path we're given -DOWNLOADDIR_PRE=${DOWNLOADDIR} -DOWNLOADDIR="$(eval cd ${DOWNLOADDIR// /\\ } 2>/dev/null && pwd)" -if [ ! -d "${DOWNLOADDIR}" ]; then - errorLog "Download directory does not exist or is not a directory (tried \"${DOWNLOADDIR_PRE}\")" - cronexit 1 -fi - -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 - BUILD="linux-ubuntu-${ARCH}" - DISTRO="redhat" - DISTRO_INSTALL="${REDHAT_INSTALL}" - else - REDHAT=no - BUILD="linux-ubuntu-${ARCH}" - DISTRO="ubuntu" - DISTRO_INSTALL="${DEBIAN_INSTALL}" - fi - elif [ -z "${DISTRO}" -o -z "${BUILD}" ]; then - errorLog "You must define both DISTRO and BUILD" - cronexit 255 - fi -else - if [ -z "${DISTRO}" -o -z "${BUILD}" ]; then - errorLog "Using custom DISTRO_INSTALL requires custom DISTRO and BUILD too" - cronexit 255 - fi -fi - # Useful functions rawurlencode() { local string="${1}" @@ -554,49 +192,298 @@ keypair() { echo "${key}=${val}" } -# Setup an cronexit handler so we cleanup -function cleanup { - if [ "${CRON}" = yes -a "${RAWEXIT}" -ne 5 -a -f "${FILE_CMDLOG}" ]; then - infoLog "Command Output:" >/dev/null - OUTPUTLINES=$((OUTPUTLINES+$(wc -l <${FILE_CMDLOG}))) - cat ${FILE_CMDLOG} >>${FILE_STDOUTLOG} - - errorLog "Command Errors/Warnings:" >/dev/null - OUTPUTLINES=$((OUTPUTLINES+$(wc -l <${FILE_CMDERR}))) - cat ${FILE_CMDERR} >>${FILE_STDOUTLOG} - - exec 1>&3 - tail -n ${OUTPUTLINES} "${FILE_STDOUTLOG}" - fi - rm "${FILE_POSTDATA}" 2>/dev/null >/dev/null - rm "${FILE_RAW}" 2>/dev/null >/dev/null - rm "${FILE_FAILCAUSE}" 2>/dev/null >/dev/null - rm "${FILE_KAKA}" 2>/dev/null >/dev/null - rm "${FILE_SHA}" 2>/dev/null >/dev/null - rm "${FILE_WGETLOG}" 2>/dev/null >/dev/null - rm "${FILE_CMDLOG}" 2>/dev/null >/dev/null - rm "${FILE_CMDERR}" 2>/dev/null >/dev/null - rm "${FILE_LOCAL}" 2>/dev/null >/dev/null - rm "${FILE_REMOTE}" 2>/dev/null >/dev/null +# Setup an exit handler so we cleanup +cleanup() { + for F in "${FILE_RAW}" "${FILE_FAILCAUSE}" "${FILE_POSTDATA}" "${FILE_KAKA}" "${FILE_SHA}" "${FILE_LOCAL}" "${FILE_REMOTE}"; do + rm "$F" 2>/dev/null >/dev/null + done } trap cleanup EXIT +if [ ! $# -eq 0 ]; then + HASCFG="${@: -1}" + if [ ! -z "${HASCFG}" -a ! "${HASCFG:0:1}" = "-" -a ! "${@:(-2):1}" = "--config" ]; then + if [ -f "${HASCFG}" ]; then + warn "Specifying config file as last argument is deprecated. Use --config instead." + CONFIGFILE=${HASCFG} + fi + fi +fi + +# Parse commandline +ALLARGS=( "$@" ) +optstring="acCdfFhlpPqrSsuU -l config:,dldir:,email:,pass:,server:,port:,saveconfig" +getopt -T >/dev/null +if [ $? -eq 4 ]; then + optstring="-o $optstring" +fi +GETOPTRES=$(getopt $optstring -- "$@") +if [ $? -eq 1 ]; then + exit 1 +fi + +set -- ${GETOPTRES} +while true; +do + case "$1" in + (-h) usage;; + (-a) AUTOINSTALL_CL=yes;; + (-c) error "CRON option is deprecated, please use cronwrapper (see README.md)"; exit 255;; + (-C) error "CRON option is deprecated, please use cronwrapper (see README.md)"; exit 255;; + (-d) AUTODELETE_CL=yes;; + (-f) FORCE_CL=yes;; + (-F) FORCEALL_CL=yes;; + (-l) LISTOPTS=yes;; + (-p) PUBLIC_CL=yes;; + (-P) SHOWPROGRESS=yes;; + (-q) QUIET_CL=yes;; + (-r) PRINT_URL=yes;; + (-s) AUTOSTART_CL=yes;; + (-u) AUTOUPDATE_CL=yes;; + (-U) IGNOREAUTOUPDATE=yes;; + + (--config) shift; CONFIGFILE="$1"; CONFIGFILE=$(trimQuotes ${CONFIGFILE});; + (--dldir) shift; DOWNLOADDIR_CL="$1"; DOWNLOADDIR_CL=$(trimQuotes ${DOWNLOADDIR_CL});; + (--email) shift; EMAIL_CL="$1"; EMAIL_CL=$(trimQuotes ${EMAIL_CL});; + (--pass) shift; PASS_CL="$1"; PASS_CL=$(trimQuotes ${PASS_CL});; + (--server) shift; PLEXSERVER_CL="$1"; PLEXSERVER_CL=$(trimQuotes ${PLEXSERVER_CL});; + (--port) shift; PLEXPORT_CL="$1"; PLEXPORT_CL=$(trimQuotes ${PLEXPORT_CL});; + (--saveconfig) SAVECONFIG=yes;; + + (--) ;; + (-*) error "Unrecognized option $1"; usage; exit 1;; + (*) break;; + esac + shift +done + +# Sanity, make sure wget is in our path... +if ! hash wget 2>/dev/null; then + error "This script requires wget in the path. It could also signify that you don't have the tool installed." + exit 1 +fi + +# Allow manual control of configfile +if [ ! -z "${CONFIGFILE}" ]; then + if [ -f "${CONFIGFILE}" ]; then + info "Using configuration: ${CONFIGFILE}" #>/dev/null + source "${CONFIGFILE}" + else + error "Cannot load configuration ${CONFIGFILE}" + exit 1 + fi +else + # Load settings from config file if it exists + # Also, respect SUDO_USER and try that first + 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 + elif [[ $SUDO_USER == *" "* ]]; then ERROR=1 ; # Space is not a good thing + elif [[ $SUDO_USER == *"&"* ]]; then ERROR=1 ; # Spinning off the command is bad + elif [[ $SUDO_USER == *"<"* ]]; then ERROR=1 ; # No redirection + elif [[ $SUDO_USER == *">"* ]]; then ERROR=1 ; # No redirection + elif [[ $SUDO_USER == *"|"* ]]; then ERROR=1 ; # No pipes + elif [[ $SUDO_USER == *"~"* ]]; then ERROR=1 ; # No tilde + fi + if [ ${ERROR} -gt 0 ]; then + error "SUDO_USER variable is COMPROMISED: \"${SUDO_USER}\"" + exit 255 + fi + + # Try using original user's config + CONFIGDIR="$( eval cd ~${SUDO_USER} 2>/dev/null && pwd )" + if [ -z "${CONFIGDIR}" ]; then + warn "SUDO_USER \"${SUDO_USER}\" does not have a valid home directory, ignoring." + fi + + if [ ! -z "${CONFIGDIR}" -a -f "${CONFIGDIR}/.plexupdate" ]; then + info "Using \"${SUDO_USER}\" configuration: ${CONFIGDIR}/.plexupdate" #>/dev/null + CONFIGFILE="${CONFIGDIR}/.plexupdate" + source "${CONFIGDIR}/.plexupdate" + elif [ -f ~/.plexupdate ]; then + # Fallback for compatibility + info "Using \"${SUDO_USER}\" configuration: ${HOME}/.plexupdate" #>/dev/null + CONFIGFILE="${HOME}/.plexupdate" # tilde expansion won't happen later. + source ~/.plexupdate + fi + elif [ -f ~/.plexupdate ]; then + # Fallback for compatibility + info "Using configuration: ${HOME}/.plexupdate" #>/dev/null + CONFIGFILE="${HOME}/.plexupdate" + source ~/.plexupdate + fi +fi + +# The way I wrote this, it assumes that whatever we put on the command line is what we want and should override +# any values in the configuration file. As a result, we need to check if they've been set on the command line +# and overwrite the values that may have been loaded with the config file + +for VAR in AUTOINSTALL AUTODELETE DOWNLOADDIR EMAIL PASS FORCE FORCEALL PUBLIC QUIET AUTOSTART AUTOUPDATE PLEXSERVER PLEXPORT +do + VAR2="$VAR""_CL" + if [ ! -z ${!VAR2} ]; then + eval $VAR='${!VAR2}' + fi +done + +# This will destroy and recreate the config file. Any settings that are set in the config file but are no longer +# valid will NOT be saved. +if [ "${SAVECONFIG}" = "yes" ]; then + echo "# Config file for plexupdate" >${CONFIGFILE:="${HOME}/.plexupdate"} + + for VAR in AUTOINSTALL AUTODELETE DOWNLOADDIR EMAIL PASS FORCE FORCEALL PUBLIC QUIET AUTOSTART AUTOUPDATE PLEXSERVER PLEXPORT CHECKUPDATE + do + if [ ! -z ${!VAR} ]; then + + # The following keys have defaults set in this file. We don't want to include these values if they are the default. + if [ ${VAR} = "FORCE" \ + -o ${VAR} = "FORCEALL" \ + -o ${VAR} = "PUBLIC" \ + -o ${VAR} = "AUTOINSTALL" \ + -o ${VAR} = "AUTODELETE" \ + -o ${VAR} = "AUTOUPDATE" \ + -o ${VAR} = "AUTOSTART" \ + -o ${VAR} = "QUIET" ]; then + + if [ ${!VAR} = "yes" ]; then + echo "${VAR}='${!VAR}'" >> ${CONFIGFILE} + fi + elif [ ${VAR} = "PLEXPORT" ]; then + if [ ! "${!VAR}" = "32400" ]; then + echo "${VAR}='${!VAR}'" >> ${CONFIGFILE} + fi + else + echo "${VAR}='${!VAR}'" >> ${CONFIGFILE} + fi + fi + done +fi + +if [ "${SHOWPROGRESS}" = "yes" ]; then + WGETOPTIONS="--show-progress" +fi + +if [ "${IGNOREAUTOUPDATE}" = "yes" ]; then + AUTOUPDATE=no +fi + +if [ "${CRON}" = "yes" ]; then + error "CRON has been deprecated, please use cronwrapper (see README.md)" + exit 255 +fi + +if [ "${KEEP}" = "yes" ]; then + error "KEEP is deprecated and should be removed from .plexupdate" + exit 255 +fi + +if [ ! -z "${RELEASE}" ]; then + error "RELEASE keyword is deprecated and should be removed from .plexupdate" + error "Use DISTRO and BUILD instead to manually select what to install (check README.md)" + exit 255 +fi + +if [ "${AUTOUPDATE}" = "yes" ]; then + if ! hash git 2>/dev/null; then + error "You need to have git installed for this to work" + exit 1 + fi + pushd "$(dirname "$0")" >/dev/null + if [ ! -d .git ]; then + error "This is not a git repository, auto update only works if you've done a git clone" + exit 1 + fi + git status | grep "git commit -a" >/dev/null 2>/dev/null + if [ $? -eq 0 ]; then + error "You have made changes to the script, cannot auto update" + exit 1 + fi + info "Auto updating" + git pull >/dev/null + if [ $? -ne 0 ]; then + error 'Unable to update git, try running "git pull" manually to see what is wrong' + exit 1 + fi + info "Update complete" + popd >/dev/null + + if ! type "$0" 2>/dev/null >/dev/null ; then + if [ -f "$0" ]; then + /bin/bash "$0" -U ${ALLARGS[@]} + else + error "Unable to relaunch, couldn't find $0" + exit 1 + fi + else + "$0" -U ${ALLARGS[@]} + fi + exit $? +fi + +# Sanity check +if [ -z "${EMAIL}" -o -z "${PASS}" ] && [ "${PUBLIC}" = "no" ] && [ ! -f "${FILE_KAKA}" ]; then + error "Need username & password to download PlexPass version. Otherwise run with -p to download public version." + exit 1 +fi + +if [ "${AUTOINSTALL}" = "yes" -o "${AUTOSTART}" = "yes" ]; then + id | grep -i 'uid=0(' 2>&1 >/dev/null + if [ $? -ne 0 ]; then + error "You need to be root to use autoinstall/autostart option." + exit 1 + fi +fi + + +# Remove any ~ or other oddness in the path we're given +DOWNLOADDIR_PRE=${DOWNLOADDIR} +DOWNLOADDIR="$(eval cd ${DOWNLOADDIR// /\\ } 2>/dev/null && pwd)" +if [ ! -d "${DOWNLOADDIR}" ]; then + error "Download directory does not exist or is not a directory (tried \"${DOWNLOADDIR_PRE}\")" + exit 1 +fi + +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 + BUILD="linux-ubuntu-${ARCH}" + DISTRO="redhat" + DISTRO_INSTALL="${REDHAT_INSTALL}" + else + REDHAT=no + BUILD="linux-ubuntu-${ARCH}" + DISTRO="ubuntu" + DISTRO_INSTALL="${DEBIAN_INSTALL}" + fi + elif [ -z "${DISTRO}" -o -z "${BUILD}" ]; then + error "You must define both DISTRO and BUILD" + exit 255 + fi +else + if [ -z "${DISTRO}" -o -z "${BUILD}" ]; then + error "Using custom DISTRO_INSTALL requires custom DISTRO and BUILD too" + exit 255 + fi +fi + if [ "${CHECKUPDATE}" = "yes" ]; then - ERR1=0 (wget -q https://raw.githubusercontent.com/mrworf/plexupdate/master/plexupdate.sh -O - 2>/dev/null || echo ERROR) | shasum >"${FILE_REMOTE}" 2>/dev/null - ERR2=0 + ERR1=$? (cat "$0" 2>/dev/null || echo ERROR) | shasum >"${FILE_LOCAL}" 2>/dev/null + ERR2=$? if [ $ERR1 -ne 0 -o $ERR2 -ne 0 ]; then - errorLog "CheckUpdate: Unable to confirm version of script" + error "When checking for version, was unable to confirm version of script" else # "709c7506b17090bce0d1e2464f39f4a434cf25f1" is the hash for "ERROR" :) if grep -sq "709c7506b17090bce0d1e2464f39f4a434cf25f1" "${FILE_LOCAL}" ; then - errorLog "CheckUpdate: Unable to validate local copy" + error "When checking for version, was unable to validate local copy" elif grep -sq "709c7506b17090bce0d1e2464f39f4a434cf25f1" "${FILE_REMOTE}" ; then - errorLog "CheckUpdate: Unable to validate remote copy" + error "When checking for version, was was unable to validate remote copy" elif ! diff "${FILE_LOCAL}" "${FILE_REMOTE}" >/dev/null 2>/dev/null ; then - infoLog "Newer version of this script is available at https://github.com/mrworf/plexupdate" - infoLog "(or you've made changes to this script yourself)" + info "Newer version of this script is available at https://github.com/mrworf/plexupdate" fi fi rm "${FILE_LOCAL}" 2>/dev/null >/dev/null @@ -616,7 +503,7 @@ fi # commit Sign in if [ "${PUBLIC}" = "no" ]; then - infoLogNoNewline "Authenticating..." + info "Authenticating with plex.tv" # Clean old session rm "${FILE_KAKA}" 2>/dev/null @@ -633,14 +520,15 @@ if [ "${PUBLIC}" = "no" ]; then # Provide some details to the end user RESULTCODE=$(head -n1 "${FILE_RAW}" | grep -oe '[1-5][0-9][0-9]') + info "Contents of ${FILE_RAW}" + cat "${FILE_RAW}" if [ $RESULTCODE -eq 401 ]; then - errorLog "Username and/or password incorrect" - cronexit 1 + error "Username and/or password incorrect" + exit 1 elif [ $RESULTCODE -ne 201 ]; then - errorLog "Failed to login, debug information:" - cat "${FILE_FAILCAUSE}" >&2 - LogFileContents "${FILE_FAILCAUSE}" "ERROR" - cronexit 1 + error "Failed to login, debug information:" + cat "${FILE_RAW}" >&2 + exit 1 fi # If the system got here, it means the login was successfull, so we set the TOKEN variable to the authToken from the response @@ -650,8 +538,6 @@ if [ "${PUBLIC}" = "no" ]; then # Remove this, since it contains more information than we should leave hanging around rm "${FILE_FAILCAUSE}" - infoLog "OK" - elif [ "$PUBLIC" != "no" ]; then # It's a public version, so change URL and make doubly sure that cookies are empty rm 2>/dev/null >/dev/null "${FILE_KAKA}" @@ -672,47 +558,38 @@ if [ "${LISTOPTS}" = "yes" ]; then elif [ -z "$BUILD" ]; then BUILD="$X" else - if [ "${QUIET}" = "yes" ]; then - printf "%-12s %-30s %s\n" "$DISTRO" "$BUILD" "$X" >&3 - else - printf "%-12s %-30s %s\n" "$DISTRO" "$BUILD" "$X" - fi + printf "%-12s %-30s %s\n" "$DISTRO" "$BUILD" "$X" BUILD= DISTRO= fi done - cronexit 0 + exit 0 fi # Extract the URL for our release -infoLogNoNewline "Finding download URL..." +info "Retriving list of available downloads" # 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 RELEASE=$(wget --header "X-Plex-Token:"${TOKEN}"" --load-cookies "${FILE_KAKA}" --save-cookies "${FILE_KAKA}" --keep-session-cookies "${URL_DOWNLOAD}" -O - 2>/dev/null | grep -ioe '"label"[^}]*' | grep -i "\"distro\":\"${DISTRO}\"" | grep -m1 -i "\"build\":\"${BUILD}\"") DOWNLOAD=$(echo ${RELEASE} | grep -m1 -ioe 'https://[^\"]*') CHECKSUM=$(echo ${RELEASE} | grep -ioe '\"checksum\"\:\"[^\"]*' | sed 's/\"checksum\"\:\"//') -infoLog "OK" if [ -z "${DOWNLOAD}" ]; then - errorLog "Unable to retrieve the URL needed for download (Query DISTRO: $DISTRO, BUILD: $BUILD)" - cronexit 3 + error "Unable to retrieve the URL needed for download (Query DISTRO: $DISTRO, BUILD: $BUILD)" + exit 3 fi FILENAME="$(basename 2>/dev/null ${DOWNLOAD})" if [ $? -ne 0 ]; then - errorLog "Failed to parse HTML, download cancelled." - cronexit 3 + error "Failed to parse HTML, download cancelled." + exit 3 fi echo "${CHECKSUM} ${DOWNLOADDIR}/${FILENAME}" >"${FILE_SHA}" if [ "${PRINT_URL}" = "yes" ]; then - if [ "${QUIET}" = "yes" ]; then - infoLog "${DOWNLOAD}" >&3 - else - infoLog "${DOWNLOAD}" - fi - cronexit 0 + info "${DOWNLOAD}" + exit 0 fi # By default, try downloading @@ -723,39 +600,39 @@ if [ "${REDHAT}" != "yes" ]; then INSTALLED_VERSION=$(dpkg-query -s plexmediaserver 2>/dev/null | grep -Po 'Version: \K.*') else if [ "${AUTOSTART}" = "no" ]; then - warningLog "Your distribution may require the use of the AUTOSTART [-s] option for the service to start after the upgrade completes." + warn "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) fi if [[ $FILENAME == *$INSTALLED_VERSION* ]] && [ "${FORCE}" != "yes" -a "${FORCEALL}" != "yes" ] && [ ! -z "${INSTALLED_VERSION}" ]; then - infoLog "Your OS reports the latest version of Plex ($INSTALLED_VERSION) is already installed. Use -f to force download." - cronexit 5 + info "Your OS reports the latest version of Plex ($INSTALLED_VERSION) is already installed. Use -f to force download." + exit 5 fi if [ -f "${DOWNLOADDIR}/${FILENAME}" ]; then if [ "${FORCE}" != "yes" -a "${FORCEALL}" != "yes" ]; then sha1sum --status -c "${FILE_SHA}" if [ $? -eq 0 ]; then - infoLog "File already exists (${FILENAME}), won't download." + info "File already exists (${FILENAME}), won't download." if [ "${AUTOINSTALL}" != "yes" ]; then - cronexit 2 + exit 2 fi SKIP_DOWNLOAD="yes" else - infoLog "File exists but fails checksum. Redownloading." + info "File exists but fails checksum. Redownloading." SKIP_DOWNLOAD="no" fi elif [ "${FORCEALL}" == "yes" ]; then - infoLog "Note! File exists, but asked to overwrite with new copy" + info "Note! File exists, but asked to overwrite with new copy" else sha1sum --status -c "${FILE_SHA}" if [ $? -ne 0 ]; then - infoLog "Note! File exists but fails checksum. Redownloading." + info "File exists but fails checksum. Redownloading." else - infoLog "File exists and checksum passes, won't redownload." + info "File exists and checksum passes, won't redownload." if [ "${AUTOINSTALL}" != "yes" ]; then - cronexit 2 + exit 2 fi SKIP_DOWNLOAD="yes" fi @@ -763,35 +640,35 @@ if [ -f "${DOWNLOADDIR}/${FILENAME}" ]; then fi if [ "${SKIP_DOWNLOAD}" = "no" ]; then - infoLogNoNewline "Downloading release \"${FILENAME}\"..." + info "Downloading release \"${FILENAME}\"" wget ${WGETOPTIONS} -o "${FILE_WGETLOG}" --load-cookies "${FILE_KAKA}" --save-cookies "${FILE_KAKA}" --keep-session-cookies "${DOWNLOAD}" -O "${DOWNLOADDIR}/${FILENAME}" 2>&1 CODE=$? if [ ${CODE} -eq 2 ]; then - errorLog "!! Your wget is too old to support --show-progress" - infoLogNoNewline "Trying to download release \"${FILENAME}\" again..." + error "Your wget is too old to support --show-progress" + info "Trying to download release \"${FILENAME}\" again" wget -o "${FILE_WGETLOG}" --load-cookies "${FILE_KAKA}" --save-cookies "${FILE_KAKA}" --keep-session-cookies "${DOWNLOAD}" -O "${DOWNLOADDIR}/${FILENAME}" 2>&1 CODE=$? fi - ERROR=$(cat ${FILE_WGETLOG}) if [ ${CODE} -ne 0 ]; then - errorLog "!! Download failed with code ${CODE}, \"${ERROR}\"" - cronexit ${CODE} + error "Download failed with code ${CODE}:" + cat "${FILE_WGETLOG}" >&2 + exit ${CODE} fi - infoLog "OK" + info "File downloaded" fi sha1sum --status -c "${FILE_SHA}" if [ $? -ne 0 ]; then - errorLog "Downloaded file corrupt. Try again." - cronexit 4 + error "Downloaded file corrupt. Try again." + exit 4 fi if [ ! -z "${PLEXSERVER}" -a "${AUTOINSTALL}" = "yes" ]; then # Check if server is in-use before continuing (thanks @AltonV, @hakong and @sufr3ak)... if running ${PLEXSERVER} ${TOKEN} ${PLEXPORT}; then - errorLog "Server ${PLEXSERVER} is currently being used by one or more users, skipping installation. Please run again later" - cronexit 6 + error "Server ${PLEXSERVER} is currently being used by one or more users, skipping installation. Please run again later" + exit 6 fi fi @@ -807,15 +684,15 @@ fi if [ "${AUTODELETE}" = "yes" ]; then if [ "${AUTOINSTALL}" = "yes" ]; then rm -rf "${DOWNLOADDIR}/${FILENAME}" - infoLog "Deleted \"${FILENAME}\"" + info "Deleted \"${FILENAME}\"" else - infoLog "Will not auto delete without [-a] auto install" + info "Will not auto delete without [-a] auto install" fi fi if [ "${AUTOSTART}" = "yes" ]; then if [ "${REDHAT}" = "no" ]; then - warningLog "The AUTOSTART [-s] option may not be needed on your distribution." + warn "The AUTOSTART [-s] option may not be needed on your distribution." fi # Check for systemd if hash systemctl 2>/dev/null; then @@ -825,9 +702,9 @@ if [ "${AUTOSTART}" = "yes" ]; then elif [ -x /etc/init.d/plexmediaserver ]; then /etc/init.d/plexmediaserver start else - errorLog "AUTOSTART was specified but no startup scripts were found for 'plexmediaserver'." - cronexit 1 + error "AUTOSTART was specified but no startup scripts were found for 'plexmediaserver'." + exit 1 fi fi -cronexit 0 +exit 0 From 3b4a0d82afd44e2f1e5540e9b8a8b3b1b6f32c44 Mon Sep 17 00:00:00 2001 From: Henric Andersson Date: Wed, 16 Nov 2016 21:48:16 -0800 Subject: [PATCH 02/42] Removed last pieces of quiet --- plexupdate.sh | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index 67f3772..e0a1abf 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -60,7 +60,6 @@ AUTOINSTALL=no AUTODELETE=no AUTOUPDATE=no AUTOSTART=no -QUIET=no ARCH=$(uname -m) IGNOREAUTOUPDATE=no SHOWPROGRESS=no @@ -114,7 +113,6 @@ usage() { echo " -l List available builds and distros" echo " -p Public Plex Media Server version" echo " -P Show progressbar when downloading big files" - echo " -q Quiet mode. No stdout, only stderr and exit codes" echo " -r Print download URL and exit" echo " -s Auto start (needed for some distros)" echo " -u Auto update plexupdate.sh before running it (experimental)" @@ -236,7 +234,7 @@ do (-l) LISTOPTS=yes;; (-p) PUBLIC_CL=yes;; (-P) SHOWPROGRESS=yes;; - (-q) QUIET_CL=yes;; + (-q) error "QUIET option is deprecated, please redirect to /dev/null instead"; exit 255;; (-r) PRINT_URL=yes;; (-s) AUTOSTART_CL=yes;; (-u) AUTOUPDATE_CL=yes;; @@ -319,7 +317,7 @@ fi # any values in the configuration file. As a result, we need to check if they've been set on the command line # and overwrite the values that may have been loaded with the config file -for VAR in AUTOINSTALL AUTODELETE DOWNLOADDIR EMAIL PASS FORCE FORCEALL PUBLIC QUIET AUTOSTART AUTOUPDATE PLEXSERVER PLEXPORT +for VAR in AUTOINSTALL AUTODELETE DOWNLOADDIR EMAIL PASS FORCE FORCEALL PUBLIC AUTOSTART AUTOUPDATE PLEXSERVER PLEXPORT do VAR2="$VAR""_CL" if [ ! -z ${!VAR2} ]; then @@ -332,7 +330,7 @@ done if [ "${SAVECONFIG}" = "yes" ]; then echo "# Config file for plexupdate" >${CONFIGFILE:="${HOME}/.plexupdate"} - for VAR in AUTOINSTALL AUTODELETE DOWNLOADDIR EMAIL PASS FORCE FORCEALL PUBLIC QUIET AUTOSTART AUTOUPDATE PLEXSERVER PLEXPORT CHECKUPDATE + for VAR in AUTOINSTALL AUTODELETE DOWNLOADDIR EMAIL PASS FORCE FORCEALL PUBLIC AUTOSTART AUTOUPDATE PLEXSERVER PLEXPORT CHECKUPDATE do if [ ! -z ${!VAR} ]; then @@ -343,8 +341,7 @@ if [ "${SAVECONFIG}" = "yes" ]; then -o ${VAR} = "AUTOINSTALL" \ -o ${VAR} = "AUTODELETE" \ -o ${VAR} = "AUTOUPDATE" \ - -o ${VAR} = "AUTOSTART" \ - -o ${VAR} = "QUIET" ]; then + -o ${VAR} = "AUTOSTART" ]; then if [ ${!VAR} = "yes" ]; then echo "${VAR}='${!VAR}'" >> ${CONFIGFILE} From 979878b4d83bbdf70e62cf733bc04d7e62a9ddf6 Mon Sep 17 00:00:00 2001 From: Henric Andersson Date: Wed, 16 Nov 2016 21:53:53 -0800 Subject: [PATCH 03/42] Fixed wiki link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 31faf64..65e1b6a 100755 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ Most of these options can be specified on the command-line as well, this is just Generally, you copy the cronwrapper from the extras folder into the ```/etc/cron.weekly``` and edit the script to suite your needs. It will require you to create a configuration file (or at least, it's highly recommended). It also provides logging via syslog if needed. Should the script fail, it will print why and in most cases, this will result in an email to you. -[Also checkout the newly written wiki for more details] +[Also checkout the wiki for more details](https://github.com/mrworf/plexupdate/wiki/Ubuntu%3A-Run-plexupdate.sh-from-cron) ### Command Line Options From aafcf27ad98f1ada998243e6294e7c4fb2984f0e Mon Sep 17 00:00:00 2001 From: Henric Andersson Date: Wed, 16 Nov 2016 22:08:54 -0800 Subject: [PATCH 04/42] Fixed various minor issues and spelling --- plexupdate.sh | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index e0a1abf..4fd2c78 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -77,8 +77,8 @@ URL_DOWNLOAD='https://plex.tv/api/downloads/1.json?channel=plexpass' URL_DOWNLOAD_PUBLIC='https://plex.tv/api/downloads/1.json' FILE_POSTDATA=$(mktemp /tmp/plexupdate.postdata.XXXX) -FILE_RAW=$(mktemp /tmp/plexupdate.failcause.XXXX) -FILE_FAILCAUSE=$(mktemp /tmp/plexupdate.raw.XXXX) +FILE_RAW=$(mktemp /tmp/plexupdate.raw.XXXX) +FILE_FAILCAUSE=$(mktemp /tmp/plexupdate.failcause.XXXX) FILE_KAKA=$(mktemp /tmp/plexupdate.kaka.XXXX) FILE_SHA=$(mktemp /tmp/plexupdate.sha.XXXX) FILE_WGETLOG=$(mktemp /tmp/plexupdate.wget.XXXX) @@ -419,7 +419,7 @@ if [ "${AUTOUPDATE}" = "yes" ]; then fi # Sanity check -if [ -z "${EMAIL}" -o -z "${PASS}" ] && [ "${PUBLIC}" = "no" ] && [ ! -f "${FILE_KAKA}" ]; then +if [ -z "${EMAIL}" -o -z "${PASS}" ] && [ "${PUBLIC}" = "no" ]; then error "Need username & password to download PlexPass version. Otherwise run with -p to download public version." exit 1 fi @@ -427,7 +427,7 @@ fi if [ "${AUTOINSTALL}" = "yes" -o "${AUTOSTART}" = "yes" ]; then id | grep -i 'uid=0(' 2>&1 >/dev/null if [ $? -ne 0 ]; then - error "You need to be root to use autoinstall/autostart option." + error "You need to be root to use AUTOINSTALL/AUTOSTART option." exit 1 fi fi @@ -517,8 +517,6 @@ if [ "${PUBLIC}" = "no" ]; then # Provide some details to the end user RESULTCODE=$(head -n1 "${FILE_RAW}" | grep -oe '[1-5][0-9][0-9]') - info "Contents of ${FILE_RAW}" - cat "${FILE_RAW}" if [ $RESULTCODE -eq 401 ]; then error "Username and/or password incorrect" exit 1 @@ -564,7 +562,7 @@ if [ "${LISTOPTS}" = "yes" ]; then fi # Extract the URL for our release -info "Retriving list of available downloads" +info "Retrieving list of available distributions" # 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 RELEASE=$(wget --header "X-Plex-Token:"${TOKEN}"" --load-cookies "${FILE_KAKA}" --save-cookies "${FILE_KAKA}" --keep-session-cookies "${URL_DOWNLOAD}" -O - 2>/dev/null | grep -ioe '"label"[^}]*' | grep -i "\"distro\":\"${DISTRO}\"" | grep -m1 -i "\"build\":\"${BUILD}\"") From 353563a648ee36832e7e6623332f9314e812a0a5 Mon Sep 17 00:00:00 2001 From: Henric Andersson Date: Wed, 16 Nov 2016 22:09:07 -0800 Subject: [PATCH 05/42] More error checking and documentation --- extras/cronwrapper | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/extras/cronwrapper b/extras/cronwrapper index abcb3e4..6315de2 100755 --- a/extras/cronwrapper +++ b/extras/cronwrapper @@ -1,18 +1,44 @@ #!/bin/bash - +# +# Script to be placed in one of +# /etc/cron.daily +# /etc/cron.weekly +# +# or called directly via /etc/crontab +# +# Do NOT rename it so it has a dot "." in the name, this will cause +# ubuntu (and perhaps other distros) to ignore it. +# +# CONF is used to point out a configuration file (optional) +# SCRIPT points out where to find plexupdate.sh +# LOGGING if true, logs all output to syslog daemon facility +# (typically /var/log/daemon.log or /var/log/syslog) +# +# Set CONFIGURED to true once you've setup the previous three +# options. +# CONFIGURED=false CONF= SCRIPT=/home/john.doe/plexupdate/plexupdate.sh -LOGGING=false +LOGGING=true if ! $CONFIGURED; then echo "ERROR: You have not configured this script" >&2 exit 255 fi +if [ -z "${SCRIPT}" -o ! -f "${SCRIPT}" ]; then + echo "ERROR: Cannot find plexupdate.sh (tried ${SCRIPT})" >&2 + exit 255 +fi + if [ ! -z "$CONF" ]; then # We have a config file, prefix it with parameter + if [ ! -f "${CONF}" ]; then + echo "ERROR: Cannot find config file (tried ${CONF})" >&2 + exit 255 + fi CONF="--config \"${CONF}\"" fi From 78256a08c45c5019bc872774b003fa28aa5b0042 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Thu, 17 Nov 2016 20:53:36 -0800 Subject: [PATCH 06/42] Fixes for cronwrapper behavior (#138) * Fix cronwrapper failing if CONF is specified * Exit code 0 means an update was applied, so we should send output * Make sure wget log gets cleaned up along with other temp files * Fix cronwrapper showing unwanted output even while logging --- extras/cronwrapper | 6 +++--- plexupdate.sh | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/extras/cronwrapper b/extras/cronwrapper index 6315de2..67a5abf 100755 --- a/extras/cronwrapper +++ b/extras/cronwrapper @@ -39,7 +39,7 @@ if [ ! -z "$CONF" ]; then echo "ERROR: Cannot find config file (tried ${CONF})" >&2 exit 255 fi - CONF="--config \"${CONF}\"" + CONF="--config=${CONF}" fi LOGFILE=$(mktemp /tmp/plexupdate.cron.XXXX) @@ -51,11 +51,11 @@ if $LOGGING; then RET=1 fi else - "${SCRIPT}" "${CONF}" 2>&1 >${LOGFILE} + "${SCRIPT}" "${CONF}" >${LOGFILE} 2>&1 RET=$? fi -if [ $RET -ne 0 -a $RET -ne 2 -a $RET -ne 5 ]; then +if [ $RET -ne 2 -a $RET -ne 5 ]; then # Make sure user gets an email about this cat ${LOGFILE} >&2 else diff --git a/plexupdate.sh b/plexupdate.sh index 4fd2c78..a160549 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -192,7 +192,7 @@ keypair() { # Setup an exit handler so we cleanup cleanup() { - for F in "${FILE_RAW}" "${FILE_FAILCAUSE}" "${FILE_POSTDATA}" "${FILE_KAKA}" "${FILE_SHA}" "${FILE_LOCAL}" "${FILE_REMOTE}"; do + for F in "${FILE_RAW}" "${FILE_FAILCAUSE}" "${FILE_POSTDATA}" "${FILE_KAKA}" "${FILE_SHA}" "${FILE_LOCAL}" "${FILE_REMOTE}" "${FILE_WGETLOG}"; do rm "$F" 2>/dev/null >/dev/null done } From d16417d29f905c9fe9c8b350f082bf08fb6d4530 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sat, 19 Nov 2016 12:23:36 -0800 Subject: [PATCH 07/42] Remove obsolete getopt version check Support for older versions of getopt was broken with #106 back in October and nobody has complained yet, so I think it's safe to remove this. --- plexupdate.sh | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index a160549..434ea3a 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -210,11 +210,7 @@ fi # Parse commandline ALLARGS=( "$@" ) -optstring="acCdfFhlpPqrSsuU -l config:,dldir:,email:,pass:,server:,port:,saveconfig" -getopt -T >/dev/null -if [ $? -eq 4 ]; then - optstring="-o $optstring" -fi +optstring="-o acCdfFhlpPqrSsuU -l config:,dldir:,email:,pass:,server:,port:,saveconfig" GETOPTRES=$(getopt $optstring -- "$@") if [ $? -eq 1 ]; then exit 1 From 2bb7b06e2cd0d07f3afc7a0736143686923396c7 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sat, 19 Nov 2016 19:29:05 -0800 Subject: [PATCH 08/42] Add installer script --- extras/installer.sh | 196 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 extras/installer.sh diff --git a/extras/installer.sh b/extras/installer.sh new file mode 100644 index 0000000..b62627d --- /dev/null +++ b/extras/installer.sh @@ -0,0 +1,196 @@ +#!/bin/bash + +ORIGIN_REPO="https://github.com/mrworf/plexupdate" +OPT_PATH="/opt" +FULL_PATH="$OPT_PATH/plexupdate" +CONFIGFILE="/etc/plexupdate.conf" +CONFIGCRON="/etc/plexpass.cron.conf" + +install() { + echo "'$req' is required but not installed, attempting to install..." + sleep 1 + + if $UBUNTU; then + DISTRO_INSTALL="apt install $1" + elif $REDHAT; then + if hash dnf 2>/dev/null; then + DISTRO_INSTALL="dnf install $1" + else + DISTRO_INSTALL="yum install $1" + fi + fi + + if [ $EUID != 0 ]; then + echo "You don't have permissions to continue, trying sudo instead..." + sleep 1 + sudo $DISTRO_INSTALL + else + $DISTRO_INSTALL + fi +} + +yesno() { + read -n 1 -p "[Y/n] " answer + + if [ "$answer" == "n" -o "$answer" == "N" ]; then + echo + return 1 + elif [ ! -z "$answer" ]; then + echo + fi + + return 0 +} + +noyes() { + read -n 1 -p "[N/y] " answer + + if [ "$answer" == "y" -o "$answer" == "Y" ]; then + echo + return 1 + elif [ ! -z "$answer" ]; then + echo + fi + + return 0 +} + +abort() { + echo "$@" + exit 1 +} + +configure_plexupdate() { + + CONFIGTEMP=$(mktemp /tmp/plexupdate.tempconf.XXX) + + [ -f "$CONFIGFILE" ] && source "$CONFIGFILE" + + echo -n "Do you want to install the latest PlexPass releases? " + if yesno; then + read -e -p "PlexPass Email Address: " -i "$EMAIL" EMAIL + read -e -p "PlexPass Password: " -i "$PASS" PASS + else + EMAIL= + PASS= + PUBLIC=yes + fi + + echo -n "Would you like to automatically install the latest release when it is downloaded? " + if yesno; then + AUTOINSTALL=yes + else + AUTOINSTALL=no + fi + + if [ "$AUTOINSTALL" == "yes" ]; then + echo -n "When using the auto-install option, would you like to check if the server is in use before upgrading? " + if yesno; then + if [ -z "$PLEXSERVER" ]; then + PLEXSERVER="127.0.0.1" + fi + read -e -p "Plex Server IP/DNS name: " -i "$PLEXSERVER" PLEXSERVER + if [ -z "$PLEXPORT" ]; then + PLEXPORT=32400 + fi + + read -e -p "Plex Server Port: " -i "$PLEXPORT" PLEXPORT + else + PLEXSERVER= + PLEXPORT= + fi + fi + + save_config "EMAIL PASS PUBLIC AUTOINSTALL PLEXSERVER PLEXPORT" "$CONFIGFILE" +} + +configure_cron() { + echo -n "Would you like to set up automatic daily updates for Plex? " + if yesno; then + CONF="$CONFIGFILE" + SCRIPT="${FULL_PATH}/plexupdate.sh" + LOGGING=false + + echo -n "Do you want to log the daily update runs to syslog so you can examine the output later? " + if yesno; then + LOGGING=true + fi + + save_config "CONF SCRIPT LOGGING" "/etc/plexupdate.cron.conf" + + echo "Installing daily cron job..." + if [ $EUID -ne 0 ]; then + sudo ln -sf ${FULL_PATH}/extras/cronwrapper /etc/cron.daily/plexupdate + else + ln -sf ${FULL_PATH}/extras/cronwrapper /etc/cron.daily/plexupdate + fi + fi +} + + +save_config() { + CONFIGTEMP=$(mktemp /tmp/plexupdate.XXX) + for VAR in $1; do + if [ ! -z ${!VAR} ]; then + echo "${VAR}='${!VAR}'" >> $CONFIGTEMP + fi + done + + echo "Writing configuration file '$2'..." + if [ $EUID -ne 0 ]; then + # make sure that new file is owned by root instead of owner of CONFIGTEMP + sudo tee "$2" > /dev/null < "$CONFIGTEMP" + rm "$CONFIGTEMP" + else + mv "$CONFIGTEMP" "$2" + fi +} + +if [ -f /etc/redhat-release ]; then + REDHAT=true +else + UBUNTU=true +fi + +for req in wget git; do + if ! hash $req 2>/dev/null; then + install $req + fi +done + +echo -e "\n" + +read -e -p "Directory to install into: " -i "/opt/plexupdate" FULL_PATH +if [ ! -d "$FULL_PATH" ]; then + echo -n "'$FULL_PATH' doesn't exist, attempting to create... " + if ! mkdir -p "$FULL_PATH" 2>/dev/null; then + echo "failed" + echo "trying with sudo... " + sudo mkdir -p "$FULL_PATH" || abort "failed, cannot continue" + sudo chown $(whoami) "$FULL_PATH" || abort "failed, cannot continue" + fi + echo "done" +elif [ ! -w "$FULL_PATH" ]; then + echo "'$FULL_PATH' exists, but you don't have permission to write to it. Changing owner with sudo..." + sudo chown $(whoami) "$FULL_PATH" || abort "failed, cannot continue" +fi + +if [ -d "${FULL_PATH}/.git" ]; then + cd "$FULL_PATH" + if git remote -v | grep -q "mrworf/plexupdate"; then + echo "Found existing plexupdate repository in '$FULL_PATH', updating..." + git pull >/dev/null || abort "Unknown error while updating, please check '$FULL_PATH' and then try again." + else + abort "'$FULL_PATH' appears to contain a different git repository, cannot continue" + fi + cd - &> /dev/null +else + git clone "$ORIGIN_REPO" "$FULL_PATH" + # FIXME These 3 lines are just to allow us to test easily while we're still using this branch. Remember to take this out before merging to master. + cd "$FULL_PATH" + git checkout reworklog + cd - &> /dev/null +fi + +configure_plexupdate +configure_cron From f25528558877e61f3b397833c146f189f23025a4 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sat, 19 Nov 2016 19:29:27 -0800 Subject: [PATCH 09/42] Update cronwrapper to work with installer script --- extras/cronwrapper | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/extras/cronwrapper b/extras/cronwrapper index 67a5abf..0bc6c32 100755 --- a/extras/cronwrapper +++ b/extras/cronwrapper @@ -17,15 +17,13 @@ # Set CONFIGURED to true once you've setup the previous three # options. # -CONFIGURED=false -CONF= -SCRIPT=/home/john.doe/plexupdate/plexupdate.sh -LOGGING=true -if ! $CONFIGURED; then - echo "ERROR: You have not configured this script" >&2 +if [ ! -f /etc/plexpass.cron.conf ]; then + echo "ERROR: You have not configured /etc/plexpass.cron.conf" >&2 exit 255 +else + source /etc/plexpass.cron.conf fi if [ -z "${SCRIPT}" -o ! -f "${SCRIPT}" ]; then From cb4b702f117464c3f619b611360de16467946f95 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 01:52:56 -0800 Subject: [PATCH 10/42] Clean up some of the prompts for easier reading --- extras/installer.sh | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/extras/installer.sh b/extras/installer.sh index b62627d..44ad419 100644 --- a/extras/installer.sh +++ b/extras/installer.sh @@ -66,6 +66,7 @@ configure_plexupdate() { [ -f "$CONFIGFILE" ] && source "$CONFIGFILE" + echo echo -n "Do you want to install the latest PlexPass releases? " if yesno; then read -e -p "PlexPass Email Address: " -i "$EMAIL" EMAIL @@ -76,6 +77,7 @@ configure_plexupdate() { PUBLIC=yes fi + echo echo -n "Would you like to automatically install the latest release when it is downloaded? " if yesno; then AUTOINSTALL=yes @@ -84,6 +86,7 @@ configure_plexupdate() { fi if [ "$AUTOINSTALL" == "yes" ]; then + echo echo -n "When using the auto-install option, would you like to check if the server is in use before upgrading? " if yesno; then if [ -z "$PLEXSERVER" ]; then @@ -105,12 +108,14 @@ configure_plexupdate() { } configure_cron() { + echo echo -n "Would you like to set up automatic daily updates for Plex? " if yesno; then CONF="$CONFIGFILE" SCRIPT="${FULL_PATH}/plexupdate.sh" LOGGING=false + echo echo -n "Do you want to log the daily update runs to syslog so you can examine the output later? " if yesno; then LOGGING=true @@ -118,12 +123,14 @@ configure_cron() { save_config "CONF SCRIPT LOGGING" "/etc/plexupdate.cron.conf" - echo "Installing daily cron job..." + echo + echo -n "Installing daily cron job... " if [ $EUID -ne 0 ]; then sudo ln -sf ${FULL_PATH}/extras/cronwrapper /etc/cron.daily/plexupdate else ln -sf ${FULL_PATH}/extras/cronwrapper /etc/cron.daily/plexupdate fi + echo "done" fi } @@ -136,6 +143,7 @@ save_config() { fi done + echo echo "Writing configuration file '$2'..." if [ $EUID -ne 0 ]; then # make sure that new file is owned by root instead of owner of CONFIGTEMP @@ -164,31 +172,34 @@ read -e -p "Directory to install into: " -i "/opt/plexupdate" FULL_PATH if [ ! -d "$FULL_PATH" ]; then echo -n "'$FULL_PATH' doesn't exist, attempting to create... " if ! mkdir -p "$FULL_PATH" 2>/dev/null; then - echo "failed" echo "trying with sudo... " sudo mkdir -p "$FULL_PATH" || abort "failed, cannot continue" sudo chown $(whoami) "$FULL_PATH" || abort "failed, cannot continue" fi echo "done" elif [ ! -w "$FULL_PATH" ]; then - echo "'$FULL_PATH' exists, but you don't have permission to write to it. Changing owner with sudo..." + echo -n "'$FULL_PATH' exists, but you don't have permission to write to it. Changing owner with sudo... " sudo chown $(whoami) "$FULL_PATH" || abort "failed, cannot continue" + echo "done" fi if [ -d "${FULL_PATH}/.git" ]; then cd "$FULL_PATH" if git remote -v | grep -q "mrworf/plexupdate"; then - echo "Found existing plexupdate repository in '$FULL_PATH', updating..." + echo -n "Found existing plexupdate repository in '$FULL_PATH', updating... " git pull >/dev/null || abort "Unknown error while updating, please check '$FULL_PATH' and then try again." else abort "'$FULL_PATH' appears to contain a different git repository, cannot continue" fi + echo "done" cd - &> /dev/null else - git clone "$ORIGIN_REPO" "$FULL_PATH" + echo -n "Installing plexupdate into '$FULL_PATH'... " + git clone "$ORIGIN_REPO" "$FULL_PATH" &> /dev/null || abort "install failed, cannot continue" + echo "done" # FIXME These 3 lines are just to allow us to test easily while we're still using this branch. Remember to take this out before merging to master. cd "$FULL_PATH" - git checkout reworklog + git checkout reworklog > /dev/null cd - &> /dev/null fi From 2e4a6d52fc6df44c07f2eba46850866798fcb37e Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 01:57:15 -0800 Subject: [PATCH 11/42] Add AUTOUPDATE to default config --- extras/installer.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extras/installer.sh b/extras/installer.sh index 44ad419..c0c7883 100644 --- a/extras/installer.sh +++ b/extras/installer.sh @@ -63,6 +63,7 @@ abort() { configure_plexupdate() { CONFIGTEMP=$(mktemp /tmp/plexupdate.tempconf.XXX) + AUTOUPDATE=yes [ -f "$CONFIGFILE" ] && source "$CONFIGFILE" @@ -104,7 +105,7 @@ configure_plexupdate() { fi fi - save_config "EMAIL PASS PUBLIC AUTOINSTALL PLEXSERVER PLEXPORT" "$CONFIGFILE" + save_config "AUTOUPDATE EMAIL PASS PUBLIC AUTOINSTALL PLEXSERVER PLEXPORT" "$CONFIGFILE" } configure_cron() { From dcb2a97cd4232b1cab31e182bd727ae81ba04347 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 02:02:16 -0800 Subject: [PATCH 12/42] Remember to unset option when config is changed --- extras/installer.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/extras/installer.sh b/extras/installer.sh index c0c7883..0f05b14 100644 --- a/extras/installer.sh +++ b/extras/installer.sh @@ -70,6 +70,7 @@ configure_plexupdate() { echo echo -n "Do you want to install the latest PlexPass releases? " if yesno; then + PUBLIC= read -e -p "PlexPass Email Address: " -i "$EMAIL" EMAIL read -e -p "PlexPass Password: " -i "$PASS" PASS else @@ -103,6 +104,9 @@ configure_plexupdate() { PLEXSERVER= PLEXPORT= fi + else + PLEXSERVER= + PLEXPORT= fi save_config "AUTOUPDATE EMAIL PASS PUBLIC AUTOINSTALL PLEXSERVER PLEXPORT" "$CONFIGFILE" From c30559b5f5af3b9a2093e4cc510fe72de7f3c329 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 02:30:39 -0800 Subject: [PATCH 13/42] Remove old cron config if changed in setup script --- extras/installer.sh | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/extras/installer.sh b/extras/installer.sh index 0f05b14..4941acb 100644 --- a/extras/installer.sh +++ b/extras/installer.sh @@ -4,7 +4,8 @@ ORIGIN_REPO="https://github.com/mrworf/plexupdate" OPT_PATH="/opt" FULL_PATH="$OPT_PATH/plexupdate" CONFIGFILE="/etc/plexupdate.conf" -CONFIGCRON="/etc/plexpass.cron.conf" +CONFIGCRON="/etc/plexupdate.cron.conf" +CRONWRAPPER="/etc/cron.daily/plexupdate" install() { echo "'$req' is required but not installed, attempting to install..." @@ -131,11 +132,21 @@ configure_cron() { echo echo -n "Installing daily cron job... " if [ $EUID -ne 0 ]; then - sudo ln -sf ${FULL_PATH}/extras/cronwrapper /etc/cron.daily/plexupdate + sudo ln -sf ${FULL_PATH}/extras/cronwrapper "$CRONWRAPPER" else - ln -sf ${FULL_PATH}/extras/cronwrapper /etc/cron.daily/plexupdate + ln -sf ${FULL_PATH}/extras/cronwrapper "$CRONWRAPPER" fi echo "done" + elif [ -f "$CRONWRAPPER" -o -f "$CONFIGCRON" ]; then + echo + echo -n "Cleaning up old cron configuration... " + if [ -f "$CRONWRAPPER" ]; then + sudo rm "$CRONWRAPPER" || echo "Failed to remove old cron script, please check '$CRONWRAPPER'" + fi + if [ -f "$CONFIGCRON" ]; then + sudo rm "$CONFIGCRON" || echo "Failed to remove old cron configuration, please check '$CONFIGCRON'" + fi + echo done fi } @@ -149,7 +160,7 @@ save_config() { done echo - echo "Writing configuration file '$2'..." + echo -n "Writing configuration file '$2'... " if [ $EUID -ne 0 ]; then # make sure that new file is owned by root instead of owner of CONFIGTEMP sudo tee "$2" > /dev/null < "$CONFIGTEMP" @@ -157,6 +168,7 @@ save_config() { else mv "$CONFIGTEMP" "$2" fi + echo "done" } if [ -f /etc/redhat-release ]; then From 1d9e15d7a42639103a2b6fcae63846cb6c422de2 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 02:31:14 -0800 Subject: [PATCH 14/42] Clean up sudo usage a bit --- extras/installer.sh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/extras/installer.sh b/extras/installer.sh index 4941acb..bf707ee 100644 --- a/extras/installer.sh +++ b/extras/installer.sh @@ -22,8 +22,6 @@ install() { fi if [ $EUID != 0 ]; then - echo "You don't have permissions to continue, trying sudo instead..." - sleep 1 sudo $DISTRO_INSTALL else $DISTRO_INSTALL @@ -171,6 +169,12 @@ save_config() { echo "done" } +if [ $EUID -ne 0 ]; then + echo + echo "This script needs to be run with root/sudo, but you are running as '$(whoami)'. Enabling sudo." + sudo -v +fi + if [ -f /etc/redhat-release ]; then REDHAT=true else @@ -189,13 +193,12 @@ read -e -p "Directory to install into: " -i "/opt/plexupdate" FULL_PATH if [ ! -d "$FULL_PATH" ]; then echo -n "'$FULL_PATH' doesn't exist, attempting to create... " if ! mkdir -p "$FULL_PATH" 2>/dev/null; then - echo "trying with sudo... " sudo mkdir -p "$FULL_PATH" || abort "failed, cannot continue" sudo chown $(whoami) "$FULL_PATH" || abort "failed, cannot continue" fi echo "done" elif [ ! -w "$FULL_PATH" ]; then - echo -n "'$FULL_PATH' exists, but you don't have permission to write to it. Changing owner with sudo... " + echo -n "'$FULL_PATH' exists, but you don't have permission to write to it. Changing owner... " sudo chown $(whoami) "$FULL_PATH" || abort "failed, cannot continue" echo "done" fi From 7de3c8d758bfee1040ab73ffa48c5145e3ea78dd Mon Sep 17 00:00:00 2001 From: Henric Andersson Date: Sun, 20 Nov 2016 12:16:44 -0800 Subject: [PATCH 15/42] Added sanity checks - Yes/No questions now require yes or no (or enter for default) - Email is checked so it's an actual email address (well, close to anyway) - Password cannot be blank - Server address is ping:ed and if it fails, prompts user to confirm the choice - Port is tested so it's an actual port (1 and up, all numeric) - Cron option isn't run if the /etc/cron.daily exists (notifies user) --- extras/installer.sh | 85 ++++++++++++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 24 deletions(-) mode change 100644 => 100755 extras/installer.sh diff --git a/extras/installer.sh b/extras/installer.sh old mode 100644 new mode 100755 index bf707ee..df24414 --- a/extras/installer.sh +++ b/extras/installer.sh @@ -29,29 +29,31 @@ install() { } yesno() { - read -n 1 -p "[Y/n] " answer + while true; do + read -n 1 -p "[Y/n] " answer - if [ "$answer" == "n" -o "$answer" == "N" ]; then - echo - return 1 - elif [ ! -z "$answer" ]; then - echo - fi - - return 0 + if [ "$answer" == "n" -o "$answer" == "N" ]; then + echo + return 1 + elif [ -z "$answer" -o "$answer" == "y" -o "$answer" == "Y" ]; then + echo + return 0 + fi + done } noyes() { - read -n 1 -p "[N/y] " answer + while true; do + read -n 1 -p "[N/y] " answer - if [ "$answer" == "y" -o "$answer" == "Y" ]; then - echo - return 1 - elif [ ! -z "$answer" ]; then - echo - fi - - return 0 + if [ "$answer" == "y" -o "$answer" == "Y" ]; then + echo + return 1 + elif [ -z "$answer" -o "$answer" == "n" -o "$answer" == "N" ]; then + echo + return 0 + fi + done } abort() { @@ -70,8 +72,22 @@ configure_plexupdate() { echo -n "Do you want to install the latest PlexPass releases? " if yesno; then PUBLIC= - read -e -p "PlexPass Email Address: " -i "$EMAIL" EMAIL - read -e -p "PlexPass Password: " -i "$PASS" PASS + while true; do + read -e -p "PlexPass Email Address: " -i "$EMAIL" EMAIL + if [ -z "$EMAIL" ] || [[ "$EMAIL" != *"@"*"."* ]]; then + echo "Please provide a valid email address" + else + break + fi + done + while true; do + read -e -p "PlexPass Password: " -i "$PASS" PASS + if [ -z "$PASS" ]; then + echo "Please provide a password" + else + break + fi + done else EMAIL= PASS= @@ -93,12 +109,29 @@ configure_plexupdate() { if [ -z "$PLEXSERVER" ]; then PLEXSERVER="127.0.0.1" fi - read -e -p "Plex Server IP/DNS name: " -i "$PLEXSERVER" PLEXSERVER + while true; do + read -e -p "Plex Server IP/DNS name: " -i "$PLEXSERVER" PLEXSERVER + if ! ping -c 1 -w 1 "$PLEXSERVER" &>/dev/null ; then + echo -n "Server $PLEXSERVER isn't responding, are you sure you entered it correctly? " + if ! noyes; then + break + fi + else + break + fi + done if [ -z "$PLEXPORT" ]; then PLEXPORT=32400 fi - - read -e -p "Plex Server Port: " -i "$PLEXPORT" PLEXPORT + while true; do + read -e -p "Plex Server Port: " -i "$PLEXPORT" PLEXPORT + if ! [[ "$PLEXPORT" =~ ^[1-9][0-9]*$ ]]; then + echo "Port $PLEXPORT isn't valid, please try again" + PLEXPORT=32400 + else + break + fi + done else PLEXSERVER= PLEXPORT= @@ -224,4 +257,8 @@ else fi configure_plexupdate -configure_cron +if [ -d "$(dirname "$CRONWRAPPER")" ]; then + configure_cron +else + echo "Seems like you don't have a supported cron job setup, please see README.md for more details." +fi From f3242f7ae3f4618e5637da1e74b14daedde921c5 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 13:29:19 -0800 Subject: [PATCH 16/42] Read /etc/plexupdate.conf before other configs, if available --- plexupdate.sh | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index 434ea3a..fdf543d 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -268,7 +268,13 @@ if [ ! -z "${CONFIGFILE}" ]; then fi else # Load settings from config file if it exists - # Also, respect SUDO_USER and try that first + if [ -f /etc/plexupdate.conf ]; then + info "Reading configuration in: /etc/plexupdate.conf" + CONFIGFILE=/etc/plexupdate.conf + source /etc/plexupdate.conf + fi + + # Check for a SUDO_USER config if [ ! -z "${SUDO_USER}" ]; then # Make sure nothing bad comes from this (since we use eval) ERROR=0 @@ -292,18 +298,18 @@ else fi if [ ! -z "${CONFIGDIR}" -a -f "${CONFIGDIR}/.plexupdate" ]; then - info "Using \"${SUDO_USER}\" configuration: ${CONFIGDIR}/.plexupdate" #>/dev/null + info "Reading \"${SUDO_USER}\" configuration in: ${CONFIGDIR}/.plexupdate" CONFIGFILE="${CONFIGDIR}/.plexupdate" source "${CONFIGDIR}/.plexupdate" elif [ -f ~/.plexupdate ]; then # Fallback for compatibility - info "Using \"${SUDO_USER}\" configuration: ${HOME}/.plexupdate" #>/dev/null + info "Reading \"${SUDO_USER}\" configuration in: ${HOME}/.plexupdate" CONFIGFILE="${HOME}/.plexupdate" # tilde expansion won't happen later. source ~/.plexupdate fi elif [ -f ~/.plexupdate ]; then # Fallback for compatibility - info "Using configuration: ${HOME}/.plexupdate" #>/dev/null + info "Reading configuration in: ${HOME}/.plexupdate" CONFIGFILE="${HOME}/.plexupdate" source ~/.plexupdate fi From ce947c13ea2a1c9f7882a1bb1e2af32a34cec362 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 14:02:42 -0800 Subject: [PATCH 17/42] Use dnf on newer redhat systems --- plexupdate.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index fdf543d..13aca15 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -67,7 +67,7 @@ WGETOPTIONS="" # extra options for wget. Used for progress bar. CHECKUPDATE=yes # Default options for package managers, override if needed -REDHAT_INSTALL="yum -y install" +REDHAT_INSTALL="dnf -y install" DEBIAN_INSTALL="dpkg -i" DISTRO_INSTALL="" @@ -260,7 +260,7 @@ fi # Allow manual control of configfile if [ ! -z "${CONFIGFILE}" ]; then if [ -f "${CONFIGFILE}" ]; then - info "Using configuration: ${CONFIGFILE}" #>/dev/null + info "Using configuration: ${CONFIGFILE}" source "${CONFIGFILE}" else error "Cannot load configuration ${CONFIGFILE}" @@ -450,7 +450,11 @@ if [ -z "${DISTRO_INSTALL}" ]; then REDHAT=yes BUILD="linux-ubuntu-${ARCH}" DISTRO="redhat" - DISTRO_INSTALL="${REDHAT_INSTALL}" + if ! hash dnf 2>/dev/null; then + DISTRO_INSTALL="${REDHAT_INSTALL/dnf/yum}" + else + DISTRO_INSTALL="${REDHAT_INSTALL}" + fi else REDHAT=no BUILD="linux-ubuntu-${ARCH}" From c6f19afdaa5892aea88ca8824062ce1d4f9816cc Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 18:49:48 -0800 Subject: [PATCH 18/42] Only use plexupdate.conf or --config --- plexupdate.sh | 58 ++------------------------------------------------- 1 file changed, 2 insertions(+), 56 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index 13aca15..a71c562 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -257,63 +257,9 @@ if ! hash wget 2>/dev/null; then exit 1 fi -# Allow manual control of configfile -if [ ! -z "${CONFIGFILE}" ]; then - if [ -f "${CONFIGFILE}" ]; then - info "Using configuration: ${CONFIGFILE}" - source "${CONFIGFILE}" - else - error "Cannot load configuration ${CONFIGFILE}" - exit 1 - fi -else - # Load settings from config file if it exists - if [ -f /etc/plexupdate.conf ]; then - info "Reading configuration in: /etc/plexupdate.conf" - CONFIGFILE=/etc/plexupdate.conf - source /etc/plexupdate.conf - fi +# If a config file was specified, or if /etc/plexupdate.conf exists, we'll use it. Otherwise, just skip it. - # Check for a SUDO_USER config - 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 - elif [[ $SUDO_USER == *" "* ]]; then ERROR=1 ; # Space is not a good thing - elif [[ $SUDO_USER == *"&"* ]]; then ERROR=1 ; # Spinning off the command is bad - elif [[ $SUDO_USER == *"<"* ]]; then ERROR=1 ; # No redirection - elif [[ $SUDO_USER == *">"* ]]; then ERROR=1 ; # No redirection - elif [[ $SUDO_USER == *"|"* ]]; then ERROR=1 ; # No pipes - elif [[ $SUDO_USER == *"~"* ]]; then ERROR=1 ; # No tilde - fi - if [ ${ERROR} -gt 0 ]; then - error "SUDO_USER variable is COMPROMISED: \"${SUDO_USER}\"" - exit 255 - fi - - # Try using original user's config - CONFIGDIR="$( eval cd ~${SUDO_USER} 2>/dev/null && pwd )" - if [ -z "${CONFIGDIR}" ]; then - warn "SUDO_USER \"${SUDO_USER}\" does not have a valid home directory, ignoring." - fi - - if [ ! -z "${CONFIGDIR}" -a -f "${CONFIGDIR}/.plexupdate" ]; then - info "Reading \"${SUDO_USER}\" configuration in: ${CONFIGDIR}/.plexupdate" - CONFIGFILE="${CONFIGDIR}/.plexupdate" - source "${CONFIGDIR}/.plexupdate" - elif [ -f ~/.plexupdate ]; then - # Fallback for compatibility - info "Reading \"${SUDO_USER}\" configuration in: ${HOME}/.plexupdate" - CONFIGFILE="${HOME}/.plexupdate" # tilde expansion won't happen later. - source ~/.plexupdate - fi - elif [ -f ~/.plexupdate ]; then - # Fallback for compatibility - info "Reading configuration in: ${HOME}/.plexupdate" - CONFIGFILE="${HOME}/.plexupdate" - source ~/.plexupdate - fi -fi +source "${CONFIGFILE:-"/etc/plexupdate.conf"}" 2>/dev/null # The way I wrote this, it assumes that whatever we put on the command line is what we want and should override # any values in the configuration file. As a result, we need to check if they've been set on the command line From 47a294a4ad15207e4c85bfbbd87e2a955f926c06 Mon Sep 17 00:00:00 2001 From: Henric Andersson Date: Sun, 20 Nov 2016 18:58:58 -0800 Subject: [PATCH 19/42] Give existing users explaination as to why plexupdate.sh failed We must make sure to tell existing users about the change in behavior. If they used ~/.plexupdate it will now properly warn them about the change and exit out. --- plexupdate.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index a71c562..75662c1 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -258,8 +258,15 @@ if ! hash wget 2>/dev/null; then fi # If a config file was specified, or if /etc/plexupdate.conf exists, we'll use it. Otherwise, just skip it. - -source "${CONFIGFILE:-"/etc/plexupdate.conf"}" 2>/dev/null +if [ -z "${CONFIGFILE}" ]; then + if [ -f "/etc/plexupdate.conf" ]; then + CONFIGFILE=/etc/plexupdate.conf + else + error "Due to recent changes, config file must be specified or placed in /etc/plexupdate.conf" + exit 1 + fi +fi +source "${CONFIGFILE}" 2>/dev/null # The way I wrote this, it assumes that whatever we put on the command line is what we want and should override # any values in the configuration file. As a result, we need to check if they've been set on the command line From 03ad32b53132163be42674cd22f3c0e57de8c603 Mon Sep 17 00:00:00 2001 From: Henric Andersson Date: Sun, 20 Nov 2016 19:02:33 -0800 Subject: [PATCH 20/42] Revert "Give existing users explaination as to why plexupdate.sh failed" This reverts commit 47a294a4ad15207e4c85bfbbd87e2a955f926c06. --- plexupdate.sh | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index 75662c1..a71c562 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -258,15 +258,8 @@ if ! hash wget 2>/dev/null; then fi # If a config file was specified, or if /etc/plexupdate.conf exists, we'll use it. Otherwise, just skip it. -if [ -z "${CONFIGFILE}" ]; then - if [ -f "/etc/plexupdate.conf" ]; then - CONFIGFILE=/etc/plexupdate.conf - else - error "Due to recent changes, config file must be specified or placed in /etc/plexupdate.conf" - exit 1 - fi -fi -source "${CONFIGFILE}" 2>/dev/null + +source "${CONFIGFILE:-"/etc/plexupdate.conf"}" 2>/dev/null # The way I wrote this, it assumes that whatever we put on the command line is what we want and should override # any values in the configuration file. As a result, we need to check if they've been set on the command line From 4cb2e3ff78fe5b41a148f2554b98bb361f8d4001 Mon Sep 17 00:00:00 2001 From: Henric Andersson Date: Sun, 20 Nov 2016 19:24:32 -0800 Subject: [PATCH 21/42] Fixed incorrect filename --- extras/cronwrapper | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/extras/cronwrapper b/extras/cronwrapper index 0bc6c32..452941f 100755 --- a/extras/cronwrapper +++ b/extras/cronwrapper @@ -19,11 +19,11 @@ # -if [ ! -f /etc/plexpass.cron.conf ]; then - echo "ERROR: You have not configured /etc/plexpass.cron.conf" >&2 +if [ ! -f /etc/plexupdate.cron.conf ]; then + echo "ERROR: You have not configured /etc/plexupdate.cron.conf" >&2 exit 255 else - source /etc/plexpass.cron.conf + source /etc/plexupdate.cron.conf fi if [ -z "${SCRIPT}" -o ! -f "${SCRIPT}" ]; then From 1faad1ed8697d493ac8459bb50cd51ca109d4c2f Mon Sep 17 00:00:00 2001 From: Henric Andersson Date: Sun, 20 Nov 2016 19:24:44 -0800 Subject: [PATCH 22/42] Removed references to .plexupdate --- plexupdate.sh | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index a71c562..576f450 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -7,13 +7,16 @@ # as well as the PlexPass versions. # # PlexPass users: -# Create a separate .plexupdate file in your home directory with these +# Create a plexupdate.conf file in your home directory with these # values: # # EMAIL='' # PASS='' # DOWNLOADDIR='' # +# And run the tool using: ./plexupdate.sh --config plexupdate.conf +# or place the config in /etc/plexupdate.conf +# # See https://github.com/mrworf/plexupdate for more details. # # Returns 0 on success @@ -33,16 +36,16 @@ # Check out https://github.com/mrworf/plexupdate for latest version # and also what's new. # -#################################################################### +############################################################################## # Quick-check before we allow bad things to happen if [ -z "${BASH_VERSINFO}" ]; then echo "ERROR: You must execute this script with BASH" >&2 exit 255 fi -################################################################# -# Don't change anything below this point, use a .plexupdate file -# in your home directory to override this section. +############################################################################## +# Don't change anything below this point, use a plexupdate.conf file +# to override this section. # DOWNLOADDIR is the full directory path you would like the download to go. # EMAIL= @@ -276,7 +279,7 @@ done # This will destroy and recreate the config file. Any settings that are set in the config file but are no longer # valid will NOT be saved. if [ "${SAVECONFIG}" = "yes" ]; then - echo "# Config file for plexupdate" >${CONFIGFILE:="${HOME}/.plexupdate"} + echo "# Config file for plexupdate" >${CONFIGFILE} for VAR in AUTOINSTALL AUTODELETE DOWNLOADDIR EMAIL PASS FORCE FORCEALL PUBLIC AUTOSTART AUTOUPDATE PLEXSERVER PLEXPORT CHECKUPDATE do @@ -319,12 +322,12 @@ if [ "${CRON}" = "yes" ]; then fi if [ "${KEEP}" = "yes" ]; then - error "KEEP is deprecated and should be removed from .plexupdate" + error "KEEP is deprecated and should be removed from config file" exit 255 fi if [ ! -z "${RELEASE}" ]; then - error "RELEASE keyword is deprecated and should be removed from .plexupdate" + error "RELEASE keyword is deprecated and should be removed from config file" error "Use DISTRO and BUILD instead to manually select what to install (check README.md)" exit 255 fi From 76bfad663a050ca9db25c8d035aecd031bc7e593 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 19:32:37 -0800 Subject: [PATCH 23/42] Better error handling for wget show-progress --- plexupdate.sh | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index 576f450..6712c98 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -309,7 +309,11 @@ if [ "${SAVECONFIG}" = "yes" ]; then fi if [ "${SHOWPROGRESS}" = "yes" ]; then - WGETOPTIONS="--show-progress" + if ! wget --show-progress -V &>/dev/null; then + warn "Your wget is too old to support --show-progress, ignoring" + else + WGETOPTIONS="--show-progress" + fi fi if [ "${IGNOREAUTOUPDATE}" = "yes" ]; then @@ -593,12 +597,6 @@ if [ "${SKIP_DOWNLOAD}" = "no" ]; then info "Downloading release \"${FILENAME}\"" wget ${WGETOPTIONS} -o "${FILE_WGETLOG}" --load-cookies "${FILE_KAKA}" --save-cookies "${FILE_KAKA}" --keep-session-cookies "${DOWNLOAD}" -O "${DOWNLOADDIR}/${FILENAME}" 2>&1 CODE=$? - if [ ${CODE} -eq 2 ]; then - error "Your wget is too old to support --show-progress" - info "Trying to download release \"${FILENAME}\" again" - wget -o "${FILE_WGETLOG}" --load-cookies "${FILE_KAKA}" --save-cookies "${FILE_KAKA}" --keep-session-cookies "${DOWNLOAD}" -O "${DOWNLOADDIR}/${FILENAME}" 2>&1 - CODE=$? - fi if [ ${CODE} -ne 0 ]; then error "Download failed with code ${CODE}:" From 450e53c6dcca007495ac5cda3e540cf0f431f32b Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 19:39:29 -0800 Subject: [PATCH 24/42] Remove saveconfig --- plexupdate.sh | 36 +----------------------------------- 1 file changed, 1 insertion(+), 35 deletions(-) diff --git a/plexupdate.sh b/plexupdate.sh index 6712c98..e035a21 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -128,7 +128,6 @@ usage() { echo " --pass Plex.TV password" echo " --server Address of Plex Server" echo " --port Port for Plex Server. Used with --server" - echo " --saveconfig Save the configuration to config file" echo exit 0 } @@ -213,7 +212,7 @@ fi # Parse commandline ALLARGS=( "$@" ) -optstring="-o acCdfFhlpPqrSsuU -l config:,dldir:,email:,pass:,server:,port:,saveconfig" +optstring="-o acCdfFhlpPqrSsuU -l config:,dldir:,email:,pass:,server:,port:" GETOPTRES=$(getopt $optstring -- "$@") if [ $? -eq 1 ]; then exit 1 @@ -245,7 +244,6 @@ do (--pass) shift; PASS_CL="$1"; PASS_CL=$(trimQuotes ${PASS_CL});; (--server) shift; PLEXSERVER_CL="$1"; PLEXSERVER_CL=$(trimQuotes ${PLEXSERVER_CL});; (--port) shift; PLEXPORT_CL="$1"; PLEXPORT_CL=$(trimQuotes ${PLEXPORT_CL});; - (--saveconfig) SAVECONFIG=yes;; (--) ;; (-*) error "Unrecognized option $1"; usage; exit 1;; @@ -276,38 +274,6 @@ do fi done -# This will destroy and recreate the config file. Any settings that are set in the config file but are no longer -# valid will NOT be saved. -if [ "${SAVECONFIG}" = "yes" ]; then - echo "# Config file for plexupdate" >${CONFIGFILE} - - for VAR in AUTOINSTALL AUTODELETE DOWNLOADDIR EMAIL PASS FORCE FORCEALL PUBLIC AUTOSTART AUTOUPDATE PLEXSERVER PLEXPORT CHECKUPDATE - do - if [ ! -z ${!VAR} ]; then - - # The following keys have defaults set in this file. We don't want to include these values if they are the default. - if [ ${VAR} = "FORCE" \ - -o ${VAR} = "FORCEALL" \ - -o ${VAR} = "PUBLIC" \ - -o ${VAR} = "AUTOINSTALL" \ - -o ${VAR} = "AUTODELETE" \ - -o ${VAR} = "AUTOUPDATE" \ - -o ${VAR} = "AUTOSTART" ]; then - - if [ ${!VAR} = "yes" ]; then - echo "${VAR}='${!VAR}'" >> ${CONFIGFILE} - fi - elif [ ${VAR} = "PLEXPORT" ]; then - if [ ! "${!VAR}" = "32400" ]; then - echo "${VAR}='${!VAR}'" >> ${CONFIGFILE} - fi - else - echo "${VAR}='${!VAR}'" >> ${CONFIGFILE} - fi - fi - done -fi - if [ "${SHOWPROGRESS}" = "yes" ]; then if ! wget --show-progress -V &>/dev/null; then warn "Your wget is too old to support --show-progress, ignoring" From 2acc47a151ce02696a5143c5f9d4605562978cc7 Mon Sep 17 00:00:00 2001 From: Henric Andersson Date: Sun, 20 Nov 2016 19:44:16 -0800 Subject: [PATCH 25/42] Improved README.md which matches reality --- README.md | 153 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 94 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 65e1b6a..251fc12 100755 --- a/README.md +++ b/README.md @@ -5,12 +5,35 @@ Plex Update is a BASH script which simplifies the life of headless Linux Plex Me This tool will automatically download the latest version for linux (Using plexpass or public version) and if you **kindly ask**, also install it for you. +# What happened to `.plexupdate` ? + +It has gone away. Due to the fact that most of the time, the script is run as root, it meant a lot of code which had the potential to include the wrong configuration file. So it has been streamlined to only check for `/etc/plexupdate.conf`. You can always call out any config you want using `--config` parameter, but from now on, `.plexupdate` is dead. + +On the other hand, `plexupdate.sh` now supports cron out of the box and comes with an easy to use installer (see more below). + # Installation -It's fairly easy, but let's take it step by step (if it seems too much, skip to the end for a short version) +In the old days, this used to be a bit of a chore. But no more! + +``` +bash -c "$(wget -O - https://raw.githubusercontent.com/mrworf/plexupdate/master/extras/installer.sh)" +``` + +will automatically install the tool as well as any dependencies. This has been tested on Ubuntu, Fedora and CentOS but should, for the most part, work on any modern linux distribution. + +But, if it failed, read on and we'll guide you. ## 1. Getting the code +####Using git to clone (Recommended) +Using git is way easier and recommended +``` +git clone https://github.com/mrworf/plexupdate.git +``` +Note that git is required (`sudo apt-get install git`) + +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. + ####Using wget and unzip Download it as a [zip file](https://github.com/mrworf/plexupdate/archive/master.zip) and unzip it on your server. @@ -19,33 +42,71 @@ wget https://github.com/mrworf/plexupdate/archive/master.zip && unzip master.zip ``` Note that unzip is required (`sudo apt-get install unzip`). -####Using git to clone (Recommended) -Using git is way easier and recommended, if you ask me. -``` -git clone https://github.com/mrworf/plexupdate.git -``` -Note that git is required (`sudo apt-get install git`) - -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 -To quickly setup plexupdate.sh, you should run it the first time like below: +In order to use `plexupdate.sh`, it's recommended you create a configuration file. ``` -./plexupdate.sh --email='my.email@plex-server.com' --pass='my-secret-plex-password' --dldir='/a/folder/to/save/the/files/in' --saveconfig +nano -w plexupdate.conf ``` -Obviously you need to change these so they match your account information. And if you don't put anything as for the ```--dldir``` option, the tool will use the folder you're executing the script from. So take care. +In the newly opened editor, insert the following (and make *sure* to change email and password) -## 3. Advanced options +``` +EMAIL='john.doe@void.com' +PASS='verySecretPassword' +DOWNLOADDIR='/tmp/' +``` -You can point out a different file than ```.plexupdate``` by providing it as the argument to the ```--config``` option. Any options set by the config file can be overridden with command-line options. +This will make `plexupdate.sh` login and download the latest version and save it to /tmp/ folder. + +If you don't have plexpass, you can still use `plexupdate.sh`, just set `PUBLIC=yes` instead. The section above becomes + +``` +PUBLIC=yes +DOWNLOADDIR='/tmp/' +``` + +## 3. Cronjob + +You might be more interested in running this on a regular basis. To accomplish this, we need to do the following. Locate the `extras` folder which was included with plexupdate. In this folder you'll find `cronwrapper`. You need to "symlink" this into `/etc/cron.daily/`. Symlink means we tell the system that there should be reference/link to the file included in plexupdate. By not copying, we will automatically get updates to the `cronwrapper` when we update plexupdate. + +When doing the symlink, it's important to provide the complete path to the file in question, so you will need to edit the path to it in the following snippet. Also, we need to run as root, since only root is allowed to edit files under `/etc`. + +``` +sudo ln -s /home/john/plexupdate/extras/cronwrapper /etc/cron.daily/plexupdate +``` + +We also need to tell cronwrapper where to find plexupdate, again, this needs to be done as root for the same reasons as above. + +``` +sudo nano -w /etc/plexupdate.cron.conf +``` + +In the new file, we simply point out the location of `plexupdate.sh` and `plexupdate.conf` + +``` +SCRIPT=/home/john/plexupdate/plexupdate.sh +CONF=/home/john/plexupdate.conf +``` + +If you've installed it somewhere else and/or the path to the config is somewhere else, please *make sure* to write the correct paths. + +Almost done. Final step is to make `plexupdate.sh` a bit smarter and have it install the newly downloaded version, so open the `plexupdate.conf` file you created previously and add the following: + +``` +AUTOINSTALL=yes +AUTODELETE=yes +``` + +This tells `plexupdate.sh` to install the file once downloaded and delete it when done, keeping your server nice and clean. + +## 4. Advanced options There are also a few additional options for the more enterprising user. Setting any of these to `yes` will enable the function. - CHECKUPDATE - If set (and it is by default), it will compare your local copy with the one stored on github. If there is any difference, it will let you know. This is handy if you're not using ```git clone``` but want to be alerted to new versions. + If set (and it is by default), it will compare your local copy with the one stored on github. If there is any difference, it will let you know. This is handy if you're not using `git clone` but want to be alerted to new versions. - PLEXSERVER If set, and combined with AUTOINSTALL, the script will automatically check if the server is in-use and defer 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 - PLEXPORT @@ -72,69 +133,43 @@ There are also a few additional options for the more enterprising user. Setting Most of these options can be specified on the command-line as well, this is just a more convenient way of doing it if you're scripting it. Which brings us to... -### Using it from CRON - -Generally, you copy the cronwrapper from the extras folder into the ```/etc/cron.weekly``` and edit the script to suite your needs. It will require you to create a configuration file (or at least, it's highly recommended). It also provides logging via syslog if needed. Should the script fail, it will print why and in most cases, this will result in an email to you. - -[Also checkout the wiki for more details](https://github.com/mrworf/plexupdate/wiki/Ubuntu%3A-Run-plexupdate.sh-from-cron) - ### Command Line Options -Several new command line options are available. They can be specified in any order. +Plexupdate comes with many command line options, for the most up-to-date, I'd recommend you run plexupdate.sh with -h -- ```--config ``` +But here are some of the more useful ones: + +- `--config ` Defines the location the script should look for the config file. -- ```--email ``` +- `--email ` Email to sign in to Plex.tv -- ```--pass ``` +- `--pass ` Password to sign in to Plex.tv -- ```--dldir ``` +- `--dldir ` This is the folder that the files will be downloaded to. -- ```--server ``` +- `--server ` This is the address that Plex Media Server is on. Setting this will enable a check to see if users are on the server prior to the software being updated. -- ```--port ``` +- `--port ` This is the port that Plex Media Server uses. -- ```--saveconfig``` - Saves the configuration as it is currently. This will take whatever is in the config file, plus whatever is specified on the command line and will save the config file with that information. Any information in the config file that plexupdate.sh does not understand or use WILL BE LOST. - -### Logs - -The script now outputs everything to a file (by default `/tmp/plexupdate.log`). This log ***MAY*** contain passwords, so if you post it online, ***USE CAUTION***. - -To change the default log file, you can specify a new location: - -```FILE_STDOUTLOG="" ./plexupdate.sh ``` - -# Running it - -It's very simple, just execute the tool once configured. It will complain if you've forgotten to set it up. If you want to use the autoinstall (-a option or `AUTOINSTALL=YES` is set), you must run as root or use sudo when executing or plexupdate.sh will stop and give you an error. - -Overall it tries to give you hints regarding why it isn't doing what you expected it to. # Trivia - "kaka" is Swedish for "cookie" -# TL;DR -Open a terminal or SSH on the server running Plex Media Center -## First install -``` -git clone https://github.com/mrworf/plexupdate.git -sudo plexupdate/plexupdate.sh -p -a -``` -## Updating Plex and the script -``` -sudo plexupdate/plexupdate.sh -p -u -a -``` - # FAQ -## What username and password are you talking about +## Where is `.plexupdate` -The username and password for http://plex.tv +See explaination in the top of this document. + +## What email and password are you talking about + +The email and password for http://plex.tv ## My password is rejected even though correct 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 (`"`). i.e. `PASS="MyP4$$w0rD"` will not work, but changing to it to `PASS='MyP4$$w0rD'` will + +If it's still not working, run `plexupdate.sh` with `-v` which prints out the email and password used to login which might help you understand what the problem is. \ No newline at end of file From 0f8d27caae5a1f5a7435cd32c82e4d14e75b6e49 Mon Sep 17 00:00:00 2001 From: Henric Andersson Date: Sun, 20 Nov 2016 19:46:42 -0800 Subject: [PATCH 26/42] Less verbose --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 251fc12..d9ceb39 100755 --- a/README.md +++ b/README.md @@ -7,9 +7,7 @@ This tool will automatically download the latest version for linux (Using plexpa # What happened to `.plexupdate` ? -It has gone away. Due to the fact that most of the time, the script is run as root, it meant a lot of code which had the potential to include the wrong configuration file. So it has been streamlined to only check for `/etc/plexupdate.conf`. You can always call out any config you want using `--config` parameter, but from now on, `.plexupdate` is dead. - -On the other hand, `plexupdate.sh` now supports cron out of the box and comes with an easy to use installer (see more below). +It has gone away to keep it simpler and more secure. You can either provide the config you want using `--config` parameter or place it in `/etc/plexupdate.conf`. # Installation From 3991374c31b010b787fde24e0c42b80b729ec724 Mon Sep 17 00:00:00 2001 From: Henric Andersson Date: Sun, 20 Nov 2016 19:48:12 -0800 Subject: [PATCH 27/42] Better english --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d9ceb39..fcc50a3 100755 --- a/README.md +++ b/README.md @@ -23,14 +23,14 @@ But, if it failed, read on and we'll guide you. ## 1. Getting the code -####Using git to clone (Recommended) -Using git is way easier and recommended +####Using git to clone +Using git is the recommended way of getting it ``` git clone https://github.com/mrworf/plexupdate.git ``` Note that git is required (`sudo apt-get install git`) -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. +The main benefit with git clone is that you can update to latest version very easily and it also allows you to use the auto update feature. ####Using wget and unzip From 7e4018e129ec6be5953381228c4c4584491ab511 Mon Sep 17 00:00:00 2001 From: Henric Andersson Date: Sun, 20 Nov 2016 19:49:11 -0800 Subject: [PATCH 28/42] English is hard --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fcc50a3..7f6bb4b 100755 --- a/README.md +++ b/README.md @@ -158,7 +158,7 @@ But here are some of the more useful ones: ## Where is `.plexupdate` -See explaination in the top of this document. +See explanation in the top of this document. ## What email and password are you talking about From d25646634b63e929e94d4dffe8f412f1d746b1ea Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 19:47:40 -0800 Subject: [PATCH 29/42] Move plexupdate version check to variable --- plexupdate.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plexupdate.sh b/plexupdate.sh index e035a21..9e7fb01 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -79,6 +79,9 @@ URL_LOGIN='https://plex.tv/users/sign_in.json' URL_DOWNLOAD='https://plex.tv/api/downloads/1.json?channel=plexpass' URL_DOWNLOAD_PUBLIC='https://plex.tv/api/downloads/1.json' +#URL for new version check +UPSTREAM_GIT_URL='https://raw.githubusercontent.com/demonbane/plexupdate/reworklog/plexupdate.sh' #FIXME + FILE_POSTDATA=$(mktemp /tmp/plexupdate.postdata.XXXX) FILE_RAW=$(mktemp /tmp/plexupdate.raw.XXXX) FILE_FAILCAUSE=$(mktemp /tmp/plexupdate.failcause.XXXX) @@ -392,7 +395,7 @@ else fi if [ "${CHECKUPDATE}" = "yes" ]; then - (wget -q https://raw.githubusercontent.com/mrworf/plexupdate/master/plexupdate.sh -O - 2>/dev/null || echo ERROR) | shasum >"${FILE_REMOTE}" 2>/dev/null + (wget -q "$UPSTREAM_GIT_URL" -O - 2>/dev/null || echo ERROR) | shasum >"${FILE_REMOTE}" 2>/dev/null ERR1=$? (cat "$0" 2>/dev/null || echo ERROR) | shasum >"${FILE_LOCAL}" 2>/dev/null ERR2=$? From a64e3721d93076d9e99109d0be020e7abb673ae0 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 19:48:25 -0800 Subject: [PATCH 30/42] Rearranged some logic and better error handling throughout * Better distro checking * Make yesno and noyes less confusing, hopefully * Better handling of previously specified options * Enable AUDOSTART on distros that need it * Abort if we can't get sudo permissions * Option to run plexupdate with the newly created config --- extras/installer.sh | 165 +++++++++++++++++++++++++++++--------------- 1 file changed, 111 insertions(+), 54 deletions(-) diff --git a/extras/installer.sh b/extras/installer.sh index df24414..efe6f6c 100755 --- a/extras/installer.sh +++ b/extras/installer.sh @@ -1,59 +1,99 @@ #!/bin/bash -ORIGIN_REPO="https://github.com/mrworf/plexupdate" +ORIGIN_REPO="https://github.com/demonbane/plexupdate" #FIXME OPT_PATH="/opt" FULL_PATH="$OPT_PATH/plexupdate" CONFIGFILE="/etc/plexupdate.conf" CONFIGCRON="/etc/plexupdate.cron.conf" CRONWRAPPER="/etc/cron.daily/plexupdate" +# default options +AUTOINSTALL=yes +AUTOUPDATE=yes +PUBLIC= + install() { echo "'$req' is required but not installed, attempting to install..." sleep 1 - if $UBUNTU; then - DISTRO_INSTALL="apt install $1" - elif $REDHAT; then - if hash dnf 2>/dev/null; then - DISTRO_INSTALL="dnf install $1" - else - DISTRO_INSTALL="yum install $1" - fi - fi + [ -z "$DISTRO_INSTALL" ] && check_distro + echo "DISTRO_INSTALL='$DISTRO_INSTALL'" if [ $EUID != 0 ]; then - sudo $DISTRO_INSTALL + sudo $DISTRO_INSTALL $1 else - $DISTRO_INSTALL + $DISTRO_INSTALL $1 fi } -yesno() { - while true; do - read -n 1 -p "[Y/n] " answer +check_distro() { + if [ -f /etc/redhat-release ] && hash dnf 2>/dev/null; then + DISTRO="redhat" + DISTRO_INSTALL="dnf -y install" + elif [ -f /etc/redhat-release ] && hash yum 2>/dev/null; then + DISTRO="redhat" #or CentOS but functionally the same + DISTRO_INSTALL="yum -y install" + elif hash apt 2>/dev/null; then + DISTRO="debian" #or Ubuntu + DISTRO_INSTALL="apt install" + elif hash apt-get 2>/dev/null; then + DISTRO="debian" + DISTRO_INSTALL="apt-get install" + else + DISTRO="unknown" + fi + echo "DISTRO='$DISTRO'" + echo "DISTRO_INSTALL='$DISTRO_INSTALL'" + echo "check_distro completed" +} - if [ "$answer" == "n" -o "$answer" == "N" ]; then - echo - return 1 - elif [ -z "$answer" -o "$answer" == "y" -o "$answer" == "Y" ]; then +yesno() { + case "$1" in + "") + default="Y" + ;; + yes) + default="Y" + ;; + true) + default="Y" + ;; + no) + default="N" + ;; + false) + default="N" + ;; + *) + default="$1" + ;; + esac + + default="$(tr "[:lower:]" "[:upper:]" <<< "$default")" + if [ "$default" == "Y" ]; then + prompt="[Y/n] " + else + if [ "$default" != "N" ]; then echo "default='$default'"; fi + prompt="[N/y] " + fi + + while true; do + read -n 1 -p "$prompt" answer + answer=${answer:-$default} + answer="$(tr "[:lower:]" "[:upper:]" <<< "$answer")" + + if [ "$answer" == "Y" ]; then echo return 0 + elif [ "$answer" == "N" ]; then + echo + return 1 fi done } noyes() { - while true; do - read -n 1 -p "[N/y] " answer - - if [ "$answer" == "y" -o "$answer" == "Y" ]; then - echo - return 1 - elif [ -z "$answer" -o "$answer" == "n" -o "$answer" == "N" ]; then - echo - return 0 - fi - done + yesno N } abort() { @@ -64,13 +104,16 @@ abort() { configure_plexupdate() { CONFIGTEMP=$(mktemp /tmp/plexupdate.tempconf.XXX) - AUTOUPDATE=yes [ -f "$CONFIGFILE" ] && source "$CONFIGFILE" echo echo -n "Do you want to install the latest PlexPass releases? " - if yesno; then + # The answer to this question and the value of PUBLIC are basically inverted + if [ "$PUBLIC" == "yes" ]; then + default=N + fi + if yesno $default; then PUBLIC= while true; do read -e -p "PlexPass Email Address: " -i "$EMAIL" EMAIL @@ -89,6 +132,7 @@ configure_plexupdate() { fi done else + # don't forget to erase old settings if they changed their answer EMAIL= PASS= PUBLIC=yes @@ -96,15 +140,20 @@ configure_plexupdate() { echo echo -n "Would you like to automatically install the latest release when it is downloaded? " - if yesno; then - AUTOINSTALL=yes - else - AUTOINSTALL=no - fi - if [ "$AUTOINSTALL" == "yes" ]; then + if yesno "$AUTOINSTALL"; then + AUTOINSTALL=yes + + [ -z "$DISTRO" ] && check_distro + if [ "$DISTRO" == "redhat" ]; then + AUTOSTART=yes + else + AUTOSTART= + fi + echo echo -n "When using the auto-install option, would you like to check if the server is in use before upgrading? " + #We can't tell if they previously selected no or if this is their first run, so we have to assume Yes if yesno; then if [ -z "$PLEXSERVER" ]; then PLEXSERVER="127.0.0.1" @@ -113,7 +162,7 @@ configure_plexupdate() { read -e -p "Plex Server IP/DNS name: " -i "$PLEXSERVER" PLEXSERVER if ! ping -c 1 -w 1 "$PLEXSERVER" &>/dev/null ; then echo -n "Server $PLEXSERVER isn't responding, are you sure you entered it correctly? " - if ! noyes; then + if yesno N; then break fi else @@ -137,24 +186,32 @@ configure_plexupdate() { PLEXPORT= fi else + AUTOINSTALL=no PLEXSERVER= PLEXPORT= fi - save_config "AUTOUPDATE EMAIL PASS PUBLIC AUTOINSTALL PLEXSERVER PLEXPORT" "$CONFIGFILE" + save_config "AUTOUPDATE EMAIL PASS PUBLIC AUTOINSTALL AUTOSTART PLEXSERVER PLEXPORT" "$CONFIGFILE" } configure_cron() { + if [ ! -d "$(dirname "$CRONWRAPPER")" ]; then + echo "Seems like you don't have a supported cron job setup, please see README.md for more details." + return 1 + fi + + [ -f "$CONFIGCRON" ] && source "$CONFIGCRON" + echo echo -n "Would you like to set up automatic daily updates for Plex? " if yesno; then CONF="$CONFIGFILE" SCRIPT="${FULL_PATH}/plexupdate.sh" - LOGGING=false + LOGGING=${LOGGING:-false} echo echo -n "Do you want to log the daily update runs to syslog so you can examine the output later? " - if yesno; then + if yesno $LOGGING; then LOGGING=true fi @@ -205,13 +262,7 @@ save_config() { if [ $EUID -ne 0 ]; then echo echo "This script needs to be run with root/sudo, but you are running as '$(whoami)'. Enabling sudo." - sudo -v -fi - -if [ -f /etc/redhat-release ]; then - REDHAT=true -else - UBUNTU=true + sudo -v || abort "Root permissions are required for setup, cannot continue" fi for req in wget git; do @@ -238,7 +289,7 @@ fi if [ -d "${FULL_PATH}/.git" ]; then cd "$FULL_PATH" - if git remote -v | grep -q "mrworf/plexupdate"; then + if git remote -v | grep -q "plexupdate"; then echo -n "Found existing plexupdate repository in '$FULL_PATH', updating... " git pull >/dev/null || abort "Unknown error while updating, please check '$FULL_PATH' and then try again." else @@ -257,8 +308,14 @@ else fi configure_plexupdate -if [ -d "$(dirname "$CRONWRAPPER")" ]; then - configure_cron -else - echo "Seems like you don't have a supported cron job setup, please see README.md for more details." +configure_cron + +echo +echo -n "Configuration complete. Would you like to run plexupdate with these settings now? " +if yesno; then + if [ "$AUTOINSTALL" == "yes" -a $EUID -ne 0 ]; then + sudo "$FULL_PATH/plexupdate.sh" -P --config "$CONFIGFILE" + else + "$FULL_PATH/plexupdate.sh" -P --config "$CONFIGFILE" + fi fi From 82ccb3985823ff2928108c3c96253cb0866e2311 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 20:25:07 -0800 Subject: [PATCH 31/42] Remove temporary debugging messages --- extras/installer.sh | 5 ----- 1 file changed, 5 deletions(-) diff --git a/extras/installer.sh b/extras/installer.sh index efe6f6c..d2facc1 100755 --- a/extras/installer.sh +++ b/extras/installer.sh @@ -17,7 +17,6 @@ install() { sleep 1 [ -z "$DISTRO_INSTALL" ] && check_distro - echo "DISTRO_INSTALL='$DISTRO_INSTALL'" if [ $EUID != 0 ]; then sudo $DISTRO_INSTALL $1 @@ -42,9 +41,6 @@ check_distro() { else DISTRO="unknown" fi - echo "DISTRO='$DISTRO'" - echo "DISTRO_INSTALL='$DISTRO_INSTALL'" - echo "check_distro completed" } yesno() { @@ -73,7 +69,6 @@ yesno() { if [ "$default" == "Y" ]; then prompt="[Y/n] " else - if [ "$default" != "N" ]; then echo "default='$default'"; fi prompt="[N/y] " fi From e38a115372cfb37fe0f14a7eefe6e901cc79fe0c Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 21:04:09 -0800 Subject: [PATCH 32/42] Cleaning up README --- README.md | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 7f6bb4b..7706a45 100755 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ ![plexupdate.sh](http://i.imgur.com/ThY5Rvl.png "plexupdate") # plexupdate -Plex Update is a BASH script which simplifies the life of headless Linux Plex Media Server users (how's that for a strange description). +Plex Update is a bash script which helps you keep Plex Media Server up to date on Linux. -This tool will automatically download the latest version for linux (Using plexpass or public version) and if you **kindly ask**, also install it for you. +plexupdate will automatically download the latest version of Plex Media Server for Linux and, optionally, also install it for you. -# What happened to `.plexupdate` ? +### What happened to `.plexupdate` ? -It has gone away to keep it simpler and more secure. You can either provide the config you want using `--config` parameter or place it in `/etc/plexupdate.conf`. +It has gone away to keep things simpler and more secure. You can either provide the config you want using the `--config` parameter or place it in `/etc/plexupdate.conf`. # Installation @@ -17,20 +17,21 @@ In the old days, this used to be a bit of a chore. But no more! bash -c "$(wget -O - https://raw.githubusercontent.com/mrworf/plexupdate/master/extras/installer.sh)" ``` -will automatically install the tool as well as any dependencies. This has been tested on Ubuntu, Fedora and CentOS but should, for the most part, work on any modern linux distribution. +will automatically install the tool as well as any dependencies. This has been tested on Ubuntu, Fedora and CentOS but should, for the most part, work on any modern Linux distribution. -But, if it failed, read on and we'll guide you. +If you'd ever like to change your configuration, you can just re-run this from the extras folder inside your plexupdate directory. (`/opt/plexupdate/extras/installer.sh` by default) + +If you have any trouble with the installer, or would just prefer to set plexupdate up manually, read on. ## 1. Getting the code -####Using git to clone -Using git is the recommended way of getting it +####Using git to clone (recommended) ``` git clone https://github.com/mrworf/plexupdate.git ``` Note that git is required (`sudo apt-get install git`) -The main benefit with git clone is that you can update to latest version very easily and it also allows you to use the auto update feature. +This is the recommended way to install plexupdate. Using git allows you to know when a new version is available as well allowing plexupdate to keep itself up to date (with the AUTOUPDATE option). ####Using wget and unzip @@ -45,7 +46,7 @@ Note that unzip is required (`sudo apt-get install unzip`). In order to use `plexupdate.sh`, it's recommended you create a configuration file. ``` -nano -w plexupdate.conf +sudo nano -w /etc/plexupdate.conf ``` In the newly opened editor, insert the following (and make *sure* to change email and password) @@ -58,7 +59,7 @@ DOWNLOADDIR='/tmp/' This will make `plexupdate.sh` login and download the latest version and save it to /tmp/ folder. -If you don't have plexpass, you can still use `plexupdate.sh`, just set `PUBLIC=yes` instead. The section above becomes +If you don't have PlexPass, you can still use `plexupdate.sh`, just set `PUBLIC=yes` instead. The section above becomes ``` PUBLIC=yes @@ -116,7 +117,7 @@ There are also a few additional options for the more enterprising user. Setting - AUTODELETE Once successfully downloaded and installed, it will delete the package (want not, waste not? ;-)) - 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. + The default behavior of plexupdate.sh is to download the PlexPass edition of Plex Media Server. 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. Using this option will force it to download again UNLESS the file already downloaded has the correct checksum. If you have AUTOINSTALL set, plexupdate.sh will then reinstall it. - FORCEALL @@ -133,7 +134,7 @@ Most of these options can be specified on the command-line as well, this is just ### Command Line Options -Plexupdate comes with many command line options, for the most up-to-date, I'd recommend you run plexupdate.sh with -h +Plexupdate comes with many command line options. For the most up-to-date list, I'd recommend you run plexupdate.sh with -h But here are some of the more useful ones: @@ -170,4 +171,4 @@ If you use certain characters (such as `$`) in your password, bash will interpre i.e. `PASS="MyP4$$w0rD"` will not work, but changing to it to `PASS='MyP4$$w0rD'` will -If it's still not working, run `plexupdate.sh` with `-v` which prints out the email and password used to login which might help you understand what the problem is. \ No newline at end of file +If it's still not working, run `plexupdate.sh` with `-v` which prints out the email and password used to login which might help you understand what the problem is. From 4e738f1e56fe5569bdf70c4ad2bf3537d6930934 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 21:51:30 -0800 Subject: [PATCH 33/42] Warn users that .plexupdate is going away --- plexupdate.sh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/plexupdate.sh b/plexupdate.sh index 9e7fb01..a86fdf5 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -261,8 +261,18 @@ if ! hash wget 2>/dev/null; then exit 1 fi -# If a config file was specified, or if /etc/plexupdate.conf exists, we'll use it. Otherwise, just skip it. +#FIXME: Temporary error checking to notify people of change from .plexupdate to plexupdate.conf +if [ -z "${CONFIGFILE}" -a -f ~/.plexupdate -a ! -f /etc/plexupdate.conf ] || [ `stat -Lc %i "${CONFIGFILE}"` == `stat -Lc %i ~/.plexupdate` ]; then + warn ".plexupdate has been deprecated. You should move your configuration to /etc/plexupdate.conf" + if [ -t 1 ]; then + for i in `seq 1 5`; do echo -n .\ ; sleep 1; done + echo . + fi + CONFIGFILE=~/.plexupdate +fi +#FIXME +# If a config file was specified, or if /etc/plexupdate.conf exists, we'll use it. Otherwise, just skip it. source "${CONFIGFILE:-"/etc/plexupdate.conf"}" 2>/dev/null # The way I wrote this, it assumes that whatever we put on the command line is what we want and should override From b858198dc552ed14a0c1f80527a266e4c7036c20 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sun, 20 Nov 2016 21:56:37 -0800 Subject: [PATCH 34/42] Don't delete existing settings in conf file when running installer --- extras/installer.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extras/installer.sh b/extras/installer.sh index d2facc1..ded5069 100755 --- a/extras/installer.sh +++ b/extras/installer.sh @@ -186,7 +186,7 @@ configure_plexupdate() { PLEXPORT= fi - save_config "AUTOUPDATE EMAIL PASS PUBLIC AUTOINSTALL AUTOSTART PLEXSERVER PLEXPORT" "$CONFIGFILE" + save_config "AUTOINSTALL AUTODELETE DOWNLOADDIR EMAIL PASS FORCE FORCEALL PUBLIC AUTOSTART AUTOUPDATE PLEXSERVER PLEXPORT CHECKUPDATE" "$CONFIGFILE" } configure_cron() { From 24dd899bf53d1431d420fa8bcdbec99133aedcc9 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Fri, 25 Nov 2016 20:17:00 -0800 Subject: [PATCH 35/42] Double-check for files to stat in .plexupdate check --- plexupdate.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plexupdate.sh b/plexupdate.sh index a86fdf5..a40f143 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -262,7 +262,9 @@ if ! hash wget 2>/dev/null; then fi #FIXME: Temporary error checking to notify people of change from .plexupdate to plexupdate.conf -if [ -z "${CONFIGFILE}" -a -f ~/.plexupdate -a ! -f /etc/plexupdate.conf ] || [ `stat -Lc %i "${CONFIGFILE}"` == `stat -Lc %i ~/.plexupdate` ]; then +# We have to double-check that both files exist before trying to stat them. This is going away soon. +if [ -z "${CONFIGFILE}" -a -f ~/.plexupdate -a ! -f /etc/plexupdate.conf ] || \ + [ -f "${CONFIGFILE}" -a -f ~/.plexupdate ] && [ `stat -Lc %i "${CONFIGFILE}"` == `stat -Lc %i ~/.plexupdate` ]; then warn ".plexupdate has been deprecated. You should move your configuration to /etc/plexupdate.conf" if [ -t 1 ]; then for i in `seq 1 5`; do echo -n .\ ; sleep 1; done From 5040bed986143875afb70933a6547dbff602066b Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Fri, 25 Nov 2016 20:17:41 -0800 Subject: [PATCH 36/42] Offer to import settings in ~/.plexupdate.conf in installer (if they exist) --- extras/installer.sh | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/extras/installer.sh b/extras/installer.sh index ded5069..619655c 100755 --- a/extras/installer.sh +++ b/extras/installer.sh @@ -199,7 +199,7 @@ configure_cron() { echo echo -n "Would you like to set up automatic daily updates for Plex? " - if yesno; then + if yesno $CRON; then CONF="$CONFIGFILE" SCRIPT="${FULL_PATH}/plexupdate.sh" LOGGING=${LOGGING:-false} @@ -233,7 +233,6 @@ configure_cron() { fi } - save_config() { CONFIGTEMP=$(mktemp /tmp/plexupdate.XXX) for VAR in $1; do @@ -266,8 +265,17 @@ for req in wget git; do fi done -echo -e "\n" +if [ -f ~/.plexupdate ]; then + echo + echo -n "Existing configuration found in ~/.plexupdate, would you like to import these settings? " + if yesno; then + echo "Backing up old configuration as ~/.plexupdate.old. All new settings should be modified through this script, or by editing ${CONFIGFILE} directly. Please see README.md for more details." + source ~/.plexupdate + mv ~/.plexupdate ~/.plexupdate.old + fi +fi +echo read -e -p "Directory to install into: " -i "/opt/plexupdate" FULL_PATH if [ ! -d "$FULL_PATH" ]; then echo -n "'$FULL_PATH' doesn't exist, attempting to create... " @@ -287,6 +295,7 @@ if [ -d "${FULL_PATH}/.git" ]; then if git remote -v | grep -q "plexupdate"; then echo -n "Found existing plexupdate repository in '$FULL_PATH', updating... " git pull >/dev/null || abort "Unknown error while updating, please check '$FULL_PATH' and then try again." + echo else abort "'$FULL_PATH' appears to contain a different git repository, cannot continue" fi From 5a485b7192d72858937991a030d28c3c0eec95dc Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Fri, 25 Nov 2016 20:27:32 -0800 Subject: [PATCH 37/42] Only show AUTOSTART warning if AUTOINSTALL is enabled --- plexupdate.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plexupdate.sh b/plexupdate.sh index a40f143..553f910 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -534,7 +534,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 [ "${AUTOINSTALL}" = "yes" -a "${AUTOSTART}" = "no" ]; then warn "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) From 911fc8a3efb129bac14d16e70ed3a1f612c7bb17 Mon Sep 17 00:00:00 2001 From: Jon Shaulis Date: Mon, 28 Nov 2016 19:12:07 -0500 Subject: [PATCH 38/42] Tilde in directory name breaks mkdir --- extras/installer.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/extras/installer.sh b/extras/installer.sh index 619655c..cad3c19 100755 --- a/extras/installer.sh +++ b/extras/installer.sh @@ -277,6 +277,9 @@ fi echo read -e -p "Directory to install into: " -i "/opt/plexupdate" FULL_PATH +if [[ "$FULL_PATH" == *"~"* ]]; then # If path contains a tilde + FULL_PATH=${FULL_PATH/[~]/$HOME} # Replace tilde with the home path +fi if [ ! -d "$FULL_PATH" ]; then echo -n "'$FULL_PATH' doesn't exist, attempting to create... " if ! mkdir -p "$FULL_PATH" 2>/dev/null; then From 3d19b4ee01a94519fc3dc65d4c52615d050fd93e Mon Sep 17 00:00:00 2001 From: Jon Shaulis Date: Mon, 28 Nov 2016 20:28:22 -0500 Subject: [PATCH 39/42] Change default download directory to /tmp --- plexupdate.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plexupdate.sh b/plexupdate.sh index 553f910..a0f3b7f 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -50,7 +50,7 @@ fi # EMAIL= PASS= -DOWNLOADDIR="." +DOWNLOADDIR="/tmp" PLEXSERVER= PLEXPORT=32400 From 114a42271f0e246be9b50af1a0fe612fdf4dedf7 Mon Sep 17 00:00:00 2001 From: Jon Shaulis Date: Mon, 28 Nov 2016 20:29:13 -0500 Subject: [PATCH 40/42] Fix tilde issue --- extras/installer.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extras/installer.sh b/extras/installer.sh index cad3c19..e10d2fa 100755 --- a/extras/installer.sh +++ b/extras/installer.sh @@ -277,8 +277,8 @@ fi echo read -e -p "Directory to install into: " -i "/opt/plexupdate" FULL_PATH -if [[ "$FULL_PATH" == *"~"* ]]; then # If path contains a tilde - FULL_PATH=${FULL_PATH/[~]/$HOME} # Replace tilde with the home path +if [[ "$FULL_PATH" == *"~"* ]]; then + abort "Invalid character in path, cannot continue" fi if [ ! -d "$FULL_PATH" ]; then echo -n "'$FULL_PATH' doesn't exist, attempting to create... " From 93589330903c1f3b9c2cebc2be44325130a39f29 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Mon, 28 Nov 2016 17:43:46 -0800 Subject: [PATCH 41/42] Clean up error message on tilde use --- extras/installer.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/extras/installer.sh b/extras/installer.sh index e10d2fa..e71d7d9 100755 --- a/extras/installer.sh +++ b/extras/installer.sh @@ -277,9 +277,13 @@ fi echo read -e -p "Directory to install into: " -i "/opt/plexupdate" FULL_PATH -if [[ "$FULL_PATH" == *"~"* ]]; then - abort "Invalid character in path, cannot continue" -fi + +while [[ "$FULL_PATH" == *"~"* ]]; do + echo "Using '~' in your path can cause problems, please type out the full path instead" + echo + read -e -p "Directory to install into: " -i "/opt/plexupdate" FULL_PATH +done + if [ ! -d "$FULL_PATH" ]; then echo -n "'$FULL_PATH' doesn't exist, attempting to create... " if ! mkdir -p "$FULL_PATH" 2>/dev/null; then From 65f18f3da27f5ebeaf9a1c9905f4fdfbde72fc04 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Mon, 28 Nov 2016 18:51:16 -0800 Subject: [PATCH 42/42] Warn users to run the installer as sudo --- extras/installer.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/extras/installer.sh b/extras/installer.sh index e71d7d9..6cc6e7a 100755 --- a/extras/installer.sh +++ b/extras/installer.sh @@ -257,6 +257,9 @@ if [ $EUID -ne 0 ]; then echo echo "This script needs to be run with root/sudo, but you are running as '$(whoami)'. Enabling sudo." sudo -v || abort "Root permissions are required for setup, cannot continue" +elif [ ! -z "$SUDO_USER" ]; then + echo + abort "This script will ask for sudo as necessary, but you should not run it as sudo. Please try again." fi for req in wget git; do @@ -299,9 +302,9 @@ fi if [ -d "${FULL_PATH}/.git" ]; then cd "$FULL_PATH" - if git remote -v | grep -q "plexupdate"; then + if git remote -v 2>/dev/null | grep -q "plexupdate"; then echo -n "Found existing plexupdate repository in '$FULL_PATH', updating... " - git pull >/dev/null || abort "Unknown error while updating, please check '$FULL_PATH' and then try again." + git pull &>/dev/null || abort "Unknown error while updating, please check '$FULL_PATH' and then try again." echo else abort "'$FULL_PATH' appears to contain a different git repository, cannot continue"