From 730edd01f60098535abb61fa7d801f826fd1a0d7 Mon Sep 17 00:00:00 2001 From: Guido Trotter Date: Mon, 26 Jan 2009 12:19:19 +0000 Subject: [PATCH] Abstract cleanups in a CLEANUP array Rather than just having two cleanups for the whole script (namely the umounting and removal of the temp directory) we can change cleanups with an array structure, which is then executed in reverse order. This will be used to add more operations that need their own cleanup, in a structured way. Reviewed-by: iustinp --- create.in | 21 ++++++++++++++++++--- 1 files changed, 18 insertions(+), 3 deletions(-) diff --git a/create.in b/create.in index e34c719..7840789 100755 --- a/create.in +++ b/create.in @@ -19,6 +19,20 @@ set -e +CLEANUP=( ) + +cleanup() { + if [ ${#CLEANUP[*]} -gt 0 ]; then + LAST_ELEMENT=$((${#CLEANUP[*]}-1)) + REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0) + for i in $REVERSE_INDEXES; do + ${CLEANUP[$i]} + done + fi +} + +trap cleanup EXIT + DEFAULT_FILE="@sysconfdir@/default/ganeti-instance-debootstrap" if [ -f "$DEFAULT_FILE" ]; then . "$DEFAULT_FILE" @@ -56,7 +70,7 @@ if [ -n "$swapdev" ]; then fi TMPDIR=`mktemp -d` || exit 1 -trap "umount $TMPDIR; rmdir $TMPDIR" EXIT +CLEANUP+=("rmdir $TMPDIR") # If it's not a block device try to mount it via loopback device. # This is needed for file disks. @@ -65,6 +79,7 @@ if [ ! -b $blockdev ]; then MOUNT_OPTIONS="$MOUNT_OPTIONS -o loop" fi mount $MOUNT_OPTIONS $blockdev $TMPDIR +CLEANUP+=("umount $TMPDIR") # remove the cache file if it's old (> 2 weeks) and writable by the owner (the # default due to the standard umask) @@ -135,8 +150,8 @@ elif [ -e $TMPDIR/etc/event.d/tty1 ]; then rm $TMPDIR/etc/event.d/tty1 fi -umount $TMPDIR -rmdir $TMPDIR +# execute cleanups +cleanup trap - EXIT exit 0 -- 1.7.2.5