---
- name: Weave pre-upgrade | Stop legacy weave
  command: weave stop
  failed_when: false

- name: Weave pre-upgrade | Stop legacy systemd weave services
  service:
    name: "{{ item }}"
    enabled: no
    state: stopped
  with_items:
    - weaveexpose
    - weaveproxy
    - weave
  failed_when: false

- name: Weave pre-upgrade | Purge legacy systemd weave systemd unit files
  file:
    path: "{{ item }}"
    state: absent
  register: purged_weave_systemd_units
  with_items:
    - "/etc/systemd/system/weaveexpose.service"
    - "/etc/systemd/system/weaveproxy.service"
    - "/etc/systemd/system/weave.service"

- name: Weave pre-upgrade | Reload systemd
  command: systemctl daemon-reload
  when: ansible_service_mgr == "systemd" and purged_weave_systemd_units.changed

- name: Weave pre-upgrade | Purge legacy weave configs and binary
  file:
    path: "{{ item }}"
    state: absent
  with_items:
    - "{{ bin_dir }}/weave"
    - "/etc/weave.env"

- name: Weave pre-upgrade | Purge legacy weave docker containers
  shell: "docker ps -af 'name=^/weave.*' -q | xargs --no-run-if-empty docker rm -f"
  retries: 3
  failed_when: false
