Files
shell_sequencer/seqs/element-web.sh

163 lines
4.3 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 [ ! -z "${1:-}" ] ; then
latestVersion="$1"
else
latestVersion=$(curl --silent "$latestUrl" | grep -Po '"tag_name": "v\K.*?(?=")')
fi
local isInstalled=$(grep -E "${latestVersion}" "${ELEMENT_WEB_LOC:-}/version" >>/dev/null 2>&1 && echo "1" || echo "0")
if (( isInstalled )) ; then
echo " [I] Version $latestVersion is already installed"
return 1
else
echo " [I] 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 [ ! -z $ELEMENT_WEB_BACKUP ] ; then
exe mkdir -p "$ELEMENT_WEB_BACKUP"
fi
if [ -n "${1:-}" ] ; then
tempRoot="$1"
else
tempRoot="$ELEMENT_WEB_LOC"
fi
local wwwBackup="$ELEMENT_WEB_BACKUP/${toolName}_www_`date +%Y%m%d-%H%M%S`.tar.gz"
echo " [I] 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": "\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 [ ! -z "${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
exe read -p "Install $latestVersion to $ELEMENT_WEB_LOC [n]o/(y)es? " answer
case $answer in
[yY])
;;
*)
info -e "Upgrade aborted"
return 1
;;
esac
fi
local isInstalled=$(grep -E "${latestVersion}" "${ELEMENT_WEB_LOC}/version" >>/dev/null && echo "1" || echo "0")
if [ $isInstalled -eq 1 ] ; then
echo " [I] 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 -o $? "Download failed: $downUrl"
exe cd "$tempDown"
exe tar -xf "$tempLoc"
endReturn -o $? "Extract failed: $tempLoc"
else
echo " [I] Found existing download: $tempExtract"
fi
# Installation
local tempBu="${ELEMENT_WEB_LOC}_bu_`date +%Y%m%d-%H%M%S`"
exe mv "$ELEMENT_WEB_LOC" "$tempBu"
step backup "$tempBu"
endReturn -o $? "Backup failed; $ELEMENT_WEB_LOC renamed!"
echo " [I] Installing version $latestVersion to $ELEMENT_WEB_LOC"
exe mv "$tempExtract" "$ELEMENT_WEB_LOC"
exe chown -R www-data: "$ELEMENT_WEB_LOC"
# Configuration
echo " [I] Copying configuration"
exe cp -ar "$tempBu/config.json" "$ELEMENT_WEB_LOC/"
echo " [I] 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