From e49d347ba9b3b5d50a6e8db5c01f4447fc27e480 Mon Sep 17 00:00:00 2001
From: Ben Nemec <bnemec@redhat.com>
Date: Fri, 24 Jun 2016 13:29:19 -0500
Subject: [PATCH] Generalize logic for skipping final image generation

Since the ironic-agent element builds the ramdisk and extracts the
kernel itself, there's no need to actually generate an image at the
end of the process.  Previously the unnecessary image was being
deleted, but this wastes a bunch of time compressing and converting
the image.  It's better to just not create the image at all.

This change adds a noop element called no-final-image that
disk-image-create looks for in the element list and, if found, will
cause it to skip the final image generation.  This is more flexible
than the previous ironic-agent-specific method that would have
required changes to disk-image-create for every element that wanted
to behave similarly.

Note that this cannot be done using an environment variable, because
element environments.d entries do not propagate out to
disk-image-create.  It also doesn't make sense as a user option
because it should be set by the element author, not the user.

Change-Id: I168feb18f0d578b3babbe4784d3ef75e755e1ebd
---
 bin/disk-image-create              | 18 +++++++++---------
 elements/ironic-agent/element-deps |  1 +
 elements/no-final-image/README.rst | 13 +++++++++++++
 3 files changed, 23 insertions(+), 9 deletions(-)
 create mode 100644 elements/no-final-image/README.rst

diff --git a/bin/disk-image-create b/bin/disk-image-create
index 3a913b278..acb504923 100755
--- a/bin/disk-image-create
+++ b/bin/disk-image-create
@@ -450,9 +450,9 @@ for X in ${!IMAGE_TYPES[@]} ; do
   fi
 done
 
-if [[ ! $IMAGE_ELEMENT =~ ironic-agent ]]; then
-    # Prep filesystem by discarding all unused space
-    fstrim_image
+if [[ ! $IMAGE_ELEMENT =~ no-final-image ]]; then
+  # Prep filesystem by discarding all unused space
+  fstrim_image
 fi
 
 # Unmount and cleanup the /mnt and /build subdirectories, to save
@@ -460,8 +460,8 @@ fi
 unmount_image
 cleanup_build_dir
 
-has_raw_type=
-if [[ ! $IMAGE_ELEMENT =~ ironic-agent && "$IS_RAMDISK" == "0" ]]; then
+if [[ (! $IMAGE_ELEMENT =~ no-final-image) && "$IS_RAMDISK" == "0" ]]; then
+  has_raw_type=
   for IMAGE_TYPE in ${IMAGE_TYPES[@]} ; do
     # We have to do raw last because it is destructive
     if [ "$IMAGE_TYPE" = "raw" ]; then
@@ -470,10 +470,10 @@ if [[ ! $IMAGE_ELEMENT =~ ironic-agent && "$IS_RAMDISK" == "0" ]]; then
       compress_and_save_image $IMAGE_NAME.$IMAGE_TYPE
     fi
   done
-fi
-if [ -n "$has_raw_type" ]; then
-  IMAGE_TYPE="raw"
-  compress_and_save_image $IMAGE_NAME.$IMAGE_TYPE
+  if [ -n "$has_raw_type" ]; then
+    IMAGE_TYPE="raw"
+    compress_and_save_image $IMAGE_NAME.$IMAGE_TYPE
+  fi
 fi
 
 # Remove the leftovers, i.e. the temporary image directory.
diff --git a/elements/ironic-agent/element-deps b/elements/ironic-agent/element-deps
index ee8155c45..27cda407b 100644
--- a/elements/ironic-agent/element-deps
+++ b/elements/ironic-agent/element-deps
@@ -1,4 +1,5 @@
 dhcp-all-interfaces
+no-final-image
 package-installs
 pip-and-virtualenv
 pkg-map
diff --git a/elements/no-final-image/README.rst b/elements/no-final-image/README.rst
new file mode 100644
index 000000000..737dc1e86
--- /dev/null
+++ b/elements/no-final-image/README.rst
@@ -0,0 +1,13 @@
+==============
+no-final-image
+==============
+
+This is a noop element which can be used to indicate to diskimage-builder that
+it should not bother creating a final image out of the generated filesystem.
+It is useful in cases where an element handles all of the image building
+itself, such as ironic-agent or Docker images.  In those cases the final image
+normally generated by diskimage-builder is not the desired output, so there's
+no reason to spend time creating it.
+
+Elements that wish to behave this way should include this element in their
+element-deps file.