update: commit before update, and use the hg commit timestamp as the serial dns-new
authorTero Marttila <terom@paivola.fi>
Thu, 19 Dec 2013 23:22:12 +0200
branchdns-new
changeset 95 a756f317d083
parent 94 1b02d8075676
child 96 bed4765fc56f
update: commit before update, and use the hg commit timestamp as the serial
bin/update
lib/update.hg
lib/update.operations
lib/update.utils
--- a/bin/update	Thu Dec 19 22:00:37 2013 +0200
+++ b/bin/update	Thu Dec 19 23:22:12 2013 +0200
@@ -13,12 +13,28 @@
 
 source lib/update
 
-function update {
+function commit {
+    ## Commit
+    # pre-commit check
     log "Testing hosts..."
     for hosts in $(list_files etc/hosts); do
         log_warn "TODO: check_hosts $hosts"
     done
 
+    # commit, unless noop'd
+    log "Commit..."
+        update_commit       etc
+}
+
+function update {
+    if hg_modified etc; then
+        serial=$(unix_time)
+        log_warn "Using local unix time for uncommited changes: $serial"
+    else
+        serial=$(hg_time etc)
+        log_update "Using HG commit timestamp: $serial"
+    fi
+
     ## Hosts
     log "Updating forward host zones..."
     for zone in $(list_dirs etc/hosts/forward); do
@@ -46,13 +62,14 @@
 
     log "Updating zone serials..."
     for zone in $(list_files etc/zones); do
-        update_serial       "var/serials/$zone"             \
+        update_serial       "var/serials/$zone"             $serial \
             "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" \
+            $(zone_includes var/include-cache/$zone etc/zones/$zone var/zones/)
     done
 
     log "Updating DHCP confs..."
@@ -79,8 +96,6 @@
     log "Reload dhcp..."
         reload_dhcp
 
-    log "Commit..."
-        commit      etc
 }
 
 ## Main entry point
@@ -94,7 +109,7 @@
     
     ## Output dirs
     ensure_dir      var
-    for dir in var/dhcp var/zones var/serials var/include-cache; do
+    for dir in var/dhcp var/zones var/include-cache var/serials; do
         ensure_dir  $dir
     done
     for dir in var/dhcp/hosts; do
@@ -108,6 +123,7 @@
     done
 
     ## Go
+    commit
     update
     deploy
 }
--- a/lib/update.hg	Thu Dec 19 22:00:37 2013 +0200
+++ b/lib/update.hg	Thu Dec 19 23:22:12 2013 +0200
@@ -10,7 +10,22 @@
 
 ## Does the repo have local modifications?
 function hg_modified {
-    hg $1 id | grep -q '+'
+    hg $1 id -i | grep -q '+'
+}
+
+## Get the date for the current commit as an unix timestamp
+function hg_time {
+    local repo=$1
+    local hg_unix=
+    local hg_tz=
+
+    local hg_date=$(hg $repo log -r . --template '{date|hgdate}')
+    local hg_unix=${hg_date% *}
+    local hg_tz=${hg_date#* }
+
+    [ -n "$hg_unix" ] || fail "failed to read hg time"
+
+    echo "$hg_unix"
 }
 
 ## Output possible -u flag for commit.
--- a/lib/update.operations	Thu Dec 19 22:00:37 2013 +0200
+++ b/lib/update.operations	Thu Dec 19 23:22:12 2013 +0200
@@ -106,49 +106,44 @@
 
 ## Update .serial number:
 #
-#   do_update_serial $serial
+#   do_update_serial .../serials/$zone  $serial
 #
-# Shows old/new serial on debug.
 function do_update_serial {
-    local serial="$1"
+    local dst="$1"
+    local serial="$2"
 
-    # read
-    local old=$(test -e "$serial" && cat "$serial" || echo '')
-
-    cmd $OPT/bin/pvl.dns-serial "$serial"
-    
-    # read
-    local new=$(cat "$serial")
-        
-    debug "  $old -> $new"
+    echo $serial > $dst
 }
 
 
 ## Generate new serial for zone using pvl.dns-serial, if the zone data has changed:
 #
-#   update_serial   $zone   $deps...
+#   update_serial   .../serials/$zone   $serial     $deps...
 #
 # Supports SERIAL_FORCE/NOOP.
 # Updates $SERIALS/$zone.serial.
 function update_serial {
+    local dst="$1"; shift
     local serial="$1"; shift
+
+    local old=$(test -e "$dst" && cat "$dst" || echo '')
     
     # test
     if [ $SERIAL_FORCE ]; then
-        log_force "Updating $serial: forced"
+        log_force "Updating $dst: $old <- $serial: forced"
 
-        do_update_serial "$serial"
+        do_update_serial "$dst" "$serial"
 
-    elif ! check_update "$serial" "$@"; then
-        log_skip "Updating $serial: not changed"
+    elif ! check_update "$dst" "$@"; then
+        log_skip "Updating $dst: $old <- $serial: not changed"
 
     elif [ $SERIAL_NOOP ]; then
-        log_noop "Updating $serial: skipped"
+        log_noop "Updating $dst: $old <- $serial: skipped"
 
     else
-        log_update "Updating $serial..."
+        log_update "Updating $dst: $old <- $serial"
 
-        do_update_serial "$serial"
+        do_update_serial "$dst" "$serial"
     fi
 }
 
@@ -156,21 +151,20 @@
 #
 #   update_zone out/zones/$zone in/zones/$zone var/serials/$zone
 function update_zone {
-    local out="$1"
-    local src="$2"
-    local serial="$3"
+    local out="$1"; shift
+    local src="$1"; shift
+    local serial="$1"; shift
     local serial_opt=
 
     if [ -n "$serial" -a -f "$serial" ]; then
         serial_opt="--serial=$(cat "$serial")"
-
     elif [ $SERIAL_NOOP ]; then
         warn "$out: noop'd serial, omitting"
     else
         fail "$out: missing serial: $serial"
     fi
 
-    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" \
@@ -356,7 +350,7 @@
 #   commit_data
 #
 # Invokes `hg commit`, first showing the diff.
-function commit {
+function update_commit {
     local repo=$1
     local commit_msg="$COMMIT_MSG"
 
@@ -364,7 +358,7 @@
 
     # operate?
     if [ $COMMIT_FORCE ]; then
-        log_force   "$msg..."
+        log_force   "$msg: $commit_msg"
 
         do_commit "$commit_msg"
 
@@ -377,7 +371,7 @@
         # still show diff, though
         [ $LOG_DIFF ] && indent "    " hg_diff $repo
     else
-        log_update  "$msg..."
+        log_update  "$msg: $commit_msg"
 
         do_commit $repo "$commit_msg"
     fi
--- a/lib/update.utils	Thu Dec 19 22:00:37 2013 +0200
+++ b/lib/update.utils	Thu Dec 19 23:22:12 2013 +0200
@@ -91,3 +91,8 @@
         echo -n "${file#$dir/} "
     done
 }
+
+## Get current unix (utc) timestamp
+function unix_time {
+    date +'%s'
+}