From ce3c8e9c8c9c75b99949ed4ebafdcedc31c1fedc Mon Sep 17 00:00:00 2001 From: Asif Bacchus Date: Fri, 13 Mar 2020 02:21:59 -0600 Subject: [PATCH] update script template file --- template-update.sh | 125 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 template-update.sh diff --git a/template-update.sh b/template-update.sh new file mode 100644 index 0000000..42783b5 --- /dev/null +++ b/template-update.sh @@ -0,0 +1,125 @@ +#!/bin/sh + +### update script for 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 + +# zero counters +updatesAvailable=0 +downloadFailed=0 +downloadSuccess=0 +updateFailed=0 +updateSuccess=0 + +# reference constants +containerName='' +server='https://git.asifbacchus.app/ab-docker/scripts/raw/branch/master/' +checksumFilename='checksums.sha256' + +# files to update +localScriptName='update.sh' +repoScriptName='template-update.sh' +updateFiles="" + +printf "\n*** Updating %s container service scripts ***\n\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" files.sha256 | grep -o '^\S*' ) +localScriptChecksum=$( sha256sum "$localScriptName" | grep -o '^\S*' ) +if [ "$localScriptChecksum" = "$repoScriptChecksum" ]; then + printf "[NONE]\n\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 "Checking '%s' for updates... " "$updateTarget" + repoFile=$( grep "$updateTarget" files.sha256 | 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 \ No newline at end of file