|
1 ## vim: set ft=sh : |
|
2 # |
|
3 # Operations on zonefiles/hosts/whatever |
|
4 |
|
5 ## Hosts |
|
6 ## Update hosts from verbatim from input zone data: |
|
7 # |
|
8 # copy_hosts $ZONES/$zone $DATA/$base |
|
9 # |
|
10 # Writes updated zone to $zone, deps on $base. |
|
11 function copy_hosts { |
|
12 local zone=$1 |
|
13 local base=$2 |
|
14 |
|
15 if check_update $zone $base; then |
|
16 log_update "Copying hosts $zone <- $base..." |
|
17 |
|
18 do_update $zone \ |
|
19 cat $ROOT/$base |
|
20 else |
|
21 log_skip "Copying hosts $zone <- $base: not changed" |
|
22 fi |
|
23 } |
|
24 |
|
25 ## Generate hosts from input zone data using $BIN/process-zone: |
|
26 # |
|
27 # update_hosts $ZONES/$zone $DATA/$base |
|
28 # |
|
29 # Writes process-zone'd data to $zone, deps on $base. |
|
30 function update_hosts { |
|
31 local zone=$1; shift |
|
32 local base=$1; shift |
|
33 |
|
34 if check_update $zone $base; then |
|
35 log_update "Generating hosts $zone <- $base..." |
|
36 |
|
37 do_update $zone \ |
|
38 $BIN/process-zone $PROCESS_ARGS $ROOT/$base "$@" |
|
39 else |
|
40 log_skip "Generating hosts $zone <- $base: not changed" |
|
41 fi |
|
42 } |
|
43 |
|
44 ## Generate new serial for zone using $BIN/update-serial, if the zone data has changed: |
|
45 # |
|
46 # update_serial $zone $deps... |
|
47 # |
|
48 # Supports SERIAL_FORCE/NOOP. |
|
49 # Updates $SERIALS/$zone.serial. |
|
50 function update_serial { |
|
51 local zone=$1; shift |
|
52 |
|
53 local serial=$SERIALS/$zone.serial |
|
54 |
|
55 # test |
|
56 if [ $SERIAL_FORCE ]; then |
|
57 log_force "Updating $serial: forced" |
|
58 |
|
59 do_update_serial $serial |
|
60 |
|
61 elif ! check_update $serial "$@"; then |
|
62 log_skip "Updating $serial: not changed" |
|
63 |
|
64 elif [ $SERIAL_NOOP ]; then |
|
65 log_noop "Updating $serial: skipped" |
|
66 |
|
67 else |
|
68 log_update "Updating $serial..." |
|
69 |
|
70 do_update_serial $serial |
|
71 fi |
|
72 } |
|
73 |
|
74 ## Link serial for zone from given base-zone: |
|
75 # |
|
76 # link_serial $zone $base |
|
77 function link_serial { |
|
78 local zone=$1 |
|
79 local base=$2 |
|
80 |
|
81 local lnk=$SERIALS/$zone.serial |
|
82 local tgt=$SERIALS/$base.serial |
|
83 |
|
84 if check_link $lnk $tgt; then |
|
85 log_update "Linking $lnk -> $tgt..." |
|
86 |
|
87 do_link $lnk $tgt |
|
88 |
|
89 else |
|
90 log_skip "Linking $lnk -> $tgt: not changed" |
|
91 fi |
|
92 } |
|
93 |
|
94 ## Update zone file verbatim from source: |
|
95 # |
|
96 # copy_zone $view $zone [$base] |
|
97 # |
|
98 # Copies changed $DATA/$base zone data to $ZONES/$view/$zone. |
|
99 function copy_zone { |
|
100 local view=$1 |
|
101 local zone=$2 |
|
102 local base=${3:-$zone} |
|
103 |
|
104 local out=$ZONES/$view/$zone |
|
105 local src=$DATA/$base |
|
106 |
|
107 if check_update $out $src; then |
|
108 log_update "Copying $out <- $src..." |
|
109 |
|
110 do_update $out \ |
|
111 cat $ROOT/$src |
|
112 else |
|
113 log_skip "Copying $out <- $src: not changed" |
|
114 fi |
|
115 } |
|
116 |
|
117 ## Expand zone file from source using $BIN/expand-zone: |
|
118 # |
|
119 # update_zone $view $zone [$base] |
|
120 # |
|
121 # Processed $DATA/$base zone data through $BIN/expand-zone, writing output to $ZONES/$view/$zone. |
|
122 function update_zone { |
|
123 local view=$1 |
|
124 local zone=$2 |
|
125 local base=${3:-$zone} |
|
126 |
|
127 local out=$ZONES/$view/$zone |
|
128 local src=$DATA/$base.zone |
|
129 local lnk=$ZONES/$base |
|
130 |
|
131 local serial=$SERIALS/$base.serial |
|
132 |
|
133 if check_update $out $src $serial; then |
|
134 log_update "Generating $out <- $src..." |
|
135 |
|
136 do_update $out \ |
|
137 $BIN/expand-zone $ROOT/$src \ |
|
138 --serial $ROOT/$serial \ |
|
139 --expand zones=$(abspath $ZONES) \ |
|
140 --expand view=$view |
|
141 else |
|
142 log_skip "Generating $out <- $src: not changed" |
|
143 fi |
|
144 } |
|
145 |
|
146 ## Link zone file to ues given shared zone. |
|
147 # |
|
148 # link_zone $view $zone [$base] |
|
149 # |
|
150 # Looks for shared zone at: |
|
151 # $ZONES/$view/$base |
|
152 # $ZONES/common/$base |
|
153 function link_zone { |
|
154 local view=$1 |
|
155 local zone=$2 |
|
156 local base=${3:-$zone} |
|
157 |
|
158 local out=$ZONES/$view/$zone |
|
159 local tgt=$(choose_link $out $ZONES/$view/$base $ZONES/common/$base) |
|
160 |
|
161 if check_link $out $tgt; then |
|
162 log_update "Linking $out -> $tgt..." |
|
163 |
|
164 do_link $out $tgt |
|
165 |
|
166 else |
|
167 log_skip "Linking $out -> $tgt: not changed" |
|
168 fi |
|
169 } |
|
170 |
|
171 ## Test hosts zone for validity: |
|
172 # |
|
173 # check_hosts $DATA/$hosts --check-exempt ... |
|
174 # |
|
175 # Fails if the check fails. |
|
176 function check_hosts { |
|
177 local hosts=$1; shift 1 |
|
178 |
|
179 local cmd=($BIN/process-zone $PROCESS_ARGS $ROOT/$hosts --check-hosts "$@") |
|
180 |
|
181 if "${cmd[@]}" -q; then |
|
182 log_skip "Check $hosts: OK" |
|
183 else |
|
184 log_error " Check $hosts: Failed" |
|
185 |
|
186 indent " " "${cmd[@]}" |
|
187 |
|
188 exit 1 |
|
189 fi |
|
190 } |
|
191 |
|
192 ## Test zone file for validity using named-checkzone: |
|
193 # |
|
194 # check_zone $view $zone $origin |
|
195 # |
|
196 # Uses the zonefile at $ZONES/$view/$zone, loading it with given initial $ORIGIN. |
|
197 # Fails if the check fails. |
|
198 function check_zone { |
|
199 local view=$1 |
|
200 local zone=$2 |
|
201 local origin=$3 |
|
202 |
|
203 local src=$ZONES/$view/$zone |
|
204 |
|
205 local cmd=($NAMED_CHECKZONE $origin $ROOT/$src) |
|
206 |
|
207 # test |
|
208 # XXX: checkzone is very specific about the order of arguments, -q must be first |
|
209 if $NAMED_CHECKZONE -q $origin $ROOT/$src; then |
|
210 log_skip "Check $src ($origin): OK" |
|
211 else |
|
212 log_error " Check $src ($origin): Failed:" |
|
213 |
|
214 indent " " "${cmd[@]}" |
|
215 |
|
216 exit 1 |
|
217 fi |
|
218 } |
|
219 |
|
220 ## Load update zonefiles into bind: |
|
221 # |
|
222 # deploy_zones |
|
223 # |
|
224 # Invokes `rndc reload`, showing its output. |
|
225 function deploy_zones { |
|
226 local msg="Reload zones" |
|
227 |
|
228 if [ $DEPLOY_SKIP ]; then |
|
229 log_skip "$msg: skipped" |
|
230 |
|
231 elif [ ! -r $RNDC_KEY ]; then |
|
232 log_error " $msg: rndc: permission denied: $RNDC_KEY" |
|
233 |
|
234 else |
|
235 log_update "$msg..." |
|
236 |
|
237 # run |
|
238 indent " rndc: " \ |
|
239 $RNDC reload |
|
240 fi |
|
241 } |
|
242 ## Commit changes in $DATA to version control: |
|
243 # |
|
244 # commit_data |
|
245 # |
|
246 # Invokes `hg commit` in the $REPO, first showing the diff. |
|
247 function commit_data { |
|
248 local repo=$REPO |
|
249 local commit_msg="$COMMIT_MSG" |
|
250 |
|
251 local msg="Commit changes in $repo" |
|
252 |
|
253 # operate? |
|
254 if [ $COMMIT_FORCE ]; then |
|
255 log_force "$msg..." |
|
256 |
|
257 do_commit "$commit_msg" |
|
258 |
|
259 elif ! hg_modified; then |
|
260 log_skip "$msg: no changes" |
|
261 |
|
262 elif [ $COMMIT_SKIP ]; then |
|
263 log_noop "$msg: skipped" |
|
264 |
|
265 else |
|
266 log_update "$msg..." |
|
267 |
|
268 do_commit "$commit_msg" |
|
269 fi |
|
270 } |
|
271 |