From d1788b7c6543a86d91cbd83564f1eb8c8ff03e2a Mon Sep 17 00:00:00 2001 From: cliffmccarthy <16453869+cliffmccarthy@users.noreply.github.com> Date: Mon, 8 Sep 2025 08:39:59 -0500 Subject: [PATCH] refactor: Add MtailDeployer - This splits the existing deploy_mtail() routine into methods for the install, configure, and activate stages. --- cmdeploy/src/cmdeploy/__init__.py | 114 +++++++++++++++++------------- 1 file changed, 65 insertions(+), 49 deletions(-) diff --git a/cmdeploy/src/cmdeploy/__init__.py b/cmdeploy/src/cmdeploy/__init__.py index aae202ca..359b7ba6 100644 --- a/cmdeploy/src/cmdeploy/__init__.py +++ b/cmdeploy/src/cmdeploy/__init__.py @@ -747,59 +747,71 @@ def deploy_turn_server(config): ) -def deploy_mtail(config): - # Uninstall mtail package, we are going to install a static binary. - apt.packages(name="Uninstall mtail", packages=["mtail"], present=False) +class MtailDeployer(Deployer): + def __init__(self, *, mtail_address, **kwargs): + super().__init__(**kwargs) + self.mtail_address = mtail_address - (url, sha256sum) = { - "x86_64": ( - "https://github.com/google/mtail/releases/download/v3.0.8/mtail_3.0.8_linux_amd64.tar.gz", - "123c2ee5f48c3eff12ebccee38befd2233d715da736000ccde49e3d5607724e4", - ), - "aarch64": ( - "https://github.com/google/mtail/releases/download/v3.0.8/mtail_3.0.8_linux_arm64.tar.gz", - "aa04811c0929b6754408676de520e050c45dddeb3401881888a092c9aea89cae", - ), - }[host.get_fact(facts.server.Arch)] + @staticmethod + def install_impl(): + # Uninstall mtail package, we are going to install a static binary. + apt.packages(name="Uninstall mtail", packages=["mtail"], present=False) - server.shell( - name="Download mtail", - commands=[ - f"(echo '{sha256sum} /usr/local/bin/mtail' | sha256sum -c) || (curl -L {url} | gunzip | tar -x -f - mtail -O >/usr/local/bin/mtail.new && mv /usr/local/bin/mtail.new /usr/local/bin/mtail)", - "chmod 755 /usr/local/bin/mtail", - ], - ) + (url, sha256sum) = { + "x86_64": ( + "https://github.com/google/mtail/releases/download/v3.0.8/mtail_3.0.8_linux_amd64.tar.gz", + "123c2ee5f48c3eff12ebccee38befd2233d715da736000ccde49e3d5607724e4", + ), + "aarch64": ( + "https://github.com/google/mtail/releases/download/v3.0.8/mtail_3.0.8_linux_arm64.tar.gz", + "aa04811c0929b6754408676de520e050c45dddeb3401881888a092c9aea89cae", + ), + }[host.get_fact(facts.server.Arch)] - # Using our own systemd unit instead of `/usr/lib/systemd/system/mtail.service`. - # This allows to read from journalctl instead of log files. - files.template( - src=importlib.resources.files(__package__).joinpath("mtail/mtail.service.j2"), - dest="/etc/systemd/system/mtail.service", - user="root", - group="root", - mode="644", - address=config.mtail_address or "127.0.0.1", - port=3903, - ) + server.shell( + name="Download mtail", + commands=[ + f"(echo '{sha256sum} /usr/local/bin/mtail' | sha256sum -c) || (curl -L {url} | gunzip | tar -x -f - mtail -O >/usr/local/bin/mtail.new && mv /usr/local/bin/mtail.new /usr/local/bin/mtail)", + "chmod 755 /usr/local/bin/mtail", + ], + ) - mtail_conf = files.put( - name="Mtail configuration", - src=importlib.resources.files(__package__).joinpath( - "mtail/delivered_mail.mtail" - ), - dest="/etc/mtail/delivered_mail.mtail", - user="root", - group="root", - mode="644", - ) + def configure_impl(self): + # Using our own systemd unit instead of `/usr/lib/systemd/system/mtail.service`. + # This allows to read from journalctl instead of log files. + files.template( + src=importlib.resources.files(__package__).joinpath( + "mtail/mtail.service.j2" + ), + dest="/etc/systemd/system/mtail.service", + user="root", + group="root", + mode="644", + address=self.mtail_address or "127.0.0.1", + port=3903, + ) - systemd.service( - name="Start and enable mtail", - service="mtail.service", - running=bool(config.mtail_address), - enabled=bool(config.mtail_address), - restarted=mtail_conf.changed, - ) + mtail_conf = files.put( + name="Mtail configuration", + src=importlib.resources.files(__package__).joinpath( + "mtail/delivered_mail.mtail" + ), + dest="/etc/mtail/delivered_mail.mtail", + user="root", + group="root", + mode="644", + ) + self.need_restart = mtail_conf.changed + + def activate_impl(self): + systemd.service( + name="Start and enable mtail", + service="mtail.service", + running=bool(self.mtail_address), + enabled=bool(self.mtail_address), + restarted=self.need_restart, + ) + self.need_restart = False class IrohDeployer(Deployer): @@ -931,6 +943,7 @@ def deploy_chatmail(config_path: Path, disable_mail: bool) -> None: nginx_deployer = NginxDeployer(config=config) journald_deployer = JournaldDeployer() + mtail_deployer = MtailDeployer(mtail_address=config.mtail_address) all_deployers = [ unbound_deployer, @@ -941,6 +954,7 @@ def deploy_chatmail(config_path: Path, disable_mail: bool) -> None: postfix_deployer, nginx_deployer, journald_deployer, + mtail_deployer, ] # @@ -1116,4 +1130,6 @@ def deploy_chatmail(config_path: Path, disable_mail: bool) -> None: mode="700", ) - deploy_mtail(config) + mtail_deployer.install() + mtail_deployer.configure() + mtail_deployer.activate()