Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added image creation from public lxd server #378

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions doc/source/images.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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`.
Expand Down
56 changes: 56 additions & 0 deletions pylxd/models/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
10 changes: 10 additions & 0 deletions pylxd/tests/models/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down