bin/update
author Tero Marttila <terom@paivola.fi>
Tue, 17 Dec 2013 02:06:53 +0200
branchdns-new
changeset 85 4ad9c9b7cd0e
parent 84 77df429f63a3
child 87 cb4607af8663
permissions -rwxr-xr-x
update: many things
#!/bin/bash
# vim: set ft=sh :

set -ue

if [ $0 == './update' ]; then
    SRV=$(pwd)
    OPT=./opt
else
    SRV=${SRV:-/srv/dns}
    OPT=${SRV:-/srv/dns/opt}
    cd $SRV
fi

# charset for files under etc/
CHARSET='utf-8'

# External bins
NAMED_CHECKZONE=/usr/sbin/named-checkzone

DHCPD=/usr/sbin/dhcpd
DHCPD_CONF=/etc/dhcp/dhcpd.conf
DHCPD_INIT=/etc/init.d/isc-dhcp-server

HG=/usr/bin/hg
HG_ARGS=(--config trusted.users=root)

RNDC=/usr/sbin/rndc
RNDC_KEY=/etc/bind/rndc.key

# Library includes
source lib/update

## Flags
# set by do_reload_zone if zone data has actually been reloaded
RELOAD_ZONES=

## Site settings, used as arguments to scripts
# Origin domain to generate reverse records for in --reverse-zone
REVERSE_DOMAIN=paivola.fi

## Do things
function run_hosts {
    # test
    log "Testing hosts..."
    for hosts in $(list_files etc/hosts); do
        log_warn "TODO: check_hosts $hosts"
    done

    log "Updating forward host zones..."
    for hosts in $(list_files etc/hosts); do
        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"
    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

         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"

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

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"
    done

    log "Updating zone serials..."
    for zone in $(list_files etc/zones); do
        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"
    done

    log "Testing zones..."
    for zone in $(list_files etc/zones); do
        # check_zone          var/zones/$zone     $zone
        log_warn "TODO: check_zone $zone"
    done
}

function run_deploy {
    log "Reload zones..."
        reload_zones

    log "Reload dhcp..."
        reload_dhcp

    log "Commit etc..."
        commit_data     etc
}

## Main entry point
function main {
    parse_args "$@"

    ## Input dirs
    for dir in etc etc/dhcp etc/zones; do
        [ -d $dir ] || die "Missing directory: $dir"
    done
    
    ## Output dirs
    for dir in var var/dhcp var/zones var/serials var/include-cache; do
        ensure_dir  $dir
    done
    
    # sub-$ZONES
    for dir in var/zones/hosts var/zones/includes; do
        ensure_dir  $dir
    done

    ## Go
    run_hosts
    run_zones
    run_deploy
}

main "$@"