lib/update.operations
author Tero Marttila <terom@paivola.fi>
Tue, 17 Dec 2013 01:17:49 +0200
branchdns-new
changeset 84 77df429f63a3
parent 82 26a307558602
child 85 4ad9c9b7cd0e
permissions -rw-r--r--
update: full host forward/dhcp/reverse zones
#!/bin/bash
## vim: set ft=sh :
#
# Operations on zonefiles/hosts/whatever

function link {
    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
}

function copy {
    local out=$1
    local src=$2

    if check_update $out $src; then
        log_update "Copying $out <- $src..."

        do_update $out \
            cat $src
    else
        log_skip "Copying $out <- $src: not changed"
    fi
}

## Run check-command on given file, outputting results:
#
#   check    $src    $cmd $args...
#
function check {
    local src=$1; shift
    local cmd=$1; shift

    if cmd_test $cmd -q "$@"; then
        log_skip    "Check $src: OK"

    else
        log_error   "  Check $src: Failed"

        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

    if check_update $out $src "$@"; then
        log_update "Generating forward hosts zone $out <- $src..."
    
        do_update $out $OPT/bin/pvl.hosts-dns \
            --hosts-charset=$CHARSET \
            --forward-zone=$domain \
             $src "$@"
    
    else
        log_skip "Generating forward hosts $out <- $src: not changed"
    fi
}

function update_hosts_dhcp {
    local out=$1; shift
    local src=$1; shift

    if check_update $out $src "$@"; then
        log_update "Generating DHCP hosts $out <- $src..."

        do_update $out $OPT/bin/pvl.hosts-dhcp \
            --hosts-charset=$CHARSET \
            $src "$@"
    else
        log_skip "Generating DHCP hosts $out <- $src: not changed"
    fi
}

## Generate reverse zone from hosts hosts using pvl.hosts-dns:
#
#   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

    if check_update $out $src "$@"; then
        log_update "Generating reverse hosts zone $out <- $src..."
    
        do_update $out $OPT/bin/pvl.hosts-dns \
            --hosts-charset=$CHARSET \
            --reverse-zone=$reverse \
            $src "$@"
    
    else
        log_skip "Generating reverse hosts $out <- $src: not changed"
    fi
}

## Update .serial number:
#
#   do_update_serial $serial
#
# Shows old/new serial on debug.
function do_update_serial {
    local serial=$1

    # read
    local old=$(test -e $serial && cat $serial || echo '')

    cmd $OPT/bin/pvl.dns-serial $serial
    
    # read
    local new=$(cat $serial)
        
    debug "  $old -> $new"
}


## Generate new serial for zone using pvl.dns-serial, if the zone data has changed:
#
#   update_serial   $zone   $deps...
#
# Supports SERIAL_FORCE/NOOP.
# Updates $SERIALS/$zone.serial.
function update_serial {
    local serial=$1; shift
    
    # test
    if [ $SERIAL_FORCE ]; then
        log_force "Updating $serial: forced"

        do_update_serial $serial

    elif ! check_update $serial "$@"; then
        log_skip "Updating $serial: not changed"

    elif [ $SERIAL_NOOP ]; then
        log_noop "Updating $serial: skipped"

    else
        log_update "Updating $serial..."

        do_update_serial $serial
    fi
}

## Link serial for zone from given base-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

    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
        log_update "Generating $out <- $src..." 

        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
function check_hosts {
    local hosts=$1; shift 1
    
    # TODO
    check $hosts \
        $OPT/bin/pvl.hosts-check $hosts
}

## Test zone file for validity using named-checkzone:
#
#   check_zone      ..../$zone $origin
function check_zone {
    local zone=$1
    local origin=$2

    # checkzone is very specific about the order of arguments, -q must be first
    check $zone $NAMED_CHECKZONE $origin $zone
}

## 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}

    if [ ! -e $DHCPD ]; then
        log_warn "check_dhcp: dhcpd not installed, skipping: $conf"
        return 0
    fi

    check $conf \
        $DHCPD -cf $conf -t
}

## Test DHCP configuration of given settings/dhcp using check_dhcp $DHCP_DATA/$host.conf:
#
#   check_dhcp_conf     $conf
#
function check_dhcp_conf {
    local conf=$1;

    check_dhcp $DHCP_DATA/$conf.conf
}

### Deploy
## Run rndc reload
function do_reload_zones {
    # run
    indent "        rndc: " \
        $RNDC reload

    # set flag
    RELOAD_ZONES=y
}

## Load update zonefiles into bind:
#
#   reload_zones    
#
# Invokes `rndc reload`, showing its output.
function reload_zones {
    local msg="Reload zones"

    if [ $RELOAD_FORCE ]; then
        log_force  "$msg..."
        
        do_reload_zones

    elif [ $RELOAD_NOOP ]; then
        log_noop    "$msg: skipped"
    
    elif [ ! -e $RNDC ]; then
        log_warn "reload_zones: rndc not installed, skipping"

    elif [ ! -e $RNDC_KEY ]; then
        log_warn   "  $msg: rndc: key not found: $RNDC_KEY"

    elif [ ! -r $RNDC_KEY ]; then
        log_error   "  $msg: rndc: permission denied: $RNDC_KEY"

        return 1

    else
        log_update  "$msg..."

        # run
        do_reload_zones
    fi
}

## Reload DHCP by restarting it, if running:
#
#   do_reload_dhcp
#
# Does NOT restart dhcp if it is not running (status).
function do_reload_dhcp {
    if cmd_test $DHCPD_INIT status >/dev/null; then
        cmd $DHCPD_INIT restart
    else
        log_warn "dhcpd not running; did not restart"
    fi
}

## Reload dhcp hosts
#
#   reload_dhcp
#
# noop's if we haven't reloaded zones
function reload_dhcp {
    local msg="Reload DHCP hosts"

    if [ $RELOAD_FORCE ]; then
        log_force  "$msg..."
        
        do_reload_dhcp

    elif [ $RELOAD_NOOP ]; then
        log_noop    "$msg: skipped"
 
    elif [ ! -e $DHCPD ]; then
        log_warn "reload_dhcp: dhcpd not installed, skipping: $conf"
   
    else
        log_update  "$msg..."

        # run
        do_reload_dhcp
    fi
}

### Commit
## Perform `hg commit`
function do_commit {
    local msg=$1

    [ $LOG_DIFF ] && indent "    " hg_diff

    hg_commit "$msg"
}


## Commit changes to version control:
#
#   commit_data
#
# Invokes `hg commit`, first showing the diff.
function commit_data {
    local commit_msg="$COMMIT_MSG"

    local msg="Commit changes"

    # operate?
    if [ $COMMIT_FORCE ]; then
        log_force   "$msg..."

        do_commit "$commit_msg"

    elif ! hg_modified; then
        log_skip    "$msg: no changes"

    elif [ $COMMIT_SKIP ]; then
        log_noop    "$msg: skipped"
        
        # still show diff, though
        [ $LOG_DIFF ] && indent "    " hg_diff
    else
        log_update  "$msg..."

        do_commit "$commit_msg"
    fi
}