scripts/template-update.sh

144 lines
4.5 KiB
Bash
Raw Normal View History

2020-03-13 02:21:59 -06:00
#!/bin/sh
### update script for <ab-container> utility scripts
# version x.y.z
# 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 run this script since you must be able to run 'docker exec' commands against the container!"
fi
fi
2020-03-13 02:21:59 -06:00
# zero counters
updatesAvailable=0
downloadFailed=0
downloadSuccess=0
updateFailed=0
updateSuccess=0
# reference constants
containerName=''
2020-03-13 03:12:47 -06:00
containerUpdatePath='docker.asifbacchus.app/...'
2020-03-13 02:21:59 -06:00
server='https://git.asifbacchus.app/ab-docker/scripts/raw/branch/master/'
checksumFilename='checksums.sha256'
# files to update
localScriptName='update.sh'
repoScriptName="${containerName}-update.sh"
2020-03-13 02:21:59 -06:00
updateFiles=""
2020-03-13 03:12:47 -06:00
### update container
printf "\n*** Updating %s container and service scripts ***\n\n" "$containerName"
2020-03-13 02:21:59 -06:00
2020-03-13 03:12:47 -06:00
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
"Container updated!\n"
fi
### update scripts
printf "Updating %s service scripts\n" "$containerName"
## download latest checksums
2020-03-13 02:21:59 -06:00
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
2020-03-13 03:12:47 -06:00
## check for updates to this script
2020-03-13 02:21:59 -06:00
printf "Checking for updates to this script... "
2020-03-13 02:39:37 -06:00
repoScriptChecksum=$( grep "$repoScriptName" "$checksumFilename" | grep -o '^\S*' )
2020-03-13 02:21:59 -06:00
localScriptChecksum=$( sha256sum "$localScriptName" | grep -o '^\S*' )
if [ "$localScriptChecksum" = "$repoScriptChecksum" ]; then
printf "[NONE]\n"
2020-03-13 02:21:59 -06:00
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
2020-03-13 03:12:47 -06:00
## update files
2020-03-13 02:21:59 -06:00
set -- dummy $updateFiles
shift
for file; do
updateTarget="$file"
printf "\nChecking '%s' for updates... " "$updateTarget"
2020-03-13 02:39:37 -06:00
repoFile=$( grep "$updateTarget" "$checksumFilename" | grep -o '^\S*' )
2020-03-13 02:21:59 -06:00
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