#!/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 # 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 # zero counters updatesAvailable=0 downloadFailed=0 downloadSuccess=0 updateFailed=0 updateSuccess=0 # reference constants containerName='' containerUpdatePath='docker.asifbacchus.app/...' server='https://git.asifbacchus.app/ab-docker/scripts/raw/branch/master/' checksumFilename='checksums.sha256' # files to update localScriptName='update.sh' repoScriptName="${containerName}-update.sh" updateFiles="" ### 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 "Container updated!\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