---
#--------------------------------------------------------------#
# Clean Victoria Metrics              [vmetrics][vmetrics_clean]
#--------------------------------------------------------------#
- name: clean victoria-metrics data
  tags: [vmetrics, vmetrics_clean]
  when: vmetrics_clean|default(false)|bool
  block:
    - name: stop victoria-metrics service
      systemd: name=vmetrics state=stopped enabled=no daemon_reload=yes
    - name: remove victoria-metrics data
      file: path=/infra/metrics state=absent

#--------------------------------------------------------------#
# Config VictoriaMetrics             [vmetrics][vmetrics_config]
#--------------------------------------------------------------#
- name: config victoria-metrics
  tags: [vmetrics, vmetrics_config]
  when: vmetrics_enabled|bool
  block:
    - name: render vmetrics config
      template: src={{ item.src }} dest={{ item.dest }} owner={{ item.owner }} group={{ item.group|default(item.owner) }} mode={{ item.mode }}
      with_items:
        - { src: "victoria/vmetrics.svc"   ,dest: "{{ systemd_dir }}/vmetrics.service" ,owner: root     ,group: root  ,mode: "0644" }
        - { src: "victoria/vmetrics.env"   ,dest: "/etc/default/vmetrics"              ,owner: victoria ,group: infra ,mode: "0644" }
        - { src: "victoria/prometheus.yml" ,dest: "/infra/prometheus.yml"    ,owner: victoria ,group: infra ,mode: "0644" }
        - { src: "victoria/agent.yml"      ,dest: "/infra/rules/agent.yml"   ,owner: victoria ,group: infra ,mode: "0644" }

    - name: copy vmetrics rules and scripts
      copy: src={{ item.src }} dest={{ item.dest }} owner={{ item.owner }} group={{ item.group|default(item.owner) }} mode={{ item.mode }}
      with_items:
        - { src: "victoria/bin/"   ,dest: "/infra/bin/"   ,owner: root     ,group: infra ,mode: "0755" }
        - { src: "victoria/rules/" ,dest: "/infra/rules/" ,owner: victoria ,group: infra ,mode: "0644" }

#--------------------------------------------------------------#
# Launch VictoriaMetrics             [vmetrics][vmetrics_config]
#--------------------------------------------------------------#
- name: launch victoria-metrics
  tags: [vmetrics, vmetrics_launch]
  when: vmetrics_enabled|bool
  block:
    - name: launch vmetrics systemd service
      systemd: name=vmetrics state=restarted enabled=yes daemon_reload=yes
    - name: wait for vmetrics service online
      wait_for: host=127.0.0.1 port={{ vmetrics_port }} state=started timeout=30



#--------------------------------------------------------------#
# Clean Victoria Logs                       [vlogs][vlogs_clean]
#--------------------------------------------------------------#
- name: clean victoria-logs data
  tags: [vlogs, vlogs_clean]
  when: vlogs_clean|default(false)|bool
  block:
    - name: stop victoria-logs service
      systemd: name=vlogs state=stopped enabled=no daemon_reload=yes
    - name: remove victoria-logs data
      file: path=/infra/logs state=absent

#--------------------------------------------------------------#
# Config Victoria Logs                     [vlogs][vlogs_config]
#--------------------------------------------------------------#
- name: config victoria-logs
  tags: [vlogs, vlogs_config]
  when: vlogs_enabled|bool
  template: src={{ item.src }} dest={{ item.dest }} owner={{ item.owner }} group={{ item.group|default(item.owner) }} mode={{ item.mode }}
  with_items:
    - { src: "victoria/vlogs.svc"  ,dest: "{{ systemd_dir }}/vlogs.service"  ,owner: root     ,group: root  ,mode: "0644" }
    - { src: "victoria/vlogs.env"  ,dest: "/etc/default/vlogs"               ,owner: victoria ,group: infra ,mode: "0644" }

#--------------------------------------------------------------#
# Launch Victoria Logs                     [vlogs][vlogs_launch]
#--------------------------------------------------------------#
- name: launch victoria-logs
  tags: [vlogs, vlogs_launch]
  when: vlogs_enabled|bool
  block:
    - name: launch vlogs systemd service
      systemd: name=vlogs state=restarted enabled=yes daemon_reload=yes
    - name: wait for vlogs service online
      wait_for: host=127.0.0.1 port={{ vlogs_port }} state=started timeout=30



#--------------------------------------------------------------#
# Clean Victoria Traces                 [vtraces][vtraces_clean]
#--------------------------------------------------------------#
- name: clean victoria-traces data
  tags: [vtraces, vtraces_clean]
  when: vtraces_clean|default(false)|bool
  block:
    - name: stop victoria-traces service
      systemd: name=vtraces state=stopped enabled=no daemon_reload=yes
    - name: remove victoria-traces data
      file: path=/infra/traces state=absent

#--------------------------------------------------------------#
# Config VictoriaTraces                [vtraces][vtraces_config]
#--------------------------------------------------------------#
- name: config victoria-traces
  tags: [vtraces, vtraces_config]
  when: vtraces_enabled|bool
  template: src={{ item.src }} dest={{ item.dest }} owner={{ item.owner }} group={{ item.group|default(item.owner) }} mode={{ item.mode }}
  with_items:
    - { src: "victoria/vtraces.svc"  ,dest: "{{ systemd_dir }}/vtraces.service"  ,owner: root     ,group: root  ,mode: "0644" }
    - { src: "victoria/vtraces.env"  ,dest: "/etc/default/vtraces"               ,owner: victoria ,group: infra ,mode: "0644" }

#--------------------------------------------------------------#
# Launch VictoriaTraces                [vtraces][vtraces_launch]
#--------------------------------------------------------------#
- name: launch victoria-traces
  tags: [vtraces, vtraces_launch]
  when: vtraces_enabled|bool
  block:
    - name: launch vtraces systemd service
      systemd: name=vtraces state=restarted enabled=yes daemon_reload=yes
    - name: wait for vtraces service online
      wait_for: host=127.0.0.1 port={{ vtraces_port }} state=started timeout=30



#--------------------------------------------------------------#
# Config VMAlert                       [vmalert][vmalert_config]
#--------------------------------------------------------------#
- name: config vmalert
  tags: [vmalert, vmalert_config]
  when: vmalert_enabled|bool and vmetrics_enabled|bool
  template: src={{ item.src }} dest={{ item.dest }} owner={{ item.owner }} group={{ item.group|default(item.owner) }} mode={{ item.mode }}
  with_items:
    - { src: "victoria/vmalert.svc"  ,dest: "{{ systemd_dir }}/vmalert.service" ,owner: root     ,group: root  ,mode: "0644" }
    - { src: "victoria/vmalert.env"  ,dest: "/etc/default/vmalert"              ,owner: victoria ,group: infra ,mode: "0644" }

#--------------------------------------------------------------#
# Launch VMAlert                       [vmalert][vmalert_launch]
#--------------------------------------------------------------#
- name: launch vmalert
  tags: [vmalert, vmalert_launch]
  when: vmalert_enabled|bool and vmetrics_enabled|bool
  block:
    - name: launch vmalert systemd service
      systemd: name=vmalert state=restarted enabled=yes daemon_reload=yes
    - name: wait for vmalert service online
      wait_for: host=127.0.0.1 port={{ vmalert_port }} state=started timeout=30

...
