---
# 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


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

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


- hosts: tidb_servers
  tags:
    - tidb
  tasks:
    - name: stop TiDB by systemd
      systemd: name=tidb-{{ tidb_port }}.service state=stopped
      become: true

    - name: wait for TiDB down
      wait_for: host={{ ansible_host }} port={{ tidb_port }} state=stopped


- hosts: tikv_servers
  tags:
    - tikv
  tasks:
    - name: stop TiKV by systemd
      systemd: name=tikv-{{ tikv_port }}.service state=stopped
      become: true

    - name: wait for TiKV down (via Port)
      wait_for: host={{ ansible_host }} port={{ tikv_port }} state=stopped

    - name: wait for TiKV down (via PID)
      wait_for_pid: pid_file=/var/run/tikv/tikv.pid timeout=300 state=absent


- hosts: pd_servers
  tags:
    - pd
  tasks:
    - name: stop PD by systemd
      systemd: name=pd.service state=stopped
      become: true

    - name: wait for PD down
      wait_for: host={{ ansible_host }} port={{ pd_client_port }} state=stopped


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

    - name: wait for pushgateway down
      wait_for: host={{ ansible_host }} port={{ pushgateway_port }} state=stopped

    - name: wait for prometheus down
      wait_for: host={{ ansible_host }} port={{ prometheus_port }} state=stopped
