コード例 #1
0
 def rename(self, new_name, wait=False):
     """Rename a snapshot."""
     response = self.api.post(json={'name': new_name})
     if wait:
         Operation.wait_for_operation(self.client,
                                      response.json()['operation'])
     self.name = new_name
コード例 #2
0
    def create(cls, client, config, wait=False):
        """Create a new container config."""
        response = client.api.containers.post(json=config)

        if wait:
            Operation.wait_for_operation(client, response.json()['operation'])
        return cls(client, name=config['name'])
コード例 #3
0
    def rename(self, name, wait=False):
        """Rename a container."""
        response = self.api.post(json={'name': name})

        if wait:
            Operation.wait_for_operation(self.client,
                                         response.json()['operation'])
        self.name = name
コード例 #4
0
    def delete(self, wait=False):
        """Delete an object from the server."""
        response = self.api.delete()

        if response.json()['type'] == 'async' and wait:
            Operation.wait_for_operation(self.client,
                                         response.json()['operation'])
        self.client = None
コード例 #5
0
    def create(cls, client, container, name, stateful=False, wait=False):
        response = client.api.containers[container.name].snapshots.post(
            json={
                'name': name,
                'stateful': stateful
            })

        snapshot = cls(client, container=container, name=name)
        if wait:
            Operation.wait_for_operation(client, response.json()['operation'])
        return snapshot
コード例 #6
0
 def _set_state(self, state, timeout=30, force=True, wait=False):
     response = self.api.state.put(json={
         'action': state,
         'timeout': timeout,
         'force': force
     })
     if wait:
         Operation.wait_for_operation(self.client,
                                      response.json()['operation'])
         if 'status' in self.__dirty__:
             del self.__dirty__[self.__dirty__.index('status')]
         self.sync()
コード例 #7
0
    def create(cls, client, image_data, public=False, wait=False):
        """Create an image."""
        fingerprint = hashlib.sha256(image_data).hexdigest()

        headers = {}
        if public:
            headers['X-LXD-Public'] = '1'
        response = client.api.images.post(data=image_data, headers=headers)

        if wait:
            Operation.wait_for_operation(client, response.json()['operation'])
        return cls(client, fingerprint=fingerprint)
コード例 #8
0
def _image_create_from_config(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
    """
    response = client.api.images.post(json=config)
    if wait:
        Operation.wait_for_operation(client, response.json()['operation'])

        return Operation.get(client, response.json()['operation'])

    return response.json()['operation']
コード例 #9
0
    def save(self, wait=False):
        """Save data to the server.

        This method should write the new data to the server via marshalling.
        It should be a no-op when the object is not dirty, to prevent needless
        I/O.
        """
        marshalled = self.marshall()
        response = self.api.put(json=marshalled)

        if response.json()['type'] == 'async' and wait:
            Operation.wait_for_operation(self.client,
                                         response.json()['operation'])
        self.__dirty__.clear()
コード例 #10
0
    def publish(self, public=False, wait=False):
        """Publish a snapshot as an image.

        If wait=True, an Image is returned.

        This functionality is currently broken in LXD. Please see
        https://github.com/lxc/lxd/issues/2201 - The implementation
        here is mostly a guess. Once that bug is fixed, we can verify
        that this works, or file a bug to fix it appropriately.
        """
        data = {
            'public': public,
            'source': {
                'type': 'snapshot',
                'name': '{}/{}'.format(self.container.name, self.name),
            }
        }

        response = self.client.api.images.post(json=data)
        if wait:
            operation = Operation.wait_for_operation(
                self.client,
                response.json()['operation'])
            return self.client.images.get(operation.metadata['fingerprint'])
コード例 #11
0
    def publish(self, public=False, wait=False):
        """Publish a container as an image.

        The container must be stopped in order publish it as an image. This
        method does not enforce that constraint, so a LXDAPIException may be
        raised if this method is called on a running container.

        If wait=True, an Image is returned.
        """
        data = {
            'public': public,
            'source': {
                'type': 'container',
                'name': self.name,
            }
        }

        response = self.client.api.images.post(json=data)
        if wait:
            operation = Operation.wait_for_operation(
                self.client,
                response.json()['operation'])

            return self.client.images.get(operation.metadata['fingerprint'])