From 704a89583154da08084b1c95d6fc98f2c4594ae6 Mon Sep 17 00:00:00 2001
From: Mike McLean <mikem@redhat.com>
Date: Apr 11 2016 16:05:03 +0000
Subject: [PATCH 1/2] first stab at tar-gz image support


---

diff --git a/builder/kojid b/builder/kojid
index 82145a5..c28c4af 100755
--- a/builder/kojid
+++ b/builder/kojid
@@ -3092,6 +3092,7 @@ class BaseImageTask(OzImageTask):
         """
         fcalls = {'raw':   self._buildBase,
                   'raw-xz': self._buildXZ,
+                  'tar-gz': self._buildTarGZ,
                   'liveimg-squashfs': self._buildSquashfs,
                   'vmdk':  self._buildConvert,
                   'vdi':   self._buildConvert,
@@ -3250,6 +3251,38 @@ class BaseImageTask(OzImageTask):
             logerror=1)
         return {'image': newimg}
 
+
+    def _buildTarGZ(self, format):
+        """
+        Use tar and gzip to compress a raw disk image.
+
+        see also: https://cloud.google.com/compute/docs/creating-custom-image
+
+        @args:
+            format - a string representing the image format, "raw-xz"
+        @returns:
+            a dict with some metadata about the image
+        """
+        orig = self.base_img.base_image.data
+        newimg = os.path.join(self.workdir, self.imgname + '.tar.gz')
+
+        # the image in the tarball must be named disk.raw
+        imgdir = os.path.dirname(orig)
+        rawimg = os.path.join(imgdir, 'disk.raw')
+        cmd = ['/bin/ln', newimg, rawimg]
+        conlog = os.path.join(self.workdir,
+            'tar-gz-ln-%s-%s.log' % (format, self.arch))
+        log_output(self.session, cmd[0], cmd, conlog, self.workdir,
+            logerror=1)
+
+        cmd = ['/bin/tar', '-Sczf', newimg, 'disk.raw']
+        conlog = os.path.join(self.workdir,
+            'tar-gz-%s-%s.log' % (format, self.arch))
+        log_output(self.session, cmd[0], cmd, conlog, self.getUploadDir(),
+            logerror=1, cwd=imgdir)
+        return {'image': newimg}
+
+
     def _buildSquashfs(self, format):
         """
         Use squashfs to wrap a raw disk image into liveimg compatible image.

From 5bcfe7034a2112bf98697d3140a91225eaf780d2 Mon Sep 17 00:00:00 2001
From: Mike McLean <mikem@redhat.com>
Date: Apr 11 2016 16:05:03 +0000
Subject: [PATCH 2/2] clean up the tar-gz patch a bit


remove the temp hardlink
don't really need format arg
hook tar-gz format in a bit

---

diff --git a/builder/kojid b/builder/kojid
index c28c4af..c108755 100755
--- a/builder/kojid
+++ b/builder/kojid
@@ -3066,7 +3066,7 @@ class BaseImageTask(OzImageTask):
         Some image formats require others to be processed first, which is why
         we have to do this. raw files in particular may not be kept.
         """
-        supported = ('raw', 'raw-xz', 'liveimg-squashfs', 'vmdk', 'qcow', 'qcow2', 'vdi', 'rhevm-ova', 'vsphere-ova', 'docker', 'vagrant-virtualbox', 'vagrant-libvirt', 'vagrant-vmware-fusion', 'vpc')
+        supported = ('raw', 'raw-xz', 'liveimg-squashfs', 'vmdk', 'qcow', 'qcow2', 'vdi', 'rhevm-ova', 'vsphere-ova', 'docker', 'vagrant-virtualbox', 'vagrant-libvirt', 'vagrant-vmware-fusion', 'vpc', "tar-gz")
         for f in formats:
             if f not in supported:
                 raise koji.ApplianceError('Invalid format: %s' % f)
@@ -3256,30 +3256,30 @@ class BaseImageTask(OzImageTask):
         """
         Use tar and gzip to compress a raw disk image.
 
-        see also: https://cloud.google.com/compute/docs/creating-custom-image
-
         @args:
-            format - a string representing the image format, "raw-xz"
+            format - not used, we only handle tar-gz
         @returns:
             a dict with some metadata about the image
         """
         orig = self.base_img.base_image.data
         newimg = os.path.join(self.workdir, self.imgname + '.tar.gz')
 
+        # see also: https://cloud.google.com/compute/docs/creating-custom-image
+
         # the image in the tarball must be named disk.raw
         imgdir = os.path.dirname(orig)
         rawimg = os.path.join(imgdir, 'disk.raw')
-        cmd = ['/bin/ln', newimg, rawimg]
-        conlog = os.path.join(self.workdir,
-            'tar-gz-ln-%s-%s.log' % (format, self.arch))
-        log_output(self.session, cmd[0], cmd, conlog, self.workdir,
-            logerror=1)
+        os.link(orig, rawimg)
 
-        cmd = ['/bin/tar', '-Sczf', newimg, 'disk.raw']
-        conlog = os.path.join(self.workdir,
-            'tar-gz-%s-%s.log' % (format, self.arch))
+        # make the tarball
+        cmd = ['/bin/tar', '-Sczvf', newimg, 'disk.raw']
+        conlog = os.path.join(self.workdir, 'tar-gz-%s.log' % self.arch)
         log_output(self.session, cmd[0], cmd, conlog, self.getUploadDir(),
             logerror=1, cwd=imgdir)
+
+        # now that we've made the tarball, we don't need this hardlink
+        os.unlink(rawimg)
+
         return {'image': newimg}
 
 
@@ -3518,8 +3518,7 @@ class BaseImageTask(OzImageTask):
         # upload the results
         for format in (f for f in self.formats.keys() if self.formats[f]):
             newimg = images[format]['image']
-            if ('ova' in format or format == 'raw-xz' or
-                    format == 'liveimg-squashfs'):
+            if ('ova' in format or format in ('raw-xz', 'liveimg-squashfs', 'tar-gz')):
                 newname = self.imgname + '.' + format.replace('-', '.')
             elif 'vagrant' in format:
                 # This embeds the vagrant target and the ".box" format in the name
diff --git a/cli/koji b/cli/koji
index 6886343..1cb26cb 100755
--- a/cli/koji
+++ b/cli/koji
@@ -5354,7 +5354,7 @@ def handle_image_build(options, session, args):
     formats = ('vmdk', 'qcow', 'qcow2', 'vdi', 'vpc', 'rhevm-ova',
                'vsphere-ova', 'vagrant-virtualbox', 'vagrant-libvirt',
                'vagrant-vmware-fusion', 'docker', 'raw-xz',
-               'liveimg-squashfs')
+               'liveimg-squashfs', 'tar-gz')
     usage = _("usage: %prog image-build [options] <name> <version> " +
               "<target> <install-tree-url> <arch> [<arch>...]")
     usage += _("\n       %prog image-build --config FILE")

