[lxc-devel] [pylxd/master] WIP: Add images.copy_from_config and image.copy_to_lxd.
pcdummy on Github
lxc-bot at linuxcontainers.org
Mon Jul 25 01:16:08 UTC 2016
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 415 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160725/34017bbe/attachment.bin>
-------------- next part --------------
From 974a8952a62b6fae31983f2a76e43f2fc2df1c94 Mon Sep 17 00:00:00 2001
From: Rene Jochum <rene at jochums.at>
Date: Mon, 25 Jul 2016 03:15:04 +0200
Subject: [PATCH] Add images.copy_from_config and image.copy_to_lxd.
Signed-off-by: Rene Jochum <rene at jochums.at>
---
doc/source/images.rst | 8 ++++++++
pylxd/image.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 59 insertions(+)
diff --git a/doc/source/images.rst b/doc/source/images.rst
index 6c22152..c34bdd5 100644
--- a/doc/source/images.rst
+++ b/doc/source/images.rst
@@ -17,6 +17,14 @@ methods:
- `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`.
+ - `copy_from_config(config, wait=False)` - Copy a image from another LXD or from simplestreams.
+ The first argument must provide a 'fingerprint' item in its 'source' item,
+ See the `LXD image post documentation`_ for more.
+ Copy allows arbitary kwargs which means you can also do: copy(source_dict, wait=False, public=True, filename="abc.tar.xz").
+ If you want to copy a private Image from another LXD then that LXD needs to trust the the destination.
+
+
+.. _LXD image post documentation: https://github.com/lxc/lxd/blob/3207c2c67d02b3c7504c118f9af6262747103d65/doc/rest-api.md#post-6
Image attributes
diff --git a/pylxd/image.py b/pylxd/image.py
index 4cca19e..5f0c8dd 100644
--- a/pylxd/image.py
+++ b/pylxd/image.py
@@ -80,7 +80,58 @@ 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_config(cls, client, config, wait=False):
+ """ Create an image from the given configuration.
+
+ See: https://github.com/lxc/lxd/blob/master/doc/rest-api.md#post-6
+ """
+ if not 'source' in config or not 'fingerprint' in config['source']:
+ raise KeyError(
+ 'You need to provide a fingerprint '
+ 'in the config[\'source\'] argument'
+ )
+
+ response = client.api.images.post(json=config)
+ if wait:
+ Operation.wait_for_operation(client, response.json()['operation'])
+ return cls(client, fingerprint=config['source']['fingerprint'])
+
def export(self):
"""Export the image."""
response = self.api.export.get()
return response.content
+
+ def copy_to_lxd(self, new_client, copy_aliases=False, wait=False):
+ """ Copy an image to a another LXD.
+
+ Destination host information is contained in the client
+ connection passed in.
+ """
+ self.sync() # Make sure the object isn't stale
+ response = self.api.secret.post(json={})
+ secret = response.json()['metadata']['metadata']['secret']
+
+ cert = self.client.host_info['environment']['certificate']
+
+ # TODO: this is a hacky way to remove the /1.0 do we have a better one?
+ url = '/'.join(self.client.api._api_endpoint.split('/')[:-1])
+
+ config = {
+ 'filename': self.filename,
+ 'public': self.public,
+ 'auto_update': self.auto_update,
+ 'properties': self.properties,
+
+ 'source': {
+ 'type': 'image',
+ 'mode': 'pull',
+ 'server': url,
+ 'protocol': 'lxd',
+ 'secret': secret,
+ 'certificate': cert,
+ 'fingerprint': self.fingerprint
+ }
+ }
+
+ return new_client.images.create_from_config(config, wait)
More information about the lxc-devel
mailing list