diff --git a/seqs/matrix.sh b/seqs/matrix.sh new file mode 100755 index 0000000..2e1dc9e --- /dev/null +++ b/seqs/matrix.sh @@ -0,0 +1,171 @@ +#!/bin/bash + +toolName=synapse +toolDeps="build-essential python3-dev libffi-dev python3-pip python3-setuptools postgresql libssl-dev virtualenv libjpeg-dev libxslt1-dev libpq5" +toolDir="/opt/synapse" +toolConfig="${toolDir}/homeserver.yaml" +toolUser="synapse" +toolGroup="synapse" + +# Needed for different steps +postgresDb="" +postgresUser="" +postgresPass="" + +# 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() { + ## e.g. to source a config file manually: + #. "$CONFIG_FILE" + ## or to use sequencer api: + #initSeqConfig "$CONFIG_FILE_NAME" "$CONFIG_FILE_TEMPLATE" + #if [ $? -eq 0 ] ; then + # CONFIG=1 + #fi +#} + +step_1_info() { echo "Installing $toolName dependencies"; } +step_1_alias() { ALIAS="install"; } +step_1() { + local aptOption= + + exe apt update + endReturn -o $? "Updating apt repositories failed" + + if [ $QUIET -ne 0 ] ; then + aptOption="-y" + else + aptOption="" + fi + + exe apt install $toolDeps $aptOption +} + +step_2_info() { echo "Create postgres database for $toolName"; } +step_2_alias() { ALIAS="createdb"; } +step_2() { + readDatabaseInfos + + exe cd ~postgres + exe sudo -u postgres psql -c "CREATE USER ${postgresUser} WITH ENCRYPTED password '${postgresPass}';" + exe sudo -u postgres psql -c "CREATE DATABASE ${postgresDb} ENCODING "UTF8" LC_COLLATE='C' LC_CTYPE='C' template=template0 OWNER ${postgresUser};" + exe sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE \"${postgresDb}\" to ${postgresUser};" +} + +step_3_info() { echo "Install $toolName"; } +step_3() { + exe mkdir -p "$toolDir" + exe virtualenv -p python3 "${toolDir}/env" + exe cd "$toolDir" + exe source "${toolDir}/env/bin/activate" + exe pip install --upgrade pip + exe pip install --upgrade setuptools + exe pip install matrix-synapse[postgres] + + # Create default configuration + exe python3 -m synapse.app.homeserver --server-name matrix.mydomain.com --config-path homeserver.yaml --generate-config --report-stats=no + exe deactivate + + # Create media directories + exe mkdir ${toolDir}/media_store ${toolDir}/uploads + exe chmod 770 "${toolDir}/media_store" "${toolDir}/uploads" + # Allow matrix to write its logs in /opt/synapse + exe chmod 755 "${toolDir}" + exe chown ${toolUser}:${toolGroup} "${toolDir}" "${toolDir}/media_store" "${toolDir}/uploads" +} + +step_4_info() { echo "Configure $toolName"; } +step_4() { + exe vi "$toolConfig" +} + +step_5_info() { echo "Create $toolName systemd service"; } +step_5() { + exe addgroup "$toolGroup" + exe adduser --system --home ${toolDir}/ --no-create-home --disabled-password --shell /bin/nologin --ingroup "$toolGroup" "$toolUser" + addConf -c "$toolService" "$toolServiceLoc" +} +toolServiceLoc="/etc/systemd/system/system/matrix-synapse.service" +toolService="[Unit] +Description=Matrix Synapse service +After=network.target + +[Service] +Type=forking +WorkingDirectory=${toolDir}/ +ExecStart=${toolDir}/env/bin/synctl start +ExecStop=${toolDir}/env/bin/synctl stop +ExecReload=${toolDir}/env/bin/synctl restart +User=${toolUser} +Group=${toolGroup} +Restart=always +StandardOutput=syslog +StandardError=syslog +SyslogIdentifier=synapse + +[Install] +WantedBy=multi-user.target" + +step_20_info() { echo "Backup postgres database"; } +step_20_alias() { ALIAS="backupdb"; } +step_20() { + local DELYEAR=$(($(date +%Y)-2)) + if [ ! -s ~/.pgpass ] ; then + echo " [I] For unattended backup please define ~/.pgpass containing credentials" + echo " e.g. localhost:5432:database:user:pass" + echo "Backup custom pg format with standard user / database: synapse / synapse" + fi + exep "pg_dump -h 127.0.0.1 -U synapse -Fc synapse | bzip2 -c > ${toolDbBackupFolder}/`date +%Y-%m-%d\"_\"%H-%M-%S`.backup.bz2" + exe rm -f ${toolDbBackupFolder}/${DELYEAR}* +} +toolDbBackupFolder=/root/backupdb + + +step_22_info() { echo "Postgres database restore"; } +step_22_alias() { ALIAS="restoredb"; } +step_22() { + echo " [I] Postgres database restore procedure" + echo "1. Create a empty postgres database first (step 4)" + echo "2. psql -h -U -d -W -f " + echo " e.g. psql -h 127.0.0.1 -U synapse -d synapse -W -f 2018-06-07_18-10-56.sql" + echo "or" + echo "3. Custom postgres format dump restore:" + echo " pg_restore -h localhost -p 5432 -U synapse -d new_db -v \"10.70.0.61.backup\"" + echo + echo "Available postgresql databases:" + exe cd ~postgres && sudo -u postgres psql -c '\l' + echo "Available postgresql user:" + exe cd ~postgres && sudo -u postgres psql -c '\du' +} + +# Read postgres database information dbname/user/pass if empty +readDatabaseInfos() { + if [ "$postgresDb" == "" ] ; then + read -p "Enter postgres database name: " postgresDb + endCheckEmpty postgresDb "database" + fi + if [ "$postgresUser" == "" ] ; then + read -p "Enter postgres user name: " postgresUser + endCheckEmpty postgresUser "user name" + fi + if [ "$postgresPass" == "" ] ; then + read -s -p "Enter postgres password: " postgresPass + endCheckEmpty postgresPass "password" + fi + echo +} + +# Needs readDatabaseInfos() to execute some commands +toolScript() { + if [ ! -z "$1" ] ; then + readDatabaseInfos + fi +} + +VERSION_SEQREV=11 +. /usr/local/bin/sequencer.sh