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

@ -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
echo -n "Authenticating..."
# 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
echo "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 /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
echo -e "OK"
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,33 +380,26 @@ 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
echo "Your OS reports the latest version of Plex ($INSTALLED_VERSION) is already installed. Use -f to force download."
cronexit 5
fi
if [ -f "${DOWNLOADDIR}/${FILENAME}" -a "${FORCE}" != "yes" ]; then
if [ "${CRON}" = "no" ]; then
echo "File already exists, won't download."
fi
echo "File already exists, won't download."
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
echo "Note! File exists, but asked to overwrite with new copy"
fi
echo -ne "Downloading release \"${FILENAME}\"..."
@ -411,7 +407,7 @@ if [ "${SKIP_DOWNLOAD}" == "no" ]; then
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