update: link + check dhcpd hosts from settings/dhcp/... -> dhcp/...
authorTero Marttila <terom@paivola.fi>
Wed, 21 Mar 2012 17:04:21 +0200
changeset 585 65bf391fd2f3
parent 584 22b58b938fd0
child 586 a88766722d18
update: link + check dhcpd hosts from settings/dhcp/... -> dhcp/...
bin/update
lib/update.operations
--- a/bin/update	Wed Mar 21 16:45:59 2012 +0200
+++ b/bin/update	Wed Mar 21 17:04:21 2012 +0200
@@ -32,8 +32,12 @@
 ZONES=zones
 SERIALS=$DATA
 
+DHCP=dhcp
 DHCP_DATA=$DATA/dhcp
-DHCP_HOSTS=dhcp
+
+# global DHCP conf to test
+DHCPD=/usr/sbin/dhcpd
+DHCPD_CONF=/etc/dhcp/dhcpd.conf
 
 # hg repo to commit
 REPO=$DATA
@@ -92,9 +96,13 @@
 # List of actual domains used; will be linked to $DOMAIN_BASE
 DOMAINS=(paivola.fi paivola.net paivola.org paivola.info paivola.mobi xn--pivl-load8j.fi)
 
+# Names of dhcp hosts files
+DHCP_HOSTS=(paivola veturi verstas)
 
 ## Operate!
 # these functions are all defined in lib/update.operations
+
+# Update $ZONES/$DHCP host-files from $DATA
 function run_hosts {
     ## Hosts
     # test
@@ -117,7 +125,8 @@
         # update_hosts    $ZONES/hosts/pvl                    $DATA/pvl.txt      --forward-zone
         copy_hosts          $ZONES/hosts/pvl                $DATA/pvl.txt
 }
- 
+
+# Update $ZONES files
 function run_zones {
     ## Includes
     log "Copying zone includes..."
@@ -179,6 +188,19 @@
         done
 }
 
+# Update $DHCP files from $DATA/dhcp
+function run_dhcp {
+    log "Linking DHCP hosts..."
+        for hosts in "${DHCP_HOSTS[@]}"; do
+            #                   hosts               base
+            link_dhcp_hosts     $hosts
+            check_dhcp_hosts    $hosts
+        done
+
+    log "Testing dhcp..."
+        check_dhcp
+}
+
 function run_deploy {
     ## Reload zones
     log "Reload zones..."
@@ -196,18 +218,22 @@
     
     parse_args "$@"
 
-    ## test env
+    ## Input dirs
     [ -d $ROOT/$DATA ] || die "Missing data: $ROOT/$DATA"
-    ensure_dir  $ZONES
     
     ## Output dirs
+    for dir in $DHCP $ZONES; do
+        ensure_dir  $dir
+    done
+
     for dir in "common" "hosts" "includes" "${VIEWS[@]}"; do
-        ensure_dir $ZONES/$dir
+        ensure_dir  $ZONES/$dir
     done
 
     ## Go
     run_hosts
     run_zones
+    run_dhcp
     run_deploy
 }
 
--- a/lib/update.operations	Wed Mar 21 16:45:59 2012 +0200
+++ b/lib/update.operations	Wed Mar 21 17:04:21 2012 +0200
@@ -3,6 +3,21 @@
 #
 # Operations on zonefiles/hosts/whatever
 
+function link_generic {
+    local out=$1
+    local tgt=$2
+
+    if check_link $out $tgt; then
+        log_update "Linking $out -> $tgt..."
+
+        do_link $out $tgt
+
+    else
+        log_skip "Linking $out -> $tgt: not changed"
+    fi
+}
+
+
 ## Hosts
 ## Update hosts from verbatim from input zone data:
 #
@@ -100,17 +115,11 @@
     local zone=$1
     local base=$2
 
-    local lnk=$SERIALS/$zone.serial
+    local out=$SERIALS/$zone.serial
     local tgt=$SERIALS/$base.serial
 
-    if check_link $lnk $tgt; then
-        log_update "Linking $lnk -> $tgt..."
 
-        do_link $lnk $tgt
-
-    else
-        log_skip "Linking $lnk -> $tgt: not changed"
-    fi
+    link_generic $out $tgt
 }
 
 ## Update zone file verbatim from source:
@@ -180,14 +189,18 @@
     local out=$ZONES/$view/$zone
     local tgt=$(choose_link $out $ZONES/$view/$base $ZONES/common/$base)
 
-    if check_link $out $tgt; then
-        log_update "Linking $out -> $tgt..."
+    link_generic $out $tgt
+}
 
-        do_link $out $tgt
+## Link dhcp file directly from data to hosts
+function link_dhcp_hosts {
+    local hosts=$1
+    local base=${2:-$hosts}
 
-    else
-        log_skip "Linking $out -> $tgt: not changed"
-    fi
+    local out=$DHCP/$hosts.conf
+    local tgt=$(choose_link $out $DHCP/$base.conf $DHCP_DATA/$base.conf)
+
+    link_generic $out $tgt
 }
 
 ## Test hosts zone for validity:
@@ -239,6 +252,39 @@
     fi
 }
 
+## Test DHCP configuration for validity using dhcpd -t:
+#
+#   check_dhcp      [$conf]
+#
+# Defaults to the global $DHCPD_CONF.
+# Fails if the check fails.
+function check_dhcp {
+    local conf=${1:-$DHCPD_CONF}
+
+    local cmd=($DHCPD -cf $conf -t)
+
+    if "${cmd[@]}" -q; then
+        log_skip    "Check $conf: OK"
+
+    else
+        log_error   "  Check $conf: Failed"
+
+        indent "    " "${cmd[@]}"
+
+        exit 1
+    fi
+}
+
+## Test DHCP configuration of given hosts file using check_dhcp $DHCP/$host.conf:
+#
+#   check_dhcp_hosts    $host
+#
+function check_dhcp_hosts {
+    local host=$1
+
+    check_dhcp $DHCP/$host.conf
+}
+
 # Run rndc reload
 function do_reload {
     # run