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
This commit is contained in:
Alex Malinovich 2016-11-20 19:48:25 -08:00 committed by Henric Andersson
commit 8c0e2860ea

View file

@ -1,59 +1,99 @@
#!/bin/bash #!/bin/bash
ORIGIN_REPO="https://github.com/mrworf/plexupdate" ORIGIN_REPO="https://github.com/demonbane/plexupdate" #FIXME
OPT_PATH="/opt" OPT_PATH="/opt"
FULL_PATH="$OPT_PATH/plexupdate" FULL_PATH="$OPT_PATH/plexupdate"
CONFIGFILE="/etc/plexupdate.conf" CONFIGFILE="/etc/plexupdate.conf"
CONFIGCRON="/etc/plexupdate.cron.conf" CONFIGCRON="/etc/plexupdate.cron.conf"
CRONWRAPPER="/etc/cron.daily/plexupdate" CRONWRAPPER="/etc/cron.daily/plexupdate"
# default options
AUTOINSTALL=yes
AUTOUPDATE=yes
PUBLIC=
install() { install() {
echo "'$req' is required but not installed, attempting to install..." echo "'$req' is required but not installed, attempting to install..."
sleep 1 sleep 1
if $UBUNTU; then [ -z "$DISTRO_INSTALL" ] && check_distro
DISTRO_INSTALL="apt install $1" echo "DISTRO_INSTALL='$DISTRO_INSTALL'"
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 if [ $EUID != 0 ]; then
sudo $DISTRO_INSTALL sudo $DISTRO_INSTALL $1
else else
$DISTRO_INSTALL $DISTRO_INSTALL $1
fi fi
} }
yesno() { check_distro() {
while true; do if [ -f /etc/redhat-release ] && hash dnf 2>/dev/null; then
read -n 1 -p "[Y/n] " answer 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 yesno() {
echo case "$1" in
return 1 "")
elif [ -z "$answer" -o "$answer" == "y" -o "$answer" == "Y" ]; then 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 echo
return 0 return 0
elif [ "$answer" == "N" ]; then
echo
return 1
fi fi
done done
} }
noyes() { noyes() {
while true; do yesno N
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
} }
abort() { abort() {
@ -64,13 +104,16 @@ abort() {
configure_plexupdate() { configure_plexupdate() {
CONFIGTEMP=$(mktemp /tmp/plexupdate.tempconf.XXX) CONFIGTEMP=$(mktemp /tmp/plexupdate.tempconf.XXX)
AUTOUPDATE=yes
[ -f "$CONFIGFILE" ] && source "$CONFIGFILE" [ -f "$CONFIGFILE" ] && source "$CONFIGFILE"
echo echo
echo -n "Do you want to install the latest PlexPass releases? " 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= PUBLIC=
while true; do while true; do
read -e -p "PlexPass Email Address: " -i "$EMAIL" EMAIL read -e -p "PlexPass Email Address: " -i "$EMAIL" EMAIL
@ -89,6 +132,7 @@ configure_plexupdate() {
fi fi
done done
else else
# don't forget to erase old settings if they changed their answer
EMAIL= EMAIL=
PASS= PASS=
PUBLIC=yes PUBLIC=yes
@ -96,15 +140,20 @@ configure_plexupdate() {
echo echo
echo -n "Would you like to automatically install the latest release when it is downloaded? " echo -n "Would you like to automatically install the latest release when it is downloaded? "
if yesno; then
if yesno "$AUTOINSTALL"; then
AUTOINSTALL=yes AUTOINSTALL=yes
[ -z "$DISTRO" ] && check_distro
if [ "$DISTRO" == "redhat" ]; then
AUTOSTART=yes
else else
AUTOINSTALL=no AUTOSTART=
fi fi
if [ "$AUTOINSTALL" == "yes" ]; then
echo echo
echo -n "When using the auto-install option, would you like to check if the server is in use before upgrading? " 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 yesno; then
if [ -z "$PLEXSERVER" ]; then if [ -z "$PLEXSERVER" ]; then
PLEXSERVER="127.0.0.1" PLEXSERVER="127.0.0.1"
@ -113,7 +162,7 @@ configure_plexupdate() {
read -e -p "Plex Server IP/DNS name: " -i "$PLEXSERVER" PLEXSERVER read -e -p "Plex Server IP/DNS name: " -i "$PLEXSERVER" PLEXSERVER
if ! ping -c 1 -w 1 "$PLEXSERVER" &>/dev/null ; then if ! ping -c 1 -w 1 "$PLEXSERVER" &>/dev/null ; then
echo -n "Server $PLEXSERVER isn't responding, are you sure you entered it correctly? " echo -n "Server $PLEXSERVER isn't responding, are you sure you entered it correctly? "
if ! noyes; then if yesno N; then
break break
fi fi
else else
@ -137,24 +186,32 @@ configure_plexupdate() {
PLEXPORT= PLEXPORT=
fi fi
else else
AUTOINSTALL=no
PLEXSERVER= PLEXSERVER=
PLEXPORT= PLEXPORT=
fi fi
save_config "AUTOUPDATE EMAIL PASS PUBLIC AUTOINSTALL PLEXSERVER PLEXPORT" "$CONFIGFILE" save_config "AUTOUPDATE EMAIL PASS PUBLIC AUTOINSTALL AUTOSTART PLEXSERVER PLEXPORT" "$CONFIGFILE"
} }
configure_cron() { 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
echo -n "Would you like to set up automatic daily updates for Plex? " echo -n "Would you like to set up automatic daily updates for Plex? "
if yesno; then if yesno; then
CONF="$CONFIGFILE" CONF="$CONFIGFILE"
SCRIPT="${FULL_PATH}/plexupdate.sh" SCRIPT="${FULL_PATH}/plexupdate.sh"
LOGGING=false LOGGING=${LOGGING:-false}
echo echo
echo -n "Do you want to log the daily update runs to syslog so you can examine the output later? " 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 LOGGING=true
fi fi
@ -205,13 +262,7 @@ save_config() {
if [ $EUID -ne 0 ]; then if [ $EUID -ne 0 ]; then
echo echo
echo "This script needs to be run with root/sudo, but you are running as '$(whoami)'. Enabling sudo." echo "This script needs to be run with root/sudo, but you are running as '$(whoami)'. Enabling sudo."
sudo -v sudo -v || abort "Root permissions are required for setup, cannot continue"
fi
if [ -f /etc/redhat-release ]; then
REDHAT=true
else
UBUNTU=true
fi fi
for req in wget git; do for req in wget git; do
@ -238,7 +289,7 @@ fi
if [ -d "${FULL_PATH}/.git" ]; then if [ -d "${FULL_PATH}/.git" ]; then
cd "$FULL_PATH" 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... " 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."
else else
@ -257,8 +308,14 @@ else
fi fi
configure_plexupdate configure_plexupdate
if [ -d "$(dirname "$CRONWRAPPER")" ]; then configure_cron
configure_cron
else echo
echo "Seems like you don't have a supported cron job setup, please see README.md for more details." 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 fi