author | Tero Marttila <terom@paivola.fi> |
Tue, 20 Mar 2012 13:40:33 +0200 | |
changeset 45 | 3d6bf3864d8a |
parent 44 | 8f49e2f51c0d |
child 46 | 3613e93e4fd7 |
permissions | -rwxr-xr-x |
2 | 1 |
#!/bin/bash |
2 |
# vim: set ft=sh : |
|
3 |
||
4 |
set -ue |
|
5 |
||
44 | 6 |
|
7 |
### Paths |
|
2 | 8 |
ROOT=$(pwd) |
9 |
||
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
10 |
# resolve $0 |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
11 |
self=$0 |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
12 |
while [ -L $self ]; do |
31
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
13 |
tgt=$(readlink $self) |
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
14 |
|
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
15 |
if [ "${tgt:0:1}" == "/" ]; then |
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
16 |
self=$tgt |
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
17 |
else |
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
18 |
self=$(dirname $self)/$tgt |
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
19 |
fi |
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
20 |
done |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
21 |
|
44 | 22 |
# Our bin dir, with scripts |
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
23 |
BIN=$(dirname $self) |
2 | 24 |
|
44 | 25 |
# Data files |
21 | 26 |
DATA=settings |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
27 |
ZONES=zones |
21 | 28 |
SERIALS=$DATA |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
29 |
REPO= |
2 | 30 |
|
44 | 31 |
# Script/data args |
2 | 32 |
PROCESS_ARGS='--input-charset latin-1' |
33 |
||
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
34 |
# external progs |
10 | 35 |
NAMED_CHECKZONE=/usr/sbin/named-checkzone |
31
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
36 |
HG=/usr/bin/hg |
27 | 37 |
RNDC=/usr/sbin/rndc |
10 | 38 |
|
44 | 39 |
### Command-line argument handling |
40 |
||
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
41 |
IS_TTY= |
8 | 42 |
|
44 | 43 |
## Options |
44 |
LOG_ERROR=y |
|
31
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
45 |
LOG_WARN=y |
8 | 46 |
LOG=y |
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
47 |
LOG_FORCE=y |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
48 |
LOG_UPDATE=y |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
49 |
LOG_NOOP=y |
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
50 |
LOG_SKIP= |
8 | 51 |
LOG_DEBUG= |
52 |
LOG_CMD= |
|
53 |
||
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
54 |
UPDATE_FORCE= |
8 | 55 |
UPDATE_NOOP= |
56 |
UPDATE_DIFF= |
|
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
57 |
|
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
58 |
SERIAL_NOOP= |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
59 |
SERIAL_FORCE= |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
60 |
|
21 | 61 |
COMMIT_SKIP= |
62 |
COMMIT_FORCE= |
|
22 | 63 |
COMMIT_MSG=' ' |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
64 |
|
27 | 65 |
DEPLOY_SKIP= |
66 |
||
44 | 67 |
## Output command-line argument help. |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
68 |
function help_args { |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
69 |
local prog=$1 |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
70 |
|
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
71 |
cat <<END |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
72 |
Usage: $prog [options] |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
73 |
|
15 | 74 |
General: |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
75 |
-h display this help text |
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
76 |
-d DIR datadir |
8 | 77 |
|
15 | 78 |
Logging: |
8 | 79 |
-q quiet |
80 |
-v verbose |
|
81 |
-D debug |
|
21 | 82 |
-V debug commands |
8 | 83 |
|
15 | 84 |
Updates: |
9 | 85 |
-p show changes |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
86 |
-F force-updates without checking src mtime |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
87 |
-S do not update serial |
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
88 |
-s update serials |
21 | 89 |
-n no-op/mock-update; don't actually change anything; implies -SpC |
90 |
||
91 |
Commit |
|
92 |
-C do not commit changes |
|
93 |
-c commit changes |
|
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
94 |
-m MSG commit message |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
95 |
END |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
96 |
} |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
97 |
|
44 | 98 |
## Parse any command-line arguments, setting the global options vars. |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
99 |
function parse_args { |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
100 |
OPTIND=1 |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
101 |
|
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
102 |
while getopts 'hd:qvDVpFSsnCcm:' opt "$@"; do |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
103 |
case $opt in |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
104 |
h) |
15 | 105 |
help_args $0 |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
106 |
exit 0 |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
107 |
;; |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
108 |
|
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
109 |
d) ROOT="$OPTARG" ;; |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
110 |
|
31
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
111 |
q) |
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
112 |
LOG= |
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
113 |
LOG_WARN= |
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
114 |
LOG_UPDATE= |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
115 |
LOG_FORCE= |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
116 |
LOG_NOOP= |
31
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
117 |
;; |
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
118 |
|
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
119 |
v) LOG_SKIP=y ;; |
16 | 120 |
D) |
121 |
LOG_DEBUG=y |
|
122 |
LOG_INFO=y |
|
123 |
;; |
|
21 | 124 |
V) LOG_CMD=y ;; |
125 |
||
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
126 |
p) UPDATE_DIFF=y ;; |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
127 |
F) UPDATE_FORCE=y ;; |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
128 |
S) SERIAL_NOOP=y ;; |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
129 |
s) SERIAL_FORCE=y ;; |
8 | 130 |
|
131 |
n) |
|
132 |
UPDATE_NOOP=y |
|
9 | 133 |
# implies -Sp |
134 |
UPDATE_DIFF=y |
|
8 | 135 |
SERIAL_NOUPDATE=y |
21 | 136 |
COMMIT_SKIP=y |
27 | 137 |
DEPLOY_SKIP=y |
8 | 138 |
;; |
139 |
||
21 | 140 |
C) COMMIT_SKIP=y ;; |
141 |
c) COMMIT_FORCE=y ;; |
|
142 |
m) COMMIT_MSG="$OPTARG" ;; |
|
8 | 143 |
|
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
144 |
?) |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
145 |
die |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
146 |
;; |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
147 |
esac |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
148 |
|
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
149 |
done |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
150 |
} |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
151 |
|
44 | 152 |
### Logging |
153 |
# Output message to stderr. |
|
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
154 |
function log_msg { |
2 | 155 |
echo "$*" >&2 |
156 |
} |
|
157 |
||
44 | 158 |
# Output message to stderr, optionally with given color, if TTY. |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
159 |
function log_color { |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
160 |
local code=$1; shift |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
161 |
|
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
162 |
if [ $IS_TTY ]; then |
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
163 |
echo $'\e['${code}'m'"$*"$'\e[00m' >&2 |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
164 |
else |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
165 |
echo "$*" >&2 |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
166 |
fi |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
167 |
} |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
168 |
|
44 | 169 |
## Log at various log-levels |
170 |
||
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
171 |
function log_error { |
44 | 172 |
[ $LOG_ERROR ] && log_color '31' "$*" |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
173 |
} |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
174 |
|
31
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
175 |
function log_warn { |
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
176 |
[ $LOG_WARN ] && log_color '33' "$*" || true |
31
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
177 |
} |
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
178 |
|
44 | 179 |
# plain |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
180 |
function log { |
8 | 181 |
[ $LOG ] && log_msg "$*" || true |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
182 |
} |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
183 |
|
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
184 |
function log_force { |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
185 |
[ $LOG_FORCE ] && log_color '2;33' " $*" || true |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
186 |
} |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
187 |
|
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
188 |
function log_update { |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
189 |
[ $LOG_UPDATE ] && log_color '36' " $*" || true |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
190 |
} |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
191 |
|
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
192 |
function log_noop { |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
193 |
[ $LOG_NOOP ] && log_color '2;34' " $*" || true |
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
194 |
} |
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
195 |
|
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
196 |
function log_skip { |
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
197 |
[ $LOG_SKIP ] && log_color '1;34' " $*" || true |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
198 |
} |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
199 |
|
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
200 |
function log_debug { |
8 | 201 |
[ $LOG_DEBUG ] && log_color 32 " $*" || true |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
202 |
} |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
203 |
|
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
204 |
function log_cmd { |
8 | 205 |
[ $LOG_CMD ] && log_color 35 " \$ $*" || true |
206 |
} |
|
207 |
||
44 | 208 |
# Output stacktrace, broken. |
8 | 209 |
function log_stack { |
210 |
local level=1 |
|
211 |
||
212 |
while info=$(caller $level); do |
|
213 |
echo $info | read line sub file |
|
214 |
||
215 |
log_msg "$file:$lineno $sub()" |
|
216 |
||
217 |
level=$(($level + 1)) |
|
218 |
done |
|
219 |
} |
|
220 |
||
44 | 221 |
# Output calling function's name. |
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
222 |
function func_caller { |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
223 |
caller 1 | cut -d ' ' -f 2 |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
224 |
} |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
225 |
|
44 | 226 |
### High-level logging output |
227 |
# Log with func_caller at log_debug |
|
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
228 |
function debug { |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
229 |
printf -v prefix "%s" $(func_caller) |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
230 |
|
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
231 |
log_debug "$prefix: $*" |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
232 |
} |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
233 |
|
44 | 234 |
# Log with func_caller at log_error and exit, intended for internal errors... |
8 | 235 |
function fail { |
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
236 |
log_error "$(func_caller): $*" |
8 | 237 |
|
238 |
exit 2 |
|
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
239 |
} |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
240 |
|
44 | 241 |
# Log at log_error and exit |
2 | 242 |
function die { |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
243 |
log_error "$*" |
2 | 244 |
exit 1 |
245 |
} |
|
246 |
||
44 | 247 |
### Command execution |
248 |
## Execute command, possibly logging its execution. |
|
249 |
# |
|
250 |
# cmd $cmd... |
|
251 |
# |
|
252 |
# Fails if the command returns an error exit code. |
|
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
253 |
function cmd { |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
254 |
log_cmd "$@" |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
255 |
|
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
256 |
"$@" || die "Failed" |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
257 |
} |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
258 |
|
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
259 |
function indent () { |
8 | 260 |
local indent=$1; shift |
261 |
||
18
826103fe3c8c
update: abspath; fixup serials log; indent log_cmd
Tero Marttila <terom@paivola.fi>
parents:
17
diff
changeset
|
262 |
log_cmd "$@" |
826103fe3c8c
update: abspath; fixup serials log; indent log_cmd
Tero Marttila <terom@paivola.fi>
parents:
17
diff
changeset
|
263 |
|
26
d2fdfe98b7b9
update: fix indent leading whitespace; implement update_hosts_meta, but not used - commit workflow becomes a little weird
Tero Marttila <terom@paivola.fi>
parents:
22
diff
changeset
|
264 |
"$@" | sed "s/^/$indent/" |
31
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
265 |
|
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
266 |
return ${PIPESTATUS[0]} |
18
826103fe3c8c
update: abspath; fixup serials log; indent log_cmd
Tero Marttila <terom@paivola.fi>
parents:
17
diff
changeset
|
267 |
} |
826103fe3c8c
update: abspath; fixup serials log; indent log_cmd
Tero Marttila <terom@paivola.fi>
parents:
17
diff
changeset
|
268 |
|
44 | 269 |
|
270 |
### FS utils |
|
271 |
# Create dir in $ROOT if not exists. |
|
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
272 |
function ensure_dir { |
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
273 |
local dir=$1 |
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
274 |
|
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
275 |
if [ ! -d $ROOT/$dir ]; then |
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
276 |
log_warn "Creating output dir: $dir" |
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
277 |
cmd mkdir $ROOT/$dir |
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
278 |
fi |
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
279 |
} |
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
280 |
|
44 | 281 |
## Output absolute path from $ROOT: |
282 |
# |
|
283 |
# abspath $path |
|
284 |
# |
|
18
826103fe3c8c
update: abspath; fixup serials log; indent log_cmd
Tero Marttila <terom@paivola.fi>
parents:
17
diff
changeset
|
285 |
function abspath () { |
44 | 286 |
local path=$1 |
287 |
||
288 |
echo "$ROOT/$path" |
|
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
289 |
} |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
290 |
|
44 | 291 |
### HG wrappers |
292 |
# Run `hg ...` within $REPO. |
|
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
293 |
function hg { |
39
57551a87a93e
update: hardcode $REPO in hg
Tero Marttila <terom@paivola.fi>
parents:
38
diff
changeset
|
294 |
local repo=$REPO; shift |
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
295 |
|
31
2b7aad3466c6
update: log_warn, -q; ensure_dir; link_zone paivola-reverse; indent error exit
Tero Marttila <terom@paivola.fi>
parents:
30
diff
changeset
|
296 |
cmd $HG -R $ROOT/$repo "$@" |
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
297 |
} |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
298 |
|
44 | 299 |
# Does the repo have local modifications? |
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
300 |
function hg_modified { |
39
57551a87a93e
update: hardcode $REPO in hg
Tero Marttila <terom@paivola.fi>
parents:
38
diff
changeset
|
301 |
hg id | grep -q '+' |
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
302 |
} |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
303 |
|
44 | 304 |
# Output possible -u flag for commit. |
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
305 |
function hg_user { |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
306 |
if [ ${SUDO_USER:-} ]; then |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
307 |
echo '-u' "$SUDO_USER" |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
308 |
|
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
309 |
elif [ $HOME ] && [ -e $HOME/.hgrc ]; then |
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
310 |
debug "using .hgrc user" |
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
311 |
echo '' |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
312 |
|
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
313 |
else |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
314 |
echo '-u' "$USER" |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
315 |
fi |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
316 |
} |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
317 |
|
44 | 318 |
# Show changes in repo |
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
319 |
function hg_diff { |
39
57551a87a93e
update: hardcode $REPO in hg
Tero Marttila <terom@paivola.fi>
parents:
38
diff
changeset
|
320 |
hg diff |
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
321 |
} |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
322 |
|
44 | 323 |
## Commit changes in repo, with given message: |
324 |
# |
|
325 |
# hg_commit $msg |
|
326 |
# |
|
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
327 |
function hg_commit { |
44 | 328 |
local msg=$1 |
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
329 |
local user_opt=$(hg_user) |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
330 |
|
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
331 |
debug "$user_opt: $msg" |
39
57551a87a93e
update: hardcode $REPO in hg
Tero Marttila <terom@paivola.fi>
parents:
38
diff
changeset
|
332 |
hg commit $user_opt -m "$msg" |
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
333 |
} |
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
334 |
|
2 | 335 |
|
44 | 336 |
### Dependency-based updates |
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
337 |
|
44 | 338 |
## Compare the given output file with all given source files: |
339 |
# |
|
340 |
# check_update $out ${deps[@]} && do_update $out ... || ... |
|
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
341 |
# |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
342 |
# Returns true if the output file needs to be updated. |
8 | 343 |
function check_update { |
344 |
# target |
|
44 | 345 |
local out=$1; shift |
8 | 346 |
|
44 | 347 |
debug "$out" |
8 | 348 |
|
349 |
# need update? |
|
350 |
local update= |
|
351 |
||
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
352 |
if [ ${#@} == 0 ]; then |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
353 |
debug " update: unknown deps" |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
354 |
update=y |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
355 |
|
44 | 356 |
elif [ ! -e $out ]; then |
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
357 |
debug " update: dest missing" |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
358 |
update=y |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
359 |
|
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
360 |
elif [ $UPDATE_FORCE ]; then |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
361 |
debug " update: forced" |
8 | 362 |
update=y |
363 |
fi |
|
364 |
||
365 |
# check deps |
|
366 |
for dep in "$@"; do |
|
367 |
# don't bother checking if already figured out |
|
368 |
[ $update ] && continue |
|
369 |
||
370 |
# check |
|
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
371 |
if [ ! -e $ROOT/$dep ]; then |
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
372 |
fail "$dst: Missing source: $dep" |
8 | 373 |
|
44 | 374 |
elif [ $ROOT/$out -ot $ROOT/$dep ]; then |
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
375 |
debug " update: $dep" |
8 | 376 |
update=y |
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
377 |
else |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
378 |
debug " check: $dep" |
8 | 379 |
fi |
380 |
done |
|
381 |
||
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
382 |
[ ! $update ] && debug " up-to-date" |
8 | 383 |
|
384 |
# return |
|
385 |
[ $update ] |
|
386 |
} |
|
387 |
||
44 | 388 |
## Generate updated output file from given command's stdout: |
389 |
# |
|
390 |
# do_update $out $BIN/cmd --args |
|
391 |
# |
|
392 |
# Writes output to a temporary .new file, optionally shows a diff of changes, and commits |
|
393 |
# the new version to $out (unless noop'd). |
|
8 | 394 |
function do_update { |
44 | 395 |
local out=$1; shift |
396 |
local tmp=$out.new |
|
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
397 |
|
44 | 398 |
debug "$out" |
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
399 |
cmd "$@" > $ROOT/$tmp |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
400 |
|
8 | 401 |
# compare |
44 | 402 |
if [ -e $ROOT/$out ] && [ $UPDATE_DIFF ]; then |
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
403 |
debug " changes:" |
8 | 404 |
|
405 |
# terse |
|
44 | 406 |
indent " " diff --unified=1 $ROOT/$out $ROOT/$tmp || true |
8 | 407 |
fi |
408 |
||
44 | 409 |
# deploy |
8 | 410 |
if [ $UPDATE_NOOP ]; then |
411 |
# cleanup |
|
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
412 |
debug " no-op" |
8 | 413 |
|
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
414 |
cmd rm $ROOT/$tmp |
8 | 415 |
else |
416 |
# commit |
|
44 | 417 |
debug " deploy" |
8 | 418 |
|
44 | 419 |
cmd mv $ROOT/$tmp $ROOT/$out |
8 | 420 |
fi |
421 |
} |
|
422 |
||
44 | 423 |
## Look for a link target: |
424 |
# |
|
425 |
# find_link $lnk $tgt... |
|
426 |
# |
|
427 |
# Outputs the first given target to exist, skipping any that are the same as the given $lnk. |
|
428 |
# If no $tgt matches, outputs the last one, or '-'. |
|
429 |
function choose_link { |
|
430 |
local lnk=$1; shift |
|
431 |
local tgt=- |
|
432 |
||
433 |
for tgt in "$@"; do |
|
434 |
[ $tgt != $out ] && [ -e $ROOT/$tgt ] && break |
|
435 |
done |
|
436 |
||
437 |
echo $tgt |
|
438 |
} |
|
439 |
||
440 |
||
441 |
## Compare symlink to target: |
|
442 |
# |
|
443 |
# check_link $lnk $tgt && do_link $lnk $tgt || ... |
|
444 |
# |
|
445 |
# Tests if the symlink exists, and the target matches. |
|
446 |
# Fails if the target does not exist. |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
447 |
function check_link { |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
448 |
local lnk=$1 |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
449 |
local tgt=$2 |
44 | 450 |
|
451 |
[ ! -e $ROOT/$tgt ] && fail "$tgt: target does not exist" |
|
14
b883ef452cd8
fix handling of initial serial
Tero Marttila <terom@paivola.fi>
parents:
13
diff
changeset
|
452 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
453 |
[ ! -e $ROOT/$lnk ] || [ $(readlink $ROOT/$lnk) != $ROOT/$tgt ] |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
454 |
} |
8 | 455 |
|
44 | 456 |
## Update symlink to point to target: |
457 |
# |
|
458 |
# do_link $lnk $tgt |
|
459 |
# |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
460 |
function do_link { |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
461 |
local lnk=$1 |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
462 |
local tgt=$2 |
8 | 463 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
464 |
cmd ln -sf $ROOT/$tgt $ROOT/$lnk |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
465 |
} |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
466 |
|
44 | 467 |
## Update .serial number: |
468 |
# |
|
469 |
# do_update_serial $serial |
|
470 |
# |
|
471 |
# Shows old/new serial on debug. |
|
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
472 |
function do_update_serial { |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
473 |
local serial=$1 |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
474 |
|
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
475 |
# read |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
476 |
local old=$(test -e $ROOT/$serial && cat $ROOT/$serial || echo '') |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
477 |
|
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
478 |
|
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
479 |
cmd $BIN/update-serial $ROOT/$serial |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
480 |
|
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
481 |
# read |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
482 |
local new=$(cat $ROOT/$serial) |
8 | 483 |
|
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
484 |
debug " $old -> $new" |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
485 |
} |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
486 |
|
44 | 487 |
## Perform `hg commit` for $DATA |
488 |
function do_commit { |
|
489 |
local msg=$1 |
|
490 |
||
491 |
indent " " hg_diff |
|
492 |
||
493 |
hg_commit "$msg" |
|
494 |
} |
|
495 |
||
496 |
### Hosts |
|
497 |
## Update hosts from verbatim from input zone data: |
|
498 |
# |
|
499 |
# copy_hosts $ZONES/$zone $DATA/$base |
|
500 |
# |
|
501 |
# Writes updated zone to $zone, deps on $base. |
|
502 |
function copy_hosts { |
|
503 |
local zone=$1 |
|
504 |
local base=$2 |
|
505 |
||
506 |
if check_update $zone $base; then |
|
507 |
log_update "Copying hosts $zone <- $base..." |
|
508 |
||
509 |
do_update $zone \ |
|
510 |
cat $ROOT/$base |
|
511 |
else |
|
512 |
log_skip "Copying hosts $zone <- $base: not changed" |
|
513 |
fi |
|
514 |
} |
|
515 |
||
516 |
## Generate hosts from input zone data using $BIN/process-zone: |
|
517 |
# |
|
518 |
# update_hosts $ZONES/$zone $DATA/$base |
|
519 |
# |
|
520 |
# Writes process-zone'd data to $zone, deps on $base. |
|
521 |
function update_hosts { |
|
522 |
local zone=$1; shift |
|
523 |
local base=$1; shift |
|
524 |
||
525 |
if check_update $zone $base; then |
|
526 |
log_update "Generating hosts $zone <- $base..." |
|
527 |
||
528 |
do_update $zone \ |
|
529 |
$BIN/process-zone $PROCESS_ARGS $ROOT/$base "$@" |
|
530 |
else |
|
531 |
log_skip "Generating hosts $zone <- $base: not changed" |
|
532 |
fi |
|
533 |
} |
|
534 |
||
535 |
## Generate new serial for zone using $BIN/update-serial, if the zone data has changed: |
|
536 |
# |
|
537 |
# update_serial $zone $deps... |
|
538 |
# |
|
539 |
# Supports SERIAL_FORCE/NOOP. |
|
540 |
# Updates $SERIALS/$zone.serial. |
|
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
541 |
function update_serial { |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
542 |
local zone=$1; shift |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
543 |
|
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
544 |
local serial=$SERIALS/$zone.serial |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
545 |
|
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
546 |
# test |
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
547 |
if [ $SERIAL_FORCE ]; then |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
548 |
log_force "Updating $serial: forced" |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
549 |
|
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
550 |
do_update_serial $serial |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
551 |
|
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
552 |
elif ! check_update $serial "$@"; then |
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
553 |
log_skip "Updating $serial: not changed" |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
554 |
|
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
555 |
elif [ $SERIAL_NOOP ]; then |
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
556 |
log_noop "Updating $serial: skipped" |
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
557 |
|
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
558 |
else |
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
559 |
log_update "Updating $serial..." |
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
560 |
|
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
561 |
do_update_serial $serial |
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
562 |
fi |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
563 |
} |
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
564 |
|
44 | 565 |
## Link serial for zone from given base-zone: |
566 |
# |
|
567 |
# link_serial $zone $base |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
568 |
function link_serial { |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
569 |
local zone=$1 |
36
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
570 |
local base=$2 |
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
571 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
572 |
local lnk=$SERIALS/$zone.serial |
36
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
573 |
local tgt=$SERIALS/$base.serial |
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
574 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
575 |
if check_link $lnk $tgt; then |
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
576 |
log_update "Linking $lnk -> $tgt..." |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
577 |
|
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
578 |
do_link $lnk $tgt |
36
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
579 |
|
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
580 |
else |
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
581 |
log_skip "Linking $lnk -> $tgt: not changed" |
36
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
582 |
fi |
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
583 |
} |
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
584 |
|
44 | 585 |
## Update zone file verbatim from source: |
586 |
# |
|
587 |
# copy_zone $view $zone [$base] |
|
588 |
# |
|
589 |
# Copies changed $DATA/$base zone data to $ZONES/$view/$zone. |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
590 |
function copy_zone { |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
591 |
local view=$1 |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
592 |
local zone=$2 |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
593 |
local base=${3:-$zone} |
2 | 594 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
595 |
local out=$ZONES/$view/$zone |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
596 |
local src=$DATA/$base |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
597 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
598 |
if check_update $out $src; then |
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
599 |
log_update "Copying $out <- $src..." |
8 | 600 |
|
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
601 |
do_update $out \ |
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
602 |
cat $ROOT/$src |
8 | 603 |
else |
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
604 |
log_skip "Copying $out <- $src: not changed" |
8 | 605 |
fi |
2 | 606 |
} |
607 |
||
44 | 608 |
## Expand zone file from source using $BIN/expand-zone: |
609 |
# |
|
610 |
# update_zone $view $zone [$base] |
|
611 |
# |
|
612 |
# Processed $DATA/$base zone data through $BIN/expand-zone, writing output to $ZONES/$view/$zone. |
|
2 | 613 |
function update_zone { |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
614 |
local view=$1 |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
615 |
local zone=$2 |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
616 |
local base=${3:-$zone} |
8 | 617 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
618 |
local out=$ZONES/$view/$zone |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
619 |
local src=$DATA/$base.zone |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
620 |
local lnk=$ZONES/$base |
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
621 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
622 |
local serial=$SERIALS/$base.serial |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
623 |
|
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
624 |
if check_update $out $src $serial; then |
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
625 |
log_update "Generating $out <- $src..." |
8 | 626 |
|
627 |
do_update $out \ |
|
40 | 628 |
$BIN/expand-zone $ROOT/$src \ |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
629 |
--serial $ROOT/$serial \ |
18
826103fe3c8c
update: abspath; fixup serials log; indent log_cmd
Tero Marttila <terom@paivola.fi>
parents:
17
diff
changeset
|
630 |
--expand zones=$(abspath $ZONES) \ |
8 | 631 |
--expand view=$view |
632 |
else |
|
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
633 |
log_skip "Generating $out <- $src: not changed" |
8 | 634 |
fi |
635 |
} |
|
636 |
||
44 | 637 |
## Link zone file to ues given shared zone. |
638 |
# |
|
639 |
# link_zone $view $zone [$base] |
|
640 |
# |
|
641 |
# Looks for shared zone at: |
|
642 |
# $ZONES/$view/$base |
|
643 |
# $ZONES/common/$base |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
644 |
function link_zone { |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
645 |
local view=$1 |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
646 |
local zone=$2 |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
647 |
local base=${3:-$zone} |
36
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
648 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
649 |
local out=$ZONES/$view/$zone |
44 | 650 |
local tgt=$(choose_link $out $ZONES/$view/$base $ZONES/common/$base) |
36
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
651 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
652 |
if check_link $out $tgt; then |
43
b2ac0fd85828
update: log_force/update/noop/skip, implemented for update_serial
Tero Marttila <terom@paivola.fi>
parents:
42
diff
changeset
|
653 |
log_update "Linking $out -> $tgt..." |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
654 |
|
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
655 |
do_link $out $tgt |
44 | 656 |
|
30
1a8bc67b1eb7
update: link_zones for paivola.*.zone -> paivola.zone
Tero Marttila <terom@paivola.fi>
parents:
29
diff
changeset
|
657 |
else |
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
658 |
log_skip "Linking $out -> $tgt: not changed" |
30
1a8bc67b1eb7
update: link_zones for paivola.*.zone -> paivola.zone
Tero Marttila <terom@paivola.fi>
parents:
29
diff
changeset
|
659 |
fi |
1a8bc67b1eb7
update: link_zones for paivola.*.zone -> paivola.zone
Tero Marttila <terom@paivola.fi>
parents:
29
diff
changeset
|
660 |
} |
1a8bc67b1eb7
update: link_zones for paivola.*.zone -> paivola.zone
Tero Marttila <terom@paivola.fi>
parents:
29
diff
changeset
|
661 |
|
44 | 662 |
## Test hosts zone for validity: |
663 |
# |
|
664 |
# check_hosts $DATA/$hosts --check-exempt ... |
|
665 |
# |
|
666 |
# Fails if the check fails. |
|
13 | 667 |
function check_hosts { |
668 |
local hosts=$1; shift 1 |
|
669 |
||
40 | 670 |
local cmd=($BIN/process-zone $PROCESS_ARGS $ROOT/$hosts --check-hosts "$@") |
13 | 671 |
|
672 |
if "${cmd[@]}" -q; then |
|
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
673 |
log_skip "Check $hosts: OK" |
13 | 674 |
else |
675 |
log_error " Check $hosts: Failed" |
|
676 |
||
677 |
indent " " "${cmd[@]}" |
|
678 |
||
679 |
exit 1 |
|
680 |
fi |
|
681 |
} |
|
682 |
||
44 | 683 |
## Test zone file for validity using named-checkzone: |
684 |
# |
|
685 |
# check_zone $view $zone $origin |
|
686 |
# |
|
687 |
# Uses the zonefile at $ZONES/$view/$zone, loading it with given initial $ORIGIN. |
|
688 |
# Fails if the check fails. |
|
10 | 689 |
function check_zone { |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
690 |
local view=$1 |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
691 |
local zone=$2 |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
692 |
local origin=$3 |
10 | 693 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
694 |
local src=$ZONES/$view/$zone |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
695 |
|
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
696 |
local cmd=($NAMED_CHECKZONE $origin $ROOT/$src) |
13 | 697 |
|
10 | 698 |
# test |
13 | 699 |
# XXX: checkzone is very specific about the order of arguments, -q must be first |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
700 |
if $NAMED_CHECKZONE -q $origin $ROOT/$src; then |
41
baae373b787e
update: log_skip; simplify update_serial args; check_update tests for deps
Tero Marttila <terom@paivola.fi>
parents:
40
diff
changeset
|
701 |
log_skip "Check $src ($origin): OK" |
10 | 702 |
else |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
703 |
log_error " Check $src ($origin): Failed:" |
10 | 704 |
|
13 | 705 |
indent " " "${cmd[@]}" |
706 |
||
707 |
exit 1 |
|
10 | 708 |
fi |
709 |
} |
|
710 |
||
44 | 711 |
## Load update zonefiles into bind: |
712 |
# |
|
713 |
# deploy_zones |
|
714 |
# |
|
715 |
# Invokes `rndc reload`, showing its output. |
|
27 | 716 |
function deploy_zones { |
44 | 717 |
if [ $DEPLOY_SKIP ]; then |
718 |
log_skip "Reload zones: skipped" |
|
719 |
||
720 |
else |
|
721 |
log_update "Reload zones..." |
|
722 |
||
723 |
# run |
|
724 |
indent " rndc: " \ |
|
725 |
$RNDC reload |
|
726 |
fi |
|
27 | 727 |
} |
44 | 728 |
## Commit changes in $DATA to version control: |
729 |
# |
|
730 |
# commit_data |
|
731 |
# |
|
732 |
# Invokes `hg commit` in the $REPO, first showing the diff. |
|
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
733 |
function commit_data { |
39
57551a87a93e
update: hardcode $REPO in hg
Tero Marttila <terom@paivola.fi>
parents:
38
diff
changeset
|
734 |
local repo=$REPO |
44 | 735 |
local commit_msg="$COMMIT_MSG" |
21 | 736 |
|
44 | 737 |
local msg="Commit changes in $repo..." |
21 | 738 |
|
44 | 739 |
# operate? |
740 |
if [ $COMMIT_FORCE ]; then |
|
741 |
log_force "$msg..." |
|
742 |
||
743 |
do_commit "$commit_msg" |
|
744 |
||
745 |
elif ! hg_modified; then |
|
746 |
log_skip "$msg: no changes" |
|
747 |
||
748 |
elif [ $COMMIT_SKIP ]; then |
|
749 |
log_noop "$msg: skipped" |
|
750 |
||
21 | 751 |
else |
44 | 752 |
log_update "msg..." |
753 |
||
754 |
do_commit "$commit_msg" |
|
21 | 755 |
fi |
756 |
} |
|
757 |
||
44 | 758 |
## Site settings, used as arguments to scripts |
759 |
# MX record to generate in hosts --forward-zone |
|
760 |
FORWARD_MX=mail |
|
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
761 |
|
44 | 762 |
# IP network to generate reverse records for in --reverse-zone |
763 |
REVERSE_ZONE=194.197.235 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
764 |
|
44 | 765 |
# Origin domain to generate reverse records for in --reverse-zone |
766 |
REVERSE_DOMAIN=paivola.fi |
|
28
647f6de0d0ac
update: implement -d for $ROOT
Tero Marttila <terom@paivola.fi>
parents:
27
diff
changeset
|
767 |
|
44 | 768 |
# Views used |
769 |
VIEWS=(internal external) |
|
770 |
||
771 |
# Base domain zone for domains |
|
772 |
DOMAIN_BASE=paivola |
|
773 |
||
774 |
# List of actual domains used; will be linked to $DOMAIN_BASE |
|
775 |
DOMAINS=(paivola.fi paivola.net paivola.org paivola.info paivola.mobi xn--pivl-load8j.fi) |
|
776 |
||
777 |
## Operate! |
|
778 |
function run { |
|
779 |
## Hosts |
|
27 | 780 |
# test |
13 | 781 |
log "Testing hosts..." |
44 | 782 |
# data args... |
783 |
check_hosts $DATA/paivola.txt --check-exempt ufc |
|
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
784 |
|
27 | 785 |
# update |
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
786 |
log "Generating host zones..." |
44 | 787 |
# hosts data args... |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
788 |
update_hosts $ZONES/hosts/paivola:internal $DATA/paivola.txt --forward-zone --forward-txt --forward-mx $FORWARD_MX |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
789 |
update_hosts $ZONES/hosts/paivola:external $DATA/paivola.txt --forward-zone |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
790 |
update_hosts $ZONES/hosts/194.197.235 $DATA/paivola.txt --reverse-zone $REVERSE_ZONE --reverse-domain $REVERSE_DOMAIN |
36
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
791 |
|
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
792 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
793 |
update_hosts $ZONES/hosts/10 $DATA/pvl.txt --reverse-zone 10 --reverse-domain pvl -q |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
794 |
update_hosts $ZONES/hosts/192.168 $DATA/pvl.txt --reverse-zone 192.168 --reverse-domain pvl -q |
36
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
795 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
796 |
# XXX: unsupported --forward-zone with pvl.txt |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
797 |
# update_hosts $ZONES/hosts/pvl $DATA/pvl.txt --forward-zone |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
798 |
copy_hosts $ZONES/hosts/pvl $DATA/pvl.txt |
2 | 799 |
|
44 | 800 |
## Includes |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
801 |
log "Copying zone includes..." |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
802 |
# view zone base |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
803 |
copy_zone includes paivola:internal paivola.zone.internal |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
804 |
copy_zone includes paivola:external paivola.zone.external |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
805 |
copy_zone includes paivola.auto paivola.zone.auto |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
806 |
copy_zone includes paivola.services paivola.zone.services |
2 | 807 |
|
44 | 808 |
## Serials |
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
809 |
log "Updating serials..." |
18
826103fe3c8c
update: abspath; fixup serials log; indent log_cmd
Tero Marttila <terom@paivola.fi>
parents:
17
diff
changeset
|
810 |
|
44 | 811 |
# zone deps... |
45
3d6bf3864d8a
update: tidy up run update_serials
Tero Marttila <terom@paivola.fi>
parents:
44
diff
changeset
|
812 |
# includes... |
3d6bf3864d8a
update: tidy up run update_serials
Tero Marttila <terom@paivola.fi>
parents:
44
diff
changeset
|
813 |
update_serial pvl $ZONES/hosts/pvl $DATA/pvl.zone |
3d6bf3864d8a
update: tidy up run update_serials
Tero Marttila <terom@paivola.fi>
parents:
44
diff
changeset
|
814 |
update_serial 10 $ZONES/hosts/10 $DATA/10.zone |
3d6bf3864d8a
update: tidy up run update_serials
Tero Marttila <terom@paivola.fi>
parents:
44
diff
changeset
|
815 |
update_serial 192.168 $ZONES/hosts/192.168 $DATA/192.168.zone |
36
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
816 |
|
45
3d6bf3864d8a
update: tidy up run update_serials
Tero Marttila <terom@paivola.fi>
parents:
44
diff
changeset
|
817 |
update_serial paivola $ZONES/hosts/paivola:* $DATA/paivola.zone \ |
44 | 818 |
$ZONES/includes/paivola:* \ |
819 |
$ZONES/includes/paivola.* |
|
42
ef5bcc4145de
debug with func_caller; update_serial deps
Tero Marttila <terom@paivola.fi>
parents:
41
diff
changeset
|
820 |
|
45
3d6bf3864d8a
update: tidy up run update_serials
Tero Marttila <terom@paivola.fi>
parents:
44
diff
changeset
|
821 |
update_serial 194.197.235 $ZONES/hosts/194.197.235 $DATA/194.197.235.zone |
13 | 822 |
|
44 | 823 |
## Zones |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
824 |
log "Updating zones..." |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
825 |
# view zone base |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
826 |
update_zone internal pvl |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
827 |
update_zone internal paivola |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
828 |
update_zone external paivola |
10 | 829 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
830 |
update_zone internal 10 |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
831 |
update_zone internal 192.168 |
36
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
832 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
833 |
update_zone common 194.197.235 |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
834 |
link_zone internal 194.197.235 |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
835 |
link_zone external 194.197.235 |
36
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
836 |
|
44 | 837 |
## Test |
10 | 838 |
log "Testing zones..." |
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
839 |
# view zone origin |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
840 |
check_zone internal paivola paivola.fi |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
841 |
check_zone external paivola paivola.fi |
36
3208cd6540dc
update: restructure zones/, manage .pvl
Tero Marttila <terom@paivola.fi>
parents:
32
diff
changeset
|
842 |
|
38
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
843 |
check_zone internal 10 10.in-addr.arpa |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
844 |
check_zone internal 192.168 192.168.in-addr.arpa |
39799fc994e9
update: unify [check/copy/update/link]_[hosts/zone/serial]
Tero Marttila <terom@paivola.fi>
parents:
37
diff
changeset
|
845 |
check_zone common 194.197.235 235.197.194.in-addr.arpa |
10 | 846 |
|
44 | 847 |
## Domains... |
848 |
log "Linking domains..." |
|
849 |
for view in "${VIEWS[@]}"; do |
|
850 |
for zone in "${DOMAINS[@]}"; do |
|
851 |
# link |
|
852 |
link_zone $view $zone $DOMAIN_BASE |
|
30
1a8bc67b1eb7
update: link_zones for paivola.*.zone -> paivola.zone
Tero Marttila <terom@paivola.fi>
parents:
29
diff
changeset
|
853 |
|
44 | 854 |
# test |
855 |
check_zone $view $zone $zone |
|
856 |
done |
|
30
1a8bc67b1eb7
update: link_zones for paivola.*.zone -> paivola.zone
Tero Marttila <terom@paivola.fi>
parents:
29
diff
changeset
|
857 |
done |
44 | 858 |
|
859 |
## Deploy |
|
860 |
log "Deploy zones..." |
|
861 |
deploy_zones |
|
862 |
||
863 |
## Commit |
|
864 |
log "Commit data..." |
|
865 |
commit_data |
|
866 |
} |
|
867 |
||
868 |
## Main entry point |
|
869 |
function main { |
|
870 |
# test tty |
|
871 |
[ -t 1 ] && IS_TTY=y |
|
872 |
||
873 |
parse_args "$@" |
|
874 |
||
875 |
## test env |
|
876 |
[ -d $ROOT/$DATA ] || die "Missing data: $ROOT/$DATA" |
|
877 |
ensure_dir $ZONES |
|
878 |
||
879 |
## Output dirs |
|
880 |
for dir in "common" "hosts" "includes" "${VIEWS[@]}"; do |
|
881 |
ensure_dir $ZONES/$dir |
|
30
1a8bc67b1eb7
update: link_zones for paivola.*.zone -> paivola.zone
Tero Marttila <terom@paivola.fi>
parents:
29
diff
changeset
|
882 |
done |
1a8bc67b1eb7
update: link_zones for paivola.*.zone -> paivola.zone
Tero Marttila <terom@paivola.fi>
parents:
29
diff
changeset
|
883 |
|
44 | 884 |
## Go |
885 |
run |
|
2 | 886 |
} |
887 |
||
7
f4457348faa0
update: function update { ... }, color logging, update_serial
Tero Marttila <terom@paivola.fi>
parents:
2
diff
changeset
|
888 |
main "$@" |