# This deletes *ALL* Origin and OpenShift Enterprise content installed by
# ansible.  This includes:
#
#    configuration
#    containers
#    example templates and imagestreams
#    images
#    RPMs
---
- hosts: OSEv3:children
  become: yes
  tasks:
  - name: Detecting Operating System
    shell: ls /run/ostree-booted
    ignore_errors: yes
    failed_when: false
    register: ostree_output

  # Since we're not calling openshift_facts we'll do this for now
  - set_fact:
      is_atomic: "{{ ostree_output.rc == 0 }}"
  - set_fact:
      is_containerized: "{{ is_atomic or containerized | default(false) | bool }}"

# Stop services on all hosts prior to removing files.
- hosts: nodes
  become: yes
  tasks:
  - name: Remove dnsmasq dispatcher
    file:
      path: "{{ item }}"
      state: absent
    with_items:
    - /etc/dnsmasq.d/origin-dns.conf
    - /etc/dnsmasq.d/origin-upstream-dns.conf
    - /etc/dnsmasq.d/openshift-ansible.conf
    - /etc/NetworkManager/dispatcher.d/99-origin-dns.sh
  - service:
      name: NetworkManager
      state: restarted
  - name: Stop services
    service: name={{ item }} state=stopped
    with_items:
    - atomic-openshift-node
    - openshift-node
    - openvswitch
    - origin-node
    failed_when: false

- hosts: masters
  become: yes
  tasks:
  - name: Stop services
    service: name={{ item }} state=stopped
    with_items:
    - atomic-openshift-master
    - atomic-openshift-master-api
    - atomic-openshift-master-controllers
    - openshift-master
    - openshift-master-api
    - openshift-master-controllers
    - origin-master
    - origin-master-api
    - origin-master-controllers
    - pcsd
    failed_when: false

- hosts: etcd
  become: yes
  tasks:
  - name: Stop services
    service: name={{ item }} state=stopped
    with_items:
    - etcd
    failed_when: false

- hosts: lb
  become: yes
  tasks:
  - name: Stop services
    service: name={{ item }} state=stopped
    with_items:
    - haproxy
    failed_when: false

- hosts: nodes
  become: yes
  vars:
    node_dirs:
    - "/etc/origin"
    - "/var/lib/origin"
  tasks:
  - name: unmask services
    command: systemctl unmask "{{ item }}"
    changed_when: False
    failed_when: False
    with_items:
    - firewalld

  - block:
    - block:
      - name: Remove packages
        package: name={{ item }} state=absent
        with_items:
        - atomic-openshift
        - atomic-openshift-clients
        - atomic-openshift-excluder
        - atomic-openshift-docker-excluder
        - atomic-openshift-node
        - atomic-openshift-sdn-ovs
        - cockpit-bridge
        - cockpit-docker
        - cockpit-system
        - cockpit-ws
        - kubernetes-client
        - openshift
        - openshift-node
        - openshift-sdn
        - openshift-sdn-ovs
        - openvswitch
        - origin
        - origin-excluder
        - origin-docker-excluder
        - origin-clients
        - origin-node
        - origin-sdn-ovs
        - tuned-profiles-openshift-node
        - tuned-profiles-origin-node

      - name: Remove flannel package
        package: name=flannel state=absent
        when: openshift_use_flannel | default(false) | bool
      when: not is_atomic | bool

    - shell: systemctl reset-failed
      changed_when: False

    - shell: systemctl daemon-reload
      changed_when: False

    - name: Remove br0 interface
      shell: ovs-vsctl del-br br0
      changed_when: False
      failed_when: False

    - name: Remove linux interfaces
      shell: ip link del "{{ item }}"
      changed_when: False
      failed_when: False
      with_items:
      - lbr0
      - vlinuxbr
      - vovsbr

    - name: Remove virtual devices
      command: nmcli delete device "{{ item }}"
      failed_when: False
      with_items:
      - tun0
      - docker0

    when: openshift_remove_all | default(true) | bool

  - shell: atomic uninstall "{{ item }}"-master-api
    changed_when: False
    failed_when: False
    with_items:
    - openshift-enterprise
    - origin

  - shell: atomic uninstall "{{ item }}"-master-controllers
    changed_when: False
    failed_when: False
    with_items:
    - openshift-enterprise
    - origin

  - shell: atomic uninstall "{{ item }}"-master
    changed_when: False
    failed_when: False
    with_items:
    - openshift-enterprise
    - origin

  - shell: atomic uninstall "{{ item }}"-node
    changed_when: False
    failed_when: False
    with_items:
    - openshift-enterprise
    - origin

  - shell: atomic uninstall "{{ item }}"
    changed_when: False
    failed_when: False
    with_items:
    - etcd
    - openvswitch

  - shell: find /var/lib/origin/openshift.local.volumes -type d -exec umount {} \; 2>/dev/null || true
    changed_when: False

  - shell: docker rm -f "{{ item }}"-master "{{ item }}"-node
    changed_when: False
    failed_when: False
    with_items:
    - openshift-enterprise
    - origin

  - shell: docker ps -a | grep Exited | egrep "{{ item }}" | awk '{print $1}'
    changed_when: False
    failed_when: False
    register: exited_containers_to_delete
    with_items:
    - openshift3/ose
    - openshift3/node
    - openshift3/openvswitch
    - openshift/origin

  - shell: "docker rm {{ item.stdout_lines | join(' ') }}"
    changed_when: False
    failed_when: False
    with_items: "{{ exited_containers_to_delete.results }}"

  - block:
    - block:
      - shell: docker images | egrep {{ item }} | awk '{ print $3 }'
        changed_when: False
        failed_when: False
        register: images_to_delete
        with_items:
        - registry\.access\..*redhat\.com/openshift3
        - registry\.qe\.openshift\.com/.*
        - registry\.access\..*redhat\.com/rhel7/etcd
        - docker.io/openshift

      - shell: "docker rmi -f {{ item.stdout_lines | join(' ') }}"
        changed_when: False
        failed_when: False
        with_items: "{{ images_to_delete.results }}"
      when: openshift_uninstall_images | default(True) | bool

    - name: remove sdn drop files
      file:
        path: /run/openshift-sdn
        state: absent

    - name: Remove files owned by RPMs
      file: path={{ item }} state=absent
      with_items:
      - /etc/sysconfig/openshift-node
      - /etc/sysconfig/openvswitch
      - /run/openshift-sdn
    when: openshift_remove_all | default(True) | bool

  - find: path={{ item }} file_type=file
    register: files
    with_items:
    - "{{ node_dirs }}"

  - find: path={{ item }} file_type=directory
    register: directories
    with_items:
    - "{{ node_dirs }}"

  - file: path={{ item.1.path }} state=absent
    with_subelements:
    - "{{ files.results | default([]) }}"
    - files

  - file: path={{ item.1.path }} state=absent
    with_subelements:
    - "{{ directories.results | default([]) }}"
    - files

  - shell: systemctl daemon-reload
    changed_when: False

  - name: restart container-engine
    service: name=container-engine state=stopped enabled=no
    failed_when: false
    register: container_engine

  - name: restart docker
    service: name=docker state=stopped enabled=no
    failed_when: false
    when: not (container_engine | changed)
    register: l_docker_restart_docker_in_pb_result
    until: not l_docker_restart_docker_in_pb_result | failed
    retries: 3
    delay: 30

  - name: Remove remaining files
    file: path={{ item }} state=absent
    with_items:
    - /etc/ansible/facts.d/openshift.fact
    - /etc/openshift
    - /etc/openshift-sdn
    - /etc/pki/ca-trust/source/anchors/openshift-ca.crt
    - /etc/sysconfig/atomic-openshift-node
    - /etc/sysconfig/atomic-openshift-node-dep
    - /etc/sysconfig/openshift-node-dep
    - /etc/sysconfig/origin-node
    - /etc/sysconfig/origin-node
    - /etc/sysconfig/origin-node-dep
    - /etc/systemd/system/atomic-openshift-node-dep.service
    - /etc/systemd/system/atomic-openshift-node.service
    - /etc/systemd/system/atomic-openshift-node.service.wants
    - /etc/systemd/system/docker.service.d/docker-sdn-ovs.conf
    - /etc/systemd/system/openvswitch.service
    - /etc/systemd/system/origin-node-dep.service
    - /etc/systemd/system/origin-node.service
    - /etc/systemd/system/origin-node.service.wants
    - /var/lib/docker

  - name: Rebuild ca-trust
    command: update-ca-trust

  - name: Reset Docker proxy configuration
    lineinfile:
      state=absent
      dest=/etc/sysconfig/docker
      regexp='(NO_PROXY|HTTP_PROXY|HTTPS_PROXY)=.*'

  - name: Reset Docker registry configuration
    lineinfile:
      state=absent
      dest=/etc/sysconfig/docker
      regexp='(ADD_REGISTRY|BLOCK_REGISTRY|INSECURE_REGISTRY)=.*'

  - name: Detect Docker storage configuration
    shell: vgs -o name | grep docker
    register: docker_vg_name
    failed_when: false
    changed_when: false

  - name: Wipe out Docker storage contents
    command: vgremove -f {{ item }}
    with_items: "{{ docker_vg_name.stdout_lines }}"
    when: docker_vg_name.rc == 0

  - name: Wipe out Docker storage configuration
    file: path=/etc/sysconfig/docker-storage state=absent
    when: docker_vg_name.rc == 0


- hosts: masters
  become: yes
  vars:
    master_dirs:
    - "/etc/origin"
    - "/var/lib/origin"
  tasks:
  - name: unmask services
    command: systemctl unmask "{{ item }}"
    changed_when: False
    failed_when: False
    with_items:
    - firewalld
    - atomic-openshift-master

  - name: Remove packages
    package: name={{ item }} state=absent
    when: not is_atomic | bool and openshift_remove_all | default(True) | bool
    with_items:
    - atomic-openshift
    - atomic-openshift-clients
    - atomic-openshift-excluder
    - atomic-openshift-docker-excluder
    - atomic-openshift-master
    - cockpit-bridge
    - cockpit-docker
    - cockpit-system
    - cockpit-ws
    - corosync
    - kubernetes-client
    - openshift
    - openshift-master
    - origin
    - origin-clients
    - origin-excluder
    - origin-docker-excluder
    - origin-master
    - pacemaker
    - pcs

  - shell: systemctl reset-failed
    changed_when: False

  - shell: systemctl daemon-reload
    changed_when: False

  - name: Remove files owned by RPMs
    file: path={{ item }} state=absent
    when: openshift_remove_all | default(True) | bool
    with_items:
    - /etc/sysconfig/atomic-openshift-master
    - /etc/sysconfig/openvswitch

  - find: path={{ item }} file_type=file
    register: files
    with_items:
    - "{{ master_dirs }}"

  - find: path={{ item }} file_type=directory
    register: directories
    with_items:
    - "{{ master_dirs }}"

  - file: path={{ item.1.path }} state=absent
    with_subelements:
    - "{{ files.results | default([]) }}"
    - files

  - file: path={{ item.1.path }} state=absent
    with_subelements:
    - "{{ directories.results | default([]) }}"
    - files

  - set_fact:
      client_users: "{{ [ansible_ssh_user, 'root'] | unique }}"

  - name: Remove client kubeconfigs
    file:
      path: "~{{ item }}/.kube"
      state: absent
    with_items:
    - "{{ client_users }}"

  - name: Remove remaining files
    file: path={{ item }} state=absent
    with_items:
    - /etc/ansible/facts.d/openshift.fact
    - /etc/corosync
    - /etc/openshift
    - /etc/openshift-sdn
    - /etc/systemd/system/atomic-openshift-master.service
    - /etc/systemd/system/atomic-openshift-master-api.service
    - /etc/systemd/system/atomic-openshift-master-controllers.service
    - /etc/systemd/system/origin-master.service
    - /etc/systemd/system/origin-master-api.service
    - /etc/systemd/system/origin-master-controllers.service
    - /etc/systemd/system/openvswitch.service
    - /etc/sysconfig/atomic-openshift-master-api
    - /etc/sysconfig/atomic-openshift-master-controllers
    - /etc/sysconfig/origin-master
    - /etc/sysconfig/origin-master-api
    - /etc/sysconfig/origin-master-controllers
    - /etc/sysconfig/openshift-master
    - /etc/sysconfig/origin-master
    - /etc/sysconfig/origin-master-api
    - /etc/sysconfig/origin-master-controllers
    - /usr/share/openshift/examples
    - /var/lib/pacemaker
    - /var/lib/pcsd
    - /usr/lib/systemd/system/atomic-openshift-master-api.service
    - /usr/lib/systemd/system/atomic-openshift-master-controllers.service
    - /usr/lib/systemd/system/origin-master-api.service
    - /usr/lib/systemd/system/origin-master-controllers.service
    - /usr/local/bin/openshift
    - /usr/local/bin/oadm
    - /usr/local/bin/oc
    - /usr/local/bin/kubectl
    - /etc/flannel

  # Since we are potentially removing the systemd unit files for separated
  # master-api and master-controllers services, so we need to reload the
  # systemd configuration manager
  - name: Reload systemd manager configuration
    command: systemctl daemon-reload

- hosts: etcd
  become: yes
  vars:
    etcd_dirs:
    - "/etc/etcd"
    - "/var/lib/etcd"
  tasks:
  - name: unmask services
    command: systemctl unmask "{{ item }}"
    changed_when: False
    failed_when: False
    with_items:
    - etcd
    - etcd3
    - firewalld

  - name: Stop additional atomic services
    service: name={{ item }} state=stopped
    when: is_containerized | bool
    with_items:
    - etcd_container
    failed_when: false

  - name: Remove packages
    package: name={{ item }} state=absent
    when: not is_atomic | bool and openshift_remove_all | default(True) | bool
    with_items:
    - etcd
    - etcd3

  - shell: systemctl reset-failed
    changed_when: False

  - shell: systemctl daemon-reload
    changed_when: False

  - find: path={{ item }} file_type=file
    register: files
    with_items:
    - "{{ etcd_dirs }}"

  - find: path={{ item }} file_type=directory
    register: directories
    with_items:
    - "{{ etcd_dirs }}"

  - file: path={{ item.1.path }} state=absent
    with_subelements:
    - "{{ files.results | default([]) }}"
    - files

  - file: path={{ item.1.path }} state=absent
    with_subelements:
    - "{{ directories.results | default([]) }}"
    - files

  # Intenationally using rm command over file module because if someone had mounted a filesystem
  # at /var/lib/etcd then the contents was not removed correctly
  - name: Remove etcd data
    shell: rm -rf /var/lib/etcd/*
    args:
      warn: no
    failed_when: false

  - name: Remove remaining files
    file: path={{ item }} state=absent
    with_items:
    - /etc/ansible/facts.d/openshift.fact
    - /etc/systemd/system/etcd_container.service
    - /etc/profile.d/etcdctl.sh

- hosts: lb
  become: yes
  tasks:
  - name: unmask services
    command: systemctl unmask "{{ item }}"
    changed_when: False
    failed_when: False
    with_items:
    - firewalld

  - name: Remove packages
    package: name={{ item }} state=absent
    when: not is_atomic | bool and openshift_remove_all | default(True) | bool
    with_items:
    - haproxy

  - shell: systemctl reset-failed
    changed_when: False

  - shell: systemctl daemon-reload
    changed_when: False

  - name: Remove remaining files
    file: path={{ item }} state=absent
    with_items:
    - /etc/ansible/facts.d/openshift.fact
    - /var/lib/haproxy/stats
    # Here we remove only limits.conf rather than directory, as users may put their files.
    # - /etc/systemd/system/haproxy.service.d
    - /etc/systemd/system/haproxy.service.d/limits.conf
    - /etc/systemd/system/haproxy.service