summaryrefslogtreecommitdiff
path: root/pkgs/increment-zone-serials/increment-zone-serials.py
blob: 394075a22033b7910be2e2ea45cc8dc522e4be68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3

import datetime, json, os.path, sys

root_dir = sys.argv[1]

serial_num = lambda n: f'{today}{n:02}'
today_str  = datetime.datetime.now(datetime.UTC).date().strftime('%Y%m%d')
today      = int(today_str)

def update_serial(*, zone_class, name, expected, serial):
    old_serial_int = serial
    new_serial = serial_num(0)

    if old_serial_int is not None:
        old_serial = str(old_serial_int)
        if len(old_serial) > len(new_serial):
            new_serial = None
        elif len(old_serial) == len(new_serial):
            old_day = int(old_serial[:len(today_str)]) 
            if old_day >= today:
                new_serial = None

        if new_serial is None:
            new_serial = str(old_serial_int + 1)

    path = os.path.join(root_dir, zone_class, name, 'serial.nix')
    with open(path, 'w') as serial_file:
        print(f'''\
{{
  config = {{
    soa.serial = {new_serial};
    nullSerialHash = "{expected}";
  }};
}}
''', file=serial_file)

    return path

paths = []

for zone_class, zones in json.load(sys.stdin).items():
    for zone in zones.values():
        paths.append(update_serial(**zone, zone_class=zone_class))

# Se imprime al final para evitar estados intermedios si algo tira excepción
for path in paths:
    print(path)