scripts/ab-openldap/update.sh
Asif Bacchus ab048e2069 update directory organization
Renamed backup script, added files to update script and regenerated
checksums
2020-03-13 03:32:09 -06:00

144 lines
4.6 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
### pre-requisites
# check if wget is installed
if ! command -v wget > /dev/null 2>&1; then
printf "\nSorry, this script requires that 'wget' is installed in order to automatically update files.\nExiting.\n\n"
exit 1
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/ab-openldap:latest'
server="https://git.asifbacchus.app/ab-docker/scripts/raw/branch/master/$containerName/"
checksumFilename='checksums.sha256'
# files to update
localScriptName='update.sh'
repoScriptName='update.sh'
updateFiles="ab-openldap.sh ab-openldap.params.template backup.sh backup.params.template"
### update container
printf "\n*** Updating %s container and service scripts ***\n\n" "$containerName"
printf "Updating container:\n"
if ! docker pull "$containerUpdatePath"; then
printf "There was an error updating the container. Try again later.\n\n"
exit 1
else
printf "Container updated!\n\n"
fi
### update scripts
printf "Updating %s service scripts\n" "$containerName"
## download latest checksums
printf "Getting latest checksums from ab-git server... "
if ! wget --quiet --tries=3 --timeout=10 -N "${server}${checksumFilename}"; then
printf "[ERROR]\n"
printf "Unable to download checksums from ab-git server. Try again later.\n\n"
exit 1
else
printf "[OK]\n"
fi
## check for updates to this script
printf "Checking for updates to this script... "
repoScriptChecksum=$( grep "$repoScriptName" "$checksumFilename" | grep -o '^\S*' )
localScriptChecksum=$( sha256sum "$localScriptName" | grep -o '^\S*' )
if [ "$localScriptChecksum" = "$repoScriptChecksum" ]; then
printf "[NONE]\n"
else
# download updated script
if ! wget --quiet --tries=3 --timeout=10 -O $localScriptName "${server}${repoScriptName}"; then
printf "[ERROR]\n"
printf "Unable to download script update. Try again later.\n\n"
exit 1
else
# verify download
localScriptChecksum=$( sha256sum "$localScriptName" | grep -o '^\S*' )
if ! [ "$localScriptChecksum" = "$repoScriptChecksum" ]; then
printf "[ERROR]\n"
printf "Unable to verify checksum of updated script. Try again later.\n\n"
else
printf "[UPDATED]\n\n"
printf "*** This script has been updated. Please re-run it to load the updated version of this file. ***\n\n"
exit 0
fi
fi
fi
## update files
set -- dummy $updateFiles
shift
for file; do
updateTarget="$file"
printf "\nChecking '%s' for updates... " "$updateTarget"
repoFile=$( grep "$updateTarget" "$checksumFilename" | grep -o '^\S*' )
if [ -f "$file" ]; then
localFile=$( sha256sum "$updateTarget" | grep -o '^\S*' )
else
localFile=0
fi
if ! [ "$localFile" = "$repoFile" ]; then
printf "[AVAILABLE]\n"
updatesAvailable=$((updatesAvailable+1))
# download update
printf "Downloading updated '%s'... " "$updateTarget"
# specify a name here instead of using the server name so that wget
# overwrites the file
if ! wget --quiet --tries=3 --timeout=10 -O "$updateTarget" "${server}${updateTarget}"; then
printf "[ERROR]\n"
downloadFailed=$((downloadFailed+1))
else
printf "[OK]\n"
downloadSuccess=$((downloadSuccess+1))
# verify download
printf "Verifying '%s'... " "$updateTarget"
localFile=$( sha256sum "$updateTarget" | grep -o '^\S*' )
if ! [ "$localFile" = "$repoFile" ]; then
printf "[INVALID]\n"
updateFailed=$((updateFailed+1))
else
printf "[OK]\n"
updateSuccess=$((updateSuccess+1))
fi
fi
else
printf "[NONE]\n"
fi
done
### display results
printf "\nResults:\n"
printf "\tUpdates: %s available\n" "$updatesAvailable"
printf "\tDownloads: %s successful, %s failed\n" "$downloadSuccess" "$downloadFailed"
printf "\tUpdates: %s applied, %s failed\n" "$updateSuccess" "$updateFailed"
exit 0