---
- name: Install some base packages
  yum:
    pkg:
      - supervisor
    state: present
    update_cache: yes
  ignore_errors: yes

- name: Create required dirs
  file:
    path={{ item }}
    state=directory
  with_items:
    - "{{ APP_DIR }}"
    - "{{ APP_LOG_DIR }}"

- name: Deploy binary
  copy:
    src: tiup-log-collector.linux_amd64.out
    dest: "{{ APP_DIR }}/tiup-log-collector.out"
    owner: root
    group: root
    mode: 0755

- name: Deploy config
  template:
    src: config.json.j2
    dest: "{{ APP_DIR }}/config.json"

- name: Deploy logrotate config
  copy:
    src: logrotate.conf
    dest: "{{ APP_DIR }}/logrotate.conf"
    owner: root
    group: root
    mode: 0644

- name: Schedule logrotate
  cron:
    name: "Run logrotate" 
    minute: "0,30"
    job: "/usr/sbin/logrotate {{ APP_DIR }}/logrotate.conf"

- name: Deploy nginx config
  copy:
    src: "nginx.conf"
    dest: /etc/nginx/conf.d/telemetry.pingcap.com.conf
  notify:
    - reload nginx

- name: Test nginx conf
  shell: nginx -t

- name: Keep nginx started
  service:
    name: nginx
    state: started

- name: Deploy supervisord.conf
  copy:
    src: supervisord.conf
    dest: /etc/supervisord.conf
    owner: root
    group: root
    mode: 0644
  register: supervisord_config

- name: Restart supervisor
  service:
    name: supervisord
    state: restarted
  when:
    - supervisord_config.changed

- name: Deploy /etc/supervisor/conf.d/tiup-log-collector.conf
  template:
    src: tiup-log-collector.conf.j2
    dest: /etc/supervisord.d/tiup-log-collector.conf

- name: Keep tiup-log-collector restarted
  supervisorctl:
    name: tiup-log-collector
    state: restarted

- name: Wait for ready
  wait_for: port=8000 delay=3
