---
- import_role:
    name: service-precheck
  vars:
    service_precheck_services: "{{ prometheus_services }}"
    service_name: "{{ project_name }}"

- name: Get container facts
  become: true
  kolla_container_facts:
    container_engine: "{{ kolla_container_engine }}"
    name:
      - prometheus_server
      - prometheus_node_exporter
      - prometheus_haproxy_exporter
      - prometheus_memcached_exporter
      - prometheus_mysqld_exporter
      - prometheus_cadvisor
      - prometheus_alertmanager
      - prometheus_openstack_exporter
      - prometheus_elasticsearch_exporter
      - prometheus_blackbox_exporter
      - prometheus_libvirt_exporter
      - prometheus_msteams
  check_mode: false
  register: container_facts

- name: Check that prometheus_bcrypt_salt is correctly set
  assert:
    that:
      - prometheus_bcrypt_salt is defined
      - prometheus_bcrypt_salt is string
      - prometheus_bcrypt_salt | length == 22

- name: Check that prometheus_password is correctly set
  assert:
    that:
      - prometheus_password is defined
      - prometheus_password is string
      - prometheus_password | length > 0

- name: Check that prometheus_grafana_password is correctly set
  assert:
    that:
      - prometheus_grafana_password is defined
      - prometheus_grafana_password is string
      - prometheus_grafana_password | length > 0
  when: enable_grafana | bool

- name: Checking free port for Prometheus server
  wait_for:
    host: "{{ 'api' | kolla_address }}"
    port: "{{ prometheus_port }}"
    connect_timeout: 1
    timeout: 1
    state: stopped
  when:
    - container_facts['prometheus_server'] is not defined
    - inventory_hostname in groups['prometheus']
    - enable_prometheus_server | bool

- name: Checking free port for Prometheus node_exporter
  wait_for:
    host: "{{ 'api' | kolla_address }}"
    port: "{{ prometheus_node_exporter_port }}"
    connect_timeout: 1
    timeout: 1
    state: stopped
  when:
    - container_facts['prometheus_node_exporter'] is not defined
    - inventory_hostname in groups['prometheus-node-exporter']
    - enable_prometheus_node_exporter | bool

- name: Checking free port for Prometheus mysqld_exporter
  wait_for:
    host: "{{ 'api' | kolla_address }}"
    port: "{{ prometheus_mysqld_exporter_port }}"
    connect_timeout: 1
    timeout: 1
    state: stopped
  when:
    - container_facts['prometheus_mysqld_exporter'] is not defined
    - inventory_hostname in groups['prometheus-mysqld-exporter']
    - enable_prometheus_mysqld_exporter | bool

- name: Checking free port for Prometheus haproxy_exporter
  wait_for:
    host: "{{ 'api' | kolla_address }}"
    port: "{{ prometheus_haproxy_exporter_port }}"
    connect_timeout: 1
    timeout: 1
    state: stopped
  when:
    - container_facts['prometheus_haproxy_exporter'] is not defined
    - inventory_hostname in groups['prometheus-haproxy-exporter']
    - enable_prometheus_haproxy_exporter | bool

- name: Checking free port for Prometheus memcached_exporter
  wait_for:
    host: "{{ 'api' | kolla_address }}"
    port: "{{ prometheus_memcached_exporter_port }}"
    connect_timeout: 1
    timeout: 1
    state: stopped
  when:
    - container_facts['prometheus_memcached_exporter'] is not defined
    - inventory_hostname in groups['prometheus-memcached-exporter']
    - enable_prometheus_memcached_exporter | bool

- name: Checking free port for Prometheus cAdvisor
  wait_for:
    host: "{{ 'api' | kolla_address }}"
    port: "{{ prometheus_cadvisor_port }}"
    connect_timeout: 1
    timeout: 1
    state: stopped
  when:
    - container_facts['prometheus_cadvisor'] is not defined
    - inventory_hostname in groups['prometheus-cadvisor']
    - enable_prometheus_cadvisor | bool

- name: Checking free ports for Prometheus Alertmanager
  wait_for:
    host: "{{ 'api' | kolla_address }}"
    port: "{{ item }}"
    connect_timeout: 1
    timeout: 1
    state: stopped
  when:
    - container_facts['prometheus_alertmanager'] is not defined
    - inventory_hostname in groups['prometheus-alertmanager']
    - enable_prometheus_alertmanager | bool
  with_items:
    - "{{ prometheus_alertmanager_port }}"
    - "{{ prometheus_alertmanager_cluster_port }}"

- name: Checking free ports for Prometheus openstack-exporter
  wait_for:
    host: "{{ 'api' | kolla_address }}"
    port: "{{ item }}"
    connect_timeout: 1
    timeout: 1
    state: stopped
  when:
    - container_facts['prometheus_openstack_exporter'] is not defined
    - inventory_hostname in groups['prometheus-openstack-exporter']
    - enable_prometheus_openstack_exporter | bool
  with_items:
    - "{{ prometheus_openstack_exporter_port }}"

- name: Checking free ports for Prometheus elasticsearch-exporter
  wait_for:
    host: "{{ 'api' | kolla_address }}"
    port: "{{ item }}"
    connect_timeout: 1
    timeout: 1
    state: stopped
  when:
    - container_facts['prometheus_elasticsearch_exporter'] is not defined
    - inventory_hostname in groups['prometheus-elasticsearch-exporter']
    - enable_prometheus_elasticsearch_exporter | bool
  with_items:
    - "{{ prometheus_elasticsearch_exporter_port }}"

- name: Checking free ports for Prometheus blackbox-exporter
  wait_for:
    host: "{{ 'api' | kolla_address }}"
    port: "{{ item }}"
    connect_timeout: 1
    timeout: 1
    state: stopped
  when:
    - container_facts['prometheus_blackbox_exporter'] is not defined
    - inventory_hostname in groups['prometheus-blackbox-exporter']
    - enable_prometheus_blackbox_exporter | bool
  with_items:
    - "{{ prometheus_blackbox_exporter_port }}"

- name: Checking free ports for Prometheus libvirt-exporter
  wait_for:
    host: "{{ 'api' | kolla_address }}"
    port: "{{ item }}"
    connect_timeout: 1
    timeout: 1
    state: stopped
  when:
    - container_facts['prometheus_libvirt_exporter'] is not defined
    - inventory_hostname in groups['prometheus-libvirt-exporter']
    - enable_prometheus_libvirt_exporter | bool
  with_items:
    - "{{ prometheus_libvirt_exporter_port }}"

- name: Checking free ports for Prometheus msteams
  wait_for:
    host: "{{ 'api' | kolla_address }}"
    port: "{{ item }}"
    connect_timeout: 1
    timeout: 1
    state: stopped
  when:
    - container_facts['prometheus_msteams'] is not defined
    - inventory_hostname in groups['prometheus-msteams']
    - enable_prometheus_msteams | bool
  with_items:
    - "{{ prometheus_msteams_port }}"
