18 | 18 |
#
|
19 | 19 |
|
20 | 20 |
set -e
|
|
21 |
shopt -s nullglob
|
21 | 22 |
|
22 | 23 |
###
|
23 | 24 |
### Auxiliary functions
|
24 | 25 |
###
|
25 | 26 |
|
26 | 27 |
function display_usage {
|
27 | |
( echo "Usage: $0 [-h HOST] list"
|
28 | |
echo " $0 [-h HOST] report [milestone]"
|
29 | |
echo " $0 [-h HOST] cron"
|
|
28 |
( echo "Usage: `basename $0` [-h HOST] list"
|
|
29 |
echo " `basename $0` [-h HOST] report [{stable|testing|unstable}-YYYYMMDDHHMM]"
|
|
30 |
echo " `basename $0` [-h HOST] cron"
|
|
31 |
echo " `basename $0` [-h HOST] stabilize [testing-YYYYMMDDHHMM]"
|
30 | 32 |
echo "If -h is specified, /etc/metche/HOST.conf is used instead of /etc/metche.conf."
|
31 | 33 |
echo ""
|
32 | 34 |
) >&2
|
|
37 | 39 |
exit 2
|
38 | 40 |
}
|
39 | 41 |
|
|
42 |
function debug() {
|
|
43 |
[ $DEBUG -eq 0 ] || echo -e "debug: $@" >&2
|
|
44 |
}
|
|
45 |
|
|
46 |
function email() {
|
|
47 |
debug "email $@"
|
|
48 |
SUBJECT="$_MAIL_SUBJECT : $1"
|
|
49 |
if [ $ENCRYPT_EMAIL == "yes" ]; then
|
|
50 |
cat |
|
|
51 |
(LC_ALL="$LOCALE" gpg --batch --armor --encrypt --recipient "$EMAIL_ADDRESS") |
|
|
52 |
(LC_ALL="$LOCALE" mutt -s "$SUBJECT" "$EMAIL_ADDRESS")
|
|
53 |
else
|
|
54 |
cat |
|
|
55 |
(LC_ALL="$LOCALE" mutt -s "$SUBJECT" "$EMAIL_ADDRESS")
|
|
56 |
fi
|
|
57 |
}
|
40 | 58 |
|
41 | 59 |
###
|
42 | 60 |
### Configuration
|
43 | 61 |
###
|
44 | 62 |
|
|
63 |
DEBUG=1
|
45 | 64 |
WATCHED_DIR="/etc"
|
46 | 65 |
BACKUP_DIR="/var/lib/metche"
|
47 | 66 |
CHANGELOG_DIR="/root/changelogs"
|
48 | 67 |
DO_PACKAGES="yes"
|
49 | 68 |
TESTING_TIME="60"
|
|
69 |
STABLE_TIME="3"
|
50 | 70 |
EMAIL_ADDRESS="root@`hostname -f`"
|
51 | 71 |
ENCRYPT_EMAIL="no"
|
52 | 72 |
TAR_OPTS="--exclude *.swp --exclude #* --exclude *~ --exclude=*.key --exclude=ifstate"
|
|
91 | 111 |
|
92 | 112 |
"
|
93 | 113 |
|
94 | |
if [ "x$1" == "x-h" ]; then
|
|
114 |
if [ "$1" == "-h" ]; then
|
95 | 115 |
if [ -f /etc/metche/$2.conf ]; then
|
96 | 116 |
. /etc/metche/$2.conf
|
97 | 117 |
CMD="$3"
|
|
148 | 168 |
###
|
149 | 169 |
### A few functions to do the real work
|
150 | 170 |
###
|
|
171 |
# NB: we often use 'echo' instead of return values because of 'set -e'.
|
|
172 |
|
|
173 |
# Echoes "true" if specified milestone exists, else "false".
|
|
174 |
milestone_exists() {
|
|
175 |
MILESTONE="$1"
|
|
176 |
if [ -f "${BACKUP_DIR}/${MILESTONE}.tar.bz2" -o -L "${BACKUP_DIR}/${MILESTONE}.tar.bz2" ]; then
|
|
177 |
echo "true"
|
|
178 |
else
|
|
179 |
echo "false"
|
|
180 |
fi
|
|
181 |
}
|
|
182 |
|
|
183 |
# Echoes the given milestone's version (i.e. "stable", "testing", "unstable")
|
|
184 |
# if it has a valid version, else "none".
|
|
185 |
# The given milestone can be inexistant.
|
|
186 |
milestone_version() {
|
|
187 |
MILESTONE="$1"
|
|
188 |
VERSION="`echo $MILESTONE | sed 's/-.*$//'`"
|
|
189 |
case $VERSION in
|
|
190 |
stable|testing|unstable)
|
|
191 |
echo $VERSION;;
|
|
192 |
*)
|
|
193 |
echo "none";;
|
|
194 |
esac
|
|
195 |
}
|
|
196 |
|
|
197 |
# Echoes given milestone's date.
|
|
198 |
# Symlinks (e.g.: *-latest) are dereferenced if needed.
|
|
199 |
# The given milestone can be inexistant.
|
|
200 |
milestone_date() {
|
|
201 |
MILESTONE="$1"
|
|
202 |
[ ! -L "${BACKUP_DIR}/${MILESTONE}.tar.bz2" ] || MILESTONE="`readlink ${BACKUP_DIR}/${MILESTONE}.tar.bz2`"
|
|
203 |
echo `basename $MILESTONE` | sed 's/.*-//' | sed 's/\..*$//'
|
|
204 |
}
|
|
205 |
|
|
206 |
# Echoes "true" if the given milestone ($1) is the latest one of its type,
|
|
207 |
# looking at the date in the filename ; else "false".
|
|
208 |
# The given milestone can be inexistant.
|
|
209 |
is_latest() {
|
|
210 |
REF_MILESTONE="$1"
|
|
211 |
REF_DATE="`milestone_date $REF_MILESTONE`"
|
|
212 |
REF_VERSION="`milestone_version $REF_MILESTONE`"
|
|
213 |
for FILE in "${BACKUP_DIR}/${REF_VERSION}-"*.tar.bz2; do
|
|
214 |
MILESTONE=`basename $FILE | sed 's/\.tar\.bz2$//'`
|
|
215 |
if [ `milestone_date $MILESTONE` -gt $REF_DATE ]; then
|
|
216 |
debug "false"
|
|
217 |
echo "false"
|
|
218 |
return
|
|
219 |
fi
|
|
220 |
done
|
|
221 |
echo "true"
|
|
222 |
}
|
151 | 223 |
|
152 | 224 |
# This will save an archive of the watched directory with the given prefix
|
153 | 225 |
save_files() {
|
|
226 |
debug "save_files $@"
|
154 | 227 |
tar jcf "$BACKUP_DIR/$1-$DATE".tar.bz2 \
|
155 | 228 |
-C "$WATCHED_PARENT" $TAR_OPTS `basename "$WATCHED_DIR"`
|
156 | 229 |
ln -sf "$1-$DATE".tar.bz2 "$BACKUP_DIR/$1"-latest.tar.bz2
|
|
158 | 231 |
|
159 | 232 |
# This will save packages list with the given prefix
|
160 | 233 |
save_packages() {
|
|
234 |
debug "save_packages $@"
|
161 | 235 |
$VSERVER_EXEC_PREFIX apt-show-versions -i
|
162 | 236 |
$VSERVER_EXEC_PREFIX apt-show-versions | sort > "$BACKUP_DIR/$1-$DATE".packages
|
163 | 237 |
ln -sf "$1-$DATE".packages "$BACKUP_DIR/$1"-latest.packages
|
|
165 | 239 |
|
166 | 240 |
# This will save Changelogs with the given prefix
|
167 | 241 |
save_changelogs() {
|
|
242 |
debug "save_changelogs $@"
|
168 | 243 |
for file in "$CHANGELOG_DIR"/*/Changelog; do
|
169 | 244 |
CHANGELOG="${file##$CHANGELOG_DIR/}"
|
170 | 245 |
DOMAIN="${CHANGELOG%%/Changelog}"
|
|
176 | 251 |
|
177 | 252 |
# Save whatever reflect the current state with the given prefix
|
178 | 253 |
save_state() {
|
|
254 |
debug "save_state $@"
|
179 | 255 |
save_files "$1"
|
180 | 256 |
[ $DO_PACKAGES == "no" ] || save_packages "$1"
|
181 | 257 |
[ $DO_CHANGELOGS == "no" ] || save_changelogs "$1"
|
|
183 | 259 |
|
184 | 260 |
# Report changes against given version to standard output
|
185 | 261 |
report_changes() {
|
|
262 |
debug "report_changes $@"
|
186 | 263 |
# Just for fancy graphics
|
187 | 264 |
RULER=`echo "$WATCHED_DIR" | tr '[:print:]' '='`
|
188 | 265 |
|
|
269 | 346 |
rm -rf "$TMP" "$TMPDIR"
|
270 | 347 |
}
|
271 | 348 |
|
|
349 |
# Turns into stable the given testing.
|
|
350 |
# NB: argument validity is supposed to have been already checked.
|
|
351 |
stabilize_state() {
|
|
352 |
debug "stabilize_state $@"
|
|
353 |
TESTING="$1"
|
|
354 |
# follow symlink if needed
|
|
355 |
if [ -L "${BACKUP_DIR}/$TESTING".tar.bz2 ]; then
|
|
356 |
TESTING="`readlink ${BACKUP_DIR}/${TESTING}.tar.bz2`"
|
|
357 |
TESTING="`basename $TESTING | sed 's/\..*//'`"
|
|
358 |
fi
|
|
359 |
STABLE="`echo $TESTING | sed 's/^testing/stable/'`"
|
|
360 |
IS_LATEST="false"; [ `is_latest $STABLE` == "false" ] || IS_LATEST="true"
|
|
361 |
for FILE in "${BACKUP_DIR}/${TESTING}"*; do
|
|
362 |
DST="`echo $FILE | sed 's/\/testing-/\/stable-/'`"
|
|
363 |
cp "$FILE" "$DST"
|
|
364 |
# create/change stable-latest* links if, and only if, it's really the latest
|
|
365 |
if [ "$IS_LATEST" == "true" ]; then
|
|
366 |
ln -sf "`basename $DST`" "${BACKUP_DIR}/`basename $DST | sed 's/-[0-9]*\./-latest\./'`"
|
|
367 |
fi
|
|
368 |
done
|
|
369 |
}
|
272 | 370 |
|
273 | 371 |
###
|
274 | 372 |
### Main
|
275 | 373 |
###
|
276 | 374 |
|
277 | |
# Hey, have I been ever launched before ?!
|
278 | |
if [ ! -L "$BACKUP_DIR"/testing-latest.tar.bz2 ]; then
|
279 | |
save_state "testing"
|
280 | |
fi
|
|
375 |
# make sure we've got at least one testing and one stable
|
|
376 |
[ `milestone_exists testing-latest` == "true" ] || save_state "testing"
|
|
377 |
[ `milestone_exists stable-latest` == "true" ] || stabilize_state "testing-latest"
|
281 | 378 |
|
282 | 379 |
case "$CMD" in
|
|
380 |
|
283 | 381 |
report)
|
284 | |
if [ "x$MILESTONE" == "x" ]; then
|
|
382 |
if [ "$MILESTONE" == "" ]; then
|
285 | 383 |
report_changes "testing-latest"
|
|
384 |
elif [ `milestone_exists "$MILESTONE"` == "true" ]; then
|
|
385 |
report_changes "$MILESTONE"
|
286 | 386 |
else
|
287 | |
report_changes "testing-$MILESTONE"
|
|
387 |
display_usage
|
|
388 |
fatal "The specified state does not exist."
|
288 | 389 |
fi
|
289 | 390 |
exit 0
|
290 | 391 |
;;
|
|
392 |
|
291 | 393 |
list)
|
292 | 394 |
for file in "$BACKUP_DIR"/*.tar.bz2; do
|
293 | 395 |
echo `basename ${file%%.tar.bz2}`
|
294 | 396 |
done
|
295 | 397 |
exit 0
|
296 | 398 |
;;
|
|
399 |
|
297 | 400 |
cron)
|
298 | |
# Anyway, backup unstable if there was a change since last unstable
|
299 | |
if [ -f "$BACKUP_DIR"/unstable-latest.tar.bz2 ]; then
|
300 | |
if [ "" != "`find "$WATCHED_DIR" -newer \
|
301 | |
"$BACKUP_DIR"/unstable-latest.tar.bz2 |
|
302 | |
head -1`" ]; then
|
303 | |
save_state "unstable"
|
304 | |
fi
|
305 | |
else
|
306 | |
save_state "unstable"
|
307 | |
fi
|
308 | |
|
309 | |
# Is there any modification since "time to testing"
|
310 | |
# that is more recent than latest testing ?
|
311 | |
if [ "`find "$WATCHED_DIR" -cmin "-$TESTING_TIME" | head -1`" = "" \
|
312 | |
-a "`find "$WATCHED_DIR" -newer \
|
313 | |
"$BACKUP_DIR"/testing-latest.tar.bz2 | head -1`" != "" ]; then
|
314 | |
# Email report
|
315 | |
if [ $ENCRYPT_EMAIL == "yes" ]; then
|
316 | |
report_changes "testing-latest" |
|
317 | |
(LC_ALL="$LOCALE" gpg --batch --armor --encrypt --recipient "$EMAIL_ADDRESS") |
|
318 | |
(LC_ALL="$LOCALE" mutt -s "$_MAIL_SUBJECT" "$EMAIL_ADDRESS")
|
319 | |
else
|
320 | |
report_changes "testing-latest" |
|
321 | |
(LC_ALL="$LOCALE" mutt -s "$_MAIL_SUBJECT" "$EMAIL_ADDRESS")
|
|
401 |
### Algorithm
|
|
402 |
#
|
|
403 |
# if (no change happened for TESTING_TIME) then
|
|
404 |
# if (something has changed since the last testing) then
|
|
405 |
# send a report against last testing
|
|
406 |
# save a new testing state
|
|
407 |
# delete all saved unstable states
|
|
408 |
# elif (no change happened for STABLE_TIME) then
|
|
409 |
# if (something has changed since the last stable) then
|
|
410 |
# save a new stable state and notify EMAIL_ADDRESS
|
|
411 |
# delete all saved testing states older than STABLE_TIME
|
|
412 |
# fi
|
|
413 |
# fi
|
|
414 |
# elif (last unstable exists) then
|
|
415 |
# if (something has changed since the last unstable) then
|
|
416 |
# save a new unstable state
|
|
417 |
# fi
|
|
418 |
# else
|
|
419 |
# save a new unstable state
|
|
420 |
# fi
|
|
421 |
if [ "`find "$WATCHED_DIR" -cmin "-$TESTING_TIME" | head -1`" = "" ]; then
|
|
422 |
if [ "`find "$WATCHED_DIR" -newer "$BACKUP_DIR"/testing-latest.tar.bz2 | head -1`" != "" ]; then
|
|
423 |
report_changes "testing-latest" | email "testing-$DATE"
|
|
424 |
save_state "testing"
|
|
425 |
debug "removing all saved unstable states."
|
|
426 |
find "$BACKUP_DIR" -name 'unstable-*' -exec rm "{}" \;
|
|
427 |
elif [ "`find "$WATCHED_DIR" -ctime "-$STABLE_TIME" | head -1`" = "" ]; then
|
|
428 |
if [ "`find "$WATCHED_DIR" -newer "$BACKUP_DIR"/stable-latest.tar.bz2 | head -1`" != "" ]; then
|
|
429 |
save_state "stable"
|
|
430 |
echo "metche saved a new stable state : stable-${DATE}." | email "stable-$DATE"
|
|
431 |
debug "removing all saved testing states older that STABLE_TIME ($STABLE_TIME)."
|
|
432 |
find "$BACKUP_DIR" -name 'testing-*' -ctime +"$STABLE_TIME" -exec rm "{}" \;
|
|
433 |
fi
|
322 | 434 |
fi
|
323 | |
|
324 | |
# Create new testing
|
325 | |
save_state "testing"
|
326 | |
|
327 | |
# Remove unstable
|
328 | |
find "$BACKUP_DIR" -name 'unstable-*' -print0 | xargs -0 rm
|
329 | |
fi
|
|
435 |
elif [ `milestone_exists unstable-latest` == "true" ]; then
|
|
436 |
if [ "" != "`find "$WATCHED_DIR" -newer "$BACKUP_DIR"/unstable-latest.tar.bz2 | head -1`" ]; then
|
|
437 |
save_state "unstable"
|
|
438 |
fi
|
|
439 |
else
|
|
440 |
save_state "unstable"
|
|
441 |
fi
|
330 | 442 |
;;
|
|
443 |
|
|
444 |
stabilize)
|
|
445 |
if [ "$MILESTONE" == "" ]; then
|
|
446 |
stabilize_state "testing-latest"
|
|
447 |
elif [ "`milestone_version $MILESTONE`" == "testing" -a `milestone_exists $MILESTONE` == "true" ]; then
|
|
448 |
stabilize_state "$MILESTONE"
|
|
449 |
else
|
|
450 |
display_usage
|
|
451 |
fatal "The specified state is not an existing testing state."
|
|
452 |
fi
|
|
453 |
exit 0
|
|
454 |
;;
|
|
455 |
|
|
456 |
test)
|
|
457 |
milestone_version "stable-200507040202"
|
|
458 |
milestone_version "testing-latest"
|
|
459 |
milestone_version "testing-200507030047"
|
|
460 |
milestone_version "testing-200507030047qsfd"
|
|
461 |
milestone_date "stable-200507040202"
|
|
462 |
milestone_date "testing-latest"
|
|
463 |
milestone_date "testing-200507030047"
|
|
464 |
milestone_date "testing-200507030047qsfd"
|
|
465 |
is_latest testing-latest
|
|
466 |
is_latest testing-200507031821
|
|
467 |
is_latest stable-200507031831
|
|
468 |
is_latest stable-200507040202
|
|
469 |
;;
|
|
470 |
|
331 | 471 |
*)
|
332 | 472 |
display_usage
|
333 | 473 |
exit 1
|