0x90-pyinfra/matrix-synapse/deploy.py
2024-10-06 21:04:15 +02:00

216 lines
6.1 KiB
Python

from pyinfra import host
from pyinfra.operations import apt, server, files
from pyinfra.facts.server import LinuxName
import os
files_path = "files/matrix-synapse/"
## don't exexute on a local mashine
if host.name != "@local":
## edit fstab to set wxallowd
'''
files.line(
name="edit fstab to set wxallowd",
path="/etc/fstab",
# line="var ffs rw,nodev,",
# line="var ffs rw,wxallowed,nodev,",
# line="var /var ffs rw,wxallowed,nodev 0 0",
# line="var /var ext4 rw,wxallowed,nodev 0 0",
)
'''
## change mount option for /var for now
'''
[matrix] mount: /var: wrong fs type, bad option, bad superblock on var, missing codepage or helper program, or other error.
[matrix] dmesg(1) may have more information after failed mount system call.
[matrix] Error: executed 0 commands
'''
'''
server.shell(
name="change mount option for /var for now",
# commands="mount -uo wxallowed /var",
commands="mount -o wxallowed /var",
)
'''
## check on which Distribution the script is executed
if host.get_fact(LinuxName) == "Debian":
## enable prerelease chanel to install matrix-synapse on Debian
server.shell(
name="enable prerelease chanel to install matrix-synapse on Debian",
commands=["wget -O /usr/share/keyrings/matrix-org-archive-keyring.gpg https://packages.matrix.org/debian/matrix-org-archive-keyring.gpg",
"echo \"deb [signed-by=/usr/share/keyrings/matrix-org-archive-keyring.gpg] https://packages.matrix.org/debian/ $(lsb_release -cs) main prerelease\" | tee /etc/apt/sources.list.d/matrix-org.list",
],
)
## update apt repositories
apt.update(
name="update apt repositories",
# _doas=True,
# _doas_user="pyinfra",
# _sudo=True,
# _sudo_user="pyinfra",
)
apt.packages(
name="install build-essential python3-dev libffi-def python3-pip python3-setuptools sqlite3 libssl-dev virtualenv libjpeg-dev libxslt1-dev libicu-dev as prerequisites for matrix-synapse-py3",
packages=["build-essential",
"python3-dev",
"libffi-dev",
"python3-pip",
"python3-setuptools",
"sqlite3",
"libssl-dev",
"virtualenv",
"libjpeg-dev",
"libxslt1-dev",
"libicu-dev",
],
)
apt.packages(
name="install synapse",
# packages=["synapse"],
packages=["matrix-synapse-py3"],
)
apt.packages(
name="install libxml (for link previews)",
# packages=["py3-libxml"],
packages=["python3-lxml"],
)
apt.packages(
name="install opendoas",
packages=["opendoas"],
)
# elif host.get_fact(LinuxName) == "":
else:
print("matrix-synapse deploy.py don't support: \"" +host.get_fact(LinuxName) +"\" yet")
exit("exit from matrix-synapse/deploy.py")
## copy doas config
doas_conf_path = files_path +"doas.conf"
exi = os.path.exists(doas_conf_path)
if exi == True:
files.put(
name="copy doas config",
src=doas_conf_path,
dest="/etc/doas.conf",
)
else:
print("file: \"" +doas_conf_path +"\" dosn't exists")
## generate synapse config
server.shell(
name="generate synapse config",
# commands=["/opt/venvs/matrix-synapse/bin/python3 -m synapse.app.homeserver -c /var/synapse/homeserver.yaml --generate-config --server-name chat.heizhaus.org --report-stats=no --generate-keys --keys-directory /var/synapse",
commands=[
## --generate-config and --generate-keys can't be in the same line/command
"/opt/venvs/matrix-synapse/bin/python3 -m synapse.app.homeserver -c /var/synapse/homeserver.yaml --generate-config --server-name chat.heizhaus.org --report-stats=no --keys-directory /var/synapse",
"/opt/venvs/matrix-synapse/bin/python3 -m synapse.app.homeserver -c /var/synapse/homeserver.yaml --generate-keys --keys-directory /var/synapse",
"touch /var/synapse/homeserver.yaml",
"cd /var/synapse",
],
# )
#!!! change user to _synapse !!!#
# _su_user="matrix-synapse",
# _su_user="_synapse",
)
# os.chdir("/var/synapse")
'''
[matrix] This account is currently not available.
[matrix] Error: executed 0 commands
'''
# creates="/var/synapse/homeserver.yaml",
# chdir="/var/synapse/",
# becom_user="_synapse",
## copy synapse config
homeserver_py_path = files_path +"homeserver.yaml"
exi = os.path.exists(homeserver_py_path)
if exi == True:
files.put(
name="copy synapse config",
src=homeserver_py_path,
dest="/var/synapse/homeserver.yaml",
)
else:
print("file: \"" +homeserver_py_path +"\" dosn't exists")
## copy logging config
log_conf_path = files_path + "chat.heizhaus.org.log.config"
exi = os.path.exists(log_conf_path)
if exi == True:
files.put(
name="copy logging config",
src=log_conf_path,
dest="/var/synapse/chat.heizhaus.org.log.config",
)
else:
print("file: \"" +log_conf_path +"\" dosn't exists")
## add synapse commands to PATH
dotprofile_path = files_path +"dotprofile"
exi = os.path.exists(dotprofile_path)
if exi == True:
files.put(
name="add synapse commands to PATH",
src=dotprofile_path,
dest="/root/.profile",
)
else:
print("file: \"" +dotprofile_path +"\" dosn't exists")
## change owner for /var/synapse
# user_name = "_synapse"
# group_name = "_synapse"
user_name = "matrix-synapse"
group_name = "matrix-synapse"
server.shell(
name="chown -R " +user_name +":" +group_name +" /var/synapse",
commands="chown -R " +user_name +":" +group_name +" /var/synapse && chmod -R 0770 /var/synapse",
)
## set server name
server_name = "chat.heizhaus.org"
files.replace(
name="set server name",
path="/etc/matrix-synapse/conf.d/server_name.yaml",
text="server_name: ",
replace="server_name: " +server_name,
)
## enable and restart matrix
server.service(
name="enable and restart matrix",
# service="synapse",
service="matrix-synapse.service",
enabled=True,
restarted=True,
)
# server.shell(
# name="enable and restart matrix",
# commands="systemctl enable synapse && systemctl restart synapse",
# )
## add synapse backup script
backup_sh_path = files_path +"borgbackup.sh"
exi = os.path.exists(backup_sh_path)
if exi == True:
files.put(
name="add synapse backup script",
src=backup_sh_path,
dest="/etc/daily.d/borgbackup.sh"
)
else:
print("file: \"" +backup_sh_path +"\" dosn't exists")