From a3b44c2d3947c79d726838d052e5ec9743363afa Mon Sep 17 00:00:00 2001 From: Luca Beltrame Date: Sat, 19 Sep 2020 10:27:42 +0200 Subject: [PATCH] Script to update Element.im to its latest version --- update_element_im.sh | 83 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 update_element_im.sh diff --git a/update_element_im.sh b/update_element_im.sh new file mode 100755 index 0000000..9e6a33d --- /dev/null +++ b/update_element_im.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +set -e +set -o errexit +set -o pipefail +set -o nounset +set -x + +function version_gt() { + test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; +} + +function cleanup() { + local tmpdir=$1 + rm -r "${tmpdir:?}"/* + rmdir "${tmpdir}" + +} + +function update() { + local tarball_url=$1 + local gpg_url=$2 + local tempdir=$3 + local destination=$4 + + tarball_file=$(basename "${tarball_url}") + gpg_file=$(basename "${gpg_url}") + + pushd "${tempdir}" + echo "Downloading..." + echo "Debug - ${tempdir} ${tarball_url}" + curl -L -O "${tarball_url}" + curl -L -O "${gpg_url}" + + # GPG key of project + echo "Verifying the archive integrity..." + curl -s -O "https://github.com/RiotRobot.gpg" + gpg -o RiotRobot.ring --dearmor RiotRobot.gpg + gpg --no-default-keyring --keyring ./RiotRobot.ring --verify "${gpg_file}" + + echo "Extracting archive..." + tar xfv "${tarball_file}" + folder_name=$(basename "${tarball_file}" .tar.gz) + # Update Element, but don't destroy the existing configuration + echo "Copying new files..." + rsync -rXH "${folder_name}/" "${destination}/" \ + --delete \ + --exclude config.json \ + --chown "${WEB_USER}":"${WEB_USER}" + echo "Update complete." + popd +} + +tmpdir=$(mktemp -d) + +trap 'cleanup ${tmpdir}' EXIT + +HOMESERVER_URL="https://chat.dennogumi.org" +DESTINATION_PATH="/srv/www/htdocs/chat.dennogumi.org" +WEB_USER="nginx" +github_data=$(curl -s https://api.github.com/repos/vector-im/element-web/releases/latest) + +current_version=$(curl -s "${HOMESERVER_URL}/version") +remote_version=$(echo "${github_data}" | jq '.name') + +echo "Checking for version updates..." + +if version_gt "${remote_version}" "${current_version}" +then + asset_name=$(echo "${github_data}" | jq -r '.assets[] | + select(.browser_download_url|endswith("tar.gz")) | + select(.name|startswith("riot")).browser_download_url') + gpg_key_name=$( + echo "${github_data}" | jq -r '.assets[] | + select(.browser_download_url|endswith("asc")) | + select(.name|startswith("riot")).browser_download_url') + echo "Updating Element.im from ${current_version} to ${remote_version}..." + update "${asset_name}" "${gpg_key_name}" "${tmpdir}" "${DESTINATION_PATH}" +else + echo "No new update: nothing to do." +fi + +exit 0