Files
shell_sequencer/seqs/element-web.sh

160 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
readonly toolName=element-web
readonly latestUrl="https://api.github.com/repos/vector-im/element-web/releases/latest"
sq_config=0
seq_config() {
if initSeqConfig "${seq_configName:?}" "${seq_configTemplate:?}" ; then
echo " ${toolName} path: ${ELEMENT_WEB_LOC}"
echo " ${toolName} backup: ${ELEMENT_WEB_BACKUP}"
sq_config=1
fi
}
step_1_info() { echo "Check for updates"; }
step_1_alias() { echo "updatecheck"; }
step_1() {
shift
local latestVersion=
if [ -n "${1:-}" ] ; then
latestVersion="$1"
else
latestVersion=$(curl --silent "$latestUrl" | grep -Po '"tag_name": "v\K.*?(?=")')
fi
if grep -E "${latestVersion}" "${ELEMENT_WEB_LOC:-}/version" >>/dev/null 2>&1 ; then
info "Version $latestVersion is already installed"
return 1
else
info "Update to $latestVersion available"
fi
return 0
}
step_20_info() {
echo -n "Create a backup"
if (( sq_config )) ; then
echo " at $ELEMENT_WEB_BACKUP"
else
echo
fi
}
step_20_options() { echo "[ELEMENT WEB ROOT]"; }
step_20_alias() { echo "backup"; }
step_20() {
shift
local tempRoot=
if (( ! sq_config )) ; then
error -e "No configuration file found"
return 1
fi
if [ -n "$ELEMENT_WEB_BACKUP" ] ; then
exe mkdir -p "$ELEMENT_WEB_BACKUP"
fi
if [ -n "${1:-}" ] ; then
tempRoot="$1"
else
tempRoot="$ELEMENT_WEB_LOC"
fi
# Clear old backups
rmold -r 10 "${ELEMENT_WEB_BACKUP}" || true
local wwwBackup
wwwBackup="$ELEMENT_WEB_BACKUP/${toolName}_www_$(date +%Y%m%d-%H%M%S).tar.gz"
info "Backing up webserver directory to $wwwBackup"
exe cd "$tempRoot/.."
exe tar czf "$wwwBackup" "$(basename -- "$tempRoot")"
}
step_22_info() {
shift
if [ -z "${1:-}" ] ; then
echo -n "Get latest version from github"
if ! contextHelp ; then
echo ": $(curl --silent "$latestUrl" | grep -Po '"tag_name": "v\K.*?(?=")')"
else
echo
fi
else
echo "Get version $1 from github"
fi
}
step_22_options() { echo "[CUSTOM VERSION]"; }
step_22_alias() { echo "upgrade"; }
step_22() {
shift # don't need step number
local latestVersion=
if [ -n "${1:-}" ] ; then
latestVersion="$1"
else
latestVersion=$(curl --silent "$latestUrl" | grep -Po '"tag_name": "v\K.*?(?=")')
fi
if [ -z "$latestVersion" ] ; then
error -e "Cannot determine latest version from github repository"
return 1
elif interactive ; then
echo
if ! confirm "Install $latestVersion to $ELEMENT_WEB_LOC" ; then
info -e "Upgrade aborted"
return 1
fi
fi
if grep -E "${latestVersion}" "${ELEMENT_WEB_LOC}/version" >>/dev/null 2>&1 ; then
info "Version $latestVersion is already installed"
return 0
fi
# Download
local downUrl="https://github.com/vector-im/element-web/releases/download/v${latestVersion}/element-v${latestVersion}.tar.gz"
local tempExtract="$tempDown/element-v$latestVersion"
if [ ! -e "$tempExtract" ] ; then
exe mkdir -p "$tempDown"
exe wget -O "$tempLoc" "$downUrl"
endReturn "Download failed: $downUrl"
exe cd "$tempDown"
exe tar -xf "$tempLoc"
endReturn "Extract failed: $tempLoc"
else
info "Found existing download: $tempExtract"
fi
# Installation
local tempBu
tempBu="${ELEMENT_WEB_LOC}_bu_$(date +%Y%m%d-%H%M%S)"
exe mv "$ELEMENT_WEB_LOC" "$tempBu"
step backup "$tempBu"
endReturn "Backup failed; $ELEMENT_WEB_LOC renamed!"
info "Installing version $latestVersion to $ELEMENT_WEB_LOC"
exe mv "$tempExtract" "$ELEMENT_WEB_LOC"
exe chown -R www-data: "$ELEMENT_WEB_LOC"
# Configuration
info "Copying configuration"
addConf -c -f "$tempBu/config.json" "$ELEMENT_WEB_LOC/config.json"
info "Copying login background"
exe cp -ar "$tempBu/$tempBackImg/"* "$ELEMENT_WEB_LOC/$tempBackImg/"
exe rm -rf "$tempBu"
}
tempBackImg="themes/element/img/backgrounds"
tempDown="/tmp/${toolName}"
tempLoc="$tempDown/${toolName}.tar.gz"
step_24_info() { echo "Clean temporary files: $tempDown"; }
step_24_alias() { echo "clean"; }
step_24() {
exe rm -rf "$tempDown"
}
# shellcheck disable=SC2034 # Appears unused
readonly sqr_minVersion=16
# shellcheck disable=SC1091 # Don't follow this source
. /usr/local/bin/sequencer.sh