runv-server

server tooling for runv.club
Log | Files | Refs | README

sync_member_email_aliases.py (1386B)


      1 #!/usr/bin/env python3
      2 """Aplica email-aliases.json no Postfix local (hash). Requer /etc/runv-member-mail.json."""
      3 
      4 from __future__ import annotations
      5 
      6 import argparse
      7 import os
      8 import sys
      9 from pathlib import Path
     10 
     11 _SCRIPT_DIR = Path(__file__).resolve().parent
     12 _REPO_TOOLS_LIB = _SCRIPT_DIR.parent.parent / "tools" / "lib"
     13 if str(_REPO_TOOLS_LIB) not in sys.path:
     14     sys.path.insert(0, str(_REPO_TOOLS_LIB))
     15 
     16 import runv_mail_sync as ms  # noqa: E402
     17 
     18 
     19 def require_root(dry_run: bool) -> None:
     20     if dry_run:
     21         return
     22     geteuid = getattr(os, "geteuid", None)
     23     if geteuid is None or geteuid() != 0:
     24         print("este script precisa ser executado como root (ou use --dry-run).", file=sys.stderr)
     25         raise SystemExit(1)
     26 
     27 
     28 def main() -> int:
     29     p = argparse.ArgumentParser(
     30         description="Sincroniza aliases aprovados com Postfix (MTA local, não Mailgun)."
     31     )
     32     p.add_argument("--dry-run", action="store_true", help="simular escrita e comandos")
     33     p.add_argument(
     34         "--config",
     35         default="",
     36         help=f"ficheiro JSON (predefinição: {ms.DEFAULT_CONFIG_PATH})",
     37     )
     38     args = p.parse_args()
     39     require_root(args.dry_run)
     40 
     41     cfg = None
     42     if args.config.strip():
     43         cfg = ms.load_config(Path(args.config.strip()))
     44 
     45     return ms.sync_mail(dry_run=args.dry_run, cfg=cfg)
     46 
     47 
     48 if __name__ == "__main__":
     49     raise SystemExit(main())