# SPDX-License-Identifier: MIT
---
- name: Gather package facts
  package_facts:
  no_log: true

- name: Set role variables needed for cleanup
  include_role:
    name: linux-system-roles.mssql
    tasks_from: set_vars.yml
    public: true

- name: Purge cluster configuration
  vars:
    ha_cluster_cluster_present: false
    ha_cluster_enable_repos: false
  include_role:
    name: fedora.linux_system_roles.ha_cluster
  when: ansible_facts.packages.pcs is defined

- name: Debug ansible_python_version
  debug:
    var: ansible_python_version

- name: Purge firewall configuration
  vars:
    firewall:
      - previous: replaced
  include_role:
    name: fedora.linux_system_roles.firewall
  when: ansible_facts.packages.firewalld is defined

- name: Purge selinux configuration
  vars:
    selinux_all_purge: true
  include_role:
    name: fedora.linux_system_roles.selinux
  when: mssql_manage_selinux | d(false) | bool

- name: Leave realm
  command: realm leave
  register: realm_leave
  failed_when: false
  changed_when: >-
    not "Couldn't find a configured realm" in realm_leave.stderr
  when: ansible_facts.packages.realmd is defined

- name: Destroy Kerberos tickets  # noqa no-changed-when
  command: kdestroy -A
  when: ansible_facts.packages["krb5-workstation"] is defined

- name: Remove related packages
  package:
    name:
      - adutil
      - mssql-server*
      - mssql-tools*
      - unixODBC-devel
      - mssql-server-fts
      - mssql-server-ha
      - powershell
    state: absent
    autoremove: true

- name: Remove related files
  shell: >-
    rm -rfv /var/opt/mssql*
    /opt/mssql*
    /var/log/pacemaker/pacemaker.log
    /etc/yum.repos.d/packages-microsoft-com-*
    /tmp/*.j2_*
    /tmp/mssql_data
    /tmp/mssql_log
    /etc/systemd/system/mssql-server.service.d
    /etc/systemd/system/multi-user.target.wants/mssql-server.service
  register: __mssql_cleanup_remove
  changed_when: "'removed' in __mssql_cleanup_remove.stdout"

# On SQL Server 2017, 2019 the service remains after removing RPMs
- name: Stop the mssql-server service  # noqa command-instead-of-module
  shell: systemctl stop mssql-server || true
  changed_when: false
  when: __mssql_is_booted | bool

- name: Get SELinux policy modules
  command: semodule -l
  changed_when: false
  register: __mssql_policy_loaded

# not installed in Fedora by default
- name: Ensure that semanage command is available
  package:
    name: policycoreutils-python-utils
    state: present
  when: "'mssql' in __mssql_policy_loaded.stdout_lines"

# removing the mssql-server-selinux package fails to actually remove the
# policy (and ignores the failure)
- name: Hack around broken mssql SELinux profile and actually remove it
  shell: |-
    set -eux
    semanage fcontext -D
    semanage port -D
    semodule --priority=200 -r mssql
  changed_when: true
  when: "'mssql' in __mssql_policy_loaded.stdout_lines"

- name: Check for leftover mssql processes
  command: pgrep -au mssql
  register: __mssql_processes
  changed_when: false
  # 0: found, 1: no processes, 2: user mssql does not exist
  failed_when: not __mssql_processes.rc in [0, 1, 2]

- name: Kill leftover mssql processes
  command: pkill --signal KILL -e -u mssql
  changed_when: true
  when: __mssql_processes.rc == 0

- name: Remove system user
  user:
    name: mssql
    state: absent
    remove: true
