#!/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
# 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)
## Operate!
# these functions are all defined in lib/update.operations
function run {
## 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
## 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
## Deploy
log "Deploy zones..."
deploy_zones
## Commit
log "Commit data..."
commit_data
}
## Main entry point
function main {
# test tty
[ -t 1 ] && IS_TTY=y
parse_args "$@"
## test env
[ -d $ROOT/$DATA ] || die "Missing data: $ROOT/$DATA"
ensure_dir $ZONES
## Output dirs
for dir in "common" "hosts" "includes" "${VIEWS[@]}"; do
ensure_dir $ZONES/$dir
done
## Go
run
}
main "$@"