[lxc-devel] [pylxd/master] Fix simplestreams

felix-engelmann on Github lxc-bot at linuxcontainers.org
Thu Mar 5 20:25:54 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 486 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200305/db8a1e5e/attachment-0001.bin>
-------------- next part --------------
From 0ca5da604397ce34b618ed23cbca22468133b639 Mon Sep 17 00:00:00 2001
From: Felix Engelmann <fe-github at nlogn.org>
Date: Thu, 17 Oct 2019 13:49:40 +0200
Subject: [PATCH 1/4] added image creation from public lxd server

Signed-off-by: Felix Engelmann <fe-github at nlogn.org>
---
 pylxd/models/image.py            | 21 +++++++++++++++++++++
 pylxd/tests/models/test_image.py | 11 +++++++++++
 2 files changed, 32 insertions(+)

diff --git a/pylxd/models/image.py b/pylxd/models/image.py
index c335d34b..e2294be5 100644
--- a/pylxd/models/image.py
+++ b/pylxd/models/image.py
@@ -156,6 +156,27 @@ def create_from_simplestreams(cls, client, server, alias,
 
         return client.images.get(op.metadata['fingerprint'])
 
+    @classmethod
+    def create_from_image(cls, client, server, fingerprint=None, alias=None,
+                          public=False, auto_update=False):
+        """Copy an image from remote lxd."""
+        config = {
+            'public': public,
+            'auto_update': auto_update,
+            'source': {
+                'type': 'image',
+                'mode': 'pull',
+                'server': server,
+                'protocol': 'lxd',
+                'fingerprint': fingerprint,
+                'alias': alias
+            }
+        }
+
+        op = _image_create_from_config(client, config, wait=True)
+
+        return client.images.get(op.metadata['fingerprint'])
+
     @classmethod
     def create_from_url(cls, client, url,
                         public=False, auto_update=False):
diff --git a/pylxd/tests/models/test_image.py b/pylxd/tests/models/test_image.py
index 60d59e83..d8861047 100644
--- a/pylxd/tests/models/test_image.py
+++ b/pylxd/tests/models/test_image.py
@@ -368,6 +368,17 @@ def test_create_from_simplestreams(self):
             image.fingerprint
         )
 
+    def test_create_from_image(self):
+        """Try to create an image from image at public lxd."""
+        image = self.client.images.create_from_image(
+            'https://images.nlogn.org:8443',
+            alias='debian/8'
+        )
+        self.assertEqual(
+            'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
+            image.fingerprint
+        )
+
     def test_create_from_url(self):
         """Try to create an image from an URL."""
         image = self.client.images.create_from_url(

From 69cf48464c96004cb1880bd17f9573612844a3b0 Mon Sep 17 00:00:00 2001
From: Felix Engelmann <fe-github at nlogn.org>
Date: Fri, 18 Oct 2019 10:42:36 +0200
Subject: [PATCH 2/4] additional parameters and aliases

Signed-off-by: Felix Engelmann <fe-github at nlogn.org>
---
 pylxd/models/image.py | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/pylxd/models/image.py b/pylxd/models/image.py
index e2294be5..7f4727f9 100644
--- a/pylxd/models/image.py
+++ b/pylxd/models/image.py
@@ -158,7 +158,8 @@ def create_from_simplestreams(cls, client, server, alias,
 
     @classmethod
     def create_from_image(cls, client, server, fingerprint=None, alias=None,
-                          public=False, auto_update=False):
+                          public=False, auto_update=False, secret=None,
+                          certificate=None):
         """Copy an image from remote lxd."""
         config = {
             'public': public,
@@ -169,9 +170,13 @@ def create_from_image(cls, client, server, fingerprint=None, alias=None,
                 'server': server,
                 'protocol': 'lxd',
                 'fingerprint': fingerprint,
-                'alias': alias
+                'alias': alias,
+                'secret': secret,
+                'certificate': certificate
             }
         }
+        if alias is not None:
+            config["aliases"] = [{'name': alias}]
 
         op = _image_create_from_config(client, config, wait=True)
 

From bb6eacb9e80cb13e5209980ee55e76079c1939e3 Mon Sep 17 00:00:00 2001
From: Felix Engelmann <fe-github at nlogn.org>
Date: Sat, 19 Oct 2019 19:42:44 +0200
Subject: [PATCH 3/4] added documentation for create_from_image

Signed-off-by: Felix Engelmann <fe-github at nlogn.org>
---
 doc/source/images.rst | 22 ++++++++++++++++++++++
 pylxd/models/image.py | 24 +++++++++++++++++++++++-
 2 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/doc/source/images.rst b/doc/source/images.rst
index 0011d654..46cbea5b 100644
--- a/doc/source/images.rst
+++ b/doc/source/images.rst
@@ -24,6 +24,10 @@ image:
     set `public` to `True`.
   - `create_from_simplestreams(server, alias, public=False, auto_update=False, wait=False)` -
     Create an image from simplestreams.
+  - `create_from_imagecreate_from_image(cls, client, server, fingerprint=None, alias=None,
+                                        public=False, auto_update=False, secret=None,
+                                        certificate=None):` -
+    Create an image from a public lxd instance.
   - `create_from_url(url, public=False, auto_update=False, wait=False)` -
     Create an image from a url.
 
@@ -97,3 +101,21 @@ you may also want to `wait=True`.
     >>> image = client.images.create(image_data, public=True, wait=True)
     >>> image.fingerprint
     'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
+
+You can also download existing images from a remote lxd instance by either their alias or fingerprint.
+
+.. code-block:: python
+
+    >>> image = client.images.create_from_image("https://images.nlogn.org:8443",
+                                                alias='fedora/30', public=False, auto_update=True)
+    >>> image.fingerprint
+    'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
+
+Or fetch an image from a simplestream server with:
+
+.. code-block:: python
+
+    >>> image = client.images.create_from_simplestreams('https://cloud-images.ubuntu.com/releases',
+                                                        'trusty/amd64', public=False, auto_update=True)
+    >>> image.fingerprint
+    'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
diff --git a/pylxd/models/image.py b/pylxd/models/image.py
index 7f4727f9..98873d0b 100644
--- a/pylxd/models/image.py
+++ b/pylxd/models/image.py
@@ -160,7 +160,29 @@ def create_from_simplestreams(cls, client, server, alias,
     def create_from_image(cls, client, server, fingerprint=None, alias=None,
                           public=False, auto_update=False, secret=None,
                           certificate=None):
-        """Copy an image from remote lxd."""
+        """Copy an image from remote lxd.
+        :param client:  the pylxd client
+        :type client: pylxd.Client
+        :param server: URL of the remote LXD-API
+        :type server: str
+        :param fingerprint: The fingerprint of the image to fetch
+            (mandatory if no alias is provided)
+        :type fingerprint: str
+        :param alias: The alias of the image to fetch
+            (mandatory if no fingerprint is provided)
+        :type alias: str
+        :param public: Make the new image public
+        :type public: bool
+        :param auto_update: Set the image to auto-update
+        :type auto_update: bool
+        :param secret: Secret to authenticate to remote lxd instance
+        :type secret: str
+        :param certificate: Optional PEM certificate.
+            If not mentioned, system CA is used.
+        :type certificate: str
+        :returns: newly created image
+        :rtype: pylxd.Image
+        """
         config = {
             'public': public,
             'auto_update': auto_update,

From b54d1997cd0b3f44965eeed355ce4a51c9f2583a Mon Sep 17 00:00:00 2001
From: Felix Engelmann <fe-github at nlogn.org>
Date: Thu, 5 Mar 2020 21:24:07 +0100
Subject: [PATCH 4/4] fixed create image from simplestreams alias and
 fingerprint mixup

Signed-off-by: Felix Engelmann <fe-github at nlogn.org>
---
 pylxd/models/image.py | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/pylxd/models/image.py b/pylxd/models/image.py
index 98873d0b..14390ca2 100644
--- a/pylxd/models/image.py
+++ b/pylxd/models/image.py
@@ -137,7 +137,7 @@ def create(
 
     @classmethod
     def create_from_simplestreams(cls, client, server, alias,
-                                  public=False, auto_update=False):
+                                  public=False, auto_update=False, new_alias=None):
         """Copy an image from simplestreams."""
         config = {
             'public': public,
@@ -147,10 +147,14 @@ def create_from_simplestreams(cls, client, server, alias,
                 'type': 'image',
                 'mode': 'pull',
                 'server': server,
-                'protocol': 'simplestreams',
-                'fingerprint': alias
+                'protocol': 'simplestreams'
             }
         }
+        if alias is not None:
+            config['source']['fingerprint'] = alias
+        if new_alias is not None:
+            config['source']['alias'] = new_alias
+            config["aliases"] = [{'name': new_alias}]
 
         op = _image_create_from_config(client, config, wait=True)
 


More information about the lxc-devel mailing list