Logging/log levels, fix build selection, sudo fix

Added logging to file.
Added log levels.
Added command line parameter (-v) to set cli log verbosity.
Reworked update download section to account for if latest build is in
progress or failed.
Allowed for script to run as user with restricted sudo access to
stop/start ombi service
This commit is contained in:
Avi 2017-10-28 18:38:19 -05:00
parent 3745d1f0ad
commit 0dbbd960e5

View file

@ -6,35 +6,43 @@ ombiservicename="ombi"
## Default variables ## ## Default variables ##
## Change only if needed ## ## Change only if needed ##
logfile="/var/log/ombiupdater.log"
ombiservicefile="/etc/systemd/system/$ombiservicename.service" ombiservicefile="/etc/systemd/system/$ombiservicename.service"
defaultinstalldir="/opt/Ombi" defaultinstalldir="/opt/Ombi"
defaultuser="ombi" defaultuser="ombi"
defaultgroup="nogroup" defaultgroup="nogroup"
declare -i verbosity=-1
## Do not modify anything below this line ## ## Do not modify anything below this line ##
## unless you know what you are doing ## ## unless you know what you are doing ##
if [ -e $ombiservicefile ]; then while [ $# -gt 0 ]; do
echo "Ombi service file for systemd found...parsing..." case "$1" in
ombiservice=$(<$ombiservicefile) --verbosity|-v=*)
installdir=$(grep -Po '(?<=WorkingDirectory=)(\S|(?<=\\)\s)+' <<< "$ombiservice") verbosity="${1#*=}"
user=$(grep -Po '(?<=User=)(\w+)' <<< "$ombiservice") ;;
group=$(grep -Po '(?<=Group=)(\w+)' <<< "$ombiservice") *)
echo "Parsing complete: InstallDir: $installdir, User: $user, Group: $group" printf "***************************\n"
fi printf "* Error: Invalid argument.*\n"
printf "***************************\n"
exit 1
esac
shift
done
if [ -z ${installdir+x} ]; then declare -A LOG_LEVELS=([-1]="none" [0]="emerg" [1]="alert" [2]="crit" [3]="err" [4]="warning" [5]="notice" [6]="info" [7]="debug" [8]="trace")
echo "InstallDir not parsed...setting to default: $defaultinstalldir" function .log () {
installdir="$defaultinstalldir" local LEVEL=${1}
fi shift
if [ -z ${user+x} ]; then if [[ $verbosity =~ ^-?[0-8]$ ]]; then
echo "User not parsed...setting to default: $defaultuser" if [ $verbosity -ge $LEVEL ]; then
user="$defaultuser" echo "[${LOG_LEVELS[$LEVEL]}]" "$@"
fi fi
if [ -z ${group+x} ]; then fi
echo "Group not parsed...setting to default: $defaultgroup" if [ $verbosity -eq 8 ] || [ $LEVEL -ne 8 ]; then
group="$defaultgroup" echo "[${LOG_LEVELS[$LEVEL]}]" "$@" >> $logfile
fi fi
}
unzip-strip() ( unzip-strip() (
local zip=$1 local zip=$1
@ -48,82 +56,151 @@ unzip-strip() (
fi && rm -rf "$temp"/* "$temp" fi && rm -rf "$temp"/* "$temp"
) )
echo "Downloading Ombi update..." .log 6 "Verboity level: [${LOG_LEVELS[$verbosity]}]"
json=$(curl -sL https://ci.appveyor.com/api/projects/tidusjar/requestplex) scriptuser=$(whoami)
jobId=$(grep -Po '(?<="jobId":")([^"]+)' <<< "$json") .log 7 "Update script running as: $scriptuser"
version=$(grep -Po '(?<="version":")([^"]+)' <<< "$json") if [ -e $ombiservicefile ]; then
file=ombi_$version.tar.gz .log 6 "Ombi service file for systemd found...parsing..."
size=$(curl -sL https://ci.appveyor.com/api/buildjobs/$jobId/artifacts | grep -Po '(?<="linux.tar.gz","type":"File","size":)(\d+)') ombiservice=$(<$ombiservicefile)
wget --quiet -O $file https://ci.appveyor.com/api/buildjobs/$jobId/artifacts/linux.tar.gz installdir=$(grep -Po '(?<=WorkingDirectory=)(\S|(?<=\\)\s)+' <<< "$ombiservice")
echo "Version $version downloaded...checking file size..." user=$(grep -Po '(?<=User=)(\w+)' <<< "$ombiservice")
if [ $(wc -c < $file) != $size ]; then group=$(grep -Po '(?<=Group=)(\w+)' <<< "$ombiservice")
echo "Downloaded file size does not match expected file size...bailing!" .log 6 "Parsing complete: InstallDir: $installdir, User: $user, Group: $group"
exit 1
fi fi
echo "File size validated...checking Ombi service status..."
if [ -z ${installdir+x} ]; then
.log 5 "InstallDir not parsed...setting to default: $defaultinstalldir"
installdir="$defaultinstalldir"
fi
if [ -z ${user+x} ]; then
.log 5 "User not parsed...setting to default: $defaultuser"
user="$defaultuser"
fi
if [ -z ${group+x} ]; then
.log 5 "Group not parsed...setting to default: $defaultgroup"
group="$defaultgroup"
fi
.log 6 "Downloading Ombi update..."
declare -i i=1
declare -i j=5
while [ $i -le $j ]
do
.log 6 "Checking for latest version"
json=$(curl -sL https://ombiservice.azurewebsites.net/api/update/DotNetCore)
.log 8 "json: $json"
latestversion=$(grep -Po '(?<="updateVersionString":")([^"]+)' <<< "$json")
.log 7 "latestversion: $latestversion"
json=$(curl -sL https://ci.appveyor.com/api/projects/tidusjar/requestplex/build/$latestversion)
.log 8 "json: $json"
jobId=$(grep -Po '(?<="jobId":")([^"]+)' <<< "$json")
.log 7 "jobId: $jobId"
version=$(grep -Po '(?<="version":")([^"]+)' <<< "$json")
.log 7 "version: $version"
if [ $latestversion != $version ]; then
.log 2 "Build version does not match expected version"
exit 1
fi
.log 6 "Latest version: $version...determining expected file size..."
size=$(curl -sL https://ci.appveyor.com/api/buildjobs/$jobId/artifacts | grep -Po '(?<="linux.tar.gz","type":"File","size":)(\d+)')
.log 7 "size: $size"
if [ -e $size ]; then
if [ $i -lt $j ]; then
.log 3 "Unable to determine update file size...[attempt $i of $j]"
else
.log 2 "Unable to determine update file size...[attempt $i of $j]...Bailing!"
exit 2
fi
i+=1
continue
elif [[ $size =~ ^-?[0-9]+$ ]]; then
.log 6 "Expected file size: $size...downloading..."
break
else
.log 1 "Invalid file size value...bailing!"
exit 99
fi
done
tempdir=$(mktemp -d)
file="$tempdir/ombi_$version.tar.gz"
wget --quiet -O $file "https://ci.appveyor.com/api/buildjobs/$jobId/artifacts/linux.tar.gz"
.log 6 "Version $version downloaded...checking file size..."
if [ $(wc -c < $file) != $size ]; then
.log 3 "Downloaded file size does not match expected file size...bailing!"
exit 2
fi
.log 6 "File size validated...checking Ombi service status..."
declare -i running=0 declare -i running=0
if [ "`systemctl is-active $ombiservicename`" == "active" ]; then if [ "`systemctl is-active $ombiservicename`" == "active" ]; then
running=1 running=1
echo "Ombi is active...attempting to stop..." .log 6 "Ombi is active...attempting to stop..."
declare -i i=1 declare -i i=1
j=5 j=5
while [ $i -le $j ] while [ $i -le $j ]
do do
systemctl stop $ombiservicename.service > /dev/null 2>&1 if [ $scriptuser = "root" ]; then
systemctl stop $ombiservicename.service > /dev/null 2>&1
else
sudo systemctl stop $ombiservicename.service > /dev/null 2>&1
fi
if [ $? -ne 0 ] || [ "`systemctl is-active $ombiservicename`" == "active" ] ; then if [ $? -ne 0 ] || [ "`systemctl is-active $ombiservicename`" == "active" ] ; then
if [ $i -lt $j ]; then if [ $i -lt $j ]; then
echo "Failed to stop Ombi...[attempt $i of $j]" .log 3 "Failed to stop Ombi...[attempt $i of $j]"
else else
echo "Failed to stop Ombi...[attempt $i of $j]...Bailing!" .log 2 "Failed to stop Ombi...[attempt $i of $j]...Bailing!"
exit 2 exit 2
fi fi
i+=1 i+=1
continue continue
elif [ "`systemctl is-active $ombiservicename`" == "inactive" ]; then elif [ "`systemctl is-active $ombiservicename`" == "inactive" ]; then
echo "Ombi stopped...installing update..." .log 6 "Ombi stopped...installing update..."
break break
else else
echo "Unknown error...bailing!" .log 1 "Unknown error...bailing!"
exit 99 exit 99
fi fi
done done
else else
echo "Ombi is not active...installing update..." .log 6 "Ombi is not active...installing update..."
fi fi
unzip-strip $file $installdir unzip-strip $file $installdir
echo "Update installed...setting ownership..." .log 6 "Update installed...setting ownership..."
chown -R $user:$group $installdir chown -R $user:$group $installdir
if [ $running -eq 1 ]; then if [ $running -eq 1 ]; then
echo "Ownership set...starting Ombi..." .log 6 "Ownership set...starting Ombi..."
declare -i i=1 declare -i i=1
j=5 j=5
while [ $i -le $j ] while [ $i -le $j ]
do do
systemctl start $ombiservicename.service > /dev/null 2>&1 if [ $scriptuser = "root" ]; then
systemctl systemctl start $ombiservicename.service > /dev/null 2>&1
else
sudo systemctl start $ombiservicename.service > /dev/null 2>&1
fi
if [ $? -ne 0 ] || [ "`systemctl is-active $ombiservicename`" != "active" ] ; then if [ $? -ne 0 ] || [ "`systemctl is-active $ombiservicename`" != "active" ] ; then
if [ $i -lt $j ]; then if [ $i -lt $j ]; then
echo "Failed to start Ombi...[attempt $i of $j]" .log 3 "Failed to start Ombi...[attempt $i of $j]"
else else
echo "Failed to start Ombi...[attempt $i of $j]...Bailing!" .log 2 "Failed to start Ombi...[attempt $i of $j]...Bailing!"
exit 3 exit 3
fi fi
i+=1 i+=1
continue continue
elif [ "`systemctl is-active $ombiservicename`" == "active" ]; then elif [ "`systemctl is-active $ombiservicename`" == "active" ]; then
echo "Ombi started...cleaning up..." .log 6 "Ombi started...cleaning up..."
break break
else else
echo "Unknown error...bailing!" .log 1 "Unknown error...bailing!"
exit 99 exit 99
fi fi
done done
else else
echo "Ownership set...not starting Ombi" .log 6 "Ownership set...not starting Ombi"
fi fi
echo "Cleaning up..." .log 6 "Cleaning up..."
rm -f $file rm -rf "$tempdir"/* "$tempdir"
echo "Update complete" .log 6 "Update complete"