How to use pylxd - 10 common examples

To help you get started, we’ve selected a few pylxd examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lxc / pylxd / integration / testing.py View on Github external
def delete_container(self, name, enforce=False):
        """Delete a container in lxd."""
        # enforce is a hack. There's a race somewhere in the delete.
        # To ensure we don't get an infinite loop, let's count.
        count = 0
        try:
            result = self.lxd['containers'][name].delete()
        except exceptions.LXDAPIException as e:
            if e.response.status_code in (400, 404):
                return
            raise
        while enforce and result.status_code == 404 and count < 10:
            try:
                result = self.lxd['containers'][name].delete()
            except exceptions.LXDAPIException as e:
                if e.response.status_code in (400, 404):
                    return
                raise
            count += 1
        try:
            operation_uuid = result.json()['operation'].split('/')[-1]
            result = self.lxd.operations[operation_uuid].wait.get()
        except KeyError:
            pass  # 404 cases are okay.
github lxc / pylxd / integration / test_profiles.py View on Github external
def test_delete(self):
        """A profile is deleted."""
        self.profile.delete()

        self.assertRaises(
            exceptions.LXDAPIException,
            self.client.profiles.get, self.profile.name)
github number5 / cloud-init / tests / cloud_tests / platforms / lxd / instance.py View on Github external
def shutdown(self, wait=True, retry=1):
        """Shutdown instance."""
        if self.pylxd_container.status == 'Stopped':
            return

        try:
            LOG.debug("%s: shutting down (wait=%s)", self, wait)
            self.pylxd_container.stop(wait=wait)
        except (pylxd_exc.LXDAPIException, pylxd_exc.NotFound) as e:
            # An exception happens here sometimes (LP: #1783198)
            # LOG it, and try again.
            LOG.warning(
                ("%s: shutdown(retry=%d) caught %s in shutdown "
                 "(response=%s): %s"),
                self, retry, e.__class__.__name__, e.response, e)
            if isinstance(e, pylxd_exc.NotFound):
                LOG.debug("container_exists(%s) == %s",
                          self.name, self.platform.container_exists(self.name))
            if retry == 0:
                raise e
            return self.shutdown(wait=wait, retry=retry - 1)
github lxc / pylxd / integration / test_containers.py View on Github external
def test_delete(self):
        """The container is deleted."""
        self.container.delete(wait=True)

        self.assertRaises(
            exceptions.LXDAPIException,
            self.client.containers.get, self.container.name)
github lxc / pylxd / integration / testing.py View on Github external
def delete_profile(self, name):
        """Delete a profile."""
        try:
            self.lxd.profiles[name].delete()
        except exceptions.LXDAPIException as e:
            if e.response.status_code == 404:
                return
            raise
github number5 / cloud-init / tests / cloud_tests / platforms / lxd / instance.py View on Github external
def shutdown(self, wait=True, retry=1):
        """Shutdown instance."""
        if self.pylxd_container.status == 'Stopped':
            return

        try:
            LOG.debug("%s: shutting down (wait=%s)", self, wait)
            self.pylxd_container.stop(wait=wait)
        except (pylxd_exc.LXDAPIException, pylxd_exc.NotFound) as e:
            # An exception happens here sometimes (LP: #1783198)
            # LOG it, and try again.
            LOG.warning(
                ("%s: shutdown(retry=%d) caught %s in shutdown "
                 "(response=%s): %s"),
                self, retry, e.__class__.__name__, e.response, e)
            if isinstance(e, pylxd_exc.NotFound):
                LOG.debug("container_exists(%s) == %s",
                          self.name, self.platform.container_exists(self.name))
            if retry == 0:
                raise e
            return self.shutdown(wait=wait, retry=retry - 1)
github lxc / pylxd / integration / test_networks.py View on Github external
def test_delete(self):
        """A network is deleted"""
        self.network.delete()

        self.assertRaises(
            exceptions.LXDAPIException,
            self.client.networks.get, self.network.name)
github canonical / cloud-init / tests / cloud_tests / platforms / lxd / platform.py View on Github external
def __init__(self, config):
        """Set up platform."""
        super(LXDPlatform, self).__init__(config)
        # TODO: allow configuration of remote lxd host via env variables
        # set up lxd connection
        self.client = Client()
github lxdock / lxdock / tests / integration / test_container.py View on Github external
def mock_create(container_config, *args, **kwargs):
            """Mocks the container create call, returns the mock container object
            and also ensures that the container_config is correct
            """
            config = container_config['config']
            # Values below should not be driven by the values in container_options
            assert config['security.privileged'] != 'invalid'
            assert config['user.lxdock.homedir'] != 'invalid'
            assert config['user.lxdock.made'] != 'invalid'
            # Value below is a custom value that should be passed from container_options
            assert config['valid_key'] == 'valid_value'
            return cont_return

        client_mock = unittest.mock.Mock(**{
            'containers.create.side_effect': mock_create,
            'containers.get.side_effect': NotFound(''),
        })

        container = Container('myproject', THIS_DIR, client_mock, **container_options)

        assert container._get_container() is cont_return
        assert client_mock.containers.get.called
        assert client_mock.containers.create.called
github lxc / pylxd / integration / test_containers.py View on Github external
def test_migrate_stopped(self):
        """A stopped container is migrated."""
        from pylxd.client import Client

        first_host = 'https://10.0.3.111:8443/'
        second_host = 'https://10.0.3.222:8443/'

        client1 = \
            Client(endpoint=first_host, verify=False)
        client1.authenticate('password')

        client2 = \
            Client(endpoint=second_host, verify=False)
        client2.authenticate('password')
        an_container = \
            client1.containers.get(self.container.name)
        an_migrated_container = \
            an_container.migrate(client2, wait=True)

        self.assertEqual(an_container.name,
                         an_migrated_container.name)
        self.assertEqual(client2,
                         an_migrated_container.client)