---
#----------------------------------------------------------------#
# Preflight                              [deepflow_clickhouse_check]
#----------------------------------------------------------------#
- name: check deepflow clickhouse+s3 prerequisites
  tags: deepflow_clickhouse_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_clickhouse_s3 role"

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

#----------------------------------------------------------------#
# Configure                               [deepflow_clickhouse_conf]
#----------------------------------------------------------------#
- name: configure deepflow clickhouse+s3 stack
  tags: deepflow_clickhouse_conf
  block:
    - name: create deepflow clickhouse+s3 directories
      file:
        path: "{{ item }}"
        state: directory
        owner: root
        group: root
        mode: '0755'
      loop:
        - "{{ deepflow_clickhouse_s3_stack_dir }}"
        - "{{ deepflow_data }}"
        - "{{ deepflow_clickhouse_data }}"
        - "{{ deepflow_clickhouse_config_dir }}"
        - "{{ deepflow_s3_data }}"
      when: item != deepflow_s3_data or deepflow_s3_enabled | bool

    - name: render deepflow clickhouse+s3 environment
      template:
        src: deepflow-clickhouse-s3.env.j2
        dest: "{{ deepflow_clickhouse_s3_env_file }}"
        owner: root
        group: root
        mode: '0640'

    - name: render deepflow clickhouse config
      template:
        src: clickhouse-config.d/retention.xml.j2
        dest: "{{ deepflow_clickhouse_config_dir }}/retention.xml"
        owner: root
        group: root
        mode: '0644'

    - name: render deepflow clickhouse+s3 docker compose
      template:
        src: docker-compose.yml.j2
        dest: "{{ deepflow_clickhouse_s3_compose_file }}"
        owner: root
        group: root
        mode: '0644'

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

#----------------------------------------------------------------#
# Launch                                  [deepflow_clickhouse_up]
#----------------------------------------------------------------#
- name: launch deepflow clickhouse+s3 stack
  tags: deepflow_clickhouse_up
  block:
    - name: start deepflow clickhouse+s3 service
      systemd:
        name: deepflow-clickhouse-s3
        state: restarted
        enabled: yes
        daemon_reload: yes

    - name: wait for deepflow clickhouse tcp port
      wait_for:
        host: 127.0.0.1
        port: "{{ deepflow_clickhouse_tcp_port }}"
        timeout: 60

    - name: wait for deepflow minio api port
      wait_for:
        host: 127.0.0.1
        port: "{{ deepflow_minio_api_port }}"
        timeout: 60
      when: deepflow_s3_enabled | bool
