Files
shell_sequencer/seqs/element-web.sh

165 lines
4.4 KiB
Bash
Executable File

#!/bin/bash
toolName=element-web
latestUrl="https://api.github.com/repos/vector-im/element-web/releases/latest"
# Get script working directory
# (when called from a different directory)
WDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >>/dev/null 2>&1 && pwd )"
CONFIG=0
CONFIG_FILE_NAME="${toolName}.cfg"
CONFIG_FILE_TEMPLATE="$WDIR/${CONFIG_FILE_NAME}.example"
step_config() {
initSeqConfig "$CONFIG_FILE_NAME" "$CONFIG_FILE_TEMPLATE"
if [ $? -eq 0 ] ; then
echo " ${toolName} path: ${ELEMENT_WEB_LOC}"
echo " ${toolName} backup: ${ELEMENT_WEB_BACKUP}"
CONFIG=1
fi
}
step_18_info() { echo "Check for updates"; }
step_18_alias() { ALIAS="updatecheck"; }
step_18() {
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 && echo "1" || echo "0")
if [ $isInstalled -eq 1 ] ; 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 [ELEMENT WEB ROOT]"
if [ $CONFIG -ne 0 ] ; then
echo " at $ELEMENT_WEB_BACKUP"
else
echo
fi
}
step_20_alias() { ALIAS="backup"; }
step_20() {
shift
local tempRoot=
if [ $CONFIG -eq 0 ] ; then
echoerr " [E] No configuration file found"
return 1
fi
if [ ! -z $ELEMENT_WEB_BACKUP ] ; then
exe mkdir -p "$ELEMENT_WEB_BACKUP"
fi
if [ ! -z $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 [ $CONTEXT_HELP -eq 0 ] ; then
echo ": $(curl --silent "$latestUrl" | grep -Po '"tag_name": "\K.*?(?=")')"
else
echo " [CUSTOM VERSION]"
fi
else
echo "Get version $1 from github"
fi
}
step_22_alias() { ALIAS="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
echoerr " [E] Cannot determine latest version from github repository"
return 1
elif [ $QUIET -eq 0 ] ; then
echo
exe read -p "Install $latestVersion to $ELEMENT_WEB_LOC [n]o/(y)es? " answer
case $answer in
[yY])
;;
*)
echoerr " [I] 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() { ALIAS="clean"; }
step_24() {
exe rm -rf "$tempDown"
}
VERSION_SEQREV=11
. /usr/local/bin/sequencer.sh