Catch more specific exception in compute manager
Currently in compute manager, we simply treat all the exception as general 'Exception', we should catch more specific exception. Change-Id: Id20c663eed308f1c0d94b3528f3694ad8c532226
This commit is contained in:
parent
4b389bc03b
commit
6983c1f88f
@ -345,3 +345,7 @@ class ContainerRunningException(ZunException):
|
|||||||
message = _("The container %(id)s is running."
|
message = _("The container %(id)s is running."
|
||||||
"Please stop and delete the container.")
|
"Please stop and delete the container.")
|
||||||
code = 409
|
code = 409
|
||||||
|
|
||||||
|
|
||||||
|
class DockerError(ZunException):
|
||||||
|
message = _("Docker internal error: %(error_msg)s.")
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import six
|
||||||
|
|
||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
|
|
||||||
from zun.common import exception
|
from zun.common import exception
|
||||||
@ -32,6 +34,11 @@ class Manager(object):
|
|||||||
super(Manager, self).__init__()
|
super(Manager, self).__init__()
|
||||||
self.driver = driver.load_container_driver(container_driver)
|
self.driver = driver.load_container_driver(container_driver)
|
||||||
|
|
||||||
|
def _fail_container(self, container):
|
||||||
|
container.status = fields.ContainerStatus.ERROR
|
||||||
|
container.task_state = None
|
||||||
|
container.save()
|
||||||
|
|
||||||
def container_create(self, context, container):
|
def container_create(self, context, container):
|
||||||
utils.spawn_n(self._do_container_create, context, container)
|
utils.spawn_n(self._do_container_create, context, container)
|
||||||
|
|
||||||
@ -43,21 +50,26 @@ class Manager(object):
|
|||||||
container.save()
|
container.save()
|
||||||
try:
|
try:
|
||||||
self.driver.pull_image(container.image)
|
self.driver.pull_image(container.image)
|
||||||
|
except exception.DockerError as e:
|
||||||
|
LOG.error(_LE("Error occured while calling docker API: %s"),
|
||||||
|
six.text_type(e))
|
||||||
|
self._fail_container(container)
|
||||||
|
return
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||||
container.status = fields.ContainerStatus.ERROR
|
self._fail_container(container)
|
||||||
container.task_state = None
|
|
||||||
container.save()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
container.task_state = fields.TaskState.CONTAINER_CREATING
|
container.task_state = fields.TaskState.CONTAINER_CREATING
|
||||||
container.save()
|
container.save()
|
||||||
try:
|
try:
|
||||||
container = self.driver.create(container)
|
container = self.driver.create(container)
|
||||||
|
except exception.DockerError as e:
|
||||||
|
LOG.error(_LE("Error occured while calling docker API: %s"),
|
||||||
|
six.text_type(e))
|
||||||
|
container.status = fields.ContainerStatus.ERROR
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||||
if not isinstance(e, exception.ZunException):
|
|
||||||
e = exception.ZunException("Unexpected Error: %s" % str(e))
|
|
||||||
container.status = fields.ContainerStatus.ERROR
|
container.status = fields.ContainerStatus.ERROR
|
||||||
finally:
|
finally:
|
||||||
container.task_state = None
|
container.task_state = None
|
||||||
@ -70,6 +82,10 @@ class Manager(object):
|
|||||||
try:
|
try:
|
||||||
self.driver.delete(container)
|
self.driver.delete(container)
|
||||||
return container
|
return container
|
||||||
|
except exception.DockerError as e:
|
||||||
|
LOG.error(_LE("Error occured while calling docker API: %s"),
|
||||||
|
six.text_type(e))
|
||||||
|
raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||||
if e.response.status_code == 409:
|
if e.response.status_code == 409:
|
||||||
@ -82,6 +98,10 @@ class Manager(object):
|
|||||||
LOG.debug('Listing container...', context=context)
|
LOG.debug('Listing container...', context=context)
|
||||||
try:
|
try:
|
||||||
return self.driver.list()
|
return self.driver.list()
|
||||||
|
except exception.DockerError as e:
|
||||||
|
LOG.error(_LE("Error occured while calling docker API: %s"),
|
||||||
|
six.text_type(e))
|
||||||
|
raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||||
raise e
|
raise e
|
||||||
@ -94,6 +114,10 @@ class Manager(object):
|
|||||||
container = self.driver.show(container)
|
container = self.driver.show(container)
|
||||||
container.save()
|
container.save()
|
||||||
return container
|
return container
|
||||||
|
except exception.DockerError as e:
|
||||||
|
LOG.error(_LE("Error occured while calling docker API: %s"),
|
||||||
|
six.text_type(e))
|
||||||
|
raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||||
raise e
|
raise e
|
||||||
@ -106,6 +130,10 @@ class Manager(object):
|
|||||||
container = self.driver.reboot(container)
|
container = self.driver.reboot(container)
|
||||||
container.save()
|
container.save()
|
||||||
return container
|
return container
|
||||||
|
except exception.DockerError as e:
|
||||||
|
LOG.error(_LE("Error occured while calling docker API: %s"),
|
||||||
|
six.text_type(e))
|
||||||
|
raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||||
raise e
|
raise e
|
||||||
@ -118,6 +146,10 @@ class Manager(object):
|
|||||||
container = self.driver.stop(container)
|
container = self.driver.stop(container)
|
||||||
container.save()
|
container.save()
|
||||||
return container
|
return container
|
||||||
|
except exception.DockerError as e:
|
||||||
|
LOG.error(_LE("Error occured while calling docker API: %s"),
|
||||||
|
six.text_type(e))
|
||||||
|
raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||||
raise e
|
raise e
|
||||||
@ -130,6 +162,10 @@ class Manager(object):
|
|||||||
container = self.driver.start(container)
|
container = self.driver.start(container)
|
||||||
container.save()
|
container.save()
|
||||||
return container
|
return container
|
||||||
|
except exception.DockerError as e:
|
||||||
|
LOG.error(_LE("Error occured while calling docker API: %s"),
|
||||||
|
six.text_type(e))
|
||||||
|
raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||||
raise e
|
raise e
|
||||||
@ -142,6 +178,10 @@ class Manager(object):
|
|||||||
container = self.driver.pause(container)
|
container = self.driver.pause(container)
|
||||||
container.save()
|
container.save()
|
||||||
return container
|
return container
|
||||||
|
except exception.DockerError as e:
|
||||||
|
LOG.error(_LE("Error occured while calling docker API: %s"),
|
||||||
|
six.text_type(e))
|
||||||
|
raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.exception(_LE("Unexpected exception: %s,"), str(e))
|
LOG.exception(_LE("Unexpected exception: %s,"), str(e))
|
||||||
raise e
|
raise e
|
||||||
@ -154,6 +194,10 @@ class Manager(object):
|
|||||||
container = self.driver.unpause(container)
|
container = self.driver.unpause(container)
|
||||||
container.save()
|
container.save()
|
||||||
return container
|
return container
|
||||||
|
except exception.DockerError as e:
|
||||||
|
LOG.error(_LE("Error occured while calling docker API: %s"),
|
||||||
|
six.text_type(e))
|
||||||
|
raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||||
raise e
|
raise e
|
||||||
@ -164,6 +208,10 @@ class Manager(object):
|
|||||||
container=container)
|
container=container)
|
||||||
try:
|
try:
|
||||||
return self.driver.show_logs(container)
|
return self.driver.show_logs(container)
|
||||||
|
except exception.DockerError as e:
|
||||||
|
LOG.error(_LE("Error occured while calling docker API: %s"),
|
||||||
|
six.text_type(e))
|
||||||
|
raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||||
raise e
|
raise e
|
||||||
@ -175,6 +223,10 @@ class Manager(object):
|
|||||||
container=container)
|
container=container)
|
||||||
try:
|
try:
|
||||||
return self.driver.execute(container, command)
|
return self.driver.execute(container, command)
|
||||||
|
except exception.DockerError as e:
|
||||||
|
LOG.error(_LE("Error occured while calling docker API: %s"),
|
||||||
|
six.text_type(e))
|
||||||
|
raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||||
raise e
|
raise e
|
||||||
|
@ -17,6 +17,7 @@ import six
|
|||||||
from oslo_config import cfg
|
from oslo_config import cfg
|
||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
|
|
||||||
|
from zun.common import exception
|
||||||
from zun.common.utils import check_container_id
|
from zun.common.utils import check_container_id
|
||||||
from zun.container.docker import utils as docker_utils
|
from zun.container.docker import utils as docker_utils
|
||||||
from zun.container import driver
|
from zun.container import driver
|
||||||
@ -37,7 +38,10 @@ class DockerDriver(driver.ContainerDriver):
|
|||||||
with docker_utils.docker_client() as docker:
|
with docker_utils.docker_client() as docker:
|
||||||
LOG.debug('Pulling image %s' % image)
|
LOG.debug('Pulling image %s' % image)
|
||||||
image_repo, image_tag = docker_utils.parse_docker_image(image)
|
image_repo, image_tag = docker_utils.parse_docker_image(image)
|
||||||
docker.pull(image_repo, tag=image_tag)
|
try:
|
||||||
|
docker.pull(image_repo, tag=image_tag)
|
||||||
|
except errors.APIError as e:
|
||||||
|
raise exception.DockerError(error_msg=six.text_type(e))
|
||||||
|
|
||||||
def create(self, container):
|
def create(self, container):
|
||||||
with docker_utils.docker_client() as docker:
|
with docker_utils.docker_client() as docker:
|
||||||
@ -63,18 +67,24 @@ class DockerDriver(driver.ContainerDriver):
|
|||||||
except errors.APIError as e:
|
except errors.APIError as e:
|
||||||
container.status = fields.ContainerStatus.ERROR
|
container.status = fields.ContainerStatus.ERROR
|
||||||
container.status_reason = six.text_type(e)
|
container.status_reason = six.text_type(e)
|
||||||
raise
|
raise exception.DockerError(error_msg=six.text_type(e))
|
||||||
container.save()
|
container.save()
|
||||||
return container
|
return container
|
||||||
|
|
||||||
def delete(self, container):
|
def delete(self, container):
|
||||||
with docker_utils.docker_client() as docker:
|
with docker_utils.docker_client() as docker:
|
||||||
if container.container_id:
|
try:
|
||||||
docker.remove_container(container.container_id)
|
if container.container_id:
|
||||||
|
docker.remove_container(container.container_id)
|
||||||
|
except errors.APIError as e:
|
||||||
|
raise exception.DockerError(error_msg=six.text_type(e))
|
||||||
|
|
||||||
def list(self):
|
def list(self):
|
||||||
with docker_utils.docker_client() as docker:
|
with docker_utils.docker_client() as docker:
|
||||||
return docker.list_instances()
|
try:
|
||||||
|
return docker.list_instances()
|
||||||
|
except errors.APIError as e:
|
||||||
|
raise exception.DockerError(error_msg=six.text_type(e))
|
||||||
|
|
||||||
def show(self, container):
|
def show(self, container):
|
||||||
with docker_utils.docker_client() as docker:
|
with docker_utils.docker_client() as docker:
|
||||||
@ -88,7 +98,7 @@ class DockerDriver(driver.ContainerDriver):
|
|||||||
if '404' in str(api_error):
|
if '404' in str(api_error):
|
||||||
container.status = fields.ContainerStatus.ERROR
|
container.status = fields.ContainerStatus.ERROR
|
||||||
return container
|
return container
|
||||||
raise
|
raise exception.DockerError(error_msg=six.text_type(api_error))
|
||||||
|
|
||||||
status = result.get('State')
|
status = result.get('State')
|
||||||
if status:
|
if status:
|
||||||
@ -105,54 +115,76 @@ class DockerDriver(driver.ContainerDriver):
|
|||||||
@check_container_id
|
@check_container_id
|
||||||
def reboot(self, container):
|
def reboot(self, container):
|
||||||
with docker_utils.docker_client() as docker:
|
with docker_utils.docker_client() as docker:
|
||||||
docker.restart(container.container_id)
|
try:
|
||||||
container.status = fields.ContainerStatus.RUNNING
|
docker.restart(container.container_id)
|
||||||
return container
|
container.status = fields.ContainerStatus.RUNNING
|
||||||
|
return container
|
||||||
|
except errors.APIError as e:
|
||||||
|
raise exception.DockerError(error_msg=six.text_type(e))
|
||||||
|
|
||||||
@check_container_id
|
@check_container_id
|
||||||
def stop(self, container):
|
def stop(self, container):
|
||||||
with docker_utils.docker_client() as docker:
|
with docker_utils.docker_client() as docker:
|
||||||
docker.stop(container.container_id)
|
try:
|
||||||
container.status = fields.ContainerStatus.STOPPED
|
docker.stop(container.container_id)
|
||||||
return container
|
container.status = fields.ContainerStatus.STOPPED
|
||||||
|
return container
|
||||||
|
except errors.APIError as e:
|
||||||
|
raise exception.DockerError(error_msg=six.text_type(e))
|
||||||
|
|
||||||
@check_container_id
|
@check_container_id
|
||||||
def start(self, container):
|
def start(self, container):
|
||||||
with docker_utils.docker_client() as docker:
|
with docker_utils.docker_client() as docker:
|
||||||
docker.start(container.container_id)
|
try:
|
||||||
container.status = fields.ContainerStatus.RUNNING
|
docker.start(container.container_id)
|
||||||
return container
|
container.status = fields.ContainerStatus.RUNNING
|
||||||
|
return container
|
||||||
|
except errors.APIError as e:
|
||||||
|
raise exception.DockerError(error_msg=six.text_type(e))
|
||||||
|
|
||||||
@check_container_id
|
@check_container_id
|
||||||
def pause(self, container):
|
def pause(self, container):
|
||||||
with docker_utils.docker_client() as docker:
|
with docker_utils.docker_client() as docker:
|
||||||
docker.pause(container.container_id)
|
try:
|
||||||
container.status = fields.ContainerStatus.PAUSED
|
docker.pause(container.container_id)
|
||||||
return container
|
container.status = fields.ContainerStatus.PAUSED
|
||||||
|
return container
|
||||||
|
except errors.APIError as e:
|
||||||
|
raise exception.DockerError(error_msg=six.text_type(e))
|
||||||
|
|
||||||
@check_container_id
|
@check_container_id
|
||||||
def unpause(self, container):
|
def unpause(self, container):
|
||||||
with docker_utils.docker_client() as docker:
|
with docker_utils.docker_client() as docker:
|
||||||
docker.unpause(container.container_id)
|
try:
|
||||||
container.status = fields.ContainerStatus.RUNNING
|
docker.unpause(container.container_id)
|
||||||
return container
|
container.status = fields.ContainerStatus.RUNNING
|
||||||
|
return container
|
||||||
|
except errors.APIError as e:
|
||||||
|
raise exception.DockerError(error_msg=six.text_type(e))
|
||||||
|
|
||||||
@check_container_id
|
@check_container_id
|
||||||
def show_logs(self, container):
|
def show_logs(self, container):
|
||||||
with docker_utils.docker_client() as docker:
|
with docker_utils.docker_client() as docker:
|
||||||
return docker.get_container_logs(container.container_id)
|
try:
|
||||||
|
return docker.get_container_logs(container.container_id)
|
||||||
|
except errors.APIError as e:
|
||||||
|
raise exception.DockerError(error_msg=six.text_type(e))
|
||||||
|
|
||||||
@check_container_id
|
@check_container_id
|
||||||
def execute(self, container, command):
|
def execute(self, container, command):
|
||||||
with docker_utils.docker_client() as docker:
|
with docker_utils.docker_client() as docker:
|
||||||
if docker_utils.is_docker_library_version_atleast('1.2.0'):
|
try:
|
||||||
create_res = docker.exec_create(
|
if docker_utils.is_docker_library_version_atleast('1.2.0'):
|
||||||
container.container_id, command, True, True, False)
|
create_res = docker.exec_create(
|
||||||
exec_output = docker.exec_start(create_res, False, False,
|
container.container_id, command, True, True, False)
|
||||||
False)
|
exec_output = docker.exec_start(create_res, False, False,
|
||||||
else:
|
False)
|
||||||
exec_output = docker.execute(container.container_id, command)
|
else:
|
||||||
return exec_output
|
exec_output = docker.execute(
|
||||||
|
container.container_id, command)
|
||||||
|
return exec_output
|
||||||
|
except errors.APIError as e:
|
||||||
|
raise exception.DockerError(error_msg=six.text_type(e))
|
||||||
|
|
||||||
def _encode_utf8(self, value):
|
def _encode_utf8(self, value):
|
||||||
if six.PY2 and not isinstance(value, unicode):
|
if six.PY2 and not isinstance(value, unicode):
|
||||||
|
Loading…
Reference in New Issue
Block a user