Delete sandbox when container create fails

This patch deletes the sandbox when container create fails.

Partially-Closes-Bug: #1644570
Change-Id: I1d0bc39422109b20f69a21ce0d20799cecb6c2c9
This commit is contained in:
Pradeep Kumar Singh 2016-12-07 05:04:09 +00:00
parent 96dbcead9b
commit 7ea0d85bf4
4 changed files with 37 additions and 0 deletions

View File

@ -190,6 +190,12 @@ class NovaClient(object):
self.client().servers.delete(server_id) self.client().servers.delete(server_id)
return server_id return server_id
def stop_server(self, server):
server_id = self.get_server_id(server, raise_on_error=False)
if server_id:
self.client().servers.stop(server_id)
return server_id
def check_delete_server_complete(self, server_id): def check_delete_server_complete(self, server_id):
"""Wait for server to disappear from Nova.""" """Wait for server to disappear from Nova."""
try: try:

View File

@ -73,6 +73,13 @@ class Manager(object):
reraise=True) reraise=True)
return self._do_container_start(context, created_container) return self._do_container_start(context, created_container)
def _do_sandbox_cleanup(self, context, sandbox_id):
try:
self.driver.delete_sandbox(context, sandbox_id)
except Exception as e:
LOG.error(_LE("Error occured while deleting sandbox: %s"),
six.text_type(e))
def _do_container_create(self, context, container, reraise=False): def _do_container_create(self, context, container, reraise=False):
LOG.debug('Creating container...', context=context, LOG.debug('Creating container...', context=context,
container=container) container=container)
@ -105,6 +112,7 @@ class Manager(object):
except exception.ImageNotFound as e: except exception.ImageNotFound as e:
with excutils.save_and_reraise_exception(reraise=reraise): with excutils.save_and_reraise_exception(reraise=reraise):
LOG.error(six.text_type(e)) LOG.error(six.text_type(e))
self._do_sandbox_cleanup(context, sandbox_id)
self._fail_container(container, six.text_type(e)) self._fail_container(container, six.text_type(e))
return return
except exception.DockerError as e: except exception.DockerError as e:
@ -112,12 +120,14 @@ class Manager(object):
LOG.error(_LE( LOG.error(_LE(
"Error occured while calling docker image API: %s"), "Error occured while calling docker image API: %s"),
six.text_type(e)) six.text_type(e))
self._do_sandbox_cleanup(context, sandbox_id)
self._fail_container(container, six.text_type(e)) self._fail_container(container, six.text_type(e))
return return
except Exception as e: except Exception as e:
with excutils.save_and_reraise_exception(reraise=reraise): with excutils.save_and_reraise_exception(reraise=reraise):
LOG.exception(_LE("Unexpected exception: %s"), LOG.exception(_LE("Unexpected exception: %s"),
six.text_type(e)) six.text_type(e))
self._do_sandbox_cleanup(context, sandbox_id)
self._fail_container(container, six.text_type(e)) self._fail_container(container, six.text_type(e))
return return
@ -135,12 +145,14 @@ class Manager(object):
LOG.error(_LE( LOG.error(_LE(
"Error occured while calling docker create API: %s"), "Error occured while calling docker create API: %s"),
six.text_type(e)) six.text_type(e))
self._do_sandbox_cleanup(context, sandbox_id)
self._fail_container(container, six.text_type(e)) self._fail_container(container, six.text_type(e))
return return
except Exception as e: except Exception as e:
with excutils.save_and_reraise_exception(reraise=reraise): with excutils.save_and_reraise_exception(reraise=reraise):
LOG.exception(_LE("Unexpected exception: %s"), LOG.exception(_LE("Unexpected exception: %s"),
six.text_type(e)) six.text_type(e))
self._do_sandbox_cleanup(context, sandbox_id)
self._fail_container(container, six.text_type(e)) self._fail_container(container, six.text_type(e))
return return

View File

@ -230,6 +230,10 @@ class DockerDriver(driver.ContainerDriver):
with docker_utils.docker_client() as docker: with docker_utils.docker_client() as docker:
docker.remove_container(sandbox_id, force=True) docker.remove_container(sandbox_id, force=True)
def stop_sandbox(self, context, sandbox_id):
with docker_utils.docker_client() as docker:
docker.stop(sandbox_id)
def get_sandbox_id(self, container): def get_sandbox_id(self, container):
if container.meta: if container.meta:
return container.meta.get('sandbox_id', None) return container.meta.get('sandbox_id', None)
@ -301,6 +305,15 @@ class NovaDockerDriver(DockerDriver):
server_id = novaclient.delete_server(server_name) server_id = novaclient.delete_server(server_name)
self._ensure_deleted(novaclient, server_id) self._ensure_deleted(novaclient, server_id)
def stop_sandbox(self, context, sandbox_id):
novaclient = nova.NovaClient(context)
server_name = self._find_server_by_container_id(sandbox_id)
if not server_name:
LOG.warning(_LW("Cannot find server name for sandbox %s") %
sandbox_id)
return
novaclient.stop_server(server_name)
def _ensure_deleted(self, novaclient, server_id, timeout=300): def _ensure_deleted(self, novaclient, server_id, timeout=300):
'''Wait until the Nova instance to be deleted.''' '''Wait until the Nova instance to be deleted.'''
def _check_delete_complete(): def _check_delete_complete():

View File

@ -114,6 +114,12 @@ class ContainerDriver(object):
"""Delete a sandbox.""" """Delete a sandbox."""
raise NotImplementedError() raise NotImplementedError()
# Note: This is not currently used, but
# may be used later
def stop_sandbox(self, context, sandbox_id):
"""Stop a sandbox."""
raise NotImplementedError()
def get_sandbox_id(self, container): def get_sandbox_id(self, container):
"""Retrieve sandbox ID.""" """Retrieve sandbox ID."""
raise NotImplementedError() raise NotImplementedError()