feat: helper script to create new records if they don't exist
This commit is contained in:
5 files changed
+198
-12
No files matched your search
+29
-1
@@ -132,6 +132,34 @@ def inwx_update(cfg, zone, name, rtype, ip):
|
||||
return True
|
||||
|
||||
|
||||
PLACEHOLDER = {"A": "192.0.2.1", "AAAA": "2001:db8::1"} # documentation addresses
|
||||
|
||||
|
||||
def zone_of(host, entry):
|
||||
"""INWX zone of a hostname: the host's `zone` setting, else its last two labels."""
|
||||
return entry.get("zone") or ".".join(host.split(".")[-2:])
|
||||
|
||||
|
||||
def ensure_record(cfg, zone, name, rtype, ttl=300):
|
||||
"""Create the record with a placeholder address if missing. Return (created, record)."""
|
||||
with inwx_session(cfg) as call:
|
||||
try:
|
||||
return False, find_record(call, zone, name, rtype)
|
||||
except NoHost:
|
||||
content = PLACEHOLDER[rtype]
|
||||
call(
|
||||
"nameserver.createRecord",
|
||||
{
|
||||
"domain": zone,
|
||||
"type": rtype,
|
||||
"name": name,
|
||||
"content": content,
|
||||
"ttl": ttl,
|
||||
},
|
||||
)
|
||||
return True, {"content": content, "ttl": ttl}
|
||||
|
||||
|
||||
# --- Request handling -------------------------------------------------------
|
||||
|
||||
|
||||
@@ -214,7 +242,7 @@ def handle(env, cfg, update=inwx_update, note=None):
|
||||
# shortcut: explicit myip/myipv6 are only syntax-checked (private ranges accepted), tighten if clients misbehave
|
||||
ips["A" if ip.version == 4 else "AAAA"] = str(ip)
|
||||
|
||||
zone = cfg["hosts"][host].get("zone") or ".".join(host.split(".")[-2:])
|
||||
zone = zone_of(host, cfg["hosts"][host])
|
||||
state_path = os.path.expanduser(cfg.get("state_file", DEFAULT_STATE))
|
||||
os.makedirs(os.path.dirname(state_path), exist_ok=True)
|
||||
|
||||
|
||||
Reference in new issue
Block a user