lib/update.updates
branchdns-new
changeset 608 4ad9c9b7cd0e
parent 607 77df429f63a3
child 610 cb4607af8663
equal deleted inserted replaced
607:77df429f63a3 608:4ad9c9b7cd0e
     8 #   check_update $out ${deps[@]} && do_update $out ... || ...
     8 #   check_update $out ${deps[@]} && do_update $out ... || ...
     9 #
     9 #
    10 # Returns true if the output file needs to be updated.
    10 # Returns true if the output file needs to be updated.
    11 function check_update {
    11 function check_update {
    12     # target
    12     # target
    13     local out=$1; shift
    13     local out="$1"; shift
    14 
    14 
    15     debug "$out"
    15     debug "$out"
    16 
    16 
    17     # need update?
    17     # need update?
    18     local update=
    18     local update=
    19 
    19 
    20     if [ ${#@} == 0 ]; then
    20     if [ ${#@} == 0 ]; then
    21         debug "  update: unknown deps"
    21         debug "  update: unknown deps"
    22         update=y
    22         update=y
    23 
    23 
    24     elif [ ! -e $out ]; then
    24     elif [ ! -e "$out" ]; then
    25         debug "  update: dest missing"
    25         debug "  update: dest missing"
    26         update=y
    26         update=y
    27         
    27         
    28     elif [ $UPDATE_FORCE ]; then
    28     elif [ $UPDATE_FORCE ]; then
    29         debug "  update: forced"
    29         debug "  update: forced"
    35         # don't bother checking if already figured out
    35         # don't bother checking if already figured out
    36         [ $update ] && continue
    36         [ $update ] && continue
    37 
    37 
    38         # check
    38         # check
    39         if [ ! -e "$dep" ]; then
    39         if [ ! -e "$dep" ]; then
    40             fail "$out: Missing source: $dep"
    40             warn "$out: Missing source: $dep"
    41 
    41 
    42         elif [ $out -ot "$dep" ]; then
    42         elif [ "$out" -ot "$dep" ]; then
    43             debug "  update: $dep"
    43             debug "  update: $dep"
    44             update=y
    44             update=y
    45         else
    45         else
    46             debug "  check: $dep"
    46             debug "  check: $dep"
    47         fi
    47         fi
    58 #   do_update $out $BIN/cmd --args
    58 #   do_update $out $BIN/cmd --args
    59 #
    59 #
    60 # Writes output to a temporary .new file, optionally shows a diff of changes, and commits
    60 # Writes output to a temporary .new file, optionally shows a diff of changes, and commits
    61 # the new version to $out (unless noop'd).
    61 # the new version to $out (unless noop'd).
    62 function do_update {
    62 function do_update {
    63     local out=$1; shift
    63     local out="$1"; shift
    64     local tmp=$out.new
    64     local tmp="$out.new"
    65 
    65 
    66     debug "$out"
    66     debug "$out"
    67     cmd "$@" > $tmp
    67     cmd "$@" > "$tmp"
    68 
    68 
    69     # compare
    69     # compare
    70     if [ -e $out ] && [ $UPDATE_DIFF ]; then
    70     if [ -e "$out" ] && [ $UPDATE_DIFF ]; then
    71         debug "  changes:"
    71         debug "  changes:"
    72 
    72 
    73         # terse
    73         # terse
    74         indent "        " diff --unified=1 $out $tmp || true
    74         indent "        " diff --unified=1 "$out" "$tmp" || true
    75     fi
    75     fi
    76     
    76     
    77     # deploy
    77     # deploy
    78     if [ $UPDATE_NOOP ]; then
    78     if [ $UPDATE_NOOP ]; then
    79         # cleanup
    79         # cleanup
    80         debug "  no-op"
    80         debug "  no-op"
    81 
    81 
    82         cmd rm $tmp
    82         cmd rm "$tmp"
    83     else
    83     else
    84         # commit
    84         # commit
    85         debug "  deploy"
    85         debug "  deploy"
    86 
    86 
    87         cmd mv $tmp $out
    87         cmd mv "$tmp" "$out"
    88     fi
    88     fi
    89 }
    89 }
    90 
       
    91 ## Look for a link target:
       
    92 #
       
    93 #   find_link   $lnk    $tgt...
       
    94 #
       
    95 # Outputs the first given target to exist, skipping any that are the same as the given $lnk.
       
    96 # If no $tgt matches, outputs the last one, or '-'.
       
    97 function choose_link {
       
    98     local lnk=$1; shift
       
    99     local tgt=-
       
   100 
       
   101     for tgt in "$@"; do
       
   102         [ $tgt != $out ] && [ -e $tgt ] && break
       
   103     done
       
   104     
       
   105     echo $tgt
       
   106 }
       
   107 
       
   108 
    90 
   109 ## Compare symlink to target:
    91 ## Compare symlink to target:
   110 #
    92 #
   111 #   check_link $lnk $tgt && do_link $lnk $tgt || ...
    93 #   check_link $lnk $tgt && do_link $lnk $tgt || ...
   112 #
    94 #
   113 # Tests if the symlink exists, and the target matches.
    95 # Tests if the symlink exists, and the target matches.
   114 # Fails if the target does not exist.
    96 # Fails if the target does not exist.
   115 function check_link {
    97 function check_link {
   116     local lnk=$1
    98     local lnk="$1"
   117     local tgt=$2
    99     local tgt="$2"
   118 
   100 
   119     [ ! -e $tgt ] && fail "$tgt: target does not exist"
   101     [ ! -e "$tgt" ] && fail "$tgt: target does not exist"
   120     
   102     
   121     [ ! -e $lnk ] || [ $(readlink $lnk) != $tgt ]
   103     [ ! -e "$lnk" ] || [ $(readlink "$lnk") != "$tgt" ]
   122 }
   104 }
   123 
   105 
   124 ## Update symlink to point to target:
   106 ## Update symlink to point to target:
   125 #
   107 #
   126 #   do_link $lnk $tgt
   108 #   do_link $lnk $tgt
   127 #
   109 #
   128 function do_link {
   110 function do_link {
   129     local lnk=$1
   111     local lnk="$1"
   130     local tgt=$2
   112     local tgt="$2"
   131 
   113 
   132     cmd ln -sf $tgt $lnk
   114     cmd ln -sf "$tgt" "$lnk"
   133 }
   115 }
   134 
   116 
       
   117 ## Read include paths from file
       
   118 function read_zone_includes {
       
   119     cmd sed -n -E 's/^\$INCLUDE\s+"(.+)"/\1/p' "$@"
       
   120 }
       
   121 
       
   122 ## (cached) include paths for zone file
       
   123 function zone_includes {
       
   124     local cache="$1"
       
   125     local src="$2"
       
   126     local dir="$3"
       
   127 
       
   128     if [ ! -e "$cache" -o "$cache" -ot "$src" ]; then
       
   129         read_zone_includes "$src" > "$cache"
       
   130     fi
       
   131 
       
   132     while read include; do
       
   133         echo -n "$dir/$include "
       
   134     done < "$cache"
       
   135 }