pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
authorTero Marttila <tero.marttila@aalto.fi>
Thu, 26 Feb 2015 18:58:32 +0200
changeset 516 9615ffc647a0
parent 515 996ccf5356b8
child 517 8474e32c648a
pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
README
bin/pvl.hosts-forward
--- a/README	Thu Feb 26 18:54:15 2015 +0200
+++ b/README	Thu Feb 26 18:58:32 2015 +0200
@@ -17,7 +17,7 @@
 
 The domain name for a host is determined from the basename of the config file, so this example file would generate something like the following output for use in a `zone "test" { ... }` zonefile:
     
-    $ bin/pvl.hosts-forward --forward-zone example.com etc/hosts/example.com 
+    $ bin/pvl.hosts-forward etc/hosts/example.com 
     foo                               A     192.0.2.1
     bar                               A     192.0.2.2
 
@@ -54,7 +54,7 @@
     $ cat etc/hosts/test/test.d/bar 
     ip = 192.0.2.2
 
-    $ bin/pvl.hosts-forward --forward-zone test etc/hosts/test/test
+    $ bin/pvl.hosts-forward etc/hosts/test/test
     foo                               A     192.0.2.1
     bar                               A     192.0.2.2
 
@@ -67,7 +67,7 @@
     [quux]
         ip  = 192.0.2.5
 
-    $ bin/pvl.hosts-forward --forward-zone test etc/hosts/test/
+    $ bin/pvl.hosts-forward etc/hosts/test/
     foo                               A     192.0.2.1
     bar                               A     192.0.2.2
     quux.asdf                         A     192.0.2.5
@@ -83,8 +83,10 @@
     [host]
         ip  = 192.0.2.6
 
-    $ bin/pvl.hosts-forward --forward-zone test etc/hosts/wrong.test 
-    host.host.wrong                   A     192.0.2.6
+Using the --root-zone option to generate the full FQDN for the host:
+
+    $ bin/pvl.hosts-forward --root-zone etc/hosts/wrong.test 
+    host.host.wrong.test              A     192.0.2.6
 
 === Host aliases ===
 Hosts can specify DNS aliases:
--- a/bin/pvl.hosts-forward	Thu Feb 26 18:54:15 2015 +0200
+++ b/bin/pvl.hosts-forward	Thu Feb 26 18:58:32 2015 +0200
@@ -2,6 +2,7 @@
 
 import logging; log = logging.getLogger('pvl.hosts-forward')
 import optparse 
+import os.path
 import pvl.args
 import pvl.hosts
 import pvl.hosts.zone
@@ -15,24 +16,38 @@
     parser.add_option_group(pvl.args.parser(parser))
     parser.add_option_group(pvl.hosts.config.optparser(parser))
 
-    parser.add_option('--forward-zone',         metavar='DOMAIN',
-            help="Generate forward zone for domain")
+    parser.add_option('--zone-origin',          metavar='DOMAIN',
+            help="Generated records for given zone origin")
+
+    parser.add_option('--root-zone',            action='store_const', dest='zone_origin', const='.',
+            help="Generate root zone")
     
     parser.add_option('--add-origin',           action='store_true',
             help="Include $ORIGIN directive in zone")
 
     # input
     options, args = pvl.args.parse(parser, argv)
-    
-    if not options.forward_zone:
-        log.fatal("required --forward-zone")
+ 
+    if options.zone_origin:
+        origin = options.zone_origin
+        
+        log.info("using given zone origin: %s", origin)
+   
+    elif len(args) == 1:
+        path, = args
+        origin = os.path.basename(path.rstrip('/'))
+
+        log.info("using given hostpath for zone origin: %s", origin)
+
+    else:
+        log.fatal("--zone-origin is required if passing multiple hostfiles")
         return 1
     
     hosts = pvl.hosts.apply(options, args)
 
     # process
     try:
-        for rr in pvl.hosts.zone.apply_hosts_forward(hosts, options.forward_zone,
+        for rr in pvl.hosts.zone.apply_hosts_forward(hosts, origin,
                 add_origin  = options.add_origin,
         ):
             print unicode(rr)