Merge pull request #3837 from chouseknecht/devel

Fix #3822 stop container
reviewable/pr18780/r1
Chris Houseknecht 9 years ago
commit 93c8d923c4

@ -369,8 +369,8 @@ options:
re-create a matching container, even if it is running. Use restart to force a matching container to be stopped and re-create a matching container, even if it is running. Use restart to force a matching container to be stopped and
restarted. Use force_kill to kill a container rather than stopping it. Use keep_volumes to retain volumes associated restarted. Use force_kill to kill a container rather than stopping it. Use keep_volumes to retain volumes associated
with a removed container.' with a removed container.'
- 'I(stopped) - a container matching the specified name will be stopped. Use force_kill to kill a container rather than - 'I(stopped) - Asserts that the container is first I(present), and then if the container is running moves it to a stopped
stopping it.' state. Use force_kill to kill a container rather than stopping it.'
required: false required: false
default: started default: started
choices: choices:
@ -492,10 +492,13 @@ EXAMPLES = '''
docker_container: docker_container:
name: mycontainer name: mycontainer
state: present state: present
recreate: yes image: ubuntu:14.04
force_kill: yes command: sleep infinity
image: someplace/image
command: echo "I'm here!" - name: Stop a contianer
docker_container:
name: mycontainer
state: stopped
- name: Start 4 load-balanced containers - name: Start 4 load-balanced containers
docker_container: docker_container:
@ -1089,7 +1092,7 @@ class Container(DockerBaseClass):
self.parameters.client.module.fail_json(msg=msg) self.parameters.client.module.fail_json(msg=msg)
@property @property
def found(self): def exists(self):
return True if self.container else False return True if self.container else False
@property @property
@ -1557,7 +1560,7 @@ class ContainerManager(DockerBaseClass):
self.facts = {} self.facts = {}
state = self.parameters.state state = self.parameters.state
if state in ('started', 'present'): if state in ('stopped', 'started', 'present'):
self.present(state) self.present(state)
elif state == 'absent': elif state == 'absent':
self.absent() self.absent()
@ -1578,51 +1581,46 @@ class ContainerManager(DockerBaseClass):
container = self._get_container(self.parameters.name) container = self._get_container(self.parameters.name)
image = self._get_image() image = self._get_image()
if not container.found: if not container.exists:
self.log('No container found')
# New container # New container
self.log('No container found')
new_container = self.container_create(self.parameters.image, self.parameters.create_parameters) new_container = self.container_create(self.parameters.image, self.parameters.create_parameters)
if new_container: if new_container:
container = new_container container = new_container
container = self.update_limits(container) else:
container = self.update_networks(container) # Existing container
if state == 'started': different, differences = container.has_different_configuration(image)
container = self.container_start(container.Id) image_different = self._image_is_different(image, container)
self.facts = container.raw if image_different or different or self.parameters.recreate:
return self.diff['differences'] = differences
if image_different:
# Existing container self.diff['image_different'] = True
different, differences = container.has_different_configuration(image) self.log("differences")
image_different = self._image_is_different(image, container) self.log(differences, pretty_print=True)
if image_different or different or self.parameters.recreate: self.container_stop(container.Id)
self.diff['differences'] = differences self.container_remove(container.Id)
self.log("differences") new_container = self.container_create(self.parameters.image, self.parameters.create_parameters)
self.log(differences, pretty_print=True) if new_container:
self.container_stop(container.Id) container = new_container
self.container_remove(container.Id)
new_container = self.container_create(self.parameters.image, self.parameters.create_parameters)
if new_container:
container = new_container
if image_different:
self.diff['image_different'] = True
container = self.update_limits(container) if container and container.exists:
container = self.update_networks(container) container = self.update_limits(container)
container = self.update_networks(container)
if state == 'started' and not container.running: if state == 'started' and not container.running:
container = self.container_start(container.Id) container = self.container_start(container.Id)
elif state == 'started' and self.parameters.restart: elif state == 'started' and self.parameters.restart:
self.container_stop(container.Id) self.container_stop(container.Id)
container = self.container_start(container.Id) container = self.container_start(container.Id)
elif state == 'present' and container.running: elif state == 'stopped' and container.running:
self.container_stop(container.Id) self.container_stop(container.Id)
container = self._get_container(container.Id) container = self._get_container(container.Id)
self.facts = container.raw self.facts = container.raw
def absent(self): def absent(self):
container = Container(self.client.get_container(self.parameters.name), self.parameters) container = self._get_container(self.parameters.name)
if container.found: if container.exists:
if container.running: if container.running:
self.container_stop(container.Id) self.container_stop(container.Id)
self.container_remove(container.Id) self.container_remove(container.Id)

Loading…
Cancel
Save