bin/pvl.dns-includes
authorTero Marttila <tero.marttila@aalto.fi>
Fri, 27 Feb 2015 17:25:01 +0200
changeset 648 8e3e6be9ac70
parent 647 90a0790adf8a
child 649 88ff10dae514
bin/pvl.dns-includes
bin/pvl.dns-includes
bin/pvl.dns-process
lib/pvl/hosts/update.sh
lib/pvl/hosts/zone.sh
pvl/dns/process.py
pvl/dns/tests.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/pvl.dns-includes	Fri Feb 27 17:25:01 2015 +0200
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+
+"""
+    Extract a list of $INCLUDE paths from a zone file.
+"""
+
+import logging; log = logging.getLogger('pvl.dns-includes')
+import optparse
+import pvl.args
+import pvl.dns
+import pvl.dns.process
+
+def main (argv):
+    parser = optparse.OptionParser(main.__doc__)
+    parser.add_option_group(pvl.args.parser(parser))
+    parser.add_option_group(pvl.dns.process.optparser(parser))
+
+    parser.add_option('--include-path',         metavar='PATH',
+            help="Rewrite includes to given absolute path")
+
+    # input
+    options, args = pvl.args.parse(parser, argv)
+    
+    # process
+    zone = list(pvl.dns.process.apply_zone(options, args))
+
+    if options.include_path:
+        log.info("Set zone include path: %s", options.include_path)
+
+        zone = list(pvl.dns.process.zone_includes_path(zone, options.include_path))
+
+    for include in pvl.dns.process.zone_includes(zone):
+        # output include path only
+        print include
+
+    return 0
+
+if __name__ == '__main__':
+    pvl.args.main(main)
--- a/bin/pvl.dns-process	Fri Feb 27 17:13:18 2015 +0200
+++ b/bin/pvl.dns-process	Fri Feb 27 17:25:01 2015 +0200
@@ -37,7 +37,7 @@
     if options.include_path:
         log.info("Set zone include path: %s", options.include_path)
 
-        zone = list(pvl.dns.process.zone_includes(zone, options.include_path))
+        zone = list(pvl.dns.process.zone_includes_path(zone, options.include_path))
     
     pvl.dns.process.apply_zone_output(options, zone)
 
--- a/lib/pvl/hosts/update.sh	Fri Feb 27 17:13:18 2015 +0200
+++ b/lib/pvl/hosts/update.sh	Fri Feb 27 17:25:01 2015 +0200
@@ -2,6 +2,7 @@
 
 UPDATE_SERIAL=
 UPDATE_RELOAD=
+UPDATE_INCLUDES=
 
 . $LIB/pvl/hosts/dhcp.sh
 . $LIB/pvl/hosts/zone.sh
@@ -27,10 +28,12 @@
 
         r)  UPDATE_RELOAD=1 ;;
         R)  UPDATE_RELOAD=0 ;;
-               
+        
+        F)  UPDATE_INCLUDES=1 ;;
         n)
             UPDATE_SERIAL=0
             UPDATE_RELOAD=0
+            UPDATE_INCLUDES=0
         ;;
         *)  return 1
     esac
@@ -94,7 +97,9 @@
 
     log "Updating zones..."
     for zone in $(list_files etc/zones); do
-        zone_includes=$(zone_includes var/include-cache/$zone etc/zones/$zone var/zones/)
+        update_zone_includes "var/include-cache/$zone" "etc/zones/$zone"
+
+        zone_includes=$(cat "var/include-cache/$zone")
 
         update_zone_serial "var/serials/$zone" $serial \
             "etc/zones/$zone" $zone_includes
--- a/lib/pvl/hosts/zone.sh	Fri Feb 27 17:13:18 2015 +0200
+++ b/lib/pvl/hosts/zone.sh	Fri Feb 27 17:25:01 2015 +0200
@@ -64,37 +64,35 @@
     fi
 }
 
-## Read include paths from file
-function read_zone_includes {
-    cmd sed -n -E 's/^\$INCLUDE\s+"(.+)"/\1/p' "$@"
-}
 
-## (cached) include paths for zone file
-function zone_includes {
-    local cache="$1"
+## Update list of zone $INCLUDEs from zone file
+#
+#   update_zone_includes var/include-cache/$zone etc/zones/$zone
+#
+function update_zone_includes {
+    local out="$1"
     local src="$2"
-    local prefix="${3:-}"
+    local includes="$ZONES_INCLUDE"
 
-    if [ ! -e "$cache" -o "$cache" -ot "$src" ]; then
-        read_zone_includes "$src" > "$cache"
+    if [ "$UPDATE_INCLUDES" = 1 ]; then
+        log_force "$out: Force zone includes: $src"
+    
+    elif apply_check "$out" "$src"; then
+        log_skip "$out: Skip zone includes: $src"
+        
+        return
+
+    elif [ "$UPDATE_INCLUDES" = 0 ]; then
+        log_noop "$out: Noop zone includes: $src"
+        
+        return
+    else
+        log_apply "$out: Update zone includes: $src"
     fi
 
-    while read include; do
-        echo -n "$prefix$include "
-    done < "$cache"
-}
-
-## Search for prefix-matching includes in zone file
-function zone_includes_grep {
-    local cache="$1"
-    local src="$2"
-    local prefix="$3"
-    
-    for include in $(zone_includes $cache $src); do
-        if [ "${include#$prefix}" != "$include" ]; then
-            echo -n " ${include#$prefix}"
-        fi
-    done
+    apply_cmd "$out" $OPT/bin/pvl.dns-includes \
+            --include-path=$ZONES_INCLUDE \
+            "$src"
 }
 
 ## Update the cached .serial for the given zone, if the zone has changed:
@@ -138,6 +136,7 @@
     local out="$1"
     local src="$2"
     local serial="$3"
+    local includes="$4"
     local serial_opt=
     local msg="$out: Generate zone: $src"
 
--- a/pvl/dns/process.py	Fri Feb 27 17:13:18 2015 +0200
+++ b/pvl/dns/process.py	Fri Feb 27 17:25:01 2015 +0200
@@ -35,7 +35,7 @@
         else:
             yield rr
 
-def zone_includes (rrs, includes_path):
+def zone_includes_path (rrs, includes_path):
     """
         Rewrite include paths in zones.
     """
@@ -48,6 +48,17 @@
         else:
             yield rr
 
+def zone_includes (rrs):
+    """
+        Extract $INCLUDE paths from zone.
+    """
+
+    for rr in rrs:
+        if isinstance(rr, pvl.dns.ZoneDirective) and rr.directive == 'INCLUDE':
+            include_path, = rr.arguments
+
+            yield include_path
+
 def apply_zone_output (options, zone):
     """
         Output given ZoneDirective/ZoneRecord items to the output file/stdout.
--- a/pvl/dns/tests.py	Fri Feb 27 17:13:18 2015 +0200
+++ b/pvl/dns/tests.py	Fri Feb 27 17:25:01 2015 +0200
@@ -206,7 +206,7 @@
 """))
 
         rrs = list(process.zone_serial(rrs, 1337))
-        rrs = list(process.zone_includes(rrs, '...'))
+        rrs = list(process.zone_includes_path(rrs, '...'))
 
         self.assertZoneEqual(rrs, [
             "$TTL 3600",