|
| 1 | +from pyinfra import facts, host |
| 2 | +from pyinfra.operations import apt, files, server, systemd |
| 3 | + |
| 4 | +from cmdeploy.basedeploy import ( |
| 5 | + Deployer, |
| 6 | + get_resource, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +class MtailDeployer(Deployer): |
| 11 | + def __init__(self, mtail_address): |
| 12 | + self.mtail_address = mtail_address |
| 13 | + |
| 14 | + def install(self): |
| 15 | + # Uninstall mtail package to install a static binary. |
| 16 | + apt.packages(name="Uninstall mtail", packages=["mtail"], present=False) |
| 17 | + |
| 18 | + (url, sha256sum) = { |
| 19 | + "x86_64": ( |
| 20 | + "https://github.com/google/mtail/releases/download/v3.0.8/mtail_3.0.8_linux_amd64.tar.gz", |
| 21 | + "123c2ee5f48c3eff12ebccee38befd2233d715da736000ccde49e3d5607724e4", |
| 22 | + ), |
| 23 | + "aarch64": ( |
| 24 | + "https://github.com/google/mtail/releases/download/v3.0.8/mtail_3.0.8_linux_arm64.tar.gz", |
| 25 | + "aa04811c0929b6754408676de520e050c45dddeb3401881888a092c9aea89cae", |
| 26 | + ), |
| 27 | + }[host.get_fact(facts.server.Arch)] |
| 28 | + |
| 29 | + server.shell( |
| 30 | + name="Download mtail", |
| 31 | + commands=[ |
| 32 | + 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)", |
| 33 | + "chmod 755 /usr/local/bin/mtail", |
| 34 | + ], |
| 35 | + ) |
| 36 | + |
| 37 | + def configure(self): |
| 38 | + # Using our own systemd unit instead of `/usr/lib/systemd/system/mtail.service`. |
| 39 | + # This allows to read from journalctl instead of log files. |
| 40 | + files.template( |
| 41 | + src=get_resource("mtail/mtail.service.j2"), |
| 42 | + dest="/etc/systemd/system/mtail.service", |
| 43 | + user="root", |
| 44 | + group="root", |
| 45 | + mode="644", |
| 46 | + address=self.mtail_address or "127.0.0.1", |
| 47 | + port=3903, |
| 48 | + ) |
| 49 | + |
| 50 | + mtail_conf = files.put( |
| 51 | + name="Mtail configuration", |
| 52 | + src=get_resource("mtail/delivered_mail.mtail"), |
| 53 | + dest="/etc/mtail/delivered_mail.mtail", |
| 54 | + user="root", |
| 55 | + group="root", |
| 56 | + mode="644", |
| 57 | + ) |
| 58 | + self.need_restart = mtail_conf.changed |
| 59 | + |
| 60 | + def activate(self): |
| 61 | + systemd.service( |
| 62 | + name="Start and enable mtail", |
| 63 | + service="mtail.service", |
| 64 | + running=bool(self.mtail_address), |
| 65 | + enabled=bool(self.mtail_address), |
| 66 | + restarted=self.need_restart, |
| 67 | + ) |
| 68 | + self.need_restart = False |
0 commit comments