From f92b230485243bbe75cb2f3f5dfbf923212dd3e4 Mon Sep 17 00:00:00 2001 From: Stephane Gourichon Date: Sun, 24 Mar 2024 13:07:33 +0100 Subject: [PATCH] Script that checks if a container needs package update. --- .../containers_check_update_rebuild_switch.sh | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tools/containers_check_update_rebuild_switch.sh diff --git a/tools/containers_check_update_rebuild_switch.sh b/tools/containers_check_update_rebuild_switch.sh new file mode 100644 index 0000000..3a0a1b7 --- /dev/null +++ b/tools/containers_check_update_rebuild_switch.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +set -euxv + +echo "$@" + +function fail_one() +{ + echo >&2 "FAILURE when processing $CONTAINER_ID ($NAME_OF_CONTAINER), step: " "$@" + RC=1 +} + + +RC=0 + +for CONTAINER_ID +do + + echo "Processing $CONTAINER_ID" + TMPFILE=$(mktemp) + docker exec -u root $CONTAINER_ID bash -c "export LC_ALL=C ; apt-get update ; apt-get upgrade --dry-run" | tee $TMPFILE + trap "rm $TMPFILE" EXIT + if grep "0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded." $TMPFILE + then + echo "No update needed." + continue + fi + + PATH_OF_CONTAINER="$( docker inspect --format='{{ index .Config.Labels "com.docker.compose.project.working_dir" }}' $CONTAINER_ID )" + NAME_OF_CONTAINER="$( docker inspect --format='{{ .Name }}' $CONTAINER_ID )" + cd $PATH_OF_CONTAINER + docker-compose pull || { fail_one "pull" ; continue ; } + docker-compose build || { fail_one "build" ; continue ; } + docker-compose up -d || { fail_one "switch to newly built container" ; continue ; } + +done