bin/update
author Tero Marttila <terom@paivola.fi>
Wed, 21 Mar 2012 17:16:26 +0200
changeset 64 620f4594a09d
parent 63 a88766722d18
child 66 482d06935d96
permissions -rwxr-xr-x
update: copy_generic; use copy_dhcp_conf to get diffs
#!/bin/bash
# vim: set ft=sh :

set -ue

# resolve $0 -> bin/update
self=$0
while [ -L $self ]; do
    tgt=$(readlink $self)

    if [ "${tgt:0:1}" == "/" ]; then
        self=$tgt
    else
        self=$(dirname $self)/$tgt
    fi
done

# Our bin dir, with scripts
BIN=$(dirname $self)

# code root
CODE=$(dirname $BIN)

# library code
LIB=$CODE/lib

## Data paths
# absolute path to data files; can be changed using -d
ROOT=$(pwd)

DATA=settings
ZONES=zones
SERIALS=$DATA

DHCP=dhcp
DHCP_DATA=$DATA/dhcp

# global DHCP conf to test
DHCPD=/usr/sbin/dhcpd
DHCPD_CONF=/etc/dhcp/dhcpd.conf

# hg repo to commit
REPO=$DATA

## Settings used in lib
# Hide files under repo in commit diff output..
REPO_HIDE='*.serial'

# XXX: hosts data input charset?
PROCESS_ARGS='--input-charset latin-1'

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

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

RNDC=/usr/sbin/rndc

# Path to rndc key, must be readable to run..
RNDC_KEY=/etc/bind/rndc.key

## Library includes
# Command-line argument handling
source $LIB/update.args

# Logging
source $LIB/update.logging

# Utility functions
source $LIB/update.utils

# Dependency-based updates
source $LIB/update.updates

# Operations; the functions called from run()
source $LIB/update.operations


## Site settings, used as arguments to scripts
# MX record to generate in hosts --forward-zone
FORWARD_MX=mail

# IP network to generate reverse records for in --reverse-zone
REVERSE_ZONE=194.197.235

# Origin domain to generate reverse records for in --reverse-zone
REVERSE_DOMAIN=paivola.fi

# Views used
VIEWS=(internal external)

# Base domain zone for domains
DOMAIN_BASE=paivola

# 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 conf files
DHCP_CONFS=(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
    log "Testing hosts..."
        #                   data                            args...
        check_hosts         $DATA/paivola.txt               --check-exempt ufc

    # update
    log "Generating host zones..."
        #                   hosts                           data                args...
        update_hosts        $ZONES/hosts/paivola:internal   $DATA/paivola.txt   --forward-zone --forward-txt --forward-mx $FORWARD_MX
        update_hosts        $ZONES/hosts/paivola:external   $DATA/paivola.txt   --forward-zone
        update_hosts        $ZONES/hosts/194.197.235        $DATA/paivola.txt   --reverse-zone $REVERSE_ZONE --reverse-domain $REVERSE_DOMAIN

        
        update_hosts        $ZONES/hosts/10                 $DATA/pvl.txt       --reverse-zone 10 --reverse-domain pvl -q
        update_hosts        $ZONES/hosts/192.168            $DATA/pvl.txt       --reverse-zone 192.168 --reverse-domain pvl -q

        # XXX: unsupported --forward-zone with pvl.txt
        # 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..."
        #                   view            zone                    base
        copy_zone           includes        paivola:internal        paivola.zone.internal
        copy_zone           includes        paivola:external        paivola.zone.external
        copy_zone           includes        paivola.auto            paivola.zone.auto
        copy_zone           includes        paivola.services        paivola.zone.services

    ## Serials
    log "Updating serials..."

        #                   zone            deps...
        #   includes...
        update_serial       pvl             $ZONES/hosts/pvl            $DATA/pvl.zone
        update_serial       10              $ZONES/hosts/10             $DATA/10.zone
        update_serial       192.168         $ZONES/hosts/192.168        $DATA/192.168.zone

        update_serial       paivola         $ZONES/hosts/paivola:*      $DATA/paivola.zone          \
            $ZONES/includes/paivola:*       \
            $ZONES/includes/paivola.*

        update_serial       194.197.235     $ZONES/hosts/194.197.235    $DATA/194.197.235.zone          

    ## Zones
    log "Updating zones..."
        #                   view        zone            base
        update_zone         internal    pvl
        update_zone         internal    paivola
        update_zone         external    paivola

        update_zone         internal    10
        update_zone         internal    192.168

        update_zone         common      194.197.235
        link_zone           internal    194.197.235
        link_zone           external    194.197.235

    ## Test
    log "Testing zones..."
        #                   view        zone            origin
        check_zone          internal    paivola         paivola.fi
        check_zone          external    paivola         paivola.fi

        check_zone          internal    10              10.in-addr.arpa
        check_zone          internal    192.168         192.168.in-addr.arpa
        check_zone          common      194.197.235     235.197.194.in-addr.arpa

    ## Domains...
    log "Linking domains..."
        for view in "${VIEWS[@]}"; do
            for zone in "${DOMAINS[@]}"; do
                # link
                link_zone       $view       $zone           $DOMAIN_BASE

                # test
                check_zone      $view       $zone           $zone
            done
        done
}

# Update $DHCP files from $DATA/dhcp
function run_dhcp {
    log "Copying DHCP configs..."
        for conf in "${DHCP_CONFS[@]}"; do
            #                   conf               base
            check_dhcp_conf     $conf
            copy_dhcp_conf      $conf
        done

    log "Testing dhcp..."
        # this is partially redundant with the above check_dhcp_hosts, but doesn't matter
        check_dhcp
}

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

    ## Commit
    log "Commit data..."
        commit_data
}

## Main entry point
function main {
    # test tty
    [ -t 1 ] && IS_TTY=y
    
    parse_args "$@"

    ## Input dirs
    [ -d $ROOT/$DATA ] || die "Missing data: $ROOT/$DATA"
    
    ## Output dirs
    for dir in $DHCP $ZONES; do
        ensure_dir  $dir
    done

    for dir in "common" "hosts" "includes" "${VIEWS[@]}"; do
        ensure_dir  $ZONES/$dir
    done

    ## Go
    run_hosts
    run_zones
    run_dhcp
    run_deploy
}

main "$@"