142 |
143 |
143 function log_color { |
144 function log_color { |
144 local code=$1; shift |
145 local code=$1; shift |
145 |
146 |
146 if [ $IS_TTY ]; then |
147 if [ $IS_TTY ]; then |
147 echo $'\e[0;'${code}'m'"$*"$'\e[00m' >&2 |
148 echo $'\e['${code}'m'"$*"$'\e[00m' >&2 |
148 else |
149 else |
149 echo "$*" >&2 |
150 echo "$*" >&2 |
150 fi |
151 fi |
151 } |
152 } |
152 |
153 |
153 function log_error { |
154 function log_error { |
154 log_color 31 "$*" |
155 log_color '31' "$*" |
155 } |
156 } |
156 |
157 |
157 function log_warn { |
158 function log_warn { |
158 [ $LOG_WARN ] && log_color 33 "$*" || true |
159 [ $LOG_WARN ] && log_color '33' "$*" || true |
159 } |
160 } |
160 |
161 |
161 function log { |
162 function log { |
162 [ $LOG ] && log_msg "$*" || true |
163 [ $LOG ] && log_msg "$*" || true |
163 } |
164 } |
164 |
165 |
165 function log_info { |
166 function log_info { |
166 [ $LOG_INFO ] && log_color 36 " $*" || true |
167 [ $LOG_INFO ] && log_color '36' " $*" || true |
|
168 } |
|
169 |
|
170 function log_skip { |
|
171 [ $LOG_SKIP ] && log_color '1;34' " $*" || true |
167 } |
172 } |
168 |
173 |
169 function log_debug { |
174 function log_debug { |
170 [ $LOG_DEBUG ] && log_color 32 " $*" || true |
175 [ $LOG_DEBUG ] && log_color 32 " $*" || true |
171 } |
176 } |
369 # copy hosts input zone verbatim |
393 # copy hosts input zone verbatim |
370 function copy_hosts { |
394 function copy_hosts { |
371 local zone=$1 |
395 local zone=$1 |
372 local base=$2 |
396 local base=$2 |
373 |
397 |
374 log_debug "base: $base" |
|
375 |
|
376 if check_update $zone $base; then |
398 if check_update $zone $base; then |
377 log_info "Copying hosts $zone <- $base..." |
399 log_info "Copying hosts $zone <- $base..." |
378 |
400 |
379 do_update $zone \ |
401 do_update $zone \ |
380 cat $ROOT/$base |
402 cat $ROOT/$base |
381 else |
403 else |
382 log_info "Copying hosts $zone <- $base: not changed" |
404 log_skip "Copying hosts $zone <- $base: not changed" |
383 fi |
405 fi |
384 } |
406 } |
385 |
407 |
386 # generate hosts zone from input zone |
408 # generate hosts zone from input zone |
387 function update_hosts { |
409 function update_hosts { |
392 log_info "Generating hosts $zone <- $base..." |
414 log_info "Generating hosts $zone <- $base..." |
393 |
415 |
394 do_update $zone \ |
416 do_update $zone \ |
395 $BIN/process-zone $PROCESS_ARGS $ROOT/$base "$@" |
417 $BIN/process-zone $PROCESS_ARGS $ROOT/$base "$@" |
396 else |
418 else |
397 log_info "Generating hosts $zone <- $base: not changed" |
419 log_skip "Generating hosts $zone <- $base: not changed" |
398 fi |
420 fi |
399 } |
421 } |
400 |
422 |
401 ## actions |
423 ## actions |
402 # serial |
424 # serial |
403 function update_serial { |
425 function update_serial { |
404 local zone=$1; shift |
426 local zone=$1; shift |
405 |
427 |
406 local serial=$SERIALS/$zone.serial |
428 local serial=$SERIALS/$zone.serial |
407 |
429 |
|
430 # read |
408 local old=$(test -e $ROOT/$serial && cat $ROOT/$serial || echo '') |
431 local old=$(test -e $ROOT/$serial && cat $ROOT/$serial || echo '') |
409 |
432 |
410 log_info "Updating $serial..." |
433 log_info "Updating $serial..." |
411 |
434 |
412 cmd $BIN/update-serial $* $ROOT/$serial |
435 cmd $BIN/update-serial $ROOT/$serial |
413 |
436 |
|
437 # read |
414 local new=$(cat $ROOT/$serial) |
438 local new=$(cat $ROOT/$serial) |
415 |
439 |
416 log_debug " $old -> $new" |
440 log_debug " $old -> $new" |
417 } |
441 } |
418 |
442 |
457 local base=${3:-$zone} |
482 local base=${3:-$zone} |
458 |
483 |
459 local out=$ZONES/$view/$zone |
484 local out=$ZONES/$view/$zone |
460 local src=$DATA/$base.zone |
485 local src=$DATA/$base.zone |
461 local lnk=$ZONES/$base |
486 local lnk=$ZONES/$base |
|
487 |
462 local serial=$SERIALS/$base.serial |
488 local serial=$SERIALS/$base.serial |
463 |
489 |
464 log_debug "$out: from src $src" |
490 if check_update $out $src $serial; then |
465 |
|
466 if [ ! -e $src ]; then |
|
467 fail "Missing source: $src" |
|
468 |
|
469 elif check_update $out $src $serial; then |
|
470 log_info "Generating $out <- $src..." |
491 log_info "Generating $out <- $src..." |
471 |
492 |
472 do_update $out \ |
493 do_update $out \ |
473 $BIN/expand-zone $ROOT/$src \ |
494 $BIN/expand-zone $ROOT/$src \ |
474 --serial $ROOT/$serial \ |
495 --serial $ROOT/$serial \ |
475 --expand zones=$(abspath $ZONES) \ |
496 --expand zones=$(abspath $ZONES) \ |
476 --expand view=$view |
497 --expand view=$view |
477 else |
498 else |
478 log_info "Generating $out <- $src: not changed" |
499 log_skip "Generating $out <- $src: not changed" |
479 fi |
500 fi |
480 } |
501 } |
481 |
502 |
482 function link_zone { |
503 function link_zone { |
483 local view=$1 |
504 local view=$1 |
484 local zone=$2 |
505 local zone=$2 |
485 local base=${3:-$zone} |
506 local base=${3:-$zone} |
486 |
507 |
487 local out=$ZONES/$view/$zone |
508 local out=$ZONES/$view/$zone |
488 |
509 |
|
510 # find tgt |
489 for tgt in $ZONES/$view/$base $ZONES/common/$base; do |
511 for tgt in $ZONES/$view/$base $ZONES/common/$base; do |
490 [ $tgt != $out ] && [ -e $tgt ] && break |
512 [ $tgt != $out ] && [ -e $tgt ] && break |
491 done |
513 done |
492 |
514 |
493 log_debug "$out: from " |
|
494 |
|
495 if check_link $out $tgt; then |
515 if check_link $out $tgt; then |
496 log_info "Linking $out -> $tgt..." |
516 log_info "Linking $out -> $tgt..." |
497 |
517 |
498 do_link $out $tgt |
518 do_link $out $tgt |
499 else |
519 else |
500 log_info "Linking $out -> $tgt: not changed" |
520 log_skip "Linking $out -> $tgt: not changed" |
501 fi |
521 fi |
502 } |
522 } |
503 |
523 |
504 ## Tests |
524 ## Tests |
505 function check_hosts { |
525 function check_hosts { |
506 local hosts=$1; shift 1 |
526 local hosts=$1; shift 1 |
507 |
527 |
508 local cmd=($BIN/process-zone $PROCESS_ARGS $ROOT/$hosts --check-hosts "$@") |
528 local cmd=($BIN/process-zone $PROCESS_ARGS $ROOT/$hosts --check-hosts "$@") |
509 |
529 |
510 if "${cmd[@]}" -q; then |
530 if "${cmd[@]}" -q; then |
511 log_info "Check $hosts: OK" |
531 log_skip "Check $hosts: OK" |
512 else |
532 else |
513 log_error " Check $hosts: Failed" |
533 log_error " Check $hosts: Failed" |
514 |
534 |
515 indent " " "${cmd[@]}" |
535 indent " " "${cmd[@]}" |
516 |
536 |
528 local cmd=($NAMED_CHECKZONE $origin $ROOT/$src) |
548 local cmd=($NAMED_CHECKZONE $origin $ROOT/$src) |
529 |
549 |
530 # test |
550 # test |
531 # XXX: checkzone is very specific about the order of arguments, -q must be first |
551 # XXX: checkzone is very specific about the order of arguments, -q must be first |
532 if $NAMED_CHECKZONE -q $origin $ROOT/$src; then |
552 if $NAMED_CHECKZONE -q $origin $ROOT/$src; then |
533 log_info "Check $src ($origin): OK" |
553 log_skip "Check $src ($origin): OK" |
534 else |
554 else |
535 log_error " Check $src ($origin): Failed:" |
555 log_error " Check $src ($origin): Failed:" |
536 |
556 |
537 indent " " "${cmd[@]}" |
557 indent " " "${cmd[@]}" |
538 |
558 |