scripts/github/check.rocketchat-electron.u...

114 lines
4.1 KiB
Plaintext
Raw Normal View History

#!/bin/sh
# Purpose {{{
## Create a temp file (to monitor) if an upgrade is available for Rocket.Chat.Electron
## project on Github.
## It's based on .deb package installation to check the current version.
## It can also compare the current available version in APT repositories
## if "repo" is given as first argument.
## How-to use {{{
### 1. Needs releasetags script, in the same directory
2023-11-03 15:54:59 +01:00
### cf. https://git.ipr.univ-rennes.fr/cellinfo/scripts/src/master/github/releasetags
# wget https://git.ipr.univ-rennes.fr/cellinfo/scripts/raw/master/github/releasetags
### 2. Create a cron job, eg:
#00 20 * * * root /opt/repos/ipr.scripts/github/check.rocketchat-electron.update
### 2-1 Create a cron job to compare the version available in a Debian repository:
#00 20 * * * root /opt/repos/ipr.scripts/github/check.rocketchat-electron.update repo
### 3. Monitor the temp file: /tmp/.github.rocketchat-electron.upgrade
# Or enable MAILTO in cronjob and edit the script to print a message.
# Or send a mail.
# …
## }}}
# }}}
# Expect maximum 1 argument {{{
if [ $# -gt 1 ]
then
cat << HELP
check.rocketchat-electron.update --
Compare current version of an installed Rocket.Chat.Electron and the last available.
EXAMPLE:
- Compare the current version installed from .deb file
check.rocketchat-electron.update
- Compare the current version from apt's repository
check.rocketchat-electron.update repo
HELP
exit 1
fi
# }}}
# Vars {{{
DEBUG=1
script_wd=$(dirname "${0}")
rkt_repo_url="https://github.com/RocketChat/Rocket.Chat.Electron"
if [ $# -eq 1 ] && [ "${1}" = "repo" ] ## If repository's version should be compared
then
rkt_current_version=$(apt-cache policy rocketchat | awk '/Candidate:/ {print $2}' | sed 's/.:\(.*\)-.*/\1/')
else
rkt_current_version=$(dpkg -l rocketchat | awk '/^ii.*rocketchat/ {print $3}' | sed 's/.:\(.*\)-.*/\1/')
fi
rkt_new_version=$("${script_wd}"/releasetags "${rkt_repo_url}" | head -n1 | sed 's/v//g')
rkt_new_version_file="/tmp/.github.rocketchat-electron.upgrade"
rkt_new_pkg_url="https://github.com/RocketChat/Rocket.Chat.Electron/releases/download/${rkt_new_version}/rocketchat_${rkt_new_version}_amd64.deb"
rkt_tmp_pkg_path="/tmp/.rocketchat_${rkt_new_version}_amd64.deb"
rkt_new_pkg_path="/tmp/rocketchat_${rkt_new_version}_amd64.deb"
# }}}
# 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}) seems to be different."
## If it doesn't already exists, download this new package
if [ ! -f "${rkt_new_pkg_path}" ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Download .deb file from Rocket.Chat.Electron repository on Github to: ${rkt_new_pkg_path} ."
wget --quiet "${rkt_new_pkg_url}" --output-document="${rkt_new_pkg_path}"
fi
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test version — The current version is up-to-date."
## Ensure to remove any temp file and useless .deb file
rm --force -- "${rkt_new_version_file}" "${rkt_new_pkg_path}" "${rkt_tmp_pkg_path}"
## Exit
exit 0
fi
# }}}
# Verify downloaded package {{{
## If the downloaded package has a size greater than zero {{{
if [ -s "${rkt_new_pkg_path}" ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Empty file ? — \
Downloaded package looks good."
### Create a temp file to monitor
touch -- "${rkt_new_version_file}"
printf '\e[1;35m%-6s\e[m\n' "An upgrade is available for Rocket.Chat client Electron (current: ${rkt_current_version}): ${rkt_new_version}." >> "${rkt_new_version_file}"
### Exit
exit 0
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Empty file ? — \
Empty file, don't need to go further."
### Ensure to remove the file to monitor
rm --force -- "${rkt_new_version_file}"
### Keep a record of the downloaded package because as a new release might come soon
mv --force -- "${rkt_new_pkg_path}" "${rkt_tmp_pkg_path}"
### Exit
exit 0
fi
## }}}
# }}}
exit 255