In update-helper ask before using a cache file
authorNikos Skalkotos <skalkoto@grnet.gr>
Thu, 29 Mar 2012 14:04:46 +0000 (17:04 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Thu, 29 Mar 2012 14:04:46 +0000 (17:04 +0300)
* If -y is not specified and a cache file is found, ask the user before
using it.
* "Fix" the -c option's behaviour. With this commit, if -c is applied,
the system will use the provided cache file if present and will do a
debootstrap if the file does not exist. The result of debootstrap will
be saved in the default cache file provided by the HELPER_CACHE_FILE
variable. This way, we do not need to provide an extra "disable cache
file option". One can run the program with -c "" to get this behaviour.

snf-image-host/common.sh.in
snf-image-host/snf-image-update-helper.in

index fbac8e9..a72ddfe 100644 (file)
@@ -253,6 +253,36 @@ ganeti_os_main() {
 
 }
 
+do_debootstrap() {
+    local target=$1
+
+    echo "Debootstraping to create a new root filesystem:"
+
+    # Create a policy-rc.d file to deny init script execution
+    mkdir -p "$target/usr/sbin"
+    cat > "$target/usr/sbin/policy-rc.d" <<EOF
+#!/bin/sh
+exit 101
+EOF
+    chmod +x "$target/usr/sbin/policy-rc.d"
+
+    debootstrap --arch $(dpkg --print-architecture) \
+        --include "$HELPER_EXTRA_PKGS" --variant=minbase stable "$target" \
+        "$HELPER_MIRROR" 2>&1 | sed -e 's/^/DEBOOTSTRAP: /g'
+
+    rm "$target/usr/sbin/policy-rc.d"
+
+    # remove the downloaded debs, as they are no longer needed
+    find "$target/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
+        xargs -r0 rm -f
+
+    local tmp_cache=$(mktemp "$CACHE_FILE.XXXXXX")
+    tar cf "$tmp_cache" --one-file-system -C "$target" . || \
+        { rm "$tmp_cache"; false; }
+    # Overwrite the default cache file. Not the user specified if present.
+    mv -f "$tmp_cache" "$HELPER_CACHE_FILE"
+}
+
 cleanup() {
 # if something fails here, it souldn't call cleanup again...
     trap - EXIT
index c5cb23c..a3a1e8b 100644 (file)
@@ -57,7 +57,8 @@ EOF
 
 while getopts "c:d:hp:y" opt; do
     case $opt in
-        c) HELPER_CACHE_FILE="$OPTARG"
+        c) CACHE_FILE="$OPTARG"
+        OVERWRITTEN_CACHE_FILE="yes"
             ;;
         d) HELPER_DIR="$OPTARG"
             ;;
@@ -72,6 +73,10 @@ while getopts "c:d:hp:y" opt; do
     esac
 done
 
+if [ x"$OVERWRITTEN_CACHE_FILE" != "xyes" ] ; then
+    CACHE_FILE="$HELPER_CACHE_FILE"
+fi
+
 echo
 echo "This is the update helper image script for snf-image."
 echo "If you don't know what to do, use \`-h'."
@@ -143,38 +148,33 @@ add_cleanup rmdir "$target"
 mount "$root_dev" "$target"
 add_cleanup umount "$root_dev"
 
-echo -n "Checking for cached root filesystem file \`$HELPER_CACHE_FILE'..." 
-if [  -f "$HELPER_CACHE_FILE" ]; then
+echo -n "Checking for cached root filesystem file \`$CACHE_FILE'..."
+if [  -f "$CACHE_FILE" ]; then
     echo "found"
-    tar xf "$HELPER_CACHE_FILE" -C "$target"
-else
-    echo "not found"
-    echo "Debootstraping to create a new root filesystem:"
 
-    # Create a policy-rc.d file to deny init script execution
-    mkdir -p "$target/usr/sbin"
-    cat > "$target/usr/sbin/policy-rc.d" <<EOF
-#!/bin/sh
-exit 101
-EOF
-    chmod +x "$target/usr/sbin/policy-rc.d"
+    while [[ 1 ]]; do
+        echo -n "Use the cached file [Y/n]? "
+        if [ "x$NO_PROMPT" = "xyes" ]; then
+            echo "y";
+            break;
+        fi
 
-    debootstrap --arch $(dpkg --print-architecture) \
-        --include "$HELPER_EXTRA_PKGS" --variant=minbase stable "$target" \
-        "$HELPER_MIRROR" 2>&1 | sed -e 's/^/DEBOOTSTRAP: /g'
-
-    rm "$target/usr/sbin/policy-rc.d"
+        read answer
     
-    # remove the downloaded debs, as they are no longer needed
-    find "$target/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
-        xargs -r0 rm -f
-
-    tmp_cache=$(mktemp "$HELPER_CACHE_FILE.XXXXXX")
-    tar cf "$tmp_cache" --one-file-system -C "$target" . || \
-        { rm "$tmp_cache"; false; }
-    mv -f "$tmp_cache" "$HELPER_CACHE_FILE"
+        if [ -z "$answer" -o "$(tr [A-Z] [a-z] <<< "$answer")" = "y" ]; then
+            break;
+        elif [ "$(tr [A-Z] [a-z] <<< "$answer" )" = "n" ]; then
+            do_debootstrap "$target"
+            break;
+        fi
+    done
+else
+    echo "not found"
+    do_debootstrap "$target"
 fi
 
+tar xf "$HELPER_CACHE_FILE" -C "$target"
+
 echo -n "Configuring the helper image..."
 echo snf-image-helper > "$target/etc/hostname"