Merge "Add --force option while deleting containers in Running state"
This commit is contained in:
commit
f068b7d5e8
zun
api/controllers/v1
compute
container
tests/unit/api/controllers/v1
@ -322,7 +322,7 @@ class ContainersController(rest.RestController):
|
||||
|
||||
@pecan.expose('json')
|
||||
@exception.wrap_pecan_controller_exception
|
||||
def delete(self, container_id):
|
||||
def delete(self, container_id, force=False):
|
||||
"""Delete a container.
|
||||
|
||||
:param container_ident: UUID or Name of a container.
|
||||
@ -330,7 +330,7 @@ class ContainersController(rest.RestController):
|
||||
container = _get_container(container_id)
|
||||
check_policy_on_container(container, "container:delete")
|
||||
context = pecan.request.context
|
||||
pecan.request.rpcapi.container_delete(context, container)
|
||||
pecan.request.rpcapi.container_delete(context, container, force)
|
||||
container.destroy()
|
||||
pecan.response.status = 204
|
||||
|
||||
|
@ -36,8 +36,8 @@ class API(rpc_service.API):
|
||||
def container_create(self, context, container):
|
||||
return self._cast('container_create', container=container)
|
||||
|
||||
def container_delete(self, context, container):
|
||||
return self._call('container_delete', container=container)
|
||||
def container_delete(self, context, container, force):
|
||||
return self._call('container_delete', container=container, force=force)
|
||||
|
||||
def container_show(self, context, container):
|
||||
return self._call('container_show', container=container)
|
||||
|
@ -27,12 +27,12 @@ from zun.objects import fields
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
VALID_STATES = {
|
||||
'delete': 'Stopped',
|
||||
'start': 'Stopped',
|
||||
'stop': 'Running',
|
||||
'reboot': 'Running',
|
||||
'pause': 'Running',
|
||||
'unpause': 'Paused',
|
||||
'delete': ['Stopped', 'Error'],
|
||||
'start': ['Stopped'],
|
||||
'stop': ['Running'],
|
||||
'reboot': ['Running'],
|
||||
'pause': ['Running'],
|
||||
'unpause': ['Paused'],
|
||||
}
|
||||
|
||||
|
||||
@ -49,7 +49,7 @@ class Manager(object):
|
||||
container.save()
|
||||
|
||||
def _validate_container_state(self, container, action):
|
||||
if container.status != VALID_STATES[action]:
|
||||
if container.status not in VALID_STATES[action]:
|
||||
raise exception.InvalidStateException(
|
||||
id=container.container_id,
|
||||
action=action,
|
||||
@ -92,12 +92,13 @@ class Manager(object):
|
||||
container.save()
|
||||
|
||||
@translate_exception
|
||||
def container_delete(self, context, container):
|
||||
def container_delete(self, context, container, force):
|
||||
LOG.debug('Deleting container...', context=context,
|
||||
container=container.uuid)
|
||||
try:
|
||||
self._validate_container_state(container, 'delete')
|
||||
self.driver.delete(container)
|
||||
if not force:
|
||||
self._validate_container_state(container, 'delete')
|
||||
self.driver.delete(container, force)
|
||||
return container
|
||||
except exception.DockerError as e:
|
||||
LOG.error(_LE("Error occured while calling docker API: %s"),
|
||||
|
@ -79,14 +79,15 @@ class DockerDriver(driver.ContainerDriver):
|
||||
container.save()
|
||||
return container
|
||||
|
||||
def delete(self, container):
|
||||
def delete(self, container, force):
|
||||
with docker_utils.docker_client() as docker:
|
||||
try:
|
||||
if container.container_id:
|
||||
# TODO(hongbin): handle the case that container_id is not
|
||||
# found in docker. The deletion should continue whitout
|
||||
# exception
|
||||
docker.remove_container(container.container_id)
|
||||
docker.remove_container(container.container_id,
|
||||
force=force)
|
||||
except errors.APIError as e:
|
||||
raise exception.DockerError(error_msg=six.text_type(e))
|
||||
|
||||
|
@ -82,7 +82,7 @@ class ContainerDriver(object):
|
||||
"""Create a container."""
|
||||
raise NotImplementedError()
|
||||
|
||||
def delete(self, container):
|
||||
def delete(self, container, force):
|
||||
"""Delete a container."""
|
||||
raise NotImplementedError()
|
||||
|
||||
|
@ -507,7 +507,7 @@ class TestContainerController(api_base.FunctionalTest):
|
||||
|
||||
self.assertEqual(204, response.status_int)
|
||||
mock_container_delete.assert_called_once_with(
|
||||
mock.ANY, test_container_obj)
|
||||
mock.ANY, test_container_obj, False)
|
||||
mock_destroy.assert_called_once_with()
|
||||
|
||||
@patch('zun.compute.api.API.container_delete')
|
||||
@ -524,7 +524,7 @@ class TestContainerController(api_base.FunctionalTest):
|
||||
|
||||
self.assertEqual(204, response.status_int)
|
||||
mock_container_delete.assert_called_once_with(
|
||||
mock.ANY, test_container_obj)
|
||||
mock.ANY, test_container_obj, False)
|
||||
mock_destroy.assert_called_once_with()
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user