run pyinfra command with environment variables, without inventory.py

This commit is contained in:
missytake
2023-10-12 17:00:20 +02:00
parent 65494b207d
commit 4b0a9448d6
4 changed files with 32 additions and 10 deletions

View File

@@ -12,3 +12,9 @@ Dovecot listens on ports 143(imap) and 993 (imaps).
## DNS
For DKIM you must add a DNS entry as in /etc/opendkim/selector.txt (where selector is the opendkim_selector configured in the chatmail inventory).
## Run with pyinfra
```
CHATMAIL_DOMAIN=c1.testrun.org pyinfra c1.testrun.org deploy.py
```

View File

@@ -2,4 +2,13 @@ import os
from pyinfra import host, facts
from chatmail import deploy_chatmail
deploy_chatmail()
def main():
mail_domain = os.getenv("CHATMAIL_DOMAIN")
mail_server = os.getenv("CHATMAIL_SERVER", mail_domain)
dkim_selector = os.getenv("CHATMAIL_DKIM_SELECTOR", "2023")
deploy_chatmail(mail_domain, mail_server, dkim_selector)
main()

View File

@@ -5,3 +5,6 @@ build-backend = "setuptools.build_meta"
[project]
name = "chatmail"
version = "0.1"
dependencies = [
"pyinfra",
]

View File

@@ -88,7 +88,7 @@ def _configure_postfix(domain: str) -> bool:
return need_restart
def _configure_dovecot(domain: str) -> bool:
def _configure_dovecot(mail_server: str) -> bool:
"""Configures Dovecot IMAP server."""
need_restart = False
@@ -98,7 +98,7 @@ def _configure_dovecot(domain: str) -> bool:
user="root",
group="root",
mode="644",
config={"hostname": domain},
config={"hostname": mail_server},
)
need_restart |= main_config.changed
@@ -115,9 +115,13 @@ def _configure_dovecot(domain: str) -> bool:
return need_restart
def deploy_chatmail() -> None:
domain = host.data.domain
dkim_selector = host.data.dkim_selector
def deploy_chatmail(mail_domain: str, mail_server: str, dkim_selector: str) -> None:
"""Deploy a chat-mail instance.
:param mail_domain: the domain part of your future email addresses, so "example.org" in user@example.org
:param mail_server: the DNS name under which your mail server is reachable
:param dkim_selector:
"""
apt.update(name="apt update")
server.group(name="Create vmail group", group="vmail", system=True)
@@ -132,7 +136,7 @@ def deploy_chatmail() -> None:
)
# Deploy acmetool to have TLS certificates.
deploy_acmetool(domains=[domain])
deploy_acmetool(domains=[mail_server])
apt.packages(
name="Install Postfix",
@@ -157,9 +161,9 @@ def deploy_chatmail() -> None:
)
_install_chatctl()
dovecot_need_restart = _configure_dovecot(domain)
postfix_need_restart = _configure_postfix(domain)
opendkim_need_restart = _configure_opendkim(domain, dkim_selector)
dovecot_need_restart = _configure_dovecot(mail_server)
postfix_need_restart = _configure_postfix(mail_domain)
opendkim_need_restart = _configure_opendkim(mail_domain, dkim_selector)
systemd.service(
name="Start and enable OpenDKIM",