update: commit
authorTero Marttila <terom@paivola.fi>
Fri, 16 Mar 2012 16:10:31 +0200
changeset 21 81cd07bc5f76
parent 20 d390f9bc3237
child 22 5d2a8510a28f
update: commit
bin/update
--- a/bin/update	Fri Mar 16 16:10:21 2012 +0200
+++ b/bin/update	Fri Mar 16 16:10:31 2012 +0200
@@ -10,9 +10,9 @@
 EXPAND_ZONE=$BIN/expand-zone
 UPDATE_SERIAL=$BIN/update-serial
 
-SETTINGS=settings
+DATA=settings
 ZONES=zones
-SERIALS=$SETTINGS
+SERIALS=$DATA
 
 PROCESS_ARGS='--input-charset latin-1'
 
@@ -20,7 +20,9 @@
 REVERSE_ZONE=194.197.235
 REVERSE_DOMAIN=paivola.fi
 
+
 NAMED_CHECKZONE=/usr/sbin/named-checkzone
+HG=hg
 
 ## options
 IS_TTY=
@@ -34,6 +36,9 @@
 UPDATE_NOOP=
 UPDATE_DIFF=
 SERIAL_NOUPDATE=
+COMMIT_SKIP=
+COMMIT_FORCE=
+COMMIT_MSG=''
 
 function help_args {
     local prog=$1
@@ -48,20 +53,25 @@
     -q      quiet
     -v      verbose
     -D      debug
-    -C      debug commands
+    -V      debug commands
     
 Updates:
     -p      show changes
     -F      force-updates without checking src mtime
     -S      do not update serial
-    -n      no-op/mock-update; don't actually change anything; implies -Sp
+    -n      no-op/mock-update; don't actually change anything; implies -SpC
+
+Commit    
+    -C      do not commit changes
+    -c      commit changes
+    -m      commit message
 END
 }
 
 function parse_args {
     OPTIND=1
 
-    while getopts 'hqvDCpFSn' opt "$@"; do
+    while getopts 'hqvDVpFSnCcm:' opt "$@"; do
         case $opt in
             h)  
                 help_args $0
@@ -74,7 +84,8 @@
                 LOG_DEBUG=y
                 LOG_INFO=y
                 ;;
-            C)  LOG_CMD=y ;;
+            V)  LOG_CMD=y ;;
+
             p)  UPDATE_DIFF=y ;;
             F)  UPDATE_FORCE=y ;;
             S)  SERIAL_NOUPDATE=y ;;
@@ -84,8 +95,12 @@
                 # implies -Sp
                 UPDATE_DIFF=y
                 SERIAL_NOUPDATE=y
+                COMMIT_SKIP=y
                 ;;
 
+            C)  COMMIT_SKIP=y ;;
+            c)  COMMIT_FORCE=y ;;
+            m)  COMMIT_MSG="$OPTARG" ;;
            
             ?)  
                 die 
@@ -187,7 +202,7 @@
 }
 
 ## test
-[ -d $SETTINGS ] || die "Missing settings: $SETTINGS"
+[ -d $DATA ] || die "Missing settings: $DATA"
 [ -d $SERIALS ] || die "Missing serials: $SERIALS"
 [ -d $ZONES ] || die "Missing zones: $ZONES"
 
@@ -298,7 +313,7 @@
     local part=$2
 
     local name=$zone.zone.$part
-    local src=$SETTINGS/$name
+    local src=$DATA/$name
     local dst=$ZONES/$name
 
 
@@ -317,14 +332,14 @@
     local name=$zone.zone
 
     local out=$ZONES/$name
-    local in=$SETTINGS/$zone.zone
+    local in=$DATA/$zone.zone
     local serial=$SERIALS/$zone.serial
 
     if check_update $out $in $serial; then
         log_info "Generating $out..." 
 
         do_update $out \
-            $EXPAND_ZONE $SETTINGS/$zone.zone   \
+            $EXPAND_ZONE $DATA/$zone.zone   \
                 --serial $SERIALS/$zone.serial  \
                 --expand zones=$(abspath $ZONES)
     else
@@ -339,14 +354,14 @@
     local name=$view/$zone.zone
 
     local out=$ZONES/$name
-    local in=$SETTINGS/$zone.zone
+    local in=$DATA/$zone.zone
     local serial=$SERIALS/$zone.serial
 
     if check_update $out $in $serial; then
         log_info "Generating $out..."
 
         do_update $out \
-            $EXPAND_ZONE $SETTINGS/$zone.zone   \
+            $EXPAND_ZONE $DATA/$zone.zone   \
                 --serial $SERIALS/$zone.serial  \
                 --expand zones=$(abspath $ZONES)    \
                 --expand view=$view
@@ -404,6 +419,55 @@
     fi
 }
 
+# commit data changes
+function hg_modified {
+    local repo=$1
+
+    $HG -R $repo id | grep -q '+'
+}
+
+function hg_user {
+    if [ ${SUDO_USER:-} ]; then
+        echo '-u' "$SUDO_USER"
+
+    elif [ $HOME ] && [ -e $HOME/.hgrc ]; then
+        log_debug "using .hgrc user"
+        echo ''
+
+    else
+        echo '-u' "$USER"
+    fi
+}
+
+function hg_diff {
+    local repo=$1
+
+    $HG -R $repo diff
+}
+
+function hg_commit {
+    local repo=$1
+    local msg=$2
+    local user_opt=$(hg_user)
+    
+    log_debug "commit: $user_opt: $msg"
+    cmd $HG -R $repo commit $user_opt -m "$msg"
+}
+
+function commit_data {
+    local data=$DATA
+
+    if hg_modified $data; then
+        log_info "Commit changes in $data:"
+
+        indent "    " hg_diff $data
+
+        hg_commit $data "$COMMIT_MSG"
+    else
+        log_info "Commit changes in $data: no changes"
+    fi
+}
+
 function main {
     # test tty
     [ -t 1 ] && IS_TTY=y
@@ -411,12 +475,12 @@
     parse_args "$@"
 
     log "Testing hosts..."
-        check_hosts     $SETTINGS/paivola.txt --check-exempt ufc
+        check_hosts     $DATA/paivola.txt --check-exempt ufc
 
     log "Generating host zones..."
-        update_hosts    $ZONES/external/paivola.zone.hosts  $SETTINGS/paivola.txt --forward-zone
-        update_hosts    $ZONES/internal/paivola.zone.hosts  $SETTINGS/paivola.txt --forward-zone --forward-txt --forward-mx $FORWARD_MX
-        update_hosts    $ZONES/paivola-reverse.zone.hosts   $SETTINGS/paivola.txt --reverse-zone $REVERSE_ZONE --reverse-domain $REVERSE_DOMAIN
+        update_hosts    $ZONES/external/paivola.zone.hosts  $DATA/paivola.txt --forward-zone
+        update_hosts    $ZONES/internal/paivola.zone.hosts  $DATA/paivola.txt --forward-zone --forward-txt --forward-mx $FORWARD_MX
+        update_hosts    $ZONES/paivola-reverse.zone.hosts   $DATA/paivola.txt --reverse-zone $REVERSE_ZONE --reverse-domain $REVERSE_DOMAIN
 
     log "Copying zone parts..."
         update_zone_part    paivola         auto
@@ -446,6 +510,15 @@
         check_zone          paivola.fi                  $ZONES/external/paivola.zone
         check_zone          235.197.194.in-addr.arpa    $ZONES/paivola-reverse.zone
 
+    # XXX: deploy
+
+    if [ $COMMIT_SKIP ] && [ ! $COMMIT_FORCE ]; then
+        log "Commit data: skipped"
+
+    else
+        log "Commit data..."
+            commit_data
+    fi
 }
 
 main "$@"