---
#--------------------------------------------------------------#
# Preflight                                [deepflow_mysql_check]
#--------------------------------------------------------------#
- name: check deepflow mysql prerequisites
  tags: deepflow_mysql_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_mysql role"

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

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

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

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

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

#--------------------------------------------------------------#
# Launch                                     [deepflow_mysql_up]
#--------------------------------------------------------------#
- name: launch deepflow mysql stack
  tags: deepflow_mysql_up
  block:
    - name: start deepflow mysql service
      systemd:
        name: deepflow-mysql
        state: restarted
        enabled: yes
        daemon_reload: yes

    - name: wait for deepflow mysql port
      wait_for:
        host: 127.0.0.1
        port: "{{ deepflow_mysql_port }}"
        timeout: 60
