--- a/bin/pvl.hosts-import Tue Dec 17 10:25:44 2013 +0200
+++ b/bin/pvl.hosts-import Tue Dec 17 10:44:14 2013 +0200
@@ -9,11 +9,12 @@
import pvl.dhcp.config
import pvl.ldap.args
+import collections
import ipaddr
+import logging; log = logging.getLogger('pvl.hosts-import')
import optparse
-import collections
+import os.path
import re
-import logging; log = logging.getLogger('pvl.hosts-import')
__version__ = '0.1'
@@ -45,11 +46,8 @@
parser.add_option('--import-zone-hosts', metavar='FILE', action='append',
help="Load hosts from DNS zone")
- parser.add_option('--import-dhcp-hosts', metavar='FILE', action='append',
- help="Load hosts from DHCP config")
-
- parser.add_option('--import-dhcp-boot-server', metavar='NEXT-SERVER',
- help="Default boot_server for dpc hosts")
+ parser.add_option('--import-zone-origin', metavar='ORIGIN',
+ help="Initial origin for given zone file; default is basename")
parser.add_option('--import-zone-comments-owner', action='store_const',
dest='import_zone_comments', const='owner',
@@ -59,6 +57,13 @@
dest='import_zone_comments', const='host',
help="Import DNS zone comment as host comment")
+ parser.add_option('--import-dhcp-hosts', metavar='FILE', action='append',
+ help="Load hosts from DHCP config")
+
+ parser.add_option('--import-dhcp-boot-server', metavar='NEXT-SERVER',
+ help="Default boot_server for dpc hosts")
+
+
parser.add_option('--dump-host-comments', action='store_true',
help="Dump out info on imported host comments")
@@ -90,15 +95,44 @@
return options, args
+def import_zone_host_name (options, name, origin) :
+ """
+ Import zone name from rr
+ """
+
+ if '.' in name :
+ host, domain = name.split('.', 1)
+ domain = pvl.dns.zone.join(domain, origin)
+ else :
+ host = name
+ domain = origin
+
+ if domain :
+ # not a fqdn
+ domain = domain.rstrip('.')
+
+ log.info("%s: %s@%s", name, host, domain)
+ else :
+ log.warn("%s: no domain", name)
+
+ return host, domain
+
def import_zone_hosts (options, file) :
"""
Yield host info from zonefile records.
"""
+ origin = options.import_zone_origin or os.path.basename(file.name)
+
for rr in pvl.dns.zone.ZoneRecord.load(file,
+ # used to determine domain
+ origin = origin,
+
# generated hosts need to imported by hand...
expand_generate = False,
) :
+ host, domain = import_zone_host_name(options, rr.name, rr.origin)
+
if options.zone_unused and rr.name == options.zone_unused :
log.debug("%s: skip %s", rr.name, rr)
continue
@@ -108,19 +142,21 @@
type = { 'A': 'ip', 'AAAA': 'ip6' }[rr.type]
- yield rr.name, type, ipaddr.IPAddress(ip)
+ yield host, 'domain', domain
+ yield host, type, ipaddr.IPAddress(ip)
if rr.comment :
- yield rr.name, 'comment', rr.comment
+ yield host, 'comment', rr.comment
- if rr.origin :
- # not a fqdn
- yield rr.name, 'domain', rr.origin.rstrip('.')
elif rr.type == 'CNAME' :
- host, = rr.data
-
- yield host, 'alias', rr.name
+ alias, = rr.data
+ alias_host, alias_domain = import_zone_host_name(options, alias, rr.origin)
+
+ if domain == alias_domain :
+ yield alias_host, 'alias', host
+ else :
+ yield alias_host, 'alias', pvl.dns.zone.join(host, domain)
elif rr.type == 'TXT' :
txt, = rr.data
@@ -128,7 +164,7 @@
yield host, 'comment', txt
else :
- log.warn("%s: unknown rr: %s", rr.name, rr)
+ log.warn("%s: unknown rr: %s", host, rr)
def import_dhcp_host (options, host, items) :
"""