Add validations for container operations
Change-Id: I4247c7867ef81112083f933cf54d33a37cd118c9 Closes-Bug: #1614386
This commit is contained in:
parent
0b1c98614e
commit
993ea96bc6
@ -341,9 +341,8 @@ class ContainerAlreadyExists(ResourceExists):
|
|||||||
message = _("A container with UUID %(uuid)s already exists.")
|
message = _("A container with UUID %(uuid)s already exists.")
|
||||||
|
|
||||||
|
|
||||||
class ContainerRunningException(ZunException):
|
class InvalidStateException(ZunException):
|
||||||
message = _("The container %(id)s is running."
|
message = _("Cannot %(action)s container %(id)s in %(actual_state)s state")
|
||||||
"Please stop and delete the container.")
|
|
||||||
code = 409
|
code = 409
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,6 +26,15 @@ from zun.objects import fields
|
|||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
VALID_STATES = {
|
||||||
|
'delete': 'STOPPED',
|
||||||
|
'start': 'STOPPED',
|
||||||
|
'stop': 'RUNNING',
|
||||||
|
'reboot': 'RUNNING',
|
||||||
|
'pause': 'RUNNING',
|
||||||
|
'unpause': 'PAUSED',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class Manager(object):
|
class Manager(object):
|
||||||
'''Manages the running containers.'''
|
'''Manages the running containers.'''
|
||||||
@ -39,6 +48,13 @@ class Manager(object):
|
|||||||
container.task_state = None
|
container.task_state = None
|
||||||
container.save()
|
container.save()
|
||||||
|
|
||||||
|
def _validate_container_state(self, container, action):
|
||||||
|
if container.status != VALID_STATES[action]:
|
||||||
|
raise exception.InvalidStateException(
|
||||||
|
id=container.container_id,
|
||||||
|
action=action,
|
||||||
|
actual_state=container.status)
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
@ -80,6 +96,7 @@ class Manager(object):
|
|||||||
LOG.debug('Deleting container...', context=context,
|
LOG.debug('Deleting container...', context=context,
|
||||||
container=container.uuid)
|
container=container.uuid)
|
||||||
try:
|
try:
|
||||||
|
self._validate_container_state(container, 'delete')
|
||||||
self.driver.delete(container)
|
self.driver.delete(container)
|
||||||
return container
|
return container
|
||||||
except exception.DockerError as e:
|
except exception.DockerError as e:
|
||||||
@ -88,9 +105,6 @@ class Manager(object):
|
|||||||
raise 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:
|
|
||||||
raise exception.ContainerRunningException(
|
|
||||||
id=container.container_id)
|
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
@translate_exception
|
@translate_exception
|
||||||
@ -127,6 +141,7 @@ class Manager(object):
|
|||||||
LOG.debug('Rebooting container...', context=context,
|
LOG.debug('Rebooting container...', context=context,
|
||||||
container=container)
|
container=container)
|
||||||
try:
|
try:
|
||||||
|
self._validate_container_state(container, 'reboot')
|
||||||
container = self.driver.reboot(container)
|
container = self.driver.reboot(container)
|
||||||
container.save()
|
container.save()
|
||||||
return container
|
return container
|
||||||
@ -143,6 +158,7 @@ class Manager(object):
|
|||||||
LOG.debug('Stopping container...', context=context,
|
LOG.debug('Stopping container...', context=context,
|
||||||
container=container)
|
container=container)
|
||||||
try:
|
try:
|
||||||
|
self._validate_container_state(container, 'stop')
|
||||||
container = self.driver.stop(container)
|
container = self.driver.stop(container)
|
||||||
container.save()
|
container.save()
|
||||||
return container
|
return container
|
||||||
@ -159,6 +175,7 @@ class Manager(object):
|
|||||||
LOG.debug('Starting container...', context=context,
|
LOG.debug('Starting container...', context=context,
|
||||||
container=container.uuid)
|
container=container.uuid)
|
||||||
try:
|
try:
|
||||||
|
self._validate_container_state(container, 'start')
|
||||||
container = self.driver.start(container)
|
container = self.driver.start(container)
|
||||||
container.save()
|
container.save()
|
||||||
return container
|
return container
|
||||||
@ -175,6 +192,7 @@ class Manager(object):
|
|||||||
LOG.debug('Pausing container...', context=context,
|
LOG.debug('Pausing container...', context=context,
|
||||||
container=container)
|
container=container)
|
||||||
try:
|
try:
|
||||||
|
self._validate_container_state(container, 'pause')
|
||||||
container = self.driver.pause(container)
|
container = self.driver.pause(container)
|
||||||
container.save()
|
container.save()
|
||||||
return container
|
return container
|
||||||
@ -191,6 +209,7 @@ class Manager(object):
|
|||||||
LOG.debug('Unpausing container...', context=context,
|
LOG.debug('Unpausing container...', context=context,
|
||||||
container=container)
|
container=container)
|
||||||
try:
|
try:
|
||||||
|
self._validate_container_state(container, 'unpause')
|
||||||
container = self.driver.unpause(container)
|
container = self.driver.unpause(container)
|
||||||
container.save()
|
container.save()
|
||||||
return container
|
return container
|
||||||
|
Loading…
Reference in New Issue
Block a user