diff --git a/etc/zun/policy.json b/etc/zun/policy.json index c5bb73022..5c33532c2 100644 --- a/etc/zun/policy.json +++ b/etc/zun/policy.json @@ -17,11 +17,6 @@ "container:unpause": "rule:admin_or_user", "container:logs": "rule:admin_or_user", "container:execute": "rule:admin_or_user", - "container:start": "rule:admin_or_user", - "container:start": "rule:admin_or_user", - "container:start": "rule:admin_or_user", - "container:start": "rule:admin_or_user", - "container:start": "rule:admin_or_user", "magnum-service:get_all": "rule:admin_api" -} \ No newline at end of file +} diff --git a/zun/api/controllers/v1/containers.py b/zun/api/controllers/v1/containers.py index 40374c354..0119aafc6 100644 --- a/zun/api/controllers/v1/containers.py +++ b/zun/api/controllers/v1/containers.py @@ -309,9 +309,8 @@ class ExecuteController(object): LOG.debug('Calling compute.container_exec with %s command %s' % (container.uuid, kw['command'])) context = pecan.request.context - pecan.request.rpcapi.container_exec(context, container, - kw['command']) - return Container.convert_with_links(container) + return pecan.request.rpcapi.container_exec(context, container, + kw['command']) class ContainersController(object): diff --git a/zun/compute/manager.py b/zun/compute/manager.py index 2d6a9eb1f..41dcfff85 100644 --- a/zun/compute/manager.py +++ b/zun/compute/manager.py @@ -14,6 +14,7 @@ from oslo_log import log as logging +from zun.common import exception from zun.common.i18n import _LE from zun.container import driver @@ -36,18 +37,21 @@ class Manager(object): return container except Exception as e: LOG.exception(_LE("Unexpected exception: %s,"), str(e)) - raise + if not isinstance(e, exception.ZunException): + e = exception.ZunException("Unexpected Error: %s" % str(e)) + raise e def container_delete(self, context, container): LOG.debug('Deleting container...', context=context, container=container.uuid) try: self.driver.delete(container) - container.destroy() return container except Exception as e: LOG.exception(_LE("Unexpected exception: %s,"), str(e)) - raise + if not isinstance(e, exception.ZunException): + e = exception.ZunException("Unexpected Error: %s" % str(e)) + raise e def container_list(self, context): LOG.debug('Showing container...', context=context) @@ -55,7 +59,9 @@ class Manager(object): return self.driver.list() except Exception as e: LOG.exception(_LE("Unexpected exception: %s,"), str(e)) - raise + if not isinstance(e, exception.ZunException): + e = exception.ZunException("Unexpected Error: %s" % str(e)) + raise e def container_show(self, context, container): LOG.debug('Showing container...', context=context, @@ -66,7 +72,9 @@ class Manager(object): return container except Exception as e: LOG.exception(_LE("Unexpected exception: %s,"), str(e)) - raise + if not isinstance(e, exception.ZunException): + e = exception.ZunException("Unexpected Error: %s" % str(e)) + raise e def container_reboot(self, context, container): LOG.debug('Rebooting container...', context=context, @@ -77,7 +85,9 @@ class Manager(object): return container except Exception as e: LOG.exception(_LE("Unexpected exception: %s,"), str(e)) - raise + if not isinstance(e, exception.ZunException): + e = exception.ZunException("Unexpected Error: %s" % str(e)) + raise e def container_stop(self, context, container): LOG.debug('Stopping container...', context=context, @@ -88,7 +98,9 @@ class Manager(object): return container except Exception as e: LOG.exception(_LE("Unexpected exception: %s,"), str(e)) - raise + if not isinstance(e, exception.ZunException): + e = exception.ZunException("Unexpected Error: %s" % str(e)) + raise e def container_start(self, context, container): LOG.debug('Starting container...', context=context, @@ -99,7 +111,9 @@ class Manager(object): return container except Exception as e: LOG.exception(_LE("Unexpected exception: %s,"), str(e)) - raise + if not isinstance(e, exception.ZunException): + e = exception.ZunException("Unexpected Error: %s" % str(e)) + raise e def container_pause(self, context, container): LOG.debug('Pausing container...', context=context, @@ -110,7 +124,9 @@ class Manager(object): return container except Exception as e: LOG.exception(_LE("Unexpected exception: %s,"), str(e)) - raise + if not isinstance(e, exception.ZunException): + e = exception.ZunException("Unexpected Error: %s" % str(e)) + raise e def container_unpause(self, context, container): LOG.debug('Unpausing container...', context=context, @@ -121,7 +137,9 @@ class Manager(object): return container except Exception as e: LOG.exception(_LE("Unexpected exception: %s,"), str(e)) - raise + if not isinstance(e, exception.ZunException): + e = exception.ZunException("Unexpected Error: %s" % str(e)) + raise e def container_logs(self, context, container): LOG.debug('Showing container logs...', context=context, @@ -130,7 +148,9 @@ class Manager(object): return self.driver.show_logs(container) except Exception as e: LOG.exception(_LE("Unexpected exception: %s,"), str(e)) - raise + if not isinstance(e, exception.ZunException): + e = exception.ZunException("Unexpected Error: %s" % str(e)) + raise e def container_exec(self, context, container, command): # TODO(hongbin): support exec command interactively @@ -140,4 +160,6 @@ class Manager(object): return self.driver.execute(container, command) except Exception as e: LOG.exception(_LE("Unexpected exception: %s,"), str(e)) - raise + if not isinstance(e, exception.ZunException): + e = exception.ZunException("Unexpected Error: %s" % str(e)) + raise e diff --git a/zun/container/docker/driver.py b/zun/container/docker/driver.py index 61f57ff36..58cb1fa8c 100644 --- a/zun/container/docker/driver.py +++ b/zun/container/docker/driver.py @@ -17,6 +17,7 @@ import six from oslo_config import cfg from oslo_log import log as logging +from zun.common import exception from zun.container.docker import utils as docker_utils from zun.container import driver from zun.objects import fields @@ -64,7 +65,8 @@ class DockerDriver(driver.ContainerDriver): def delete(self, container): with docker_utils.docker_client() as docker: - return docker.remove_container(container.container_id) + if container.container_id: + docker.remove_container(container.container_id) def list(self): with docker_utils.docker_client() as docker: @@ -72,61 +74,94 @@ class DockerDriver(driver.ContainerDriver): def show(self, container): with docker_utils.docker_client() as docker: - try: - result = docker.inspect_container(container.uuid) - status = result.get('State') - if status: - if status.get('Error') is True: - container.status = fields.ContainerStatus.ERROR - elif status.get('Paused'): - container.status = fields.ContainerStatus.PAUSED - elif status.get('Running'): - container.status = fields.ContainerStatus.RUNNING - else: - container.status = fields.ContainerStatus.STOPPED + if container.container_id is None: return container + + result = None + try: + result = docker.inspect_container(container.container_id) except errors.APIError as api_error: if '404' in str(api_error): container.status = fields.ContainerStatus.ERROR return container raise + status = result.get('State') + if status: + if status.get('Error') is True: + container.status = fields.ContainerStatus.ERROR + elif status.get('Paused'): + container.status = fields.ContainerStatus.PAUSED + elif status.get('Running'): + container.status = fields.ContainerStatus.RUNNING + else: + container.status = fields.ContainerStatus.STOPPED + return container + def reboot(self, container): with docker_utils.docker_client() as docker: + if container.container_id is None: + msg = _("Cannot reboot a uncreated container.") + raise exception.Invalid(message=msg) + docker.restart(container.container_id) container.status = fields.ContainerStatus.RUNNING return container def stop(self, container): with docker_utils.docker_client() as docker: + if container.container_id is None: + msg = _("Cannot stop a uncreated container.") + raise exception.Invalid(message=msg) + docker.stop(container.container_id) container.status = fields.ContainerStatus.STOPPED return container def start(self, container): with docker_utils.docker_client() as docker: + if container.container_id is None: + msg = _("Cannot start a uncreated container.") + raise exception.Invalid(message=msg) + docker.start(container.container_id) container.status = fields.ContainerStatus.RUNNING return container def pause(self, container): with docker_utils.docker_client() as docker: + if container.container_id is None: + msg = _("Cannot pause a uncreated container.") + raise exception.Invalid(message=msg) + docker.pause(container.container_id) container.status = fields.ContainerStatus.PAUSED return container def unpause(self, container): with docker_utils.docker_client() as docker: + if container.container_id is None: + msg = _("Cannot unpause a uncreated container.") + raise exception.Invalid(message=msg) + docker.unpause(container.container_id) container.status = fields.ContainerStatus.RUNNING return container def show_logs(self, container): with docker_utils.docker_client() as docker: + if container.container_id is None: + msg = _("Cannot show logs of a uncreated container.") + raise exception.Invalid(message=msg) + return docker.get_container_logs(container.container_id) def execute(self, container, command): with docker_utils.docker_client() as docker: + if container.container_id is None: + msg = _("Cannot execute a command in a uncreated container.") + raise exception.Invalid(message=msg) + if docker_utils.is_docker_library_version_atleast('1.2.0'): create_res = docker.exec_create( container.container_id, command, True, True, False)