mirror of
https://github.com/mrworf/plexupdate.git
synced 2025-08-21 05:43:20 -07:00
Installer script and cleanup
* Stop users from saving config with invalid download directory * Added additional sanity for email checking * Add installer script * Update cronwrapper to work with installer script * Add AUTOUPDATE to default config * Read /etc/plexupdate.conf before other configs, if available * Use dnf on newer redhat systems * Only use plexupdate.conf or --config * Give existing users explaination as to why plexupdate.sh failed * Removed references to .plexupdate * Warn users that .plexupdate is going away * Offer to import settings in ~/.plexupdate.conf in installer (if they exist) * Only show AUTOSTART warning if AUTOINSTALL is enabled * Change default download directory to /tmp * Warn users to run the installer as sudo * Remove VAR_CL logic in favor of checking config before getopt run * Use sha1sum for version check and auto-fix git permissions on exit * Skip update check if AUTOUPDATE is enabled * Fix permissions for all files, not just .git * Don't enable -P in installer if we know it won't work
This commit is contained in:
parent
760ad2b564
commit
88a759a79c
4 changed files with 614 additions and 275 deletions
346
extras/installer.sh
Executable file
346
extras/installer.sh
Executable file
|
@ -0,0 +1,346 @@
|
|||
#!/bin/bash
|
||||
|
||||
ORIGIN_REPO="https://github.com/mrworf/plexupdate"
|
||||
FULL_PATH="/opt/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
|
||||
|
||||
[ -z "$DISTRO_INSTALL" ] && check_distro
|
||||
|
||||
if [ $EUID -ne 0 ]; then
|
||||
sudo $DISTRO_INSTALL $1 || abort "Failed while trying to install '$1'. Please install it manually and try again."
|
||||
fi
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
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() {
|
||||
yesno N
|
||||
}
|
||||
|
||||
abort() {
|
||||
echo "$@"
|
||||
exit 1
|
||||
}
|
||||
|
||||
install_plexupdate() {
|
||||
echo
|
||||
read -e -p "Directory to install into: " -i "/opt/plexupdate" FULL_PATH
|
||||
|
||||
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
|
||||
sudo mkdir -p "$FULL_PATH" || abort "failed, cannot continue"
|
||||
sudo chown $(id -un):$(id -gn) "$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... "
|
||||
sudo chown $(id -un):$(id -gn) "$FULL_PATH" || abort "failed, cannot continue"
|
||||
echo "done"
|
||||
fi
|
||||
|
||||
if [ -d "${FULL_PATH}/.git" ]; then
|
||||
cd "$FULL_PATH"
|
||||
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."
|
||||
else
|
||||
abort "'$FULL_PATH' appears to contain a different git repository, cannot continue"
|
||||
fi
|
||||
echo "done"
|
||||
cd - &> /dev/null
|
||||
else
|
||||
echo -n "Installing plexupdate into '$FULL_PATH'... "
|
||||
git clone "$ORIGIN_REPO" "$FULL_PATH" &> /dev/null || abort "install failed, cannot continue"
|
||||
echo "done"
|
||||
fi
|
||||
}
|
||||
|
||||
configure_plexupdate() {
|
||||
|
||||
[ -f "$CONFIGFILE" ] && source "$CONFIGFILE"
|
||||
|
||||
echo
|
||||
echo -n "Do you want to install the latest PlexPass releases? (requires PlexPass username and password) "
|
||||
# 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=no
|
||||
while true; do
|
||||
read -e -p "PlexPass Email Address: " -i "$EMAIL" EMAIL
|
||||
if [ -z "${EMAIL}" ] || [[ "$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
|
||||
# don't forget to erase old settings if they changed their answer
|
||||
EMAIL=
|
||||
PASS=
|
||||
PUBLIC=yes
|
||||
fi
|
||||
|
||||
echo
|
||||
echo -n "Would you like to automatically install the latest release when it is downloaded? "
|
||||
|
||||
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"
|
||||
fi
|
||||
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 yesno N; then
|
||||
break
|
||||
fi
|
||||
else
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ -z "$PLEXPORT" ]; then
|
||||
PLEXPORT=32400
|
||||
fi
|
||||
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=
|
||||
fi
|
||||
else
|
||||
AUTOINSTALL=no
|
||||
PLEXSERVER=
|
||||
PLEXPORT=
|
||||
fi
|
||||
|
||||
save_config "AUTOINSTALL AUTODELETE DOWNLOADDIR EMAIL PASS FORCE FORCEALL PUBLIC AUTOSTART AUTOUPDATE PLEXSERVER PLEXPORT CHECKUPDATE" "$CONFIGFILE"
|
||||
}
|
||||
|
||||
configure_cron() {
|
||||
if [ ! -d "$(dirname "$CRONWRAPPER")" ]; then
|
||||
echo "It 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 $CRON; then
|
||||
if [ $(sudo find -L "${FULL_PATH}" -perm /002 -or -not -uid 0 -or -not -gid 0 | wc -l) -ne 0 ]; then
|
||||
echo
|
||||
echo "WARNING: For security reasons, plexupdate needs to be installed as root in order to run automatically. In order to finish setting up automatic updates, we will change the ownership of '${FULL_PATH}' to root:root."
|
||||
echo -n "Do you wish to continue? "
|
||||
yesno || return 1
|
||||
echo
|
||||
echo -n "Changing ownership of '${FULL_PATH}'... "
|
||||
sudo chown -R root:root "${FULL_PATH}" || abort "Unable to change ownership, cannot continue"
|
||||
sudo chmod -R o-w "${FULL_PATH}" || abort "Unable to change permissions, cannot continue"
|
||||
echo "done"
|
||||
fi
|
||||
|
||||
CONF="$CONFIGFILE"
|
||||
SCRIPT="${FULL_PATH}/plexupdate.sh"
|
||||
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 $LOGGING; then
|
||||
LOGGING=true
|
||||
fi
|
||||
|
||||
save_config "CONF SCRIPT LOGGING" "/etc/plexupdate.cron.conf"
|
||||
|
||||
echo
|
||||
echo -n "Installing daily cron job... "
|
||||
sudo ln -sf "${FULL_PATH}/extras/cronwrapper" "$CRONWRAPPER"
|
||||
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
|
||||
}
|
||||
|
||||
save_config() {
|
||||
CONFIGTEMP=$(mktemp /tmp/plexupdate.XXX)
|
||||
for VAR in $1; do
|
||||
if [ ! -z "${!VAR}" ]; then
|
||||
echo "${VAR}='${!VAR}'" >> $CONFIGTEMP
|
||||
fi
|
||||
done
|
||||
|
||||
echo
|
||||
echo -n "Writing configuration file '$2'... "
|
||||
|
||||
# make sure that new file is owned by root instead of owner of CONFIGTEMP
|
||||
sudo tee "$2" > /dev/null < "$CONFIGTEMP"
|
||||
rm "$CONFIGTEMP"
|
||||
|
||||
echo "done"
|
||||
}
|
||||
|
||||
if [ $EUID -ne 0 ]; then
|
||||
echo
|
||||
echo "This script needs to install files in system locations and will ask for sudo/root permissions now"
|
||||
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
|
||||
if ! hash $req 2>/dev/null; then
|
||||
install $req
|
||||
fi
|
||||
done
|
||||
|
||||
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
|
||||
|
||||
if [ -f "$(dirname "$0")/../plexupdate.sh" -a -d "$(dirname "$0")/../.git" ]; then
|
||||
FULL_PATH="$(readlink -f "$(dirname "$0")/../")"
|
||||
echo
|
||||
echo "Found plexupdate.sh in '$FULL_PATH', using that as your install path"
|
||||
else
|
||||
install_plexupdate
|
||||
fi
|
||||
|
||||
|
||||
|
||||
configure_plexupdate
|
||||
configure_cron
|
||||
|
||||
echo
|
||||
echo -n "Configuration complete. Would you like to run plexupdate with these settings now? "
|
||||
if yesno; then
|
||||
if wget --show-progress -V &> /dev/null; then
|
||||
PROGRESS_OPT="-P"
|
||||
fi
|
||||
if [ "$AUTOINSTALL" == "yes" ]; then
|
||||
sudo "$FULL_PATH/plexupdate.sh" $PROGRESS_OPT --config "$CONFIGFILE"
|
||||
else
|
||||
"$FULL_PATH/plexupdate.sh" $PROGRESS_OPT --config "$CONFIGFILE"
|
||||
fi
|
||||
fi
|
Loading…
Add table
Add a link
Reference in a new issue