From a4d39e8237d4ff6d2b27179365b5ccbc9fd7379c Mon Sep 17 00:00:00 2001 From: Jerry Date: Fri, 16 Apr 2021 14:43:50 +0800 Subject: [PATCH] fix update-zone-serial.py --- scripts/check-named-zones.sh | 2 +- scripts/generate-roa.sh | 6 ++-- scripts/update-zone-serial.py | 55 ++++++++++++++++++++--------------- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/scripts/check-named-zones.sh b/scripts/check-named-zones.sh index f4a9248..219869b 100755 --- a/scripts/check-named-zones.sh +++ b/scripts/check-named-zones.sh @@ -16,7 +16,7 @@ check() { PATH=/sbin:/usr/sbin:$PATH named-checkzone -i local -l 86400 $@ } -pushd dns +pushd generated/dns check 'neo' neonetwork check '127.10.in-addr.arpa' db.10.127 diff --git a/scripts/generate-roa.sh b/scripts/generate-roa.sh index 88b5fc1..98f703d 100755 --- a/scripts/generate-roa.sh +++ b/scripts/generate-roa.sh @@ -12,7 +12,9 @@ mkdir -p generated mkdir -p generated/dns scripts/dns-generator.py +scripts/update-zone-serial.py cp -R dns/* generated/dns +scripts/check-named-zones.sh scripts/roa.py -m "$MAX_LEN_4" -M "$MAX_LEN_6" -o generated/roa46_bird2.conf scripts/roa.py -m "$MAX_LEN_4" -M "$MAX_LEN_6" -4 -o generated/roa4_bird2.conf @@ -21,7 +23,3 @@ scripts/roa.py -m "$MAX_LEN_4" -M "$MAX_LEN_6" -j -o generated/roa46.json scripts/roa.py -m "$MAX_LEN_4" -M "$MAX_LEN_6" -e -o generated/neonetwork.json scripts/roa.py -m "$MAX_LEN_4" -M "$MAX_LEN_6" -r -o generated/rfc8416.json scripts/roa.py --summary --output generated/README.md - -scripts/update-zone-serial.py - -scripts/check-named-zones.sh diff --git a/scripts/update-zone-serial.py b/scripts/update-zone-serial.py index 2e599aa..7ee4aa9 100755 --- a/scripts/update-zone-serial.py +++ b/scripts/update-zone-serial.py @@ -5,8 +5,6 @@ from time import time from re import match from os import chdir -chdir("generated") - zone_files = [ 'neonetwork', 'db.10.127', @@ -14,28 +12,39 @@ zone_files = [ ] serial_base = 1586876035 +new_serial = int(time()) - serial_base + +def update_serial_to(zone: Path, serial: int = 0) -> int: + lines = zone.read_text().split("\n") + processed = list() + assert 0 <= serial <= 2**32 + found = False + old_serial = None + for line in lines: + if not found and (m := match(r"^(\s+)([0-9]+)(\s*;\s*Serial\s*)$", line)): + prefix, old_serial, suffix = m.groups() + old_serial = int(old_serial) + print(f"{old_serial=} {serial=}") + plen = max(len(prefix) - len(str(serial)) + len(str(old_serial)), 0) + processed.append(f"{' '*plen}{serial}{suffix}") + found = True + else: + processed.append(line) + if serial: + zone.write_text("\n".join(processed)) + return old_serial for zone in zone_files: - zone = Path("dns") / zone - assert zone.exists() - p = subprocess.run(['git', 'diff', '--exit-code', str(zone)]) + gen_zone = Path("generated") / "dns" / zone + repo_zone = Path("dns") / zone + assert gen_zone.exists() + assert repo_zone.exists() + old_serial = update_serial_to(gen_zone) + update_serial_to(repo_zone, old_serial) + gen_zone.write_text(repo_zone.read_text()) + p = subprocess.run(['git', 'diff', '--exit-code', gen_zone.name], cwd=gen_zone.parent) if p.returncode == 0: - print(f"skip {zone.name}") + print(f"skip {repo_zone.name}") else: - print(f"update serial {zone.name}") - lines = zone.read_text().split("\n") - processed = list() - serial = int(time()) - serial_base - assert 0 < serial <= 2**32 - serial = str(serial) - found = False - for line in lines: - if not found and (m := match(r"^(\s+)([0-9]+)(\s*;\s*Serial\s*)$", line)): - prefix, _serial, suffix = m.groups() - print(f"{_serial=} {serial=}") - plen = max(len(prefix) - len(serial) + len(_serial), 0) - processed.append(f"{' '*plen}{serial}{suffix}") - found = True - else: - processed.append(line) - zone.write_text("\n".join(processed)) + print(f"update serial {repo_zone.name}") + update_serial_to(repo_zone, new_serial)