From 0ca5da604397ce34b618ed23cbca22468133b639 Mon Sep 17 00:00:00 2001 From: Felix Engelmann 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 --- 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 Date: Fri, 18 Oct 2019 10:42:36 +0200 Subject: [PATCH 2/4] additional parameters and aliases Signed-off-by: Felix Engelmann --- 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 Date: Sat, 19 Oct 2019 19:42:44 +0200 Subject: [PATCH 3/4] added documentation for create_from_image Signed-off-by: Felix Engelmann --- 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 c118805805bef89089716cb124b704ac6e7bffc2 Mon Sep 17 00:00:00 2001 From: Felix Engelmann Date: Fri, 23 Apr 2021 11:28:38 +0200 Subject: [PATCH 4/4] Fixed linting and merge conflict failure for classmethod Signed-off-by: Felix Engelmann --- pylxd/models/image.py | 43 +++++++++++++++++++------------- pylxd/tests/models/test_image.py | 7 +++--- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/pylxd/models/image.py b/pylxd/models/image.py index eafc3b89..7d7a3291 100644 --- a/pylxd/models/image.py +++ b/pylxd/models/image.py @@ -158,9 +158,17 @@ def create_from_simplestreams( return client.images.get(op.metadata["fingerprint"]) @classmethod - def create_from_image(cls, client, server, fingerprint=None, alias=None, - public=False, auto_update=False, secret=None, - certificate=None): + 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. :param client: the pylxd client :type client: pylxd.Client @@ -185,26 +193,27 @@ def create_from_image(cls, client, server, fingerprint=None, alias=None, :rtype: pylxd.Image """ config = { - 'public': public, - 'auto_update': auto_update, - 'source': { - 'type': 'image', - 'mode': 'pull', - 'server': server, - 'protocol': 'lxd', - 'fingerprint': fingerprint, - 'alias': alias, - 'secret': secret, - 'certificate': certificate - } + "public": public, + "auto_update": auto_update, + "source": { + "type": "image", + "mode": "pull", + "server": server, + "protocol": "lxd", + "fingerprint": fingerprint, + "alias": alias, + "secret": secret, + "certificate": certificate, + }, } if alias is not None: - config["aliases"] = [{'name': alias}] + config["aliases"] = [{"name": alias}] op = _image_create_from_config(client, config, wait=True) - return client.images.get(op.metadata['fingerprint']) + return client.images.get(op.metadata["fingerprint"]) + @classmethod def create_from_url(cls, client, url, public=False, auto_update=False): """Copy an image from an url.""" config = { diff --git a/pylxd/tests/models/test_image.py b/pylxd/tests/models/test_image.py index 34957b52..d508df63 100644 --- a/pylxd/tests/models/test_image.py +++ b/pylxd/tests/models/test_image.py @@ -394,12 +394,11 @@ def test_create_from_simplestreams(self): 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' + "https://images.nlogn.org:8443", alias="debian/8" ) self.assertEqual( - 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', - image.fingerprint + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + image.fingerprint, ) def test_create_from_url(self):