1
0
Fork 0
mirror of https://github.com/containers/ansible-podman-collections.git synced 2026-02-04 07:11:49 +00:00

Change present state to be as created state (#263)

For being more compliant with docker module.
See #257
This commit is contained in:
Sergey 2021-07-07 18:48:50 +03:00 committed by GitHub
parent 8ada1501f6
commit 9ff36649ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 96 additions and 51 deletions

View file

@ -178,7 +178,7 @@ class PodmanModuleParams:
Arguments:
action {str} -- action type from 'run', 'stop', 'create', 'delete',
'start'
'start', 'restart'
params {dict} -- dictionary of module parameters
"""
@ -195,7 +195,7 @@ class PodmanModuleParams:
Returns:
list -- list of byte strings for Popen command
"""
if self.action in ['start', 'stop', 'delete']:
if self.action in ['start', 'stop', 'delete', 'restart']:
return self.start_stop_delete()
if self.action in ['create', 'run']:
cmd = [self.action, '--name', self.params['name']]
@ -217,7 +217,7 @@ class PodmanModuleParams:
def start_stop_delete(self):
if self.action in ['stop', 'start']:
if self.action in ['stop', 'start', 'restart']:
cmd = [self.action, self.params['name']]
return [to_bytes(i, errors='surrogate_or_strict') for i in cmd]
@ -1385,7 +1385,7 @@ class PodmanContainer:
Arguments:
action {str} -- action to perform - start, create, stop, run,
delete
delete, restart
"""
b_command = PodmanModuleParams(action,
self.module_params,
@ -1432,6 +1432,10 @@ class PodmanContainer:
"""Start the container."""
self._perform_action('start')
def restart(self):
"""Restart the container."""
self._perform_action('restart')
def create(self):
"""Create the container."""
self._perform_action('create')
@ -1450,11 +1454,6 @@ class PodmanContainer:
self.delete()
self.run()
def restart(self):
"""Restart the container."""
self.stop()
self.start()
class PodmanManager:
"""Module manager class.
@ -1509,6 +1508,20 @@ class PodmanManager:
def make_started(self):
"""Run actions if desired state is 'started'."""
if not self.image:
if not self.container.exists:
self.module.fail_json(msg='Cannot start container when image'
' is not specified!')
if self.restart:
self.container.restart()
self.results['actions'].append('restarted %s' %
self.container.name)
else:
self.container.start()
self.results['actions'].append('started %s' %
self.container.name)
self.update_container_result()
return
if self.container.exists and self.restart:
if self.container.running:
self.container.restart()
@ -1568,6 +1581,21 @@ class PodmanManager:
self.container.recreate()
self.results['actions'].append('recreated %s' %
self.container.name)
if self.container.running:
self.container.start()
self.results['actions'].append('started %s' %
self.container.name)
self.update_container_result()
return
elif self.restart:
if self.container.running:
self.container.restart()
self.results['actions'].append('restarted %s' %
self.container.name)
else:
self.container.start()
self.results['actions'].append('started %s' %
self.container.name)
self.update_container_result()
return
self.update_container_result(changed=False)
@ -1606,7 +1634,7 @@ class PodmanManager:
def execute(self):
"""Execute the desired action according to map of actions & states."""
states_map = {
'present': self.make_started,
'present': self.make_created,
'started': self.make_started,
'absent': self.make_absent,
'stopped': self.make_stopped,

View file

@ -921,7 +921,7 @@ def main():
)
# work on input vars
if (module.params['state'] in ['started', 'present', 'created']
if (module.params['state'] in ['present', 'created']
and not module.params['force_restart']
and not module.params['image']):
module.fail_json(msg="State '%s' required image to be configured!" %

View file

@ -18,7 +18,7 @@
- name: Test no image with state 'started'
containers.podman.podman_container:
name: container
state: started
state: created
ignore_errors: true
register: no_image1
@ -35,8 +35,8 @@
- no_image is failed
- no_image1 is failed
- no_image2 is failed
- no_image.msg == "State 'started' required image to be configured!"
- no_image1.msg == "State 'started' required image to be configured!"
- no_image.msg == "Cannot start container when image is not specified!"
- no_image1.msg == "State 'created' required image to be configured!"
- no_image2.msg == "State 'present' required image to be configured!"
fail_msg: No image test failed!
success_msg: No image test passed!
@ -50,7 +50,7 @@
containers.podman.podman_container:
name: container
image: alpine:3.7
state: present
state: started
command: sleep 1d
register: image
@ -58,7 +58,7 @@
containers.podman.podman_container:
name: container2
image: alpine:3.7
state: present
state: started
command: sleep 1d
register: image2
@ -82,7 +82,7 @@
containers.podman.podman_container:
name: container
image: ineverneverneverexist
state: present
state: started
command: sleep 1d
register: imagefail
ignore_errors: true
@ -93,12 +93,11 @@
- imagefail is failed
- imagefail.msg == "Can't pull image ineverneverneverexist"
- name: Force container recreate
containers.podman.podman_container:
name: container
image: alpine
state: present
state: started
command: sleep 1d
recreate: true
register: recreated
@ -108,11 +107,29 @@
that:
- recreated is changed
- recreated.container is defined
- recreated.container['State']['Running']
- recreated.container['State']['Running']|bool
- "'recreated container' in recreated.actions"
fail_msg: Force recreate test failed!
success_msg: Force recreate test passed!
- name: Start container
containers.podman.podman_container:
name: container
state: started
- name: Present container
containers.podman.podman_container:
name: container
image: alpine
state: present
command: sleep 1d
register: start_present
- name: Check output is correct
assert:
that:
- start_present.container['State']['Running']
- name: Stop container
containers.podman.podman_container:
name: container
@ -238,7 +255,7 @@
- restarted is changed
- restarted.container is defined
- restarted.container['State']['Running']
- "'started container' in restarted.actions"
- "'restarted container' in restarted.actions"
fail_msg: Restart container test failed!
- name: Restart running container
@ -394,14 +411,14 @@
containers.podman.podman_container:
name: testidem
image: docker.io/alpine
state: present
state: started
command: sleep 20m
- name: Check basic idempotency of running container - run it again
containers.podman.podman_container:
name: testidem
image: alpine:latest
state: present
state: started
command: sleep 20m
register: idem
@ -414,7 +431,7 @@
containers.podman.podman_container:
name: testidem
image: alpine:latest
state: present
state: started
command: sleep 20m
force_restart: true
register: idem_r
@ -428,7 +445,7 @@
containers.podman.podman_container:
name: testidem
image: alpine:latest
state: present
state: started
command: sleep 20m
register: idem_r1
@ -441,7 +458,7 @@
containers.podman.podman_container:
name: testidem
image: alpine
state: present
state: started
command: sleep 20m
tty: true
register: idem1
@ -455,7 +472,7 @@
containers.podman.podman_container:
name: testidem
image: alpine
state: present
state: started
command: sleep 20m
register: idem2
@ -482,7 +499,7 @@
containers.podman.podman_container:
name: testidem-pod
image: docker.io/alpine
state: present
state: started
command: sleep 20m
pod: "new:testidempod"
@ -490,7 +507,7 @@
containers.podman.podman_container:
name: testidem-pod
image: alpine:latest
state: present
state: started
command: sleep 20m
pod: testidempod
register: idem3
@ -504,7 +521,7 @@
containers.podman.podman_container:
name: testidem-pod
image: alpine
state: present
state: started
command: sleep 20m
tty: true
pod: testidempod

View file

@ -8,7 +8,7 @@
containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
state: started
command: 1h
- name: Run container again

View file

@ -6,7 +6,7 @@
- containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
state: started
command: 1h
- containers.podman.podman_container:

View file

@ -8,7 +8,7 @@
name: netcontainer
image: "{{ idem_image }}"
command: 1h
state: present
state: started
network: "{{ item.first_net }}"
- name: Run container again with {{ item.first_net }}

View file

@ -6,7 +6,7 @@
- containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
state: started
command: 1h
- containers.podman.podman_container:

View file

@ -6,7 +6,7 @@
- containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
state: started
command: 1h
- containers.podman.podman_container:

View file

@ -6,7 +6,7 @@
- containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
state: started
command: 1h
- containers.podman.podman_container:

View file

@ -6,7 +6,7 @@
- containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
state: started
command: 1h
- containers.podman.podman_container:

View file

@ -6,7 +6,7 @@
- containers.podman.podman_container:
image: "{{ idem_image }}"
name: idempotency
state: present
state: started
command: 1h
- containers.podman.podman_container:

View file

@ -9,7 +9,7 @@
containers.podman.podman_container:
image: "{{ idem_image }}"
name: root-idempotency
state: present
state: started
command: 1h
- name: Run container as is again
@ -93,14 +93,14 @@
containers.podman.podman_container:
image: "{{ idem_image }}"
name: root-idempotency
state: present
state: started
command: 1h
- name: Run containers with MAC address
containers.podman.podman_container:
image: "{{ idem_image }}"
name: root-idempotency
state: present
state: started
command: 1h
mac_address: 44:55:66:77:88:99
register: info4

View file

@ -11,7 +11,7 @@
name: rootlessnet
image: "{{ idem_image }}"
command: 1h
state: present
state: started
- name: Run container again with no specified networks
containers.podman.podman_container:

View file

@ -83,11 +83,11 @@
containers:
- name: container
image: alpine:3.7
state: present
state: started
command: sleep 1d
- name: container1
image: alpine:3.7
state: present
state: started
command: sleep 1d
register: image
@ -96,11 +96,11 @@
containers:
- name: container1
image: alpine:3.7
state: present
state: started
command: sleep 1d
- name: container3
image: alpine:3.7
state: present
state: started
command: sleep 1d
register: image2
@ -129,11 +129,11 @@
containers:
- name: container1
image: alpine:3.7
state: present
state: started
command: sleep 1d
- name: container
image: ineverneverneverexist
state: present
state: started
command: sleep 1d
register: imagefail
ignore_errors: true
@ -460,15 +460,15 @@
containers:
- name: testidem
image: docker.io/alpine
state: present
state: started
command: sleep 20m
- name: testidem2
image: docker.io/alpine
state: present
state: started
command: sleep 21m
- name: testidem3
image: docker.io/alpine
state: present
state: started
command: sleep 22m
- name: Check basic idempotency of running container - run it again