From 191f20cb8fece72986246751b1a33369784a02ab Mon Sep 17 00:00:00 2001 From: Ahmad Wilson Date: Sun, 5 Jan 2025 18:44:37 -0600 Subject: [PATCH] chore: update basic-service uninstall logic --- defaults/main.yml | 1 + handlers/main.yml | 98 ++++++++++++++++++++++++++++++--------- tasks/container/setup.yml | 13 +----- tasks/main.yml | 11 +++++ tasks/systemd/setup.yml | 2 +- 5 files changed, 91 insertions(+), 34 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index 5a8ccde..5ef65a3 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -4,6 +4,7 @@ destination_directory: /usr/local/bin setup_mode: container work_dir: /var/tmp +host_data_dir: /var/tmp command: diff --git a/handlers/main.yml b/handlers/main.yml index f423707..9f2b8fd 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,22 +1,78 @@ --- -- name: Uninstall service container - community.docker.docker_container: - name: "{{ name }}" - state: absent - purge_networks: true - listen: Container uninstall - -- name: Remove generated service config files - become: true - ansible.builtin.file: - path: "{{ work_dir }}/{{ item.value.destinationPath }}" - state: absent - with_dict: "{{ config }}" - listen: Container uninstall - -- name: Remove service data directory - become: true - ansible.builtin.file: - path: "{{ hostDataDir }}" - state: absent - listen: Container uninstall +- name: Uninstall Service + listen: Uninstall Service + block: + # --- Storage Handlers --- + - name: Remove host data directory + ansible.builtin.file: + path: "{{ hostDataDir }}" + state: absent + when: hostDataDir is defined + + # --- Network Handlers --- + - name: Remove service ingress iptables rules + ansible.builtin.iptables: + chain: INPUT + protocol: tcp + destination_ports: "{{ ingress_list }}" + jump: ACCEPT + state: absent + + # --- Systemd Handlers --- + - name: Remove destination directory + ansible.builtin.file: + path: "{{ destination_directory }}" + state: absent + when: setup_mode == 'systemd' + + - name: Remove temporary binary file + ansible.builtin.file: + path: "/tmp/{{ binary_file_name }}" + state: absent + when: setup_mode == 'systemd' + + - name: Remove extracted files or moved binary + ansible.builtin.file: + path: "{{ destination_directory }}/{{ binary_file_name_override | default(binary_file_name) }}" + state: absent + when: setup_mode == 'systemd' + + - name: Remove the systemd unit file + ansible.builtin.include_role: + name: ansible-role-systemd + vars: + unit_config: + - name: "{{ name }}" + state: absent + enabled: false + perform_uninstall: true + when: setup_mode == 'systemd' + + - name: Remove generated config files + ansible.builtin.file: + path: "{{ item.value.destinationPath }}" + state: absent + with_dict: "{{ config }}" + when: setup_mode == 'systemd' + + - name: Remove config directories + ansible.builtin.file: + path: "{{ item.value.destinationPath | dirname }}" + state: absent + with_dict: "{{ config }}" + when: setup_mode == 'systemd' + + # --- Container Handlers --- + - name: Container uninstall + community.docker.docker_container: + name: "{{ name }}" + state: absent + when: setup_mode == 'container' + + # --- Kubernetes Handlers --- + - name: Uninstall Helm release + kubernetes.core.helm: + name: "{{ name }}" + state: absent + release_namespace: "{{ helm_namespace }}" + when: setup_mode == 'k8s' diff --git a/tasks/container/setup.yml b/tasks/container/setup.yml index d4a0885..363110e 100644 --- a/tasks/container/setup.yml +++ b/tasks/container/setup.yml @@ -1,20 +1,9 @@ --- -- name: Uninstall provisioned container components - when: uninstall|bool - block: - - name: Broadcast uninstall signal - ansible.builtin.command: /bin/true - notify: Container uninstall - changed_when: true - always: - - name: End play following uninstallation - ansible.builtin.meta: end_play - # Compile container resource lists - name: Determine service container volume list ansible.builtin.set_fact: - volume_list: "{{ volume_list + [work_dir + item.value.destinationPath + ':' + item.value.destinationPath] }}" + volume_list: "{{ volume_list + [item.value.destinationPath | dirname + ':' + item.value.destinationPath | dirname] }}" with_dict: "{{ config }}" - name: Add data DIRs to container volume list diff --git a/tasks/main.yml b/tasks/main.yml index 62554d7..044700b 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,4 +1,15 @@ --- +- name: Uninstall provisioned service components + when: uninstall|bool + block: + - name: Broadcast uninstall signal + ansible.builtin.command: /bin/true + notify: Uninstall Service + changed_when: true + always: + - name: End play following uninstallation + ansible.builtin.meta: end_play + - name: Manage storage/data setup ansible.builtin.include_tasks: common/storage-setup.yml diff --git a/tasks/systemd/setup.yml b/tasks/systemd/setup.yml index 1dac8d0..a4e4ed3 100644 --- a/tasks/systemd/setup.yml +++ b/tasks/systemd/setup.yml @@ -7,7 +7,7 @@ systemd_environment_directive: "{{ systemd_environment_directive + ' ' + item.key + '=' + item.value }}" with_dict: "{{ config_env }}" -- name: Set default Sysetmd unit configurations +- name: Set default Systemd unit configurations ansible.builtin.set_fact: default_unit_info: Wants: network-online.target