Cron mode now makes sense

Old -C cronmode is deprecated and will cause an error message
New -c essentially converts any non-fatal error to success

Combined with -q you will only get messages if something goes wrong.

This closes #65
This commit is contained in:
Henric Andersson 2016-07-16 13:45:45 -07:00
commit f3f24fc97e
2 changed files with 64 additions and 58 deletions

View file

@ -19,9 +19,9 @@ plexupdate.sh looks for a file named `.plexupdate` located in your home director
The contents of this file usually is
```
EMAIL=my.email@plex-server.com
EMAIL="my.email@plex-server.com"
PASS="my-secret-plex-password"
DOWNLOADDIR=/a/folder/to/save/the/files/in
DOWNLOADDIR="/a/folder/to/save/the/files/in"
```
Obviously you need to change these three so they match your account information. And if you don't put anything as a `DOWNLOADDIR`, the tool will use the folder you're executing the script from. So take care.
@ -50,6 +50,12 @@ There are a few other options for the more enterprising user. Setting any of the
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
It seems quite popular to run this via crontab, which is fine. But the behavior of the script has been somewhat, shall we say, annoying.
Starting today, the ```-C``` option is deprecated and will give an error to check the docs. The new version is ```-c``` and will make sure that only fatal errors are reported back via the exit code. No more 2, 3, 4 or 5 exitcodes. They are converted into 0. Combining this option with ```-q``` will hide any and all non-essential output from the script as well. Only error messages are emitted, so if it fails, you'll know why.
## 4. command-line
I'm going to be lazy, just run the tool with `-h` and you'll find out what you can do. It will basically be a mirror of what section 3 just stated :-)
@ -70,13 +76,17 @@ Open a terminal or SSH on the server running Plex Media Center
```
wget https://raw.githubusercontent.com/mrworf/plexupdate/master/plexupdate.sh
chmod +x plexupdate.sh
echo -e > ~/.plexupdate 'EMAIL=<plex email account>\nPASS="<plex password>"'
echo -e > ~/.plexupdate 'EMAIL="<plex email account>"\nPASS="<plex password>"'
nano -w ~/.plexupdate
sudo ./plexupdate.sh -a
```
# FAQ
## What username and password are you talking about
The username and password for http://plex.tv
## My password is rejected even though correct
If you use certain characters, such as dollar sign, in your password, bash will interpret that as a reference to a variable. To resolve this, enclose your password with single quotes instead of the normal quotes.

View file

@ -78,7 +78,8 @@ if [ -f ~/.plexupdate ]; then
fi
if [ ! "${RELEASE}" = "" ]; then
echo "ERROR: RELEASE keyword is deprecated, use DISTRO and BUILD"
echo "ERROR: RELEASE keyword is deprecated and should be removed from .plexupdate" >&2
echo " Use DISTRO and BUILD instead to manually select what to install (check README.md)" >&2
exit 255
fi
@ -87,10 +88,18 @@ 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
cronexit() {
# Don't give anything but true error codes if in CRON mode
if [ "${CRON}" = "yes" -a $1 -gt 1 -a $1 -lt 255 ]; then
cronexit 0
fi
exit $1
}
usage() {
echo "Usage: $(basename $0) [-aCfhkopqsSuU]"
echo "Usage: $(basename $0) [-acfhkopqsSuU]"
echo " -a Auto install if download was successful (requires root)"
echo " -C Cron mode. Only output to stdout on an actionable operation"
echo " -c Cron mode, only fatal errors return non-zero cronexit code"
echo " -d Auto delete after auto install"
echo " -f Force download even if it's the same version or file"
echo " already exists (WILL NOT OVERWRITE)"
@ -98,14 +107,14 @@ usage() {
echo " -k Reuse last authentication"
echo " -l List available builds and distros"
echo " -p Public Plex Media Server version"
echo " -q Quiet mode. No stdout, only stderr and exit codes"
echo " -q Quiet mode. No stdout, only stderr and cronexit codes"
echo " -r Print download URL and exit"
echo " -s Auto start (needed for some distros)"
echo " -S Silent mode. No text output, only exit codes"
echo " -u Auto update plexupdate.sh before running it (experimental)"
echo " -U Do not autoupdate plexupdate.sh (experimental, default)"
echo
exit 0
cronexit 0
}
# Parse commandline
@ -116,7 +125,8 @@ do
case "$1" in
(-h) usage;;
(-a) AUTOINSTALL=yes;;
(-C) CRON=yes;;
(-c) CRON=yes;;
(-C) echo "ERROR: CRON option has changed, please review README.md" 1>&2; cronexit 255;;
(-d) AUTODELETE=yes;;
(-f) FORCE=yes;;
(-k) KEEP=yes;;
@ -129,7 +139,7 @@ do
(-u) AUTOUPDATE=yes;;
(-U) AUTOUPDATE=no;;
(--) ;;
(-*) echo "Error: unrecognized option $1" 1>&2; usage; exit 1;;
(-*) echo "Error: unrecognized option $1" 1>&2; usage; cronexit 1;;
(*) break;;
esac
shift
@ -149,23 +159,23 @@ if [ "${AUTOUPDATE}" == "yes" ]; then
git >/dev/null 2>/dev/null
if [ $? -eq 127 ]; then
echo "Error: You need to have git installed for this to work" >&2
exit 1
cronexit 1
fi
pushd "$(dirname "$0")" >/dev/null
if [ ! -d .git ]; then
echo "Error: This is not a git repository, auto update only works if you've done a git clone" >&2
exit 1
cronexit 1
fi
git status | grep "git commit -a" >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
echo "Error: You have made changes to the script, cannot auto update" >&2
exit 1
cronexit 1
fi
echo -n "Auto updating..."
git pull >/dev/null
if [ $? -ne 0 ]; then
echo 'Error: Unable to update git, try running "git pull" manually to see what is wrong' >&2
exit 1
cronexit 1
fi
echo "OK"
popd >/dev/null
@ -174,25 +184,25 @@ if [ "${AUTOUPDATE}" == "yes" ]; then
/bin/bash "$0" ${ALLARGS} -U
else
echo "Error: Unable to relaunch, couldn't find $0" >&2
exit 1
cronexit 1
fi
else
"$0" ${ALLARGS} -U
fi
exit $?
cronexit $?
fi
# Sanity check
if [ "${EMAIL}" == "" -o "${PASS}" == "" ] && [ "${PUBLIC}" == "no" ] && [ ! -f /tmp/kaka ]; then
echo "Error: Need username & password to download PlexPass version. Otherwise run with -p to download public version." >&2
exit 1
cronexit 1
fi
if [ "${AUTOINSTALL}" == "yes" -o "${AUTOSTART}" == "yes" ]; then
id | grep -i 'uid=0(' 2>&1 >/dev/null
if [ $? -ne 0 ]; then
echo "Error: You need to be root to use autoinstall/autostart option." >&2
exit 1
cronexit 1
fi
fi
@ -201,7 +211,7 @@ fi
DOWNLOADDIR="$(eval cd ${DOWNLOADDIR// /\\ } ; if [ $? -eq 0 ]; then pwd; fi)"
if [ -z "${DOWNLOADDIR}" ]; then
echo "Error: Download directory does not exist or is not a directory" >&2
exit 1
cronexit 1
fi
if [ "${DISTRO_INSTALL}" == "" ]; then
@ -219,13 +229,13 @@ if [ "${DISTRO_INSTALL}" == "" ]; then
DISTRO_INSTALL="${DEBIAN_INSTALL}"
fi
elif [ "${DISTRO}" == "" -o "${BUILD}" == "" ]; then
echo "ERROR: You must define both DISTRO and BUILD"
exit 255
echo "ERROR: You must define both DISTRO and BUILD" >&2
cronexit 255
fi
else
if [ "${DISTRO}" == "" -o "${BUILD}" == "" ]; then
echo "Using custom DISTRO_INSTALL requires custom DISTRO and BUILD too"
exit 255
echo "Using custom DISTRO_INSTALL requires custom DISTRO and BUILD too" >&2
cronexit 255
fi
fi
@ -253,7 +263,7 @@ keypair() {
echo "${key}=${val}"
}
# Setup an exit handler so we cleanup
# Setup an cronexit handler so we cleanup
function cleanup {
rm /tmp/postdata 2>/dev/null >/dev/null
rm /tmp/raw 2>/dev/null >/dev/null
@ -276,9 +286,8 @@ trap cleanup EXIT
# If user wants, we skip authentication, but only if previous auth exists
if [ "${KEEP}" != "yes" -o ! -f /tmp/kaka ] && [ "${PUBLIC}" == "no" ]; then
if [ "${CRON}" = "no" ]; then
echo -n "Authenticating..."
fi
# Clean old session
rm /tmp/kaka 2>/dev/null
@ -296,11 +305,11 @@ if [ "${KEEP}" != "yes" -o ! -f /tmp/kaka ] && [ "${PUBLIC}" == "no" ]; then
RESULTCODE=$(head -n1 /tmp/raw | grep -oe '[1-5][0-9][0-9]')
if [ $RESULTCODE -eq 401 ]; then
echo "ERROR: Username and/or password incorrect" >&2
exit 1
cronexit 1
elif [ $RESULTCODE -ne 201 ]; then
echo "ERROR: Failed to login, debug information:" >&2
cat /tmp/failcause >&2
exit 1
cronexit 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
@ -310,9 +319,7 @@ if [ "${KEEP}" != "yes" -o ! -f /tmp/kaka ] && [ "${PUBLIC}" == "no" ]; then
# Remove this, since it contains more information than we should leave hanging around
rm /tmp/failcause
if [ "${CRON}" = "no" ]; then
echo "OK"
fi
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 /tmp/kaka
@ -338,35 +345,31 @@ if [ "${LISTOPTS}" == "yes" ]; then
DISTRO=
fi
done
exit 0
cronexit 0
fi
# Extract the URL for our release
if [ "${CRON}" = "no" ]; then
echo -n "Finding download URL to download..."
fi
# Set "X-Plex-Token" to the auth token, if no token is specified or it is invalid, the list will return public downloads by default
DOWNLOAD=$(wget --header "X-Plex-Token:"${TOKEN}"" --load-cookies /tmp/kaka --save-cookies /tmp/kaka --keep-session-cookies "${URL_DOWNLOAD}" -O - 2>/dev/null | grep -ioe '"label"[^}]*' | grep -i "\"distro\":\"${DISTRO}\"" | grep -i "\"build\":\"${BUILD}\"" | grep -m1 -ioe 'https://[^\"]*' )
if [ "${CRON}" = "no" ]; then
echo -e "OK"
fi
if [ "${DOWNLOAD}" == "" ]; then
echo "ERROR: Unable to retrieve the URL needed for download (Query DISTRO: $DISTRO, BUILD: $BUILD)"
exit 3
echo "ERROR: Unable to retrieve the URL needed for download (Query DISTRO: $DISTRO, BUILD: $BUILD)" >&2
cronexit 3
fi
FILENAME="$(basename 2>/dev/null ${DOWNLOAD})"
if [ $? -ne 0 ]; then
echo "Failed to parse HTML, download cancelled."
exit 3
echo "ERROR: Failed to parse HTML, download cancelled." >&2
cronexit 3
fi
if [ "${PRINT_URL}" == "yes" ]; then
echo "${DOWNLOAD}"
exit 0
cronexit 0
fi
# By default, try downloading
@ -377,41 +380,34 @@ if [ "${REDHAT}" != "yes" ]; then
INSTALLED_VERSION=$(dpkg-query -s plexmediaserver 2>/dev/null | grep -Po 'Version: \K.*')
else
if [ "${AUTOSTART}" == "no" ]; then
echo "Your distribution may require the use of the AUTOSTART [-s] option for the service to start after the upgrade completes."
echo "WARNING: Your distribution may require the use of the AUTOSTART [-s] option for the service to start after the upgrade completes."
fi
INSTALLED_VERSION=$(rpm -qv plexmediaserver 2>/dev/null)
fi
if [[ $FILENAME == *$INSTALLED_VERSION* ]] && [ "${FORCE}" != "yes" ] && [ ! -z "${INSTALLED_VERSION}" ]; then
if [ "${CRON}" = "no" ]; then
echo "Your OS reports the latest version of Plex ($INSTALLED_VERSION) is already installed. Use -f to force download."
exit 5
fi
exit 0
cronexit 5
fi
if [ -f "${DOWNLOADDIR}/${FILENAME}" -a "${FORCE}" != "yes" ]; then
if [ "${CRON}" = "no" ]; then
echo "File already exists, won't download."
fi
if [ "${AUTOINSTALL}" != "yes" ]; then
exit 2
cronexit 2
fi
SKIP_DOWNLOAD="yes"
fi
if [ "${SKIP_DOWNLOAD}" == "no" ]; then
if [ -f "${DOWNLOADDIR}/${FILENAME}" ]; then
if [ "${CRON}" = "no" ]; then
echo "Note! File exists, but asked to overwrite with new copy"
fi
fi
echo -ne "Downloading release \"${FILENAME}\"..."
ERROR=$(wget --load-cookies /tmp/kaka --save-cookies /tmp/kaka --keep-session-cookies "${DOWNLOAD}" -O "${DOWNLOADDIR}/${FILENAME}" 2>&1)
CODE=$?
if [ ${CODE} -ne 0 ]; then
echo -e "\n !! Download failed with code ${CODE}, \"${ERROR}\""
exit ${CODE}
cronexit ${CODE}
fi
echo "OK"
fi
@ -441,4 +437,4 @@ if [ "${AUTOSTART}" == "yes" ]; then
fi
fi
exit 0
cronexit 0