[lxc-devel] [pylxd/master] Add image.create_from_simplestreams and image.create_from_url.

pcdummy on Github lxc-bot at linuxcontainers.org
Fri Jul 29 15:34:18 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 477 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160729/1fa5fe27/attachment.bin>
-------------- next part --------------
From 65ba4351b7fabdfff8f9c7449d3bf3a14bcf9487 Mon Sep 17 00:00:00 2001
From: Rene Jochum <rene at jochums.at>
Date: Fri, 29 Jul 2016 17:33:15 +0200
Subject: [PATCH] Add image.create_from_simplestreams and
 image.create_from_url.

Signed-off-by: Rene Jochum <rene at jochums.at>
---
 doc/source/images.rst |  8 +++++++
 pylxd/image.py        | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/doc/source/images.rst b/doc/source/images.rst
index 37d89ce..88f915e 100644
--- a/doc/source/images.rst
+++ b/doc/source/images.rst
@@ -14,9 +14,17 @@ methods:
 
   - `all()` - Retrieve all images.
   - `get()` - Get a specific image, by its fingerprint.
+
+And create through the following methods,
+theres also a copy method on an image:
+
   - `create(data, public=False, wait=False)` - Create a new image. The first
     argument is the binary data of the image itself. If the image is public,
     set `public` to `True`.
+  - `create_from_simplestreams(server, alias, public=False, auto_update=False, wait=False)` -
+    Create an image from simplestreams.
+  - `create_from_url(url, public=False, auto_update=False, wait=False)` -
+    Create an image from a url.
 
 Image attributes
 ----------------
diff --git a/pylxd/image.py b/pylxd/image.py
index 9e27898..2809783 100644
--- a/pylxd/image.py
+++ b/pylxd/image.py
@@ -32,6 +32,10 @@ def _image_create_from_config(client, config, wait=False):
     if wait:
         Operation.wait_for_operation(client, response.json()['operation'])
 
+        return Operation.get(client, response.json()['operation'])
+
+    return None
+
 
 class Image(model.Model):
     """A LXD Image."""
@@ -96,6 +100,60 @@ def create(cls, client, image_data, public=False, wait=False):
             Operation.wait_for_operation(client, response.json()['operation'])
         return cls(client, fingerprint=fingerprint)
 
+    @classmethod
+    def create_from_simplestreams(cls, client, server, alias,
+                                  public=False, auto_update=False, wait=False):
+        """ Copy an image from simplestreams.
+        """
+        config = {
+            'public': public,
+            'auto_update': auto_update,
+
+            'source': {
+                'type': 'image',
+                'mode': 'pull',
+                'server': server,
+                'protocol': 'simplestreams',
+                'fingerprint': alias
+            }
+        }
+
+        op = _image_create_from_config(client, config, wait=wait)
+
+        # XXX: pcdummy (29 Jul 2016) - We might want to remove the wait
+        # argument, theres no way to retrieve the image without wait as people
+        # don't know the fingerprint.
+        if wait:
+            return client.images.get(op['metadata']['fingerprint'])
+
+        return None
+
+    @classmethod
+    def create_from_url(cls, client, url,
+                        public=False, auto_update=False, wait=False):
+        """ Copy an image from an url.
+        """
+        config = {
+            'public': public,
+            'auto_update': auto_update,
+
+            'source': {
+                'type': 'url',
+                'mode': 'pull',
+                'url': url
+            }
+        }
+
+        op = _image_create_from_config(client, config, wait=wait)
+
+        # XXX: pcdummy (29 Jul 2016) - We might want to remove the wait
+        # argument, theres no way to retrieve the image without wait as people
+        # don't know the fingerprint.
+        if wait:
+            return client.images.get(op['metadata']['fingerprint'])
+
+        return None
+
     def export(self):
         """Export the image."""
         response = self.api.export.get()


More information about the lxc-devel mailing list