Files
compras/scripts/configure-authorized-public-mail.py
T
nala 47649bd46c
tests / ci (push) Canceled after 0s
commit compras1 3011
2026-11-30 09:33:36 +02:00

37 lines
1.7 KiB
Python

"""Configure the existing public MTA for Compras and send the owner's one authorised test."""
from pathlib import Path
import json
import os
import shutil
import subprocess
root = Path('/var/www/compras-deploy/current').resolve()
assert root == Path('/var/www/compras-deploy/releases/presentation-20261129')
env = (root/'.env').resolve()
assert env == Path('/var/www/compras-deploy/demonstration-20261129/.env')
assert subprocess.check_output(['systemctl', 'is-active', 'postfix']).decode().strip() == 'active'
assert Path('/usr/sbin/sendmail').is_file()
backup = Path('/var/backups/compras-presentation-20261129/mail-configuration')
backup.mkdir(mode=0o700, exist_ok=False)
shutil.copy2(env, backup/'selected.env.private')
(backup/'selected.env.private').chmod(0o600)
updates = {'MAIL_MAILER': 'sendmail', 'MAIL_SENDMAIL_PATH': '/usr/sbin/sendmail -bs -i',
'MAIL_FROM_ADDRESS': 'nala@tempo.co.mz', 'MAIL_FROM_NAME': 'Compras'}
lines = env.read_text().splitlines()
for key, value in updates.items():
matching = [i for i, line in enumerate(lines) if line.startswith(key+'=')]
assert len(matching) <= 1
rendered = key+'='+json.dumps(value)
if matching:
lines[matching[0]] = rendered
else:
lines.append(rendered)
attrs = env.stat()
temp = env.with_name(env.name+'.mail-next')
temp.write_text('\n'.join(lines)+'\n')
temp.chmod(attrs.st_mode & 0o777)
os.chown(temp, attrs.st_uid, attrs.st_gid)
os.replace(temp, env)
subprocess.run(['runuser', '-u', 'www-data', '--', '/usr/bin/php', 'artisan', 'config:cache'], cwd=root, check=True)
subprocess.run(['/usr/bin/php', str(root/'scripts/send-authorized-mail-check.php'), '--send-authorized-test'], cwd=root, check=True)