From 3bd1c3f9bdb198d769afeec493339eff99fd11ea Mon Sep 17 00:00:00 2001 From: Matan Horovitz Date: Fri, 28 Oct 2022 11:21:14 +0300 Subject: [PATCH] Extra accuracy fixes --- HatarashiHako | 77 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/HatarashiHako b/HatarashiHako index 568363c..be85ce1 100755 --- a/HatarashiHako +++ b/HatarashiHako @@ -1,16 +1,65 @@ #!/bin/bash ##Script to update Docker container images occasionally and alert when update is done. source /root/.bash_profile -if [[ $1 =~ "-v|--verbose|verbose" ]]; then - source /home/shmick/Scripts/Okiru "$1" +arg0=$(basename "$0") +#This is where containers live +CONTAINER_DIR="/var/Red-Vol/Media/Containers/" +#Show help if arguments are misused +usage() +{ + exec 1>2 # Send standard output to standard error + help + exit 1 +} + +flag_error() +{ + echo -e "$arg0: $*." >&2 + help + exit 1 +} + +help() +{ + echo "$arg0 - show how long a process has been running" + echo " {-s|--stack} [name] -- Run update process just for specified stack (ex: vikunja)" + echo " {-v|--verbose|verbose} -- Print debug messages" + echo " {-h|--help} -- Print this help message and exit" + exit 0 +} +#Pass arguments to the script +flags() +{ + while test $# -gt 0 + do + case "$1" in + #If a stack is specified, run the process for that stack only + (-s|--stack) + shift + if [[ -d $CONTAINER_DIR/$1 ]]; then + export CONTAINER_PATHS="$CONTAINER_DIR/$1" + fi + shift;; + (-v|--verbose|verbose) + export VERBOSE="1" +# Okiru looks for^ this variable + shift;; + (-h|--help) + help;; + (*) help;; + esac + done +} +flags "$@" + +if [[ -n $verbose ]]; then + source /home/shmick/Scripts/Okiru "$verbose" else source /home/shmick/Scripts/Okiru fi source /etc/environment #Remember where you are to change back to later LOCAL_DIR=`pwd` -#This is where containers live -CONTAINER_DIR="/var/Red-Vol/Media/Containers/" declare -a CONTAINER_PATHS declare -a OUTDATED_CONTAINERS declare -a FAILED_CONTAINERS @@ -21,19 +70,27 @@ if [[ -f /tmp/docker-updated ]]; then fi #Start counting how many containers fail; appears in final mail subject. OUTDATED_COUNT="0" -CONTAINER_PATHS=$(find $CONTAINER_DIR -maxdepth 2 -type f -name docker-compose.yml ! -path '*Archive*' | xargs dirname ) +#Check if path is already set by user specified stack; otherwise, find all containers. +if [[ -z $CONTAINER_PATHS ]]; then + CONTAINER_PATHS=$(find $CONTAINER_DIR -maxdepth 2 -type f -name docker-compose.yml ! -path '*Archive*' | xargs dirname ) +fi #Find containers in ^ base dir ^ in base container path ^ by finding compose files ^ (not here) ^ and getting their directory name. for container_path in ${CONTAINER_PATHS[@]}; do cd $container_path debug "Working on container directory" "$container_path" container_stack=$(basename $container_path) info "Working on stack" "$container_stack" - container_images="$(cat $container_path/docker-compose.yml | grep -E "image: ([a-z]+)/([a-z]+)(:[a-z0-9].*$)?" | grep -Ev 'postgresql|mariadb' | awk '{print $2}')" -# search for a pattern of something:something with optional :tag ^ avoid database containers print ^ image name + #It's deadly to update tagless database images; this line is safe because it only catches tagged images. + container_images="$(cat $container_path/docker-compose.yml | grep -E "image: ([a-z]+)((/)|(:))([a-z]+)?(:)?([a-z0-9].*$)?" | awk '{print $2}')" +# search for a pattern of something:something with optional :tag print ^ image name for container_image in $container_images; do + debug "$container_stack has image" "$container_image" container_name="$(echo $container_image | awk -F/ '{print $2}' | sed "s/\:.*//")" # remove everything after the : ^ - debug "$container_stack has image" "$container_image" + if [[ -z $container_name ]] && [[ -n $(echo $container_image | grep -Ev 'postgres|mariadb') ]]; then + export container_name="$container_image" + fi + debug "$container_image has name" "$container_name" if [[ -n $(echo $container_image | grep -E "(.*:[a-z0-9].*$)") ]]; then # check if there is a :tag present ^ image_tag=":$(echo $container_image | awk -F: '{print $NF}')" @@ -42,8 +99,8 @@ for container_path in ${CONTAINER_PATHS[@]}; do export container_image=$(echo $container_image | awk -F: '{print $1}') # If the container does have a tag, keep the base name ^ without it (before the :) fi - debug "Fetching local image checksum with:" "docker inspect \"$container_image$image_tag\" | grep -Eo \"$container_image@sha256:([0-9a-zA-Z].*)(\")\" | sed -e 's/\"//g' | awk -F@ '{print \$2}" - local_image=$(docker inspect "$container_image$image_tag" | grep -Eo "$container_image@sha256:([0-9a-zA-Z].*)(\")" | sed -e 's/"//g' -e 's/\s+//g' | awk -F@ '{print $2}') + debug "Fetching local image checksum with:" "docker inspect \"$container_image$image_tag\" | grep -Eo \"($container_image@)?sha256:([0-9a-zA-Z].*)(\\\")\" | sed -e 's/\"//g' | awk -F@ '{print \$2}" + local_image=$(docker inspect "$container_image$image_tag" | grep -Eo "($container_image@)?sha256:([0-9a-zA-Z].*)(\")" | sed -e 's/"//g' -e 's/\s+//g' | awk -F@ '{print $2}') # remember, this bit ^ is empty without an image ^ this is the main image checksum remove ^ " and whitespace and^ get the checksum after the @ if [[ -z $local_image ]]; then error "Error fetching local image checksum for container $container_name!"