feat: helper script to create new records if they don't exist

This commit is contained in:
mxmehl committed 2026-10-22 14:27:54 +02:00
1 parent af8fe5e4c0
commit 835f1642d9
5 files changed
+198 -12

No files matched your search

+51 -3
View File
@@ -5,6 +5,7 @@
Generates a random token, stores only its SHA-256 in the config and prints the
token once. The token is what the client needs; the server never sees it again.
Also makes sure the DNS record exists at INWX, which needs the server's IP.
"""
import argparse
@@ -16,7 +17,7 @@ import sys
import tempfile
import tomllib
from snap_dyndns import DEFAULT_CONFIG
from snap_dyndns import DEFAULT_CONFIG, InwxError, ensure_record, zone_of
NAME = re.compile(r"^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$")
@@ -90,6 +91,23 @@ def main(argv=None):
action="store_true",
help="replace the token of an existing host without asking",
)
parser.add_argument(
"-t",
"--types",
default="A",
help="record types to check and create, A or AAAA or A,AAAA (default: A)",
)
parser.add_argument(
"--ttl",
type=int,
default=300,
help="TTL of records created at INWX (default: 300)",
)
parser.add_argument(
"--no-dns",
action="store_true",
help="do not touch INWX, e.g. when not running on the server",
)
parser.add_argument(
"-z",
"--zone",
@@ -101,6 +119,9 @@ def main(argv=None):
for value in (host, args.zone):
if value and not NAME.match(value):
sys.exit(f"invalid name: {value}")
types = [t.strip().upper() for t in args.types.split(",")]
if not set(types) <= {"A", "AAAA"}:
sys.exit(f"invalid record types: {args.types}")
path = os.path.expanduser(args.config)
token = secrets.token_urlsafe(32)
@@ -116,11 +137,38 @@ def main(argv=None):
):
sys.exit("Aborted, config unchanged")
new_text, created = set_host(text, host, digest, args.zone)
tomllib.loads(new_text) # never write a config that does not parse
write_config(path, new_text)
cfg = tomllib.loads(new_text) # never write a config that does not parse
except (OSError, ValueError) as e: # TOMLDecodeError is a ValueError
sys.exit(f"{path}: {e}")
# DNS first: if INWX is not reachable, nothing has been changed yet
if not args.no_dns:
zone = zone_of(host, cfg["hosts"][host])
try:
for rtype in types:
was_created, record = ensure_record(cfg, zone, host, rtype, args.ttl)
if was_created:
print(
f"DNS: created {rtype} record {host} ({record['content']} is a placeholder, "
f"TTL {record['ttl']}), the first update sets the real address"
)
else:
print(
f"DNS: {rtype} record {host} exists "
f"({record['content']}, TTL {record.get('ttl')})"
)
except (InwxError, OSError, KeyError, ValueError) as e:
sys.exit(
f"INWX: {type(e).__name__}: {e}\nNothing was changed in the config. The INWX "
"account is usually restricted to the server's IP, so run this there, "
"or use --no-dns."
)
try:
write_config(path, new_text)
except OSError as e:
sys.exit(f"{path}: {e}")
print(f"{'Added' if created else 'Replaced the token of'} {host} in {path}")
print("The server reads the config on every request, no restart needed.")
print(f"Token (shown only once): {token}")