Put frequently called code into decorator
Change-Id: I63c09bfd65e054cd02f1a14cac12d971fcf0306f
This commit is contained in:
parent
4901581ac0
commit
04613018b3
@ -25,6 +25,8 @@ from oslo_log import log as logging
|
||||
import pecan
|
||||
import six
|
||||
|
||||
from zun.common import exception
|
||||
from zun.common.i18n import _
|
||||
from zun.common.i18n import _LW
|
||||
|
||||
|
||||
@ -93,3 +95,34 @@ def spawn_n(func, *args, **kwargs):
|
||||
func(*args, **kwargs)
|
||||
|
||||
eventlet.spawn_n(context_wrapper, *args, **kwargs)
|
||||
|
||||
|
||||
def translate_exception(function):
|
||||
"""Wraps a method to catch exceptions.
|
||||
|
||||
If the exception is not an instance of ZunException,
|
||||
translate it into one.
|
||||
"""
|
||||
|
||||
@functools.wraps(function)
|
||||
def decorated_function(self, context, *args, **kwargs):
|
||||
try:
|
||||
return function(self, context, *args, **kwargs)
|
||||
except Exception as e:
|
||||
if not isinstance(e, exception.ZunException):
|
||||
e = exception.ZunException("Unexpected Error: %s" % str(e))
|
||||
raise e
|
||||
|
||||
return decorated_function
|
||||
|
||||
|
||||
def check_container_id(function):
|
||||
'''Check container_id property of given container instance.'''
|
||||
|
||||
@functools.wraps(function)
|
||||
def decorated_function(self, container, *args, **kwargs):
|
||||
if container.container_id is None:
|
||||
msg = _("Cannot operate an uncreated container.")
|
||||
raise exception.Invalid(message=msg)
|
||||
|
||||
return decorated_function
|
||||
|
@ -17,6 +17,7 @@ from oslo_log import log as logging
|
||||
from zun.common import exception
|
||||
from zun.common.i18n import _LE
|
||||
from zun.common import utils
|
||||
from zun.common.utils import translate_exception
|
||||
from zun.container import driver
|
||||
from zun.objects import fields
|
||||
|
||||
@ -62,6 +63,7 @@ class Manager(object):
|
||||
container.task_state = None
|
||||
container.save()
|
||||
|
||||
@translate_exception
|
||||
def container_delete(self, context, container):
|
||||
LOG.debug('Deleting container...', context=context,
|
||||
container=container.uuid)
|
||||
@ -70,20 +72,18 @@ class Manager(object):
|
||||
return container
|
||||
except Exception as e:
|
||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||
if not isinstance(e, exception.ZunException):
|
||||
e = exception.ZunException("Unexpected Error: %s" % str(e))
|
||||
raise e
|
||||
|
||||
@translate_exception
|
||||
def container_list(self, context):
|
||||
LOG.debug('Showing container...', context=context)
|
||||
LOG.debug('Listing container...', context=context)
|
||||
try:
|
||||
return self.driver.list()
|
||||
except Exception as e:
|
||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||
if not isinstance(e, exception.ZunException):
|
||||
e = exception.ZunException("Unexpected Error: %s" % str(e))
|
||||
raise e
|
||||
|
||||
@translate_exception
|
||||
def container_show(self, context, container):
|
||||
LOG.debug('Showing container...', context=context,
|
||||
container=container.uuid)
|
||||
@ -93,10 +93,9 @@ class Manager(object):
|
||||
return container
|
||||
except Exception as e:
|
||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||
if not isinstance(e, exception.ZunException):
|
||||
e = exception.ZunException("Unexpected Error: %s" % str(e))
|
||||
raise e
|
||||
|
||||
@translate_exception
|
||||
def container_reboot(self, context, container):
|
||||
LOG.debug('Rebooting container...', context=context,
|
||||
container=container)
|
||||
@ -106,10 +105,9 @@ class Manager(object):
|
||||
return container
|
||||
except Exception as e:
|
||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||
if not isinstance(e, exception.ZunException):
|
||||
e = exception.ZunException("Unexpected Error: %s" % str(e))
|
||||
raise e
|
||||
|
||||
@translate_exception
|
||||
def container_stop(self, context, container):
|
||||
LOG.debug('Stopping container...', context=context,
|
||||
container=container)
|
||||
@ -119,10 +117,9 @@ class Manager(object):
|
||||
return container
|
||||
except Exception as e:
|
||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||
if not isinstance(e, exception.ZunException):
|
||||
e = exception.ZunException("Unexpected Error: %s" % str(e))
|
||||
raise e
|
||||
|
||||
@translate_exception
|
||||
def container_start(self, context, container):
|
||||
LOG.debug('Starting container...', context=context,
|
||||
container=container.uuid)
|
||||
@ -132,10 +129,9 @@ class Manager(object):
|
||||
return container
|
||||
except Exception as e:
|
||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||
if not isinstance(e, exception.ZunException):
|
||||
e = exception.ZunException("Unexpected Error: %s" % str(e))
|
||||
raise e
|
||||
|
||||
@translate_exception
|
||||
def container_pause(self, context, container):
|
||||
LOG.debug('Pausing container...', context=context,
|
||||
container=container)
|
||||
@ -144,11 +140,10 @@ class Manager(object):
|
||||
container.save()
|
||||
return container
|
||||
except Exception as e:
|
||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||
if not isinstance(e, exception.ZunException):
|
||||
e = exception.ZunException("Unexpected Error: %s" % str(e))
|
||||
LOG.exception(_LE("Unexpected exception: %s,"), str(e))
|
||||
raise e
|
||||
|
||||
@translate_exception
|
||||
def container_unpause(self, context, container):
|
||||
LOG.debug('Unpausing container...', context=context,
|
||||
container=container)
|
||||
@ -158,10 +153,9 @@ class Manager(object):
|
||||
return container
|
||||
except Exception as e:
|
||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||
if not isinstance(e, exception.ZunException):
|
||||
e = exception.ZunException("Unexpected Error: %s" % str(e))
|
||||
raise e
|
||||
|
||||
@translate_exception
|
||||
def container_logs(self, context, container):
|
||||
LOG.debug('Showing container logs...', context=context,
|
||||
container=container)
|
||||
@ -169,10 +163,9 @@ class Manager(object):
|
||||
return self.driver.show_logs(container)
|
||||
except Exception as e:
|
||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||
if not isinstance(e, exception.ZunException):
|
||||
e = exception.ZunException("Unexpected Error: %s" % str(e))
|
||||
raise e
|
||||
|
||||
@translate_exception
|
||||
def container_exec(self, context, container, command):
|
||||
# TODO(hongbin): support exec command interactively
|
||||
LOG.debug('Executing command in container...', context=context,
|
||||
@ -181,6 +174,4 @@ class Manager(object):
|
||||
return self.driver.execute(container, command)
|
||||
except Exception as e:
|
||||
LOG.exception(_LE("Unexpected exception: %s"), str(e))
|
||||
if not isinstance(e, exception.ZunException):
|
||||
e = exception.ZunException("Unexpected Error: %s" % str(e))
|
||||
raise e
|
||||
|
@ -17,8 +17,7 @@ import six
|
||||
from oslo_config import cfg
|
||||
from oslo_log import log as logging
|
||||
|
||||
from zun.common import exception
|
||||
from zun.common.i18n import _
|
||||
from zun.common.utils import check_container_id
|
||||
from zun.container.docker import utils as docker_utils
|
||||
from zun.container import driver
|
||||
from zun.objects import fields
|
||||
@ -103,70 +102,49 @@ class DockerDriver(driver.ContainerDriver):
|
||||
container.status = fields.ContainerStatus.STOPPED
|
||||
return container
|
||||
|
||||
@check_container_id
|
||||
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
|
||||
|
||||
@check_container_id
|
||||
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
|
||||
|
||||
@check_container_id
|
||||
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
|
||||
|
||||
@check_container_id
|
||||
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
|
||||
|
||||
@check_container_id
|
||||
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
|
||||
|
||||
@check_container_id
|
||||
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)
|
||||
|
||||
@check_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)
|
||||
|
0
zun/tests/unit/common/__init__.py
Normal file
0
zun/tests/unit/common/__init__.py
Normal file
45
zun/tests/unit/common/test_utils.py
Normal file
45
zun/tests/unit/common/test_utils.py
Normal file
@ -0,0 +1,45 @@
|
||||
# Copyright 2016 IBM, Corp.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import mock
|
||||
|
||||
from zun.common import exception
|
||||
from zun.common.utils import check_container_id
|
||||
from zun.common.utils import translate_exception
|
||||
from zun.tests import base
|
||||
|
||||
|
||||
class TestUtils(base.BaseTestCase):
|
||||
"""Test cases for zun.common.utils"""
|
||||
|
||||
def test_check_container_id(self):
|
||||
|
||||
@check_container_id
|
||||
def foo(self, container):
|
||||
pass
|
||||
|
||||
fake_container = mock.MagicMock()
|
||||
fake_container.container_id = None
|
||||
|
||||
self.assertRaises(exception.Invalid, foo,
|
||||
self, fake_container)
|
||||
|
||||
def test_translate_exception(self):
|
||||
|
||||
@translate_exception
|
||||
def foo(self, context):
|
||||
raise TypeError()
|
||||
|
||||
self.assertRaises(exception.ZunException, foo,
|
||||
self, mock.MagicMock())
|
Loading…
x
Reference in New Issue
Block a user