---
- name: Remove Old Docker Packages
  apt:
    state: absent
    force: yes
    pkg:
      - docker
      - docker-engine
      - docker.io
      - containerd
      - runc

- name: Install Docker Dependencies
  apt:
    state: present
    force: yes
    pkg:
      - apt-transport-https
      - ca-certificates
      - curl
      - software-properties-common

- name: Add Docker Repository Key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present

# This seems to be breaking on some servers
- name: Add NGINX Repository Key
  apt_key:
    url: https://nginx.org/keys/nginx_signing.key
    state: present

- name: Add Docker Repository
  apt_repository:
    repo: deb https://download.docker.com/linux/ubuntu focal stable
    state: present
    update_cache: true

- name: Install Docker
  apt:
    name: docker-ce
    state: present

- name: Add Jingrow User to docker Group
  user:
    name: jingrow
    groups:
      - docker
    append: true

- name: Setup Docker Metrics
  template:
    src: daemon.json
    dest: /etc/docker/daemon.json

- name: Create Docker SystemD drop-in directory
  file:
    dest: /etc/systemd/system/docker.service.d
    state: directory
    owner: root
    group: root
    mode: 0644
    recurse: true

- name: Set Docker to depend on Mounts
  template:
    src: mounts.conf
    dest: /etc/systemd/system/docker.service.d/mounts.conf
    owner: root
    group: root
    mode: 0644
  when: docker_depends_on_mounts | default(false) | bool

- name: Restart Docker Daemon
  systemd:
    daemon_reload: true
    name: docker
    state: restarted

- name: Get Docker Info
  command: docker info

- name: Stop Agent processes
  supervisorctl:
    name: "agent:"
    state: stopped
  register: stop_result
  retries: 3
  delay: 5
  until: stop_result is success
  ignore_errors: yes

- name: Wait for Agent processes to stop
  shell: |
    for i in $(seq 1 30); do
      if ! supervisorctl status agent: | grep -q "RUNNING"; then
        exit 0
      fi
      sleep 1
    done
    exit 1
  register: wait_result
  changed_when: false
  when: stop_result is success

- name: Start Agent processes in order
  supervisorctl:
    name: "{{ item }}"
    state: started
  loop:
    - agent:redis
    - agent:web
    - agent:worker-0
    - agent:worker-1
  register: start_result
  retries: 3
  delay: 5
  until: start_result is success
  ignore_errors: yes

- name: Wait for Agent processes to be ready
  shell: |
    for i in $(seq 1 30); do
      if supervisorctl status agent: | grep -q "RUNNING" && ! supervisorctl status agent: | grep -q "STOPPED\|FATAL\|EXITED"; then
        exit 0
      fi
      sleep 1
    done
    exit 1
  register: wait_ready_result
  changed_when: false
  when: start_result is success

- name: Verify Agent processes status
  supervisorctl:
    name: "agent:"
    state: started
  register: verify_result
  retries: 3
  delay: 5
  until: verify_result is success
  ignore_errors: yes

- name: Fail if Agent processes not running
  fail:
    msg: "Agent processes failed to start properly"
  when: verify_result is failed
