cmdeploy init: show DNS entries required for deployment if not set

This commit is contained in:
missytake
2023-12-12 23:51:27 +01:00
parent 0ffe4d4996
commit 49848ec01e
2 changed files with 58 additions and 4 deletions

View File

@@ -15,6 +15,7 @@ from pathlib import Path
from termcolor import colored
from chatmaild.config import read_config, write_initial_config
import cmdeploy.dns
#
@@ -33,10 +34,29 @@ def init_cmd_options(parser):
def init_cmd(args, out):
"""Initialize chatmail config file."""
if args.inipath.exists():
out.red(f"Path exists, not modifying: {args.inipath}")
raise SystemExit(1)
write_initial_config(args.inipath, args.chatmail_domain)
out.green(f"created config file for {args.chatmail_domain} in {args.inipath}")
print(f"Path exists, not modifying: {args.inipath}")
else:
write_initial_config(args.inipath, args.chatmail_domain)
out.green(f"created config file for {args.chatmail_domain} in {args.inipath}")
ipaddress, _ = cmdeploy.dns.resolve(args.chatmail_domain)
mta_ipadress, _ = cmdeploy.dns.resolve("mta-sts." + args.chatmail_domain)
entries = 0
to_print = ["Now you should add %dnsentry% at your DNS provider:\n"]
if not ipaddress:
entries += 1
to_print.append(f"\tA\t{args.chatmail_domain}\t\t<your server's IPv4 address>")
if not mta_ipadress:
entries += 1
to_print.append(f"\tCNAME\tmta-sts.{args.chatmail_domain}\t{args.chatmail_domain}.")
if entries == 1:
singular = "this entry"
elif entries == 2:
singular = "these entries"
else:
return
to_print[0] = to_print[0].replace("%dnsentry%", singular)
for line in to_print:
print(line)
def run_cmd_options(parser):

View File

@@ -0,0 +1,34 @@
import requests
dns_types = {
"A": 1,
"AAAA": 28,
"CNAME": 5,
}
def resolve(domain: str) -> (str, str):
result, typ = get("A", domain), "A"
if not result:
result = get("CNAME", domain)
if result:
result, typ = get("A", result[:-1]), "A"
if not result:
result, typ = get("AAAA", domain), "AAAA"
return result, typ
def get(typ: str, domain: str) -> str:
"""Get a DNS entry"""
url = "https://dns.nextdns.io/dns-query"
r = requests.get(
url,
params={"name": domain, "type": typ},
headers={"accept": "application/dns-json"},
)
j = r.json()
if "Answer" in j:
for answer in j["Answer"]:
if answer["type"] == dns_types[typ]:
return answer["data"]