update: many things dns-new
authorTero Marttila <terom@paivola.fi>
Tue, 17 Dec 2013 02:06:53 +0200
branchdns-new
changeset 608 4ad9c9b7cd0e
parent 607 77df429f63a3
child 609 2fb003b8ac4c
update: many things
bin/update
lib/update.args
lib/update.log
lib/update.operations
lib/update.updates
lib/update.utils
--- a/bin/update	Tue Dec 17 01:17:49 2013 +0200
+++ b/bin/update	Tue Dec 17 02:06:53 2013 +0200
@@ -49,41 +49,49 @@
 
     log "Updating forward host zones..."
     for hosts in $(list_files etc/hosts); do
-        update_hosts_forward    var/zones/hosts/$hosts              $hosts \
-            etc/hosts/$hosts
+        update_hosts_forward    "var/zones/hosts/$hosts"            "$hosts" \
+            "etc/hosts/$hosts"
     done
 
     log "Updating DHCP hosts..."
     for hosts in $(list_files etc/hosts); do
-        update_hosts_dhcp       var/dhcp/$hosts.conf                \
-            etc/hosts/$hosts
+        update_hosts_dhcp       "var/dhcp/$hosts.conf"              \
+            "etc/hosts/$hosts"
     done
 
     log "Updating reverse host zones..."
         update_hosts_reverse    var/zones/hosts/194.197.235         194.197.235.0/24 \
-            etc/hosts/paivola.fi etc/hosts/*.paivola.fi
+            "etc/hosts/paivola.fi" etc/hosts/*.paivola.fi
+
+         update_hosts_reverse    var/zones/hosts/10                 10.0.0.0/8 \
+            "etc/hosts/*.pvl"
         
         update_hosts_reverse    var/zones/hosts/10.0                10.0.0.0/16 \
-            etc/hosts/test.pvl
-        
+            "etc/hosts/test.pvl"
+
+        update_hosts_reverse    var/zones/hosts/192.168             192.168.0.0/16 \
+            "etc/hosts/*.pvl"
+       
         update_hosts_reverse    var/zones/hosts/fdc4:4cef:395a      fdc4:4cef:395a::/48 \
-            etc/hosts/test.pvl
+            "etc/hosts/test.pvl"
 }
 
 function run_zones {
     log "Copying zone includes..."
     for zone in $(list_files etc/zones/includes); do
-        copy                var/zones/includes/$zone        etc/zones/includes/$zone
+        copy                "var/zones/includes/$zone"      "etc/zones/includes/$zone"
     done
 
     log "Updating zone serials..."
     for zone in $(list_files etc/zones); do
-        update_serial       var/serials/$zone               etc/zones/$zone 
+        update_serial       "var/serials/$zone"             "etc/zones/$zone" \
+            $(zone_includes var/include-cache/$zone etc/zones/$zone var/zones)
+
     done
 
     log "Updating zones..."
     for zone in $(list_files etc/zones); do
-        update_zone         var/zones/$zone                 etc/zones/$zone         var/serials/$zone
+        update_zone         "var/zones/$zone"               "etc/zones/$zone"       "var/serials/$zone"
     done
 
     log "Testing zones..."
@@ -114,7 +122,7 @@
     done
     
     ## Output dirs
-    for dir in var var/dhcp var/zones var/serials; do
+    for dir in var var/dhcp var/zones var/serials var/include-cache; do
         ensure_dir  $dir
     done
     
--- a/lib/update.args	Tue Dec 17 01:17:49 2013 +0200
+++ b/lib/update.args	Tue Dec 17 02:06:53 2013 +0200
@@ -109,7 +109,7 @@
                 UPDATE_NOOP=y 
                 # implies -Sp
                 UPDATE_DIFF=y
-                SERIAL_NOUPDATE=y
+                SERIAL_NOOP=y
                 COMMIT_SKIP=y
                 RELOAD_NOOP=y
                 ;;
--- a/lib/update.log	Tue Dec 17 01:17:49 2013 +0200
+++ b/lib/update.log	Tue Dec 17 02:06:53 2013 +0200
@@ -83,6 +83,10 @@
     log_debug "$prefix: $*"
 }
 
+function warn {
+    log_warn "$(func_caller): $*"
+}
+
 # Log with func_caller at log_error and exit, intended for internal errors...
 function fail {
     log_error "$(func_caller): $*"
--- a/lib/update.operations	Tue Dec 17 01:17:49 2013 +0200
+++ b/lib/update.operations	Tue Dec 17 02:06:53 2013 +0200
@@ -4,13 +4,13 @@
 # Operations on zonefiles/hosts/whatever
 
 function link {
-    local out=$1
-    local tgt=$2
+    local out="$1"
+    local tgt="$2"
 
-    if check_link $out $tgt; then
+    if check_link "$out" "$tgt"; then
         log_update "Linking $out -> $tgt..."
 
-        do_link $out $tgt
+        do_link "$out" "$tgt"
 
     else
         log_skip "Linking $out -> $tgt: not changed"
@@ -18,14 +18,14 @@
 }
 
 function copy {
-    local out=$1
-    local src=$2
+    local out="$1"
+    local src="$2"
 
-    if check_update $out $src; then
+    if check_update "$out" "$src"; then
         log_update "Copying $out <- $src..."
 
-        do_update $out \
-            cat $src
+        do_update "$out" \
+            cat "$src"
     else
         log_skip "Copying $out <- $src: not changed"
     fi
@@ -36,53 +36,36 @@
 #   check    $src    $cmd $args...
 #
 function check {
-    local src=$1; shift
-    local cmd=$1; shift
+    local src="$1"; shift
+    local cmd="$1"; shift
 
-    if cmd_test $cmd -q "$@"; then
+    if cmd_test "$cmd" -q "$@"; then
         log_skip    "Check $src: OK"
 
     else
         log_error   "  Check $src: Failed"
 
-        indent "    " $cmd "$@"
+        indent "    " "$cmd" "$@"
 
         exit 1
     fi
 }
 
-## Hosts
-## Update hosts from verbatim from input zone data:
-#
-#   copy_hosts      $ZONES/$zone    $DATA/$base
-#
-# Writes updated zone to $zone, deps on $base.
-function copy_hosts {
-    local zone=$1
-    local base=$2
-
-    # XXX: filenames given directly
-    local out=$zone
-    local src=$base
-
-    copy $out $src
-}
-
 ## Generate forward zone from hosts hosts using pvl.hosts-dns:
 #
 #   update_hosts_forward out/hosts/$hosts $hosts in/hosts/$hosts
 function update_hosts_forward {
-    local out=$1; shift
-    local domain=$1; shift
-    local src=$1; shift
+    local out="$1"; shift
+    local domain="$1"; shift
+    local src="$1"; shift
 
-    if check_update $out $src "$@"; then
+    if check_update "$out" "$src" "$@"; then
         log_update "Generating forward hosts zone $out <- $src..."
     
-        do_update $out $OPT/bin/pvl.hosts-dns \
+        do_update "$out" $OPT/bin/pvl.hosts-dns \
             --hosts-charset=$CHARSET \
-            --forward-zone=$domain \
-             $src "$@"
+            --forward-zone="$domain" \
+             "$src" "$@"
     
     else
         log_skip "Generating forward hosts $out <- $src: not changed"
@@ -108,17 +91,17 @@
 #
 #   update_hosts_reverse out/hosts/$reverse $reverse in/hosts/$hosts
 function update_hosts_reverse {
-    local out=$1; shift
-    local reverse=$1; shift
-    local src=$1; shift
+    local out="$1"; shift
+    local reverse="$1"; shift
+    local src="$1"; shift
 
-    if check_update $out $src "$@"; then
+    if check_update "$out" "$src" "$@"; then
         log_update "Generating reverse hosts zone $out <- $src..."
     
-        do_update $out $OPT/bin/pvl.hosts-dns \
+        do_update "$out" $OPT/bin/pvl.hosts-dns \
             --hosts-charset=$CHARSET \
-            --reverse-zone=$reverse \
-            $src "$@"
+            --reverse-zone="$reverse" \
+            "$src" "$@"
     
     else
         log_skip "Generating reverse hosts $out <- $src: not changed"
@@ -131,15 +114,15 @@
 #
 # Shows old/new serial on debug.
 function do_update_serial {
-    local serial=$1
+    local serial="$1"
 
     # read
-    local old=$(test -e $serial && cat $serial || echo '')
+    local old=$(test -e "$serial" && cat "$serial" || echo '')
 
-    cmd $OPT/bin/pvl.dns-serial $serial
+    cmd $OPT/bin/pvl.dns-serial "$serial"
     
     # read
-    local new=$(cat $serial)
+    local new=$(cat "$serial")
         
     debug "  $old -> $new"
 }
@@ -152,15 +135,15 @@
 # Supports SERIAL_FORCE/NOOP.
 # Updates $SERIALS/$zone.serial.
 function update_serial {
-    local serial=$1; shift
+    local serial="$1"; shift
     
     # test
     if [ $SERIAL_FORCE ]; then
         log_force "Updating $serial: forced"
 
-        do_update_serial $serial
+        do_update_serial "$serial"
 
-    elif ! check_update $serial "$@"; then
+    elif ! check_update "$serial" "$@"; then
         log_skip "Updating $serial: not changed"
 
     elif [ $SERIAL_NOOP ]; then
@@ -169,113 +152,28 @@
     else
         log_update "Updating $serial..."
 
-        do_update_serial $serial
+        do_update_serial "$serial"
     fi
 }
 
-## Link serial for zone from given base-zone:
+## Generate zone file from source using pvl.dns-zone:
 #
-#   link_serial $zone $base
-function link_serial {
-    local zone=$1
-    local base=$2
-
-    local out=$SERIALS/$zone.serial
-    local tgt=$SERIALS/$base.serial
-
-    link $out $tgt
-}
-
-## Update zone file verbatim from source:
-#
-#   copy_zone   $view   $zone   [$base]
-#
-# Copies changed $DATA/$base zone data to $ZONES/$view/$zone.
-function copy_zone {
-    local view=$1
-    local zone=$2
-    local base=${3:-$zone}
-
-    local out=$ZONES/$view/$zone
-    local src=$DATA/$base
+#   update_zone out/zones/$zone in/zones/$zone var/serials/$zone
+function update_zone {
+    local out="$1"
+    local src="$2"
+    local serial="$3"
 
-    copy $out $src
-}
-
-## Return the first zone that exists under $DATA/$name.zone
-#
-#   base=$(choose_zone $name...)
-function choose_zone {
-    # look
-    for name in "$@"; do
-        if [ $name ] && [ -e $DATA/$name.zone ]; then
-            echo $name
-            return 0
-        fi
-    done
-    
-    # failed to find
-    die "Unable to find zone in $DATA/*.zone: $@"
-}
-
-## Expand zone file from source using pvl.dns-zone:
-#
-#   update_zone $view   $zone   [$base]
-function update_zone {
-    local out=$1
-    local src=$2
-    local serial=$3
-
-    if check_update $out $src $serial; then
+    if check_update "$out" "$src" "$serial"; then
         log_update "Generating $out <- $src..." 
 
-        do_update $out $OPT/bin/pvl.dns-zone $src \
-                --serial $(cat $serial)
+        do_update "$out" $OPT/bin/pvl.dns-zone "$src" \
+                --serial $(cat "$serial")
     else
         log_skip "Generating $out <- $src: not changed" 
     fi
 }
 
-## Link zone file to ues given shared zone.
-#
-#   link_zone   $view   $zone   [$base]
-#
-# Looks for shared zone at:
-#   $ZONES/$view/$base
-#   $ZONES/common/$base
-function link_zone {
-    local view=$1
-    local zone=$2
-    local base=${3:-$zone}
-
-    local out=$ZONES/$view/$zone
-    local tgt=$(choose_link $out $ZONES/$view/$base $ZONES/common/$base)
-
-    link $out $tgt
-}
-
-## Link dhcp file directly from data to $DHCP
-function link_dhcp_conf {
-    local conf=$1
-    local base=${2:-$conf}
-
-    local out=$DHCP/$conf.conf
-    local tgt=$(choose_link $out $DHCP/$base.conf $DHCP_DATA/$base.conf)
-
-    link $out $tgt
-}
-
-## Copy dhcp conf from data to $DHCP
-function copy_dhcp_conf {
-    local conf=$1
-    local base=${2:-$conf}
-
-    local out=$DHCP/$conf.conf
-    local src=$DHCP_DATA/$base.conf
-
-    copy $out $src
-}
-
 ## Test hosts zone for validity using pvl.hosts-check:
 #
 #   check_hosts     .../hosts
--- a/lib/update.updates	Tue Dec 17 01:17:49 2013 +0200
+++ b/lib/update.updates	Tue Dec 17 02:06:53 2013 +0200
@@ -10,7 +10,7 @@
 # Returns true if the output file needs to be updated.
 function check_update {
     # target
-    local out=$1; shift
+    local out="$1"; shift
 
     debug "$out"
 
@@ -21,7 +21,7 @@
         debug "  update: unknown deps"
         update=y
 
-    elif [ ! -e $out ]; then
+    elif [ ! -e "$out" ]; then
         debug "  update: dest missing"
         update=y
         
@@ -37,9 +37,9 @@
 
         # check
         if [ ! -e "$dep" ]; then
-            fail "$out: Missing source: $dep"
+            warn "$out: Missing source: $dep"
 
-        elif [ $out -ot "$dep" ]; then
+        elif [ "$out" -ot "$dep" ]; then
             debug "  update: $dep"
             update=y
         else
@@ -60,18 +60,18 @@
 # Writes output to a temporary .new file, optionally shows a diff of changes, and commits
 # the new version to $out (unless noop'd).
 function do_update {
-    local out=$1; shift
-    local tmp=$out.new
+    local out="$1"; shift
+    local tmp="$out.new"
 
     debug "$out"
-    cmd "$@" > $tmp
+    cmd "$@" > "$tmp"
 
     # compare
-    if [ -e $out ] && [ $UPDATE_DIFF ]; then
+    if [ -e "$out" ] && [ $UPDATE_DIFF ]; then
         debug "  changes:"
 
         # terse
-        indent "        " diff --unified=1 $out $tmp || true
+        indent "        " diff --unified=1 "$out" "$tmp" || true
     fi
     
     # deploy
@@ -79,33 +79,15 @@
         # cleanup
         debug "  no-op"
 
-        cmd rm $tmp
+        cmd rm "$tmp"
     else
         # commit
         debug "  deploy"
 
-        cmd mv $tmp $out
+        cmd mv "$tmp" "$out"
     fi
 }
 
-## Look for a link target:
-#
-#   find_link   $lnk    $tgt...
-#
-# Outputs the first given target to exist, skipping any that are the same as the given $lnk.
-# If no $tgt matches, outputs the last one, or '-'.
-function choose_link {
-    local lnk=$1; shift
-    local tgt=-
-
-    for tgt in "$@"; do
-        [ $tgt != $out ] && [ -e $tgt ] && break
-    done
-    
-    echo $tgt
-}
-
-
 ## Compare symlink to target:
 #
 #   check_link $lnk $tgt && do_link $lnk $tgt || ...
@@ -113,12 +95,12 @@
 # Tests if the symlink exists, and the target matches.
 # Fails if the target does not exist.
 function check_link {
-    local lnk=$1
-    local tgt=$2
+    local lnk="$1"
+    local tgt="$2"
 
-    [ ! -e $tgt ] && fail "$tgt: target does not exist"
+    [ ! -e "$tgt" ] && fail "$tgt: target does not exist"
     
-    [ ! -e $lnk ] || [ $(readlink $lnk) != $tgt ]
+    [ ! -e "$lnk" ] || [ $(readlink "$lnk") != "$tgt" ]
 }
 
 ## Update symlink to point to target:
@@ -126,9 +108,28 @@
 #   do_link $lnk $tgt
 #
 function do_link {
-    local lnk=$1
-    local tgt=$2
+    local lnk="$1"
+    local tgt="$2"
 
-    cmd ln -sf $tgt $lnk
+    cmd ln -sf "$tgt" "$lnk"
 }
 
+## 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"
+    local src="$2"
+    local dir="$3"
+
+    if [ ! -e "$cache" -o "$cache" -ot "$src" ]; then
+        read_zone_includes "$src" > "$cache"
+    fi
+
+    while read include; do
+        echo -n "$dir/$include "
+    done < "$cache"
+}
--- a/lib/update.utils	Tue Dec 17 01:17:49 2013 +0200
+++ b/lib/update.utils	Tue Dec 17 02:06:53 2013 +0200
@@ -30,7 +30,7 @@
 #
 # Output is kept on stdout, exit status is that of the given command.
 function indent () {
-    local indent=$1; shift
+    local indent="$1"; shift
 
     "$@" | sed "s/^/$indent/"
 
@@ -41,11 +41,11 @@
 ### FS utils
 # Create dir if not exists.
 function ensure_dir {
-    local dir=$1
+    local dir="$1"
 
-    if [ ! -d $dir ]; then
+    if [ ! -d "$dir" ]; then
         log_warn "Creating output dir: $dir"
-        cmd mkdir $dir
+        cmd mkdir "$dir"
     fi
 }
 
@@ -55,7 +55,7 @@
 #
 # XXX: improve...?
 function abspath () {
-    local path=$1
+    local path="$1"
 
     echo "$SRV/$path"
 }
@@ -65,13 +65,13 @@
 #   list_files $dir $glob
 #
 function list_files {
-    local dir=$1
-    local glob=${2:-*}
+    local dir="$1"
+    local glob="${2:-*}"
     local name=
 
     for file in $dir/$glob; do
         # only files
-        [ -f $file ] || continue
+        [ -f "$file" ] || continue
 
         # strip prefix
         name=${file#$dir/}