[libvirt] [sandbox v2 4/5] virt-sandbox-image: automatically call create if needed

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


To provide a smooth user experience, run automatically calls create if
needed.
---
 libvirt-sandbox/image/cli.py                  |  5 +++++
 libvirt-sandbox/image/sources/DockerSource.py | 19 +++++++++++++++++++
 libvirt-sandbox/image/sources/Source.py       | 11 +++++++++++
 3 files changed, 35 insertions(+)

diff --git a/libvirt-sandbox/image/cli.py b/libvirt-sandbox/image/cli.py
index f067347..05d593a 100755
--- a/libvirt-sandbox/image/cli.py
+++ b/libvirt-sandbox/image/cli.py
@@ -42,6 +42,7 @@ if os.geteuid() == 0:
 else:
     default_template_dir = os.environ['HOME'] + "/.local/share/libvirt/templates"
     default_image_dir = os.environ['HOME'] + "/.local/share/libvirt/images"
+default_format = "qcow2"
 
 debug = False
 verbose = False
@@ -84,6 +85,10 @@ def run(args):
     tmpl = template.Template.from_uri(args.template)
     source = tmpl.get_source_impl()
 
+    # Create the template image if needed
+    if not source.has_template(tmpl, args.template_dir):
+        create(args)
+
     name = args.name
     if name is None:
         randomid = ''.join(random.choice(string.lowercase) for i in range(10))
diff --git a/libvirt-sandbox/image/sources/DockerSource.py b/libvirt-sandbox/image/sources/DockerSource.py
index 1f6f94f..fb21bda 100644
--- a/libvirt-sandbox/image/sources/DockerSource.py
+++ b/libvirt-sandbox/image/sources/DockerSource.py
@@ -59,6 +59,22 @@ class DockerSource(Source):
         if  (major == 2 and sys.hexversion < py2_7_9_hexversion) or (major == 3 and sys.hexversion < py3_4_3_hexversion):
             sys.stderr.write(SSL_WARNING)
 
+    def _was_downloaded(self, template, templatedir):
+        try:
+            self._get_image_list(template, templatedir)
+            return True
+        except Exception:
+            return False
+
+
+    def has_template(self, template, templatedir):
+        try:
+            configfile, diskfile = self._get_template_data(template, templatedir)
+            return os.path.exists(diskfile)
+        except Exception:
+            return False
+
+
     def download_template(self, template, templatedir):
         self._check_cert_validate()
 
@@ -244,6 +260,9 @@ class DockerSource(Source):
     def create_template(self, template, templatedir, connect=None):
         self.download_template(template, templatedir)
 
+        if not self._was_downloaded(template, templatedir):
+            self.download_template(template, templatedir)
+
         imagelist = self._get_image_list(template, templatedir)
         imagelist.reverse()
 
diff --git a/libvirt-sandbox/image/sources/Source.py b/libvirt-sandbox/image/sources/Source.py
index a31bab8..8f6ccba 100644
--- a/libvirt-sandbox/image/sources/Source.py
+++ b/libvirt-sandbox/image/sources/Source.py
@@ -34,6 +34,17 @@ class Source():
     def __init__(self):
         pass
 
+
+    @abstractmethod
+    def has_template(self, template, templatedir):
+        """
+        :param template: libvirt_sandbox.template.Template object
+        :param templatedir: local directory path in which to store the template
+
+        Check if a template has already been created.
+        """
+        pass
+
     @abstractmethod
     def create_template(self, template, templatedir,
                         connect=None):
-- 
2.1.4




More information about the libvir-list mailing list