scripts/docker/check.rocketchat.update

76 lines
3.7 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
# Purpose {{{
## Create a temp file (to monitor) if an upgrade is available for rocket.chat
## image on the Docker Hub.
## Download the new version of the image.
## How-to use {{{
### 1. Needs imagetags script, in the same directory
### cf. https://git.ipr.univ-rennes1.fr/cellinfo/scripts/src/master/docker/imagetags
# wget https://git.ipr.univ-rennes1.fr/cellinfo/scripts/raw/master/docker/imagetags
### 2. Ensure to run with a user member of the docker group.
### 3. Create a cron job, eg.
#00 20 * * * root /opt/repos/ipr.scripts/docker/check.rocketchat.update
### 4. Monitor the temp file.
# Or enable MAILTO in cronjob and print a message in the script.
# Or send a mail.
# …
## }}}
# }}}
# Vars {{{
DEBUG=1
script_wd=$(dirname "${0}")
rkt_repo_name="rocketchat"
rkt_image_name="rocket.chat"
rkt_current_version=$(docker container ls | grep -- "${rkt_repo_name}/${rkt_image_name}" | sed -- "s/.*${rkt_repo_name}\/${rkt_image_name}:\([^ ]*\) .*/\1/")
rkt_current_imageid=$(docker image inspect "${rkt_repo_name}/${rkt_image_name}:${rkt_current_version}" | grep -m 1 -- "Image.*sha256" | sed "s/.*Image.*sha256:\(.*\)\",/\1/")
rkt_grep_pattern="[[:digit:]]\\.\\+[[:digit:]]*\\.\\?[[:digit:]]*$"
rkt_new_version=$("${script_wd}"/imagetags "${rkt_repo_name}/${rkt_image_name}" "${rkt_grep_pattern}" | tail -n1)
rkt_new_imageid=$(docker image inspect "${rkt_repo_name}/${rkt_image_name}:${rkt_new_version}" | grep -m 1 -- "Image.*sha256" | sed "s/.*Image.*sha256:\(.*\)\",/\1/")
rkt_new_version_file="/tmp/.docker.rocket.chat.upgrade"
# }}}
# Check if a container already runs with the wanted image {{{
if [ ! $(docker container ls | grep -q -- "${rkt_repo_name}${rkt_image_name}") ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test ${rkt_image_name} — Current version is ${rkt_current_version}."
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test ${rkt_image_name} — No container runs with ${rkt_image_name} image on this host."
rm -f -- "${rkt_new_version_file}"
exit 1
fi
# }}}
# Check if the current version is the last one {{{
if [ "${rkt_current_version}" != "${rkt_new_version}" ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test version — Current version (${rkt_current_version}) and new one (${rkt_new_version}) of ${rkt_image_name} seems to be different."
# Ensure to download this new version of the image
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test version — Download ${rkt_repo_name}/${rkt_image_name}:${rkt_new_version} image to compare ImageID."
docker pull "${rkt_repo_name}/${rkt_image_name}:${rkt_new_version}" > /dev/null
## Compare the ImageID of current and new version {{{
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: ImageID — ID of the current version is ${rkt_current_imageid}."
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: ImageID — ID of the new version is ${rkt_new_imageid}."
if [ "${rkt_current_imageid}" != "${rkt_new_imageid}" ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: ImageID — An upgrade is available for ${rkt_image_name}: ${rkt_new_version}."
## Create a temp file to monitor
touch -- "${rkt_new_version_file}"
printf '\e[1;35m%-6s\e[m\n' "An upgrade is available for ${rkt_image_name}: ${rkt_new_version}." >> "${rkt_new_version_file}"
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: ImageID — The current version is up-to-date."
rm -f -- "${rkt_new_version_file}"
fi
## }}}
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test version — The current version is up-to-date."
rm -f -- "${rkt_new_version_file}"
fi
# }}}
exit 0