author | Tero Marttila <tero.marttila@aalto.fi> |
Mon, 02 Mar 2015 19:36:39 +0200 | |
changeset 695 | c60924eca185 |
parent 687 | f99f9e3d02cf |
child 735 | 008cfe47b194 |
permissions | -rwxr-xr-x |
458
600ad9eb6f25
pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
600ad9eb6f25
pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
2 |
|
475
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
3 |
import logging; log = logging.getLogger('pvl.hosts-forward') |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
4 |
import optparse |
516
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
5 |
import os.path |
475
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
6 |
import pvl.args |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
7 |
import pvl.hosts |
458
600ad9eb6f25
pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
8 |
import pvl.hosts.zone |
600ad9eb6f25
pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
9 |
|
475
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
10 |
def main (argv): |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
11 |
""" |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
12 |
Generate bind zonefiles from host definitions. |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
13 |
""" |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
14 |
|
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
15 |
parser = optparse.OptionParser(main.__doc__) |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
16 |
parser.add_option_group(pvl.args.parser(parser)) |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
17 |
parser.add_option_group(pvl.hosts.config.optparser(parser)) |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
18 |
|
516
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
19 |
parser.add_option('--zone-origin', metavar='DOMAIN', |
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
20 |
help="Generated records for given zone origin") |
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
21 |
|
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
22 |
parser.add_option('--root-zone', action='store_const', dest='zone_origin', const='.', |
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
23 |
help="Generate root zone") |
475
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
24 |
|
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
25 |
parser.add_option('--add-origin', action='store_true', |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
26 |
help="Include $ORIGIN directive in zone") |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
27 |
|
687
f99f9e3d02cf
pvl.hosts.zone: make non-CNAME check_conflicts optional
Tero Marttila <tero.marttila@aalto.fi>
parents:
670
diff
changeset
|
28 |
parser.add_option('--check-conflicts', action='store_true', |
f99f9e3d02cf
pvl.hosts.zone: make non-CNAME check_conflicts optional
Tero Marttila <tero.marttila@aalto.fi>
parents:
670
diff
changeset
|
29 |
help="Check for dupliate forward records, e.g. multiple A records for the same name") |
f99f9e3d02cf
pvl.hosts.zone: make non-CNAME check_conflicts optional
Tero Marttila <tero.marttila@aalto.fi>
parents:
670
diff
changeset
|
30 |
|
475
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
31 |
# input |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
32 |
options, args = pvl.args.parse(parser, argv) |
516
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
33 |
|
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
34 |
if options.zone_origin: |
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
35 |
origin = options.zone_origin |
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
36 |
|
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
37 |
log.info("using given zone origin: %s", origin) |
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
38 |
|
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
39 |
elif len(args) == 1: |
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
40 |
path, = args |
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
41 |
origin = os.path.basename(path.rstrip('/')) |
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
42 |
|
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
43 |
log.info("using given hostpath for zone origin: %s", origin) |
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
44 |
|
670
b95ad8c8bb4e
pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents:
516
diff
changeset
|
45 |
elif args: |
b95ad8c8bb4e
pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents:
516
diff
changeset
|
46 |
log.fatal("--zone-origin is required if passing multiple hostfiles") |
b95ad8c8bb4e
pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents:
516
diff
changeset
|
47 |
return 1 |
b95ad8c8bb4e
pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents:
516
diff
changeset
|
48 |
|
516
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
49 |
else: |
670
b95ad8c8bb4e
pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents:
516
diff
changeset
|
50 |
log.fatal("no hosts given as input") |
475
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
51 |
return 1 |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
52 |
|
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
53 |
hosts = pvl.hosts.apply(options, args) |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
54 |
|
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
55 |
# process |
487
920394061b6f
pvl.hosts.zone: HostZoneError(HostError), move resolve() to pvl.dns.relative(), and expect HostError's in pvl.hosts-forward/pvl.hosts-reverse
Tero Marttila <tero.marttila@aalto.fi>
parents:
475
diff
changeset
|
56 |
try: |
516
9615ffc647a0
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
489
diff
changeset
|
57 |
for rr in pvl.hosts.zone.apply_hosts_forward(hosts, origin, |
687
f99f9e3d02cf
pvl.hosts.zone: make non-CNAME check_conflicts optional
Tero Marttila <tero.marttila@aalto.fi>
parents:
670
diff
changeset
|
58 |
add_origin = options.add_origin, |
f99f9e3d02cf
pvl.hosts.zone: make non-CNAME check_conflicts optional
Tero Marttila <tero.marttila@aalto.fi>
parents:
670
diff
changeset
|
59 |
check_conflicts = options.check_conflicts, |
489
7f1bd12e0d54
pvl.hosts-reverse: move options out of pvl.hosts.zone
Tero Marttila <tero.marttila@aalto.fi>
parents:
487
diff
changeset
|
60 |
): |
487
920394061b6f
pvl.hosts.zone: HostZoneError(HostError), move resolve() to pvl.dns.relative(), and expect HostError's in pvl.hosts-forward/pvl.hosts-reverse
Tero Marttila <tero.marttila@aalto.fi>
parents:
475
diff
changeset
|
61 |
print unicode(rr) |
920394061b6f
pvl.hosts.zone: HostZoneError(HostError), move resolve() to pvl.dns.relative(), and expect HostError's in pvl.hosts-forward/pvl.hosts-reverse
Tero Marttila <tero.marttila@aalto.fi>
parents:
475
diff
changeset
|
62 |
except pvl.hosts.HostError as error: |
920394061b6f
pvl.hosts.zone: HostZoneError(HostError), move resolve() to pvl.dns.relative(), and expect HostError's in pvl.hosts-forward/pvl.hosts-reverse
Tero Marttila <tero.marttila@aalto.fi>
parents:
475
diff
changeset
|
63 |
log.error("%s", error) |
920394061b6f
pvl.hosts.zone: HostZoneError(HostError), move resolve() to pvl.dns.relative(), and expect HostError's in pvl.hosts-forward/pvl.hosts-reverse
Tero Marttila <tero.marttila@aalto.fi>
parents:
475
diff
changeset
|
64 |
return 3 |
920394061b6f
pvl.hosts.zone: HostZoneError(HostError), move resolve() to pvl.dns.relative(), and expect HostError's in pvl.hosts-forward/pvl.hosts-reverse
Tero Marttila <tero.marttila@aalto.fi>
parents:
475
diff
changeset
|
65 |
|
475
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
66 |
return 0 |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
67 |
|
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
68 |
if __name__ == '__main__': |
a76571e27c6f
pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents:
458
diff
changeset
|
69 |
pvl.args.main(main) |