cmdeploy: dns --zonefile subcommand to just print the zonefile

This commit is contained in:
missytake 2023-12-13 03:35:04 +01:00
parent 79f766b28e
commit fe675a9a72

View File

@ -92,6 +92,15 @@ def run_cmd(args, out):
out.check_call(cmd, env=env) out.check_call(cmd, env=env)
def dns_cmd_options(parser):
parser.add_argument(
"--zonefile",
dest="zonefile",
action="store_true",
help="print the whole zonefile for deploying directly",
)
def dns_cmd(args, out): def dns_cmd(args, out):
"""Generate dns zone file.""" """Generate dns zone file."""
template = importlib.resources.files(__package__).joinpath("chatmail.zone.f") template = importlib.resources.files(__package__).joinpath("chatmail.zone.f")
@ -115,7 +124,23 @@ def dns_cmd(args, out):
to_print = [] to_print = []
with open(template, "r") as f: with open(template, "r") as f:
for line in f.readlines(): zonefile = (
f.read()
.format(
acme_account_url=acme_account_url,
email=f"root@{args.config.mail_domain}",
sts_id=datetime.datetime.now().strftime("%Y%m%d%H%M"),
chatmail_domain=args.config.mail_domain,
dkim_entry=dkim_entry,
ipv6=ipv6,
)
.strip()
)
if args.zonefile:
print(zonefile)
return
started_dkim_parsing = False
for line in zonefile.splitlines():
line = line.format( line = line.format(
acme_account_url=acme_account_url, acme_account_url=acme_account_url,
email=f"root@{args.config.mail_domain}", email=f"root@{args.config.mail_domain}",
@ -128,24 +153,24 @@ def dns_cmd(args, out):
domain, typ, prio, value = line.split() domain, typ, prio, value = line.split()
current = dns.resolve_mx(domain[:-1]) current = dns.resolve_mx(domain[:-1])
if not current[0]: if not current[0]:
print(line) to_print.append(line)
elif current[1] != value: elif current[1] != value:
print(line.replace(prio, str(current[0] + 1))) print(line.replace(prio, str(current[0] + 1)))
if " SRV " in line: if " SRV " in line:
domain, typ, prio, weight, port, value = line.split() domain, typ, prio, weight, port, value = line.split()
current = dns.get("SRV", domain[:-1]) current = dns.get("SRV", domain[:-1])
if current != f"{prio} {weight} {port} {value}": if current != f"{prio} {weight} {port} {value}":
print(line) to_print.append(line)
if " AAAA " in line: if " AAAA " in line:
domain, value = line.split(" AAAA ") domain, value = line.split(" AAAA ")
current = dns.get("AAAA", domain.strip()[:-1]) current = dns.get("AAAA", domain.strip()[:-1])
if current != value: if current != value:
print(line) to_print.append(line)
if " CAA " in line: if " CAA " in line:
domain, value = line.split(" IN CAA ") domain, value = line.split(" IN CAA ")
current = dns.get("CAA", domain.strip()[:-1]) current = dns.get("CAA", domain.strip()[:-1])
if current != value: if current != value:
print(line) to_print.append(line)
if " TXT " in line: if " TXT " in line:
domain, value = line.split(" TXT ") domain, value = line.split(" TXT ")
current = dns.get("TXT", domain.strip()[:-1]) current = dns.get("TXT", domain.strip()[:-1])
@ -153,17 +178,18 @@ def dns_cmd(args, out):
if current.split("id=")[0] == value.split("id=")[0]: if current.split("id=")[0] == value.split("id=")[0]:
continue continue
if current != value: if current != value:
print(line) to_print.append(line)
if " IN TXT ( " in line: if " IN TXT ( " in line:
line += f.read() started_dkim_parsing = True
domain, data = line.split(" IN TXT ") dkim_lines = [line]
current = dns.get("TXT", domain.strip()[:-1]).replace('" "', '"\n "') if started_dkim_parsing and line.startswith('"'):
current = f"( {current} )" dkim_lines.append(" " + line)
if current.replace(";", "\\;") != data: domain, data = "\n".join(dkim_lines).split(" IN TXT ")
print( current = dns.get("TXT", domain.strip()[:-1]).replace('" "', '"\n "')
"wrong: '", current.replace(";", "\\;"), "'" current = f"( {current} )"
) if current.replace(";", "\\;") != data:
print("missing: '", data, "'") to_print.append("current: '" + current.replace(";", "\\;") + "'")
to_print.append("expected: '" + data + "'")
if to_print: if to_print:
to_print.insert( to_print.insert(
0, "\nYou should configure the following DNS entries at your provider:\n" 0, "\nYou should configure the following DNS entries at your provider:\n"