deploy chatmaild in a virtualenv to make it easier to add dependencies

This commit is contained in:
holger krekel
2023-12-08 20:34:27 +01:00
parent 59c3730d84
commit 652b9688d3
4 changed files with 84 additions and 64 deletions

View File

@@ -2,7 +2,7 @@
Description=Dict authentication proxy for dovecot Description=Dict authentication proxy for dovecot
[Service] [Service]
ExecStart=/usr/local/bin/doveauth /run/dovecot/doveauth.socket vmail /home/vmail/passdb.sqlite ExecStart={execpath} /run/dovecot/doveauth.socket vmail /home/vmail/passdb.sqlite
Restart=always Restart=always
RestartSec=30 RestartSec=30

View File

@@ -2,7 +2,7 @@
Description=Chatmail Postfix BeforeQeue filter Description=Chatmail Postfix BeforeQeue filter
[Service] [Service]
ExecStart=/usr/local/bin/filtermail 10080 ExecStart={execpath} 10080
Restart=always Restart=always
RestartSec=30 RestartSec=30

View File

@@ -1,39 +1,66 @@
""" """
Chat Mail pyinfra deploy. Chat Mail pyinfra deploy.
""" """
import sys
import importlib.resources import importlib.resources
import subprocess
import shutil
import io
import configparser import configparser
from pathlib import Path from pathlib import Path
from pyinfra import host from pyinfra import host
from pyinfra.operations import apt, files, server, systemd from pyinfra.operations import apt, files, server, systemd, pip
from pyinfra.facts.files import File from pyinfra.facts.files import File
from pyinfra.facts.systemd import SystemdEnabled from pyinfra.facts.systemd import SystemdEnabled
from .acmetool import deploy_acmetool from .acmetool import deploy_acmetool
def _install_chatmaild() -> None: def _build_chatmaild(dist_dir) -> None:
chatmaild_filename = "chatmaild-0.1.tar.gz" dist_dir = Path(dist_dir).resolve()
chatmaild_path = importlib.resources.files(__package__).joinpath( if dist_dir.exists():
f"../../../dist/{chatmaild_filename}" shutil.rmtree(dist_dir)
) dist_dir.mkdir()
remote_path = f"/tmp/{chatmaild_filename}" subprocess.check_output(
if Path(str(chatmaild_path)).exists(): [sys.executable, "-m", "build", "-n"]
files.put( + ["--sdist", "chatmaild", "--outdir", str(dist_dir)]
name="Upload chatmaild source package",
src=chatmaild_path.open("rb"),
dest=remote_path,
) )
entries = list(dist_dir.iterdir())
assert len(entries) == 1
return entries[0]
def _install_remote_venv_with_chatmaild() -> None:
dist_file = _build_chatmaild(dist_dir=Path("chatmaild/dist"))
remote_base_dir = "/usr/local/lib/chatmaild"
remote_dist_file = f"{remote_base_dir}/dist/{dist_file.name}"
remote_venv_dir = f"{remote_base_dir}/venv"
root_owned = dict(user="root", group="root", mode="644")
apt.packages( apt.packages(
name="apt install python3-aiosmtpd python3-pip python3-venv", name="apt install python3-virtualenv",
packages=["python3-aiosmtpd", "python3-pip", "python3-venv"], packages=["python3-virtualenv"],
)
files.put(
name="Upload chatmaild source package",
src=dist_file.open("rb"),
dest=remote_dist_file,
create_remote_dir=True,
**root_owned,
)
pip.virtualenv(
name=f"chatmaild virtualenv {remote_venv_dir}",
path=remote_venv_dir,
always_copy=True,
) )
# --no-deps because aiosmtplib is installed with `apt`.
server.shell( server.shell(
name="install chatmaild with pip", name=f"forced pip-install {dist_file.name}",
commands=[f"pip install --break-system-packages {remote_path}"], commands=[
f"{remote_venv_dir}/bin/pip install --force-reinstall {remote_dist_file}"
],
) )
# disable legacy doveauth-dictproxy.service # disable legacy doveauth-dictproxy.service
@@ -51,15 +78,15 @@ def _install_chatmaild() -> None:
"doveauth", "doveauth",
"filtermail", "filtermail",
): ):
execpath = f"{remote_venv_dir}/bin/{fn}"
source_path = importlib.resources.files("chatmaild").joinpath(f"{fn}.service.f")
content = source_path.read_text().format(execpath=execpath).encode()
files.put( files.put(
name=f"Upload {fn}.service", name=f"Upload {fn}.service",
src=importlib.resources.files("chatmaild") src=io.BytesIO(content),
.joinpath(f"{fn}.service")
.open("rb"),
dest=f"/etc/systemd/system/{fn}.service", dest=f"/etc/systemd/system/{fn}.service",
user="root", **root_owned,
group="root",
mode="644",
) )
systemd.service( systemd.service(
name=f"Setup {fn} service", name=f"Setup {fn} service",
@@ -381,7 +408,7 @@ def deploy_chatmail(mail_domain: str, mail_server: str, dkim_selector: str) -> N
build_webpages(src_dir, build_dir, config) build_webpages(src_dir, build_dir, config)
files.rsync(f"{build_dir}/", "/var/www/html", flags=["-avz"]) files.rsync(f"{build_dir}/", "/var/www/html", flags=["-avz"])
_install_chatmaild() _install_remote_venv_with_chatmaild()
debug = False debug = False
dovecot_need_restart = _configure_dovecot(mail_server, debug=debug) dovecot_need_restart = _configure_dovecot(mail_server, debug=debug)
postfix_need_restart = _configure_postfix(mail_domain, debug=debug) postfix_need_restart = _configure_postfix(mail_domain, debug=debug)

View File

@@ -4,12 +4,5 @@ echo -----------------------------------------
echo deploying to $CHATMAIL_DOMAIN echo deploying to $CHATMAIL_DOMAIN
echo ----------------------------------------- echo -----------------------------------------
echo WARNING: in five seconds deploy to $CHATMAIL_DOMAIN starts
sleep 5
venv/bin/python3 -m build -n --sdist chatmaild --outdir dist
venv/bin/pyinfra --ssh-user root "$CHATMAIL_DOMAIN" \ venv/bin/pyinfra --ssh-user root "$CHATMAIL_DOMAIN" \
deploy-chatmail/src/deploy_chatmail/deploy.py deploy-chatmail/src/deploy_chatmail/deploy.py
rm -r dist/