Skip to content

Commit

Permalink
Add a force field to podman_generate_systemd (#624)
Browse files Browse the repository at this point in the history
* Add tests for podman_generate_systemd

Signed-off-by: nishipy <goodisonev4@gmail.com>

* Add force option for podman_generate_systemd

Signed-off-by: nishipy <goodisonev4@gmail.com>

* Fix test code for podman_generate_systemd

Signed-off-by: nishipy <goodisonev4@gmail.com>

* Fix CI error

Signed-off-by: nishipy <goodisonev4@gmail.com>

---------

Signed-off-by: nishipy <goodisonev4@gmail.com>
  • Loading branch information
nishipy authored Aug 13, 2023
1 parent c6a80a5 commit 04f455b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
20 changes: 18 additions & 2 deletions plugins/modules/podman_generate_systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
- Use C(/etc/systemd/system) for the system-wide systemd instance.
- Use C(/etc/systemd/user) or C(~/.config/systemd/user) for use with per-user instances of systemd.
type: path
force:
description:
- Replace the systemd unit file(s) even if it already exists.
- This works with dest option.
type: bool
default: false
new:
description:
- Generate unit files that create containers and pods, not only start them.
Expand Down Expand Up @@ -446,8 +452,13 @@ def generate_systemd(module):
unit_file_name,
)

# See if we need to write the unit file, default yes
need_to_write_file = bool(compare_systemd_file_content(unit_file_full_path, unit_content))
if module.params['force']:
# Force to replace the existing unit file
need_to_write_file = True
else:
# See if we need to write the unit file, default yes
need_to_write_file = bool(compare_systemd_file_content(
unit_file_full_path, unit_content))

# Write the file, if needed
if need_to_write_file:
Expand Down Expand Up @@ -488,6 +499,11 @@ def run_module():
'required': False,
'default': False,
},
'force': {
'type': 'bool',
'required': False,
'default': False,
},
'restart_policy': {
'type': 'str',
'required': False,
Expand Down
42 changes: 42 additions & 0 deletions tests/integration/targets/podman_generate_systemd/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,48 @@
path: "/tmp/podman_generate_systemd/{{ item.key }}.service"
loop: "{{ postgres_local_systemd_unit.systemd_units | dict2items }}"

- name: Try to create a systemd unit file on the same path
containers.podman.podman_generate_systemd:
name: postgres_local
dest: /tmp/podman_generate_systemd
register: generate1

- name: Check the unit files exists
ansible.builtin.stat:
path: "/tmp/podman_generate_systemd/{{ item.key }}.service"
loop: "{{ generate1.systemd_units | dict2items }}"
register: unitfile1

- name: Get checksum value
set_fact:
checksum1: "{{ item.stat.checksum }}"
with_items: "{{ unitfile1.results }}"

- name: Force to create a systemd unit file on the same path
containers.podman.podman_generate_systemd:
name: postgres_local
dest: /tmp/podman_generate_systemd
force: true
register: generate2

- name: Check the unit files exists again
ansible.builtin.stat:
path: "/tmp/podman_generate_systemd/{{ item.key }}.service"
loop: "{{ generate2.systemd_units | dict2items }}"
register: unitfile2

- name: Get checksum value again
set_fact:
checksum2: "{{ item.stat.checksum }}"
with_items: "{{ unitfile2.results }}"

- name: Check if the sytemd unit files are as expected
assert:
that:
- generate1 is not changed
- generate2 is changed
- checksum1 != checksum2

- name: Regenerate the systemd units with all the options
containers.podman.podman_generate_systemd:
name: postgres_local
Expand Down

0 comments on commit 04f455b

Please sign in to comment.