def _do_container_action()

in libcloud/container/drivers/lxd.py [0:0]


    def _do_container_action(self, container, action, timeout, force, stateful):
        """
        change the container state by performing the given action
        action may be either stop, start, restart, freeze or unfreeze
        """

        if action not in LXD_API_STATE_ACTIONS:
            raise ValueError("Invalid action specified")

        # cache the state of the container
        state = container.state

        data = {"action": action, "timeout": timeout}
        data = json.dumps(data)

        # checkout this for stateful:
        # https://discuss.linuxcontainers.org/t/error-in-live-migration/1928
        # looks like we are getting "err":"Unable to perform
        # container live migration. CRIU isn't installed"
        # in the response when stateful is True so remove it for now
        req = "/{}/containers/{}/state".format(self.version, container.name)
        response = self.connection.request(req, method="PUT", data=data)

        response_dict = response.parse_body()

        # a background operation is expected to
        # be returned status_code = 100 --> Operation created
        assert_response(response_dict=response_dict, status_code=100)

        if not timeout:
            timeout = LXDContainerDriver.default_time_out

        try:
            id = response_dict["metadata"]["id"]
            req = "/{}/operations/{}/wait?timeout={}".format(self.version, id, timeout)
            response = self.connection.request(req)

        except BaseHTTPError as err:
            lxd_exception = self._get_lxd_api_exception_for_error(err)
            # if not found assume the operation completed
            if lxd_exception.message != "not found":
                raise lxd_exception

        # if the container is ephemeral and the action is to stop
        # then the container is removed so return sth dummy
        if state == ContainerState.RUNNING and container.extra["ephemeral"] and action == "stop":
            # return a dummy container otherwise we get 404 error
            container = Container(
                driver=self,
                name=container.name,
                id=container.name,
                state=ContainerState.TERMINATED,
                image=None,
                ip_addresses=[],
                extra=None,
            )
            return container

        return self.get_container(id=container.name)