2020-03-13 02:31:05 -06:00
|
|
|
#!/bin/sh
|
|
|
|
|
2020-09-14 23:18:28 -06:00
|
|
|
#
|
|
|
|
# update script for ab-openldap container and utility scripts
|
2020-03-13 02:31:05 -06:00
|
|
|
# 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
|
2020-09-14 23:18:28 -06:00
|
|
|
#
|
2020-03-13 02:31:05 -06:00
|
|
|
|
2020-09-14 23:36:17 -06:00
|
|
|
### functions
|
|
|
|
|
|
|
|
consoleError() {
|
|
|
|
printf "\n%s%s%s\n\n" "$err" "$2" "$norm"
|
|
|
|
exit "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-14 23:36:03 -06:00
|
|
|
### 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)
|
2020-09-14 23:42:39 -06:00
|
|
|
yellow=$(tput setaf 11)
|
2020-09-14 23:36:03 -06:00
|
|
|
else
|
|
|
|
cyan=''
|
|
|
|
err=''
|
|
|
|
norm=''
|
|
|
|
ok=''
|
2020-09-14 23:42:39 -06:00
|
|
|
yellow=''
|
2020-09-14 23:36:03 -06:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2020-03-13 02:31:05 -06:00
|
|
|
### pre-requisites
|
|
|
|
|
|
|
|
# check if wget is installed
|
|
|
|
if ! command -v wget > /dev/null 2>&1; then
|
2020-09-14 23:36:34 -06:00
|
|
|
consoleError '1' "Sorry, this script requires that 'wget' is installed in order to automatically update files."
|
2020-03-13 02:31:05 -06:00
|
|
|
fi
|
|
|
|
|
2020-03-13 03:21:53 -06:00
|
|
|
# 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
|
|
|
|
|
2020-03-13 02:31:05 -06:00
|
|
|
# zero counters
|
|
|
|
updatesAvailable=0
|
|
|
|
downloadFailed=0
|
|
|
|
downloadSuccess=0
|
|
|
|
updateFailed=0
|
|
|
|
updateSuccess=0
|
|
|
|
|
|
|
|
# reference constants
|
|
|
|
containerName='ab-openldap'
|
2020-09-14 23:50:04 -06:00
|
|
|
containerUpdatePath="docker.asifbacchus.app/ldap/$containerName:latest"
|
2020-09-14 23:18:28 -06:00
|
|
|
serverPath="https://asifbacchus.app/public/$containerName/"
|
2020-03-13 02:31:05 -06:00
|
|
|
checksumFilename='checksums.sha256'
|
|
|
|
|
|
|
|
# files to update
|
2020-09-14 23:50:04 -06:00
|
|
|
scriptName="$containerName-update.sh"
|
|
|
|
updateFiles="$containerName-backup.params.template $containerName-backup.sh $containerName.params.template $containerName.sh"
|
2020-09-14 23:18:28 -06:00
|
|
|
|
2020-09-14 23:50:04 -06:00
|
|
|
printf "\n%sUpdating %s%s%s:%s\n" "$cyan" "$yellow" "$containerName" "$cyan" "$norm"
|
2020-03-13 02:31:05 -06:00
|
|
|
|
|
|
|
|
2020-03-13 03:21:53 -06:00
|
|
|
### update container
|
2020-03-13 02:31:05 -06:00
|
|
|
|
2020-09-14 23:18:28 -06:00
|
|
|
printf "updating container... "
|
2020-09-14 23:21:37 -06:00
|
|
|
if ! docker pull "$containerUpdatePath" > /dev/null 2>&1; then
|
2020-09-14 23:50:04 -06:00
|
|
|
printf "%s[ERROR]\n\n" "$err"
|
|
|
|
printf "There was a problem updating the container. Please try again later.%s\n\n" "$norm"
|
2020-03-13 03:21:53 -06:00
|
|
|
exit 1
|
|
|
|
else
|
2020-09-14 23:50:04 -06:00
|
|
|
printf "%s[OK]%s\n" "$ok" "$norm"
|
2020-03-13 03:21:53 -06:00
|
|
|
fi
|
|
|
|
|
2020-09-14 23:18:28 -06:00
|
|
|
### checksums
|
|
|
|
printf "downloading latest checksums... "
|
|
|
|
if ! wget --quiet --tries=3 --timeout=10 -O "$checksumFilename" "$serverPath$checksumFilename"; then
|
2020-09-14 23:50:04 -06:00
|
|
|
printf "%s[ERROR]\n\n" "$err"
|
|
|
|
printf "Unable to download updated checksums. Please try again later.%s\n\n" "$norm"
|
2020-03-13 02:31:05 -06:00
|
|
|
exit 1
|
|
|
|
else
|
2020-09-14 23:50:04 -06:00
|
|
|
printf "%s[OK]%s\n" "$ok" "$norm"
|
2020-03-13 02:31:05 -06:00
|
|
|
fi
|
|
|
|
|
2020-09-14 23:18:28 -06:00
|
|
|
|
|
|
|
### script self-update
|
|
|
|
printf "checking for updates to this script... "
|
2020-09-15 00:31:29 -06:00
|
|
|
localScriptChecksum=$( sha256sum "./$scriptName" | grep -o '^\S*' )
|
2020-09-14 23:18:28 -06:00
|
|
|
repoScriptChecksum=$( grep "$scriptName" "$checksumFilename" | grep -o '^\S*' )
|
2020-03-13 02:31:05 -06:00
|
|
|
if [ "$localScriptChecksum" = "$repoScriptChecksum" ]; then
|
2020-03-13 02:44:00 -06:00
|
|
|
printf "[NONE]\n"
|
2020-03-13 02:31:05 -06:00
|
|
|
else
|
|
|
|
# download updated script
|
2020-09-14 23:18:28 -06:00
|
|
|
if ! wget --quiet --tries=3 --timeout=10 -O "$scriptName" "$serverPath$scriptName"; then
|
2020-09-14 23:50:04 -06:00
|
|
|
printf "%s[ERROR]\n\n" "$err"
|
|
|
|
printf "Unable to download script update. Please try again later.%s\n\n" "$norm"
|
2020-03-13 02:31:05 -06:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
# verify download
|
2020-09-14 23:18:28 -06:00
|
|
|
localScriptChecksum=$( sha256sum "$scriptName" | grep -o '^\S*' )
|
2020-03-13 02:31:05 -06:00
|
|
|
if ! [ "$localScriptChecksum" = "$repoScriptChecksum" ]; then
|
2020-09-14 23:50:04 -06:00
|
|
|
printf "%s[ERROR]\n\n" "$err"
|
|
|
|
printf "Unable to verify checksum of updated script. Please try again later.%s\n\n" "$norm"
|
2020-03-13 02:31:05 -06:00
|
|
|
else
|
2020-09-14 23:50:04 -06:00
|
|
|
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"
|
2020-03-13 02:31:05 -06:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-03-13 03:21:53 -06:00
|
|
|
## update files
|
2020-09-15 00:40:55 -06:00
|
|
|
# remember: do NOT quote dummy var!
|
|
|
|
set -- dummy $updateFiles
|
2020-03-13 02:31:05 -06:00
|
|
|
shift
|
|
|
|
for file; do
|
2020-09-15 23:11:52 -06:00
|
|
|
printf "checking '%s' for updates... " "$file"
|
2020-09-14 23:18:28 -06:00
|
|
|
repoFileChecksum=$( grep "$file" "$checksumFilename" | grep -o '^\S*' )
|
2020-03-13 02:31:05 -06:00
|
|
|
if [ -f "$file" ]; then
|
2020-09-14 23:18:28 -06:00
|
|
|
localFileChecksum=$( sha256sum "$file" | grep -o '^\S*' )
|
2020-03-13 02:31:05 -06:00
|
|
|
else
|
2020-09-14 23:18:28 -06:00
|
|
|
localFileChecksum=0
|
2020-03-13 02:31:05 -06:00
|
|
|
fi
|
2020-09-14 23:18:28 -06:00
|
|
|
if ! [ "$localFileChecksum" = "$repoFileChecksum" ]; then
|
2020-09-15 23:11:52 -06:00
|
|
|
printf "%s[AVAILABLE]%s " "$yellow" "$norm"
|
2020-03-13 02:31:05 -06:00
|
|
|
updatesAvailable=$((updatesAvailable+1))
|
|
|
|
# download update
|
2020-09-14 23:18:28 -06:00
|
|
|
if ! wget --quiet --tries=3 --timeout=10 -O "$file" "$serverPath$file"; then
|
2020-09-15 23:11:52 -06:00
|
|
|
printf "%s[ERROR]%s\n" "$err" "$norm"
|
2020-03-13 02:31:05 -06:00
|
|
|
downloadFailed=$((downloadFailed+1))
|
|
|
|
else
|
2020-09-15 23:11:52 -06:00
|
|
|
printf "%s[DOWNLOADED] %s" "$ok" "$norm"
|
2020-03-13 02:31:05 -06:00
|
|
|
downloadSuccess=$((downloadSuccess+1))
|
|
|
|
# verify download
|
2020-09-14 23:18:28 -06:00
|
|
|
localFileChecksum=$( sha256sum "$file" | grep -o '^\S*' )
|
|
|
|
if ! [ "$localFileChecksum" = "$repoFileChecksum" ]; then
|
2020-09-14 23:50:04 -06:00
|
|
|
printf "%s[INVALID]%s\n" "$err" "$norm"
|
2020-03-13 02:31:05 -06:00
|
|
|
updateFailed=$((updateFailed+1))
|
|
|
|
else
|
2020-09-14 23:50:04 -06:00
|
|
|
printf "%s[VERIFIED]%s\n" "$ok" "$norm"
|
2020-03-13 02:31:05 -06:00
|
|
|
updateSuccess=$((updateSuccess+1))
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
printf "[NONE]\n"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
### display results
|
2020-09-14 23:50:04 -06:00
|
|
|
printf "\n%sResults:%s\n" "$cyan" "$norm"
|
2020-03-13 02:31:05 -06:00
|
|
|
printf "\tUpdates: %s available\n" "$updatesAvailable"
|
2020-09-15 23:20:05 -06:00
|
|
|
if [ "$updatesAvailable" -gt '0' ]; then
|
|
|
|
if [ "$downloadFailed" -gt '0' ]; then
|
|
|
|
printf "\tDownloads: %s successful, %s%s failed%s\n" "$downloadSuccess" "$err" "$downloadFailed" "$norm"
|
|
|
|
else
|
|
|
|
printf "\tDownloads: %s successful\n" "$downloadSuccess"
|
|
|
|
fi
|
|
|
|
if [ "$updateFailed" -gt '0' ]; then
|
|
|
|
printf "\tUpdates: %s applied, %s%s failed%s\n\n" "$updateSuccess" "$err" "$updateFailed" "$norm"
|
|
|
|
else
|
|
|
|
printf "\tUpdates: %s applied\n\n" "$updateSuccess"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
exit 0
|
2020-03-13 02:31:05 -06:00
|
|
|
|
2020-09-15 23:20:05 -06:00
|
|
|
#EOF
|