#!/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 # ### functions consoleError() { printf "\n%s%s%s\n\n" "$err" "$2" "$norm" exit "$1" } ### text formatting presets if command -v tput > /dev/null; then cyan=$(tput setaf 6) err=$(tput bold)$(tput setaf 1) magenta=$(tput setaf 5) norm=$(tput sgr0) ok=$(tput setaf 2) else cyan='' err='' magenta='' norm='' ok='' fi ### 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:latestx' serverPath="https://asifbacchus.app/public/$containerName/" checksumFilename='checksums.sha256' # files to update scriptName='ab-openldap-update.sh' updateFiles="ab-openldap-backup.params.template ab-openldap-backup.sh ab-openldap.params.template ab-openldap.sh" printf "\nUpdating %s:\n" "$containerName" ### update container printf "updating container... " if ! docker pull "$containerUpdatePath" > /dev/null 2>&1; then printf "[ERROR]\n\n" printf "There was an error updating the container. Try again later.\n\n" exit 1 else printf "[OK]\n" fi ### checksums printf "downloading latest checksums... " if ! wget --quiet --tries=3 --timeout=10 -O "$checksumFilename" "$serverPath$checksumFilename"; then printf "[ERROR]\n\n" printf "Unable to download updated checksums. Try again later.\n\n" exit 1 else printf "[OK]\n" fi ### script self-update printf "checking for updates to this script... " localScriptChecksum=$( sha256 "./$scriptName" | grep -o '^\S*' ) repoScriptChecksum=$( grep "$scriptName" "$checksumFilename" | grep -o '^\S*' ) if [ "$localScriptChecksum" = "$repoScriptChecksum" ]; then printf "[NONE]\n" else # download updated script if ! wget --quiet --tries=3 --timeout=10 -O "$scriptName" "$serverPath$scriptName"; then printf "[ERROR]\n\n" printf "Unable to download script update. Try again later.\n\n" exit 1 else # verify download localScriptChecksum=$( sha256sum "$scriptName" | 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 printf "\nchecking '%s' for updates... " "$file" repoFileChecksum=$( grep "$file" "$checksumFilename" | grep -o '^\S*' ) if [ -f "$file" ]; then localFileChecksum=$( sha256sum "$file" | grep -o '^\S*' ) else localFileChecksum=0 fi if ! [ "$localFileChecksum" = "$repoFileChecksum" ]; then printf "[AVAILABLE]\n" updatesAvailable=$((updatesAvailable+1)) # download update printf "Downloading updated '%s'... " "$file" if ! wget --quiet --tries=3 --timeout=10 -O "$file" "$serverPath$file"; then printf "[ERROR]\n" downloadFailed=$((downloadFailed+1)) else printf "[OK] " downloadSuccess=$((downloadSuccess+1)) # verify download localFileChecksum=$( sha256sum "$file" | grep -o '^\S*' ) if ! [ "$localFileChecksum" = "$repoFileChecksum" ]; then printf "[INVALID]\n" updateFailed=$((updateFailed+1)) else printf "[VERIFIED]\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