feat(updater): remove loop

This commit is contained in:
agaucher 2021-04-13 13:19:35 +02:00
parent 9846ce0fec
commit 51a0510143

View file

@ -60,7 +60,7 @@ archshort=${arch:0:3}
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case "$1" in case "$1" in
--verbosity|-v=*) --verbosity | -v=*)
shift shift
if [[ ${1#*=} =~ ^-?[0-8]$ ]]; then if [[ ${1#*=} =~ ^-?[0-8]$ ]]; then
verbosity="${1#*=}" verbosity="${1#*=}"
@ -71,7 +71,7 @@ while [ $# -gt 0 ]; do
exit 1 exit 1
fi fi
;; ;;
--force|-f=*) --force | -f=*)
shift shift
if [[ ${1#*=} =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then if [[ ${1#*=} =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
force="${1#*=}" force="${1#*=}"
@ -87,12 +87,13 @@ while [ $# -gt 0 ]; do
printf "* Error: Invalid argument. *\n" printf "* Error: Invalid argument. *\n"
printf "****************************\n" printf "****************************\n"
exit 1 exit 1
;;
esac esac
shift shift
done done
declare -A LOG_LEVELS=([-1]="none" [0]="emerg" [1]="alert" [2]="crit" [3]="err" [4]="warning" [5]="notice" [6]="info" [7]="debug" [8]="trace") declare -A LOG_LEVELS=([-1]="none" [0]="emerg" [1]="alert" [2]="crit" [3]="err" [4]="warning" [5]="notice" [6]="info" [7]="debug" [8]="trace")
function .log () { function .log() {
local LEVEL=${1} local LEVEL=${1}
shift shift
if [[ $verbosity =~ ^-?[0-8]$ ]]; then if [[ $verbosity =~ ^-?[0-8]$ ]]; then
@ -101,7 +102,7 @@ function .log () {
fi fi
fi fi
if [ $verbosity -eq 8 ] || [ $LEVEL -ne 8 ]; then if [ $verbosity -eq 8 ] || [ $LEVEL -ne 8 ]; then
echo "[$(date '+%Y-%m-%d %H:%M:%S %Z' -u)] [${LOG_LEVELS[$LEVEL]}]" "$@" >> $logfile echo "[$(date '+%Y-%m-%d %H:%M:%S %Z' -u)] [${LOG_LEVELS[$LEVEL]}]" "$@" >>$logfile
fi fi
} }
@ -110,7 +111,7 @@ unzip-strip() (
local dest=${2:-.} local dest=${2:-.}
local temp=$(mktemp -d) && tar -zxf "$zip" -C "$temp" && mkdir -p "$dest" && local temp=$(mktemp -d) && tar -zxf "$zip" -C "$temp" && mkdir -p "$dest" &&
shopt -s dotglob && local f=("$temp"/*) && shopt -s dotglob && local f=("$temp"/*) &&
if (( ${#f[@]} == 1 )) && [[ -d "${f[0]}" ]] ; then if ((${#f[@]} == 1)) && [[ -d "${f[0]}" ]]; then
cp -r "$temp"/*/* "$dest" cp -r "$temp"/*/* "$dest"
else else
cp -r "$temp"/* "$dest" cp -r "$temp"/* "$dest"
@ -120,9 +121,9 @@ unzip-strip() (
# Import any custom config to override the defaults, if necessary # Import any custom config to override the defaults, if necessary
configfile="$(dirname $0)/update_ombi.conf" configfile="$(dirname $0)/update_ombi.conf"
if [ -e $configfile ]; then if [ -e $configfile ]; then
source $configfile > /dev/null 2>&1 source $configfile >/dev/null 2>&1
.log 6 "Script config file found...parsing..." .log 6 "Script config file found...parsing..."
if [ $? -ne 0 ] ; then if [ $? -ne 0 ]; then
.log 3 "Unable to use config file...using defaults..." .log 3 "Unable to use config file...using defaults..."
else else
.log 6 "Parsed config file" .log 6 "Parsed config file"
@ -139,24 +140,24 @@ if [ -e $ombiservicefile ]; then
.log 6 "Ombi service file for systemd found...parsing..." .log 6 "Ombi service file for systemd found...parsing..."
parseresults="Parsing complete: " parseresults="Parsing complete: "
ombiservice=$(<$ombiservicefile) ombiservice=$(<$ombiservicefile)
installdir=$(grep -Po '(?<=WorkingDirectory=)(\S|(?<=\\)\s)+' <<< "$ombiservice") installdir=$(grep -Po '(?<=WorkingDirectory=)(\S|(?<=\\)\s)+' <<<"$ombiservice")
if [ -n "${installdir}" ]; then if [ -n "${installdir}" ]; then
parseresults+="InstallDir: $installdir, " parseresults+="InstallDir: $installdir, "
fi fi
user=$(grep -Po '(?<=User=)(\w+)' <<< "$ombiservice") user=$(grep -Po '(?<=User=)(\w+)' <<<"$ombiservice")
if [ -n "${user}" ]; then if [ -n "${user}" ]; then
parseresults+="User: $user, " parseresults+="User: $user, "
fi fi
group=$(grep -Po '(?<=Group=)(\w+)' <<< "$ombiservice") group=$(grep -Po '(?<=Group=)(\w+)' <<<"$ombiservice")
if [ -n "${group}" ]; then if [ -n "${group}" ]; then
parseresults+="Group: $group, " parseresults+="Group: $group, "
fi fi
url=$(grep -Po '(?<=\-\-host )(http://.+)$' <<< "$ombiservice") url=$(grep -Po '(?<=\-\-host )(http://.+)$' <<<"$ombiservice")
ip=$(grep -Po '(?<=http://)((\d{1,3}\.){3}\d{1,3})(?=:)' <<< "$url") ip=$(grep -Po '(?<=http://)((\d{1,3}\.){3}\d{1,3})(?=:)' <<<"$url")
if [ -n "${ip}" ]; then if [ -n "${ip}" ]; then
parseresults+="IP: $ip, " parseresults+="IP: $ip, "
fi fi
port=$(grep -Po '(?<=:)(\d+)$' <<< "$url") port=$(grep -Po '(?<=:)(\d+)$' <<<"$url")
if [ -n "${port}" ]; then if [ -n "${port}" ]; then
parseresults+="Port: $port " parseresults+="Port: $port "
fi fi
@ -189,56 +190,51 @@ fi
.log 6 "Downloading Ombi update..." .log 6 "Downloading Ombi update..."
if [ "$archshort" = 'arm' ]; then if [ "$archshort" = 'arm' ]; then
filename='linux-arm64.tar.gz' filename='linux-arm64.tar.gz'
else else
filename='linux-x64.tar.gz' filename='linux-x64.tar.gz'
fi fi
declare -i i=1 .log 6 "Checking for latest version"
declare -i j=5 if [ -z "$baseurl" ]; then
while [ $i -le $j ]
do
.log 6 "Checking for latest version"
if [ -z "$baseurl" ]; then
json=$(curl -sL http://$ip:$port/$baseurl/api/v1/Update) json=$(curl -sL http://$ip:$port/$baseurl/api/v1/Update)
else else
json=$(curl -sL http://$ip:$port/api/v1/Update) json=$(curl -sL http://$ip:$port/api/v1/Update)
fi fi
.log 8 "json: $json" .log 8 "json: $json"
latestversion=$(grep -Po '(?<="updateVersionString":")([^"]+)' <<< "$json") latestversion=$(grep -Po '(?<="updateVersionString":")([^"]+)' <<<"$json")
.log 7 "latestversion: $latestversion" .log 7 "latestversion: $latestversion"
if [ -n "$force" ]; then if [ -n "$force" ]; then
latestversion=$force latestversion=$force
.log 7 "forcing version: $latestversion" .log 7 "forcing version: $latestversion"
fi fi
#json=$(curl -sL https://github.com/Ombi-app/Ombi.Releases/releases/tag/$latestversion) #json=$(curl -sL https://github.com/Ombi-app/Ombi.Releases/releases/tag/$latestversion)
#.log 8 "json: $json" #.log 8 "json: $json"
#jobId=$(grep -Po '(?<="jobId":")([^"]+)' <<< "$json") #jobId=$(grep -Po '(?<="jobId":")([^"]+)' <<< "$json")
#.log 7 "jobId: $jobId" #.log 7 "jobId: $jobId"
#version=$(grep -Po '(?<="version":")([^"]+)' <<< "$json") #version=$(grep -Po '(?<="version":")([^"]+)' <<< "$json")
#.log 7 "version: $version" #.log 7 "version: $version"
#if [ $latestversion != $version ]; then #if [ $latestversion != $version ]; then
# .log 2 "Build version does not match expected version" # .log 2 "Build version does not match expected version"
# exit 1 # exit 1
#fi #fi
#.log 6 "Latest version: $version...determining expected file size..." #.log 6 "Latest version: $version...determining expected file size..."
#size=$(curl -sL https://ci.appveyor.com/api/buildjobs/$jobId/artifacts | grep -Po '(?<="'$filename'","type":"File","size":)(\d+)') #size=$(curl -sL https://ci.appveyor.com/api/buildjobs/$jobId/artifacts | grep -Po '(?<="'$filename'","type":"File","size":)(\d+)')
#.log 7 "size: $size" #.log 7 "size: $size"
#if [ -e $size ]; then #if [ -e $size ]; then
# if [ $i -lt $j ]; then # if [ $i -lt $j ]; then
# .log 3 "Unable to determine update file size...[attempt $i of $j]" # .log 3 "Unable to determine update file size...[attempt $i of $j]"
# else # else
# .log 2 "Unable to determine update file size...[attempt $i of $j]...Bailing!" # .log 2 "Unable to determine update file size...[attempt $i of $j]...Bailing!"
# exit 2 # exit 2
# fi # fi
# i+=1 # i+=1
# continue # continue
#elif [[ $size =~ ^-?[0-9]+$ ]]; then #elif [[ $size =~ ^-?[0-9]+$ ]]; then
# .log 6 "Expected file size: $size...downloading..." # .log 6 "Expected file size: $size...downloading..."
# break # break
#else #else
# .log 1 "Invalid file size value...bailing!" # .log 1 "Invalid file size value...bailing!"
# exit 99 # exit 99
#fi #fi
done
tempdir=$(mktemp -d) tempdir=$(mktemp -d)
file="$tempdir/ombi_$version.tar.gz" file="$tempdir/ombi_$version.tar.gz"
wget --quiet --show-progress -O $file "https://github.com/Ombi-app/Ombi/releases/download/$latestversion/$filename" wget --quiet --show-progress -O $file "https://github.com/Ombi-app/Ombi/releases/download/$latestversion/$filename"
@ -250,19 +246,18 @@ wget --quiet --show-progress -O $file "https://github.com/Ombi-app/Ombi/releases
.log 6 "Checking Ombi service status..." .log 6 "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
.log 6 "Ombi is active...attempting to stop..." .log 6 "Ombi is active...attempting to stop..."
declare -i i=1 declare -i i=1
declare -i j=5 declare -i j=5
while [ $i -le $j ] while [ $i -le $j ]; do
do
if [ $scriptuser = "root" ]; then if [ $scriptuser = "root" ]; then
systemctl stop $ombiservicename.service > /dev/null 2>&1 systemctl stop $ombiservicename.service >/dev/null 2>&1
else else
sudo systemctl stop $ombiservicename.service > /dev/null 2>&1 sudo systemctl stop $ombiservicename.service >/dev/null 2>&1
fi 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
.log 3 "Failed to stop Ombi...[attempt $i of $j]" .log 3 "Failed to stop Ombi...[attempt $i of $j]"
sleep 1 sleep 1
@ -272,7 +267,7 @@ if [ "`systemctl is-active $ombiservicename`" == "active" ]; then
fi fi
i+=1 i+=1
continue continue
elif [ "`systemctl is-active $ombiservicename`" == "inactive" ]; then elif [ "$(systemctl is-active $ombiservicename)" == "inactive" ]; then
.log 6 "Ombi stopped...installing update..." .log 6 "Ombi stopped...installing update..."
break break
else else
@ -296,14 +291,13 @@ if [ $running -eq 1 ]; then
.log 6 "Ownership set...starting Ombi..." .log 6 "Ownership set...starting Ombi..."
declare -i i=1 declare -i i=1
declare -i j=5 declare -i j=5
while [ $i -le $j ] while [ $i -le $j ]; do
do
if [ $scriptuser = "root" ]; then if [ $scriptuser = "root" ]; then
systemctl start $ombiservicename.service > /dev/null 2>&1 systemctl start $ombiservicename.service >/dev/null 2>&1
else else
sudo systemctl start $ombiservicename.service > /dev/null 2>&1 sudo systemctl start $ombiservicename.service >/dev/null 2>&1
fi 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
.log 3 "Failed to start Ombi...[attempt $i of $j]" .log 3 "Failed to start Ombi...[attempt $i of $j]"
sleep 1 sleep 1
@ -313,14 +307,13 @@ if [ $running -eq 1 ]; then
fi fi
i+=1 i+=1
continue continue
elif [ "`systemctl is-active $ombiservicename`" == "active" ]; then elif [ "$(systemctl is-active $ombiservicename)" == "active" ]; then
.log 6 "Ombi started...waiting for confirmation..." .log 6 "Ombi started...waiting for confirmation..."
declare -i k=1 declare -i k=1
declare -i l=5 declare -i l=5
while [ $k -le $l ] while [ $k -le $l ]; do
do
sleep 5 sleep 5
curl -sIL $ip:$port > /dev/null 2>&1 curl -sIL $ip:$port >/dev/null 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
if [ $k -lt $l ]; then if [ $k -lt $l ]; then
.log 4 "Ombi startup not confirmed...waiting 5 seconds...[attempt $k of $l]" .log 4 "Ombi startup not confirmed...waiting 5 seconds...[attempt $k of $l]"