[libvirt] [sandbox v2 2/5] virt-image-sandbox: tarball extracting function to be reusable

Cédric Bosdonnat cbosdonnat at suse.com
Mon Oct 5 14:37:56 UTC 2015


The virt-builder source will need this function too, let's move it to
the Source abstraction for reusability. Note that the function now
checks for tarball filename to end with ".gz" to add the proper
tar option.
---
 libvirt-sandbox/image/sources/DockerSource.py | 25 +++++--------------------
 libvirt-sandbox/image/sources/Source.py       | 20 ++++++++++++++++++++
 2 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/libvirt-sandbox/image/sources/DockerSource.py b/libvirt-sandbox/image/sources/DockerSource.py
index 93d7f17..0d76717 100644
--- a/libvirt-sandbox/image/sources/DockerSource.py
+++ b/libvirt-sandbox/image/sources/DockerSource.py
@@ -266,7 +266,11 @@ class DockerSource(Source):
             if parentImage is None:
                 self.format_disk(templateImage,format,connect)
 
-            self._extract_tarballs(templatedir + "/" + imagetagid + "/template.",format,connect)
+            path = templatedir + "/" + imagetagid + "/template."
+            self.extract_tarball(path + "qcow2",
+                                 format,
+                                 path + "tar.gz",
+                                 connect)
             parentImage = templateImage
 
 
@@ -302,25 +306,6 @@ class DockerSource(Source):
             imagetagid = parent
         return imagelist
 
-    def _extract_tarballs(self,directory,format,connect):
-        tarfile = directory + "tar.gz"
-        diskfile = directory + "qcow2"
-        cmd = ['virt-sandbox']
-        if connect is not None:
-            cmd.append("-c")
-            cmd.append(connect)
-        cmd.append("-p")
-        params = ['-m',
-                  'host-image:/mnt=%s,format=%s' %(diskfile,format),
-                  '--',
-                  '/bin/tar',
-                  'zxf',
-                  '%s' %tarfile,
-                  '-C',
-                  '/mnt']
-        cmd = cmd + params
-        subprocess.call(cmd)
-
     def delete_template(self, template, templatedir):
         imageusage = {}
         imageparent = {}
diff --git a/libvirt-sandbox/image/sources/Source.py b/libvirt-sandbox/image/sources/Source.py
index 708f2aa..15737c1 100644
--- a/libvirt-sandbox/image/sources/Source.py
+++ b/libvirt-sandbox/image/sources/Source.py
@@ -119,3 +119,23 @@ class Source():
                   '/dev/disk/by-tag/disk_image']
         cmd = cmd + params
         subprocess.call(cmd)
+
+    def extract_tarball(self, diskfile, format, tarfile, connect):
+        cmd = ['virt-sandbox']
+        if connect is not None:
+            cmd.append("-c")
+            cmd.append(connect)
+        cmd.append("-p")
+        compression = ""
+        if tarfile.endswith(".gz"):
+            compression = "z"
+        params = ['-m',
+                  'host-image:/mnt=%s,format=%s' % (diskfile, format),
+                  '--',
+                  '/bin/tar',
+                  'xf%s' % compression,
+                  '%s' % tarfile,
+                  '-C',
+                  '/mnt']
+        cmd = cmd + params
+        subprocess.call(cmd)
-- 
2.1.4




More information about the libvir-list mailing list