---
#--------------------------------------------------------------#
# Preflight                            [deepflow_connector_check]
#--------------------------------------------------------------#
- name: check deepflow connector prerequisites
  tags: deepflow_connector_check
  block:
    - name: assert docker is enabled
      assert:
        that:
          - docker_enabled is defined
          - docker_enabled | bool
        fail_msg: "docker_enabled=true is required for deepflow_connector"

    - name: check docker binary exists
      command: docker --version
      changed_when: false

#--------------------------------------------------------------#
# Configure                             [deepflow_connector_conf]
#--------------------------------------------------------------#
- name: configure deepflow connector stack
  tags: deepflow_connector_conf
  block:
    - name: create deepflow connector directories
      file:
        path: "{{ item }}"
        state: directory
        owner: root
        group: root
        mode: '0755'
      loop:
        - "{{ deepflow_connector_stack_dir }}"

    - name: render deepflow connector environment
      template:
        src: deepflow-connector.env.j2
        dest: "{{ deepflow_connector_env_file }}"
        owner: root
        group: root
        mode: '0640'

    - name: render deepflow connector collector config
      template:
        src: otel-collector.yaml.j2
        dest: "{{ deepflow_connector_config_file }}"
        owner: root
        group: root
        mode: '0644'

    - name: render deepflow connector docker compose
      template:
        src: docker-compose.yml.j2
        dest: "{{ deepflow_connector_compose_file }}"
        owner: root
        group: root
        mode: '0644'

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

#--------------------------------------------------------------#
# Launch                               [deepflow_connector_launch]
#--------------------------------------------------------------#
- name: launch deepflow connector stack
  tags: deepflow_connector_launch
  block:
    - name: restart deepflow connector service
      systemd:
        name: deepflow-connector
        state: restarted
        enabled: yes
        daemon_reload: yes

    - name: wait for deepflow connector service online
      wait_for:
        host: 127.0.0.1
        port: "{{ deepflow_connector_listen_port }}"
        timeout: 60
