2018-08-27 16:29:09 +02:00
#!/bin/sh
# Purpose {{{
## Create a temp file (to monitor) if an upgrade is available for rocket.chat
## image on the Docker Hub.
2018-08-28 11:20:33 +02:00
## Download the new version of the image.
2018-08-27 16:29:09 +02:00
## 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/")
2018-08-29 10:23:25 +02:00
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/")
2018-08-27 16:29:09 +02:00
rkt_grep_pattern="[[:digit:]]\\.\\+[[:digit:]]*\\.\\?[[:digit:]]*$"
rkt_new_version=$("${script_wd}"/imagetags "${rkt_repo_name}/${rkt_image_name}" "${rkt_grep_pattern}" | tail -n1)
2018-08-29 10:23:25 +02:00
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/")
2018-08-27 16:29:09 +02:00
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."
2018-08-28 11:20:33 +02:00
rm -f -- "${rkt_new_version_file}"
2018-08-27 16:29:09 +02:00
exit 1
fi
# }}}
# Check if the current version is the last one {{{
if [ "${rkt_current_version}" != "${rkt_new_version}" ]; then
2018-08-29 10:23:25 +02:00
[ "${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."
2018-08-28 11:20:33 +02:00
docker pull "${rkt_repo_name}/${rkt_image_name}:${rkt_new_version}" > /dev/null
2018-08-29 10:23:25 +02:00
## 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
## }}}
2018-08-27 16:29:09 +02:00
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