---
# Copyright 2016 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

# The playbook of TiDB

- name: check config locally
  hosts: localhost
  any_errors_fatal: true
  tags:
    - always
  roles:
    - check_config_static

- name: gather all facts, and check dest
  hosts: all
  any_errors_fatal: true
  tags:
    - always
  roles:
    - check_system_dynamic


- hosts: monitoring_servers
  tags:
    - pushgateway
    - prometheus
  tasks:
    - name: start monitoring modules by systemd
      systemd: name={{ item }} state=started enabled=no
      become: true
      with_items:
        - pushgateway-{{ pushgateway_port }}.service
        - prometheus-{{ prometheus_port }}.service

    - name: wait for pushgateway up
      wait_for: |
        host={{ ansible_host }} port={{ pushgateway_port }} state=present
        send='GET /metrics HTTP/1.0\r\n\r\n' search_regex='200 OK'

    - name: wait for prometheus up
      wait_for: |
        host={{ ansible_host }} port={{ prometheus_port }} state=present
        send='GET /metrics HTTP/1.0\r\n\r\n' search_regex='200 OK'


- hosts: pd_servers
  tags:
    - pd
  tasks:
    - name: start PD by systemd
      systemd: name=pd.service state=started enabled=no
      become: true

    - name: wait for PD up
      wait_for: |
        host={{ ansible_host }} port={{ pd_client_port }} state=present
        send='GET /pd/api/v1/members HTTP/1.0\r\n\r\n' search_regex='200 OK'


- hosts: tikv_servers
  tags:
    - tikv
  tasks:
    - name: start TiKV by systemd
      systemd: name=tikv-{{ tikv_port }}.service state=started enabled=no
      become: true

    - name: wait for TiKV up
      wait_for_pid: |
        pid_file=/var/run/tikv/tikv.pid timeout=300
        thread_name_regex='tikv-server' state=present


- hosts: tidb_servers
  tags:
    - tidb
  tasks:
    - name: start TiDB by systemd
      systemd: name=tidb-{{ tidb_port }}.service state=started enabled=no
      become: true

    - name: wait for TiDB up
      wait_for: |
        host={{ ansible_host }} port={{ tidb_status_port }} state=present
        send='GET /status HTTP/1.0\r\n\r\n' search_regex='TiDB'


- hosts: monitoring_servers
  tags:
    - dashboard
  tasks:
    - name: start dashboard by systemd
      systemd: name=dashboard-{{ dashboard_port }}.service state=started enabled=no
      become: true

    - name: wait for dashboard up
      wait_for: |
        host={{ ansible_host }} port={{ dashboard_port }} state=present
