scripts/ab-openldap/ab-openldap-update.sh
2020-09-14 23:53:46 -06:00

161 lines
5.0 KiB
Bash
Executable File

#!/bin/sh
#
# update script for ab-openldap container and utility scripts
# version 1.0.0
# script by Asif Bacchus
# usage of this script is subject to the license terms found at:
# https://git.asifbacchus.app/ab-docker/scripts/LICENSE
#
### functions
consoleError() {
printf "\n%s%s%s\n\n" "$err" "$2" "$norm"
exit "$1"
}
### text formatting presets
if command -v tput > /dev/null; then
cyan=$(tput setaf 6)
err=$(tput bold)$(tput setaf 1)
norm=$(tput sgr0)
ok=$(tput setaf 2)
yellow=$(tput setaf 11)
else
cyan=''
err=''
norm=''
ok=''
yellow=''
fi
### pre-requisites
# check if wget is installed
if ! command -v wget > /dev/null 2>&1; then
consoleError '1' "Sorry, this script requires that 'wget' is installed in order to automatically update files."
fi
# is user root or in the docker group?
if [ ! "$( id -u )" -eq 0 ]; then
if ! id -Gn | grep docker > /dev/null; then
consoleError '1' "You must either be root or in the 'docker' group to pull container updates."
fi
fi
# zero counters
updatesAvailable=0
downloadFailed=0
downloadSuccess=0
updateFailed=0
updateSuccess=0
# reference constants
containerName='ab-openldap'
containerUpdatePath="docker.asifbacchus.app/ldap/$containerName:latest"
serverPath="https://asifbacchus.app/public/$containerName/"
checksumFilename='checksums.sha256'
# files to update
scriptName="$containerName-update.sh"
updateFiles="$containerName-backup.params.template $containerName-backup.sh $containerName.params.template $containerName.sh"
printf "\n%sUpdating %s%s%s:%s\n" "$cyan" "$yellow" "$containerName" "$cyan" "$norm"
### update container
printf "updating container... "
if ! docker pull "$containerUpdatePath" > /dev/null 2>&1; then
printf "%s[ERROR]\n\n" "$err"
printf "There was a problem updating the container. Please try again later.%s\n\n" "$norm"
exit 1
else
printf "%s[OK]%s\n" "$ok" "$norm"
fi
### checksums
printf "downloading latest checksums... "
if ! wget --quiet --tries=3 --timeout=10 -O "$checksumFilename" "$serverPath$checksumFilename"; then
printf "%s[ERROR]\n\n" "$err"
printf "Unable to download updated checksums. Please try again later.%s\n\n" "$norm"
exit 1
else
printf "%s[OK]%s\n" "$ok" "$norm"
fi
### script self-update
printf "checking for updates to this script... "
localScriptChecksum=$( sha256 "./$scriptName" | grep -o '^\S*' )
repoScriptChecksum=$( grep "$scriptName" "$checksumFilename" | grep -o '^\S*' )
if [ "$localScriptChecksum" = "$repoScriptChecksum" ]; then
printf "[NONE]\n"
else
# download updated script
if ! wget --quiet --tries=3 --timeout=10 -O "$scriptName" "$serverPath$scriptName"; then
printf "%s[ERROR]\n\n" "$err"
printf "Unable to download script update. Please try again later.%s\n\n" "$norm"
exit 1
else
# verify download
localScriptChecksum=$( sha256sum "$scriptName" | grep -o '^\S*' )
if ! [ "$localScriptChecksum" = "$repoScriptChecksum" ]; then
printf "%s[ERROR]\n\n" "$err"
printf "Unable to verify checksum of updated script. Please try again later.%s\n\n" "$norm"
else
printf "%s[UPDATED]%s\n\n" "$ok" "$norm"
printf "%s*** This script has been updated. Please re-run it to load the updated version of this file. ***%s\n\n" "$yellow" "$norm"
exit 0
fi
fi
fi
## update files
set -- dummy "$updateFiles"
shift
for file; do
printf "\nchecking '%s' for updates... " "$file"
repoFileChecksum=$( grep "$file" "$checksumFilename" | grep -o '^\S*' )
if [ -f "$file" ]; then
localFileChecksum=$( sha256sum "$file" | grep -o '^\S*' )
else
localFileChecksum=0
fi
if ! [ "$localFileChecksum" = "$repoFileChecksum" ]; then
printf "%s[AVAILABLE]%s\n" "$yellow" "$norm"
updatesAvailable=$((updatesAvailable+1))
# download update
printf "Downloading updated '%s'... " "$file"
if ! wget --quiet --tries=3 --timeout=10 -O "$file" "$serverPath$file"; then
printf "%s[ERROR]%s\n\n" "$err" "$norm"
downloadFailed=$((downloadFailed+1))
else
printf "%s[OK] %s" "$ok" "$norm"
downloadSuccess=$((downloadSuccess+1))
# verify download
localFileChecksum=$( sha256sum "$file" | grep -o '^\S*' )
if ! [ "$localFileChecksum" = "$repoFileChecksum" ]; then
printf "%s[INVALID]%s\n" "$err" "$norm"
updateFailed=$((updateFailed+1))
else
printf "%s[VERIFIED]%s\n" "$ok" "$norm"
updateSuccess=$((updateSuccess+1))
fi
fi
else
printf "[NONE]\n"
fi
done
### display results
printf "\n%sResults:%s\n" "$cyan" "$norm"
printf "\tUpdates: %s available\n" "$updatesAvailable"
printf "\tDownloads: %s successful, %s%s failed%s\n" "$downloadSuccess" "$err" "$downloadFailed" "$norm"
printf "\tUpdates: %s applied, %s%s failed%s\n" "$updateSuccess" "$err" "$updateFailed" "$norm"
exit 0