Skip to content

Commit

Permalink
Fix: Service errors when installing client only from source
Browse files Browse the repository at this point in the history
Salvoxia committed Dec 1, 2024
1 parent 9a1789b commit 9683850
Showing 9 changed files with 152 additions and 12 deletions.
31 changes: 31 additions & 0 deletions molecule/install_from_source_client_only/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
- name: Converge
hosts: all
gather_facts: true
pre_tasks:
- name: Create NUT configuration folder.
ansible.builtin.file:
path: "/etc/nut"
state: directory
mode: '0755'
- name: Copy dummy-ups.dev file
ansible.builtin.copy:
src: "../dummy-ups.dev"
dest: "/etc/nut/dummy-ups.dev"
mode: '0755'
roles:
- role: salvoxia.nut
nut_install_from_source: true
nut_source_tag: v2.8.2
nut_packages:
- nut-client
nut_users:
- name: monitor
password: changeme
role: secondary
nut_ups:
- name: dummy_ups
monitoruser: monitor
driver: dummy-ups
device: /etc/nut/dummy-ups.dev
description: "Dummy UPS for testing"
40 changes: 40 additions & 0 deletions molecule/install_from_source_client_only/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
driver:
name: docker
platforms:
- name: debian-12
image: geerlingguy/docker-debian12-ansible
command: ${MOLECULE_DOCKER_COMMAND:-""}
pre_build_image: true
cgroupns_mode: host
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
- name: debian-11
image: geerlingguy/docker-debian11-ansible
command: ${MOLECULE_DOCKER_COMMAND:-""}
pre_build_image: true
cgroupns_mode: host
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
- name: ubuntu-2404
image: geerlingguy/docker-ubuntu2404-ansible
command: ${MOLECULE_DOCKER_COMMAND:-""}
pre_build_image: true
cgroupns_mode: host
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
- name: ubuntu-2204
image: geerlingguy/docker-ubuntu2404-ansible
command: ${MOLECULE_DOCKER_COMMAND:-""}
pre_build_image: true
cgroupns_mode: host
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
provisioner:
name: ansible
verifier:
name: ansible
34 changes: 34 additions & 0 deletions molecule/install_from_source_client_only/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
- name: Converge
hosts: all
gather_facts: true
tasks:
- name: Get installed NUT version
ansible.builtin.include_tasks: ../../tasks/get_installed_nut_version.yml

- name: Verify installed version matches desired version
ansible.builtin.assert:
that:
- nut_installed_version is defined
- nut_installed_version == "v2.8.2"

- name: Gather installed services
ansible.builtin.service_facts:

- name: Verify NUT services are running
ansible.builtin.assert:
that:
- ansible_facts.services['nut-monitor.service'] is defined
- ansible_facts.services['nut-monitor.service'].state == 'running'
- ansible_facts.services['nut-server.service'].state == 'stopped'
- ansible_facts.services['nut-server.service'].status == 'masked'

- name: Gather package facts
ansible.builtin.package_facts:
when: nut_installed_version is defined

- name: Verify no NUT packages are installed
ansible.builtin.assert:
that:
- ansible_facts.packages['nut-client'] is not defined
- ansible_facts.packages['nut-server'] is not defined
3 changes: 1 addition & 2 deletions tasks/delete_service.yml
Original file line number Diff line number Diff line change
@@ -3,12 +3,11 @@
ansible.builtin.systemd_service:
name: "{{ service_name }}"
register: service_status
when: nut_state == "absent"

- name: Remove service unit files
ansible.builtin.file:
state: absent
path: "{{ service_status.status['FragmentPath'] }}"
notify:
- Systemctl daemon-reload
when: nut_state == "absent" and service_status.status['FragmentPath'] is defined
when: service_status.status['FragmentPath'] is defined
29 changes: 25 additions & 4 deletions tasks/manage_install_from_source.yml
Original file line number Diff line number Diff line change
@@ -98,7 +98,7 @@
deb: "{{ nut_dir.path }}/{{ build_deps_kg['build-deps-package'] }}"

- name: Build list of NUT drivers
when: "'nut-server' in nut_packages and nut_reinstall
when: "nut_reinstall
or (nut_state == 'present' and not nut_installed)
or (nut_state == 'absent' and nut_installed)"
block:
@@ -108,11 +108,11 @@
when: __nut_packages_to_drivers | selectattr(item, 'defined') | list | count > 0
with_items: "{{ nut_packages }}"

- name: Add dummy-ups driver to nut-drivers for uninstall purposes
- name: Add dummy-ups driver to nut-drivers for uninstall or client-only purposes
ansible.builtin.set_fact:
nut_drivers:
- dummy-ups
when: nut_state == 'absent' and nut_drivers | length == 0
when: (nut_state == 'absent' or 'nut-server' not in nut_packages) and nut_drivers | length == 0

- name: Verify at least one driver is selected
ansible.builtin.assert:
@@ -162,9 +162,30 @@
- name: Define NUT services to enable
ansible.builtin.set_fact:
nut_services:
- nut-client.service
- nut-monitor.service

- name: Add NUT services for NUT server
ansible.builtin.set_fact:
nut_services: "{{ nut_services + ['nut-server.service', 'nut-driver.service'] }}"
when: "'nut-server' in nut_packages"

- name: Disable non-monitor services
when: nut_state == 'present' and 'nut-server' not in nut_packages
block:
- name: Systemctl daemon-reload
ansible.builtin.systemd:
daemon_reload: true

- name: Get installed NUT services
ansible.builtin.include_tasks: get_installed_nut_services.yml

- name: Stop and mask services
ansible.builtin.service:
state: stopped
enabled: false
masked: true
name: "{{ item }}"
notify:
- Systemctl daemon-reload
when: item != 'nut-driver@.service'
loop: "{{ nut_services_installed | rejectattr('name', 'equalto', 'nut-monitor.service') | rejectattr('status', 'equalto', 'masked') | map(attribute='name') | list }}"
8 changes: 4 additions & 4 deletions tasks/restart.yml
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@
when: nut_enable_service

- name: "Restart nut."
ansible.builtin.service:
name: "{{ item }}"
state: restarted
with_items: "{{ nut_services_installed | map(attribute='name') | list | difference(['nut-driver.service', 'nut-driver@.service']) }}"
ansible.builtin.include_tasks: restart_service.yml
loop_control:
loop_var: service_name
loop: "{{ nut_services_installed | map(attribute='name') | list | difference(['nut-driver.service', 'nut-driver@.service']) }}"
when: nut_enable_service
11 changes: 11 additions & 0 deletions tasks/restart_service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Determine service unit file
ansible.builtin.systemd_service:
name: "{{ service_name }}"
register: service_status

- name: "Restart service."
ansible.builtin.service:
name: "{{ service_name }}"
state: restarted
when: service_status.status['LoadState'] != 'masked'
5 changes: 5 additions & 0 deletions tasks/service.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
---
- name: Systemctl daemon-reload
ansible.builtin.systemd:
daemon_reload: true

- name: Get service facts
ansible.builtin.service_facts:
when: nut_services is defined
@@ -19,6 +23,7 @@
ansible.builtin.service:
name: "{{ item }}"
state: started
masked: false
enabled: true
when: "nut_services is not undefined
and ansible_facts.services[item] is defined
3 changes: 1 addition & 2 deletions tasks/uninstall.yml
Original file line number Diff line number Diff line change
@@ -4,10 +4,9 @@

- name: Gather the package facts
ansible.builtin.package_facts:
when: nut_installed_version is defined
when: nut_installed

- name: Disable services
when: nut_installed_version is defined
block:
- name: Populate service facts
ansible.builtin.service_facts:

0 comments on commit 9683850

Please sign in to comment.