From c2d5911b44bab24cb25102890bf1ecb120071ea6 Mon Sep 17 00:00:00 2001 From: Daniel Hansson Date: Thu, 6 Jun 2019 22:30:50 +0200 Subject: [PATCH] fix watchtower (#844) --- lib.sh | 14 ++++++++++++++ nextcloud_update.sh | 35 +++++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/lib.sh b/lib.sh index 0a36c82a..e38fa3f1 100644 --- a/lib.sh +++ b/lib.sh @@ -964,6 +964,20 @@ systemctl daemon-reload systemctl restart docker } +# Check if old docker exists +# FULL NAME e.g. ark74/nc_fts or containrrr/watchtower or collabora/code +does_this_docker_exist() { +if [ "$(docker ps -a >/dev/null 2>&1 && echo yes || echo no)" == "yes" ] +then + if [ "$(docker images "$1" | awk '{print $1}' | tail -1)" == "$1" ] + then + return 0 + else + return 1 + fi +fi +} + # Remove all dockers excluding one # docker_prune_except_this fts_esror 'Full Text Search' docker_prune_except_this() { diff --git a/nextcloud_update.sh b/nextcloud_update.sh index 368058ca..7111094f 100644 --- a/nextcloud_update.sh +++ b/nextcloud_update.sh @@ -36,7 +36,6 @@ https://shop.hanssonit.se/product/premium-support-per-30-minutes/" fi fi - # System Upgrade if which mysql > /dev/null then @@ -161,16 +160,40 @@ then ln -s "$ADMINERDIR"/latest.php "$ADMINERDIR"/adminer.php fi -# Update ALL Docker images automatically with watchtower: -if [ "$(docker ps -a >/dev/null 2>&1 && echo yes || echo no)" == "yes" ] +# Remove old watchtower if existing +if does_this_docker_exist v2tec/watchtower then - cont_name=watchtower - if ! docker ps -a --format '{{.Names}}' | grep -Eq "^${cont_name}\$"; + # Get Env values (https://github.com/koalaman/shellcheck/issues/1601) + get_env_values() { + # shellcheck disable=SC2016 + docker inspect -f '{{range $index, $value := .Config.Env}}{{$value}}{{println}}{{end}}' watchtower > env.list + } + get_env_values + + # Remove empty lines + sed -i '/^[[:space:]]*$/d' env.list + + # Get Cmd values + CmdDocker=$(docker inspect --format='{{.Config.Cmd}}' watchtower | cut -d "]" -f 1 | cut -d "[" -f 2;) + + # Check if env.list is empty and run the docker accordingly + if [ -s env.list ] then - docker run -d --restart=unless-stopped --name watchtower -v /var/run/docker.sock:/var/run/docker.sock v2tec/watchtower --cleanup --interval 3600 + docker_prune_this v2tec/watchtower + docker run -d --restart=unless-stopped --name watchtower -v /var/run/docker.sock:/var/run/docker.sock --env-file ./env.list containrrr/watchtower "$CmdDocker" + rm -f env.list + else + docker_prune_this v2tec/watchtower + docker run -d --restart=unless-stopped --name watchtower -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower "$CmdDocker" fi fi +# Update ALL Docker images automatically with watchtower +if ! does_this_docker_exist containrrr/watchtower +then + docker run -d --restart=unless-stopped --name watchtower -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower --cleanup --interval 3600 +fi + # Cleanup un-used packages apt autoremove -y apt autoclean