---
#--------------------------------------------------------------#
# Preflight                                [deepflow_agent_check]
#--------------------------------------------------------------#
- name: check deepflow agent mode
  tags: deepflow_agent_check
  assert:
    that:
      - deepflow_agent_mode in ['binary', 'docker', 'k8s']
    fail_msg: "deepflow_agent_mode must be one of: binary, docker, k8s"

- name: check deepflow agent grpc endpoint
  tags: deepflow_agent_check
  assert:
    that:
      - deepflow_agent_grpc_endpoint | default('', true) | length > 0
    fail_msg: "deepflow_agent_grpc_endpoint is required"

- name: check deepflow agent docker prerequisite
  tags: deepflow_agent_check
  when: deepflow_agent_mode == 'docker'
  block:
    - name: assert docker is enabled for docker agent mode
      assert:
        that:
          - docker_enabled is defined
          - docker_enabled | bool
        fail_msg: "docker_enabled=true is required when deepflow_agent_mode=docker"

    - name: check docker binary exists for docker agent mode
      command: docker --version
      changed_when: false

#--------------------------------------------------------------#
# Configure                                [deepflow_agent_conf]
#--------------------------------------------------------------#
- name: create deepflow agent directories
  tags: deepflow_agent_conf
  file:
    path: "{{ item }}"
    state: directory
    owner: root
    group: root
    mode: '0755'
  loop:
    - "{{ deepflow_agent_stack_dir }}"

- name: render deepflow agent environment
  tags: deepflow_agent_conf
  template:
    src: deepflow-agent.env.j2
    dest: "{{ deepflow_agent_env_file }}"
    owner: root
    group: root
    mode: '0640'

- name: configure binary deepflow agent
  tags: deepflow_agent_conf
  when: deepflow_agent_mode == 'binary'
  block:
    - name: download deepflow agent binary when url is provided
      get_url:
        url: "{{ deepflow_agent_download_url }}"
        dest: "{{ deepflow_agent_binary_path }}"
        mode: '0755'
      when: deepflow_agent_download_url | default('', true) | length > 0

    - name: verify deepflow agent binary exists
      stat:
        path: "{{ deepflow_agent_binary_path }}"
      register: deepflow_agent_binary_stat

    - name: assert binary path exists
      assert:
        that:
          - deepflow_agent_binary_stat.stat.exists
        fail_msg: "deepflow_agent_binary_path does not exist. Set deepflow_agent_download_url or provide an existing binary."

    - name: render deepflow agent run script
      template:
        src: run-deepflow-agent.sh.j2
        dest: "{{ deepflow_agent_run_script }}"
        owner: root
        group: root
        mode: '0755'

    - name: install deepflow agent systemd unit
      template:
        src: deepflow-agent.svc.j2
        dest: "{{ systemd_dir }}/deepflow-agent.service"
        owner: root
        group: root
        mode: '0644'

- name: configure docker deepflow agent
  tags: deepflow_agent_conf
  when: deepflow_agent_mode == 'docker'
  block:
    - name: render docker deepflow agent compose
      template:
        src: docker-compose.yml.j2
        dest: "{{ deepflow_agent_compose_file }}"
        owner: root
        group: root
        mode: '0644'

    - name: install docker deepflow agent systemd unit
      template:
        src: deepflow-agent-docker.svc.j2
        dest: "{{ systemd_dir }}/deepflow-agent.service"
        owner: root
        group: root
        mode: '0644'

- name: configure kubernetes deepflow agent
  tags: deepflow_agent_conf
  when: deepflow_agent_mode == 'k8s'
  block:
    - name: render deepflow agent kubernetes manifest
      template:
        src: deepflow-agent.yaml.j2
        dest: "{{ deepflow_agent_k8s_file }}"
        owner: root
        group: root
        mode: '0644'

#--------------------------------------------------------------#
# Launch                                  [deepflow_agent_launch]
#--------------------------------------------------------------#
- name: launch binary/docker deepflow agent
  tags: deepflow_agent_launch
  when: deepflow_agent_mode in ['binary', 'docker']
  block:
    - name: restart deepflow agent systemd service
      systemd:
        name: deepflow-agent
        state: restarted
        enabled: yes
        daemon_reload: yes

- name: optionally apply kubernetes manifest
  tags: deepflow_agent_launch
  when:
    - deepflow_agent_mode == 'k8s'
    - deepflow_agent_k8s_apply | bool
  command: kubectl apply -f {{ deepflow_agent_k8s_file }}
  changed_when: true
