diff --git a/doc/source/images.rst b/doc/source/images.rst index 5b7c5f85..5cf71326 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. @@ -99,6 +103,23 @@ you may also want to `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' Finally, delete an image. As this is an asynchonous operation, you may also want to `wait=True`. diff --git a/pylxd/models/image.py b/pylxd/models/image.py index 8eef425f..7d7a3291 100644 --- a/pylxd/models/image.py +++ b/pylxd/models/image.py @@ -157,6 +157,62 @@ 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, + ): + """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, + "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}] + + 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): """Copy an image from an url.""" diff --git a/pylxd/tests/models/test_image.py b/pylxd/tests/models/test_image.py index 8696e65d..d508df63 100644 --- a/pylxd/tests/models/test_image.py +++ b/pylxd/tests/models/test_image.py @@ -391,6 +391,16 @@ 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("https://dl.stgraber.org/lxd")