Re: [sandbox 09/11] virt-sandbox-image: automatically call download and create if needed

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Sep 23, 2015 at 09:53:39AM +0200, Cédric Bosdonnat wrote:
> To provide a smooth user experience, run automatically calls create if
> needed and create automatically calls download if needed.
> ---
>  libvirt-sandbox/image/cli.py                       | 18 +++++++++++++++---
>  libvirt-sandbox/image/sources/DockerSource.py      | 16 ++++++++++++++++
>  libvirt-sandbox/image/sources/Source.py            | 22 ++++++++++++++++++++++
>  libvirt-sandbox/image/sources/VirtBuilderSource.py |  7 +++++++
>  4 files changed, 60 insertions(+), 3 deletions(-)

This makes sense, though see my previous suggestion about us merging
downkoad+create into a single prepare step, which would simplify this
even more.

> diff --git a/libvirt-sandbox/image/cli.py b/libvirt-sandbox/image/cli.py
> index fb1104a..f4472d9 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
> @@ -86,10 +87,18 @@ def create(args):
>      try:
>          tmpl = template.Template.from_uri(args.template)
>          source = tmpl.get_source_impl()
> +
> +        if not source.was_downloaded(tmpl, args.template_dir):
> +            download(args)
> +
> +        fmt = default_format
> +        if "format" in vars(args):
> +            fmt = args.format

Lets just kill format from the arguments for now and hardcode
qcow2. We can re-think it later when we actually want to
consider using different storage backends.

> +
>          source.create_template(template=tmpl,
>                                 templatedir=args.template_dir,
>                                 connect=args.connect,
> -                               format=args.format)
> +                               format=fmt)
>      except Exception,e:
>          print "Create Error %s" % str(e)
>  
> @@ -97,10 +106,13 @@ def run(args):
>      try:
>          if args.connect is not None:
>              check_connect(args.connect)
> -
>          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))
> @@ -213,7 +225,7 @@ def gen_create_args(subparser):
>      requires_connect(parser)
>      requires_template_dir(parser)
>      parser.add_argument("-f","--format",
> -                        default="qcow2",
> +                        default=default_format,
>                          help=_("format format for image"))
>      parser.set_defaults(func=create)
>  
> diff --git a/libvirt-sandbox/image/sources/DockerSource.py b/libvirt-sandbox/image/sources/DockerSource.py
> index 41df7a7..be9063d 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()
>  
> diff --git a/libvirt-sandbox/image/sources/Source.py b/libvirt-sandbox/image/sources/Source.py
> index 444baa3..e647448 100644
> --- a/libvirt-sandbox/image/sources/Source.py
> +++ b/libvirt-sandbox/image/sources/Source.py
> @@ -35,6 +35,28 @@ class Source():
>          pass
>  
>      @abstractmethod
> +    def was_downloaded(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 downloaded.
> +        """
> +        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 download_template(self, template, templatedir):
>          """
>          :param template: libvirt_sandbox.template.Template object
> diff --git a/libvirt-sandbox/image/sources/VirtBuilderSource.py b/libvirt-sandbox/image/sources/VirtBuilderSource.py
> index 4a7e383..2dde715 100644
> --- a/libvirt-sandbox/image/sources/VirtBuilderSource.py
> +++ b/libvirt-sandbox/image/sources/VirtBuilderSource.py
> @@ -31,6 +31,13 @@ class VirtBuilderSource(Source):
>          # nobody can try to alter the folders structure later.
>          return template.path[1:].replace('/', '_')
>  
> +    def was_downloaded(self, template, templatedir):
> +        return True
> +
> +    def has_template(self, template, templatedir):
> +        imagepath = "%s/%s.qcow2" % (templatedir, template.path)
> +        return os.path.exists(imagepath)
> +
>      def download_template(self, template, templatedir):
>          # We don't do anything here: virt-builder will do it for us in
>          # the create action
> -- 
> 2.1.4
> 
> --
> libvir-list mailing list
> libvir-list@xxxxxxxxxx
> https://www.redhat.com/mailman/listinfo/libvir-list

Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]