---
- name: Create Prometheus Directories
  become: yes
  become_user: jingrow
  file:
    path: "{{ item }}"
    state: directory
    mode: 0755
  with_items:
    - /home/jingrow/prometheus
    - /home/jingrow/prometheus/data
    - /home/jingrow/prometheus/rules
    - /home/jingrow/prometheus/file_sd

- name: Set Architecture
  set_fact:
    arch: "{{'amd64' if (ansible_architecture == 'x86_64') else 'arm64'}}"

- name: Download Prometheus Archive
  become: yes
  become_user: jingrow
  unarchive:
    src: "http://git.jingrow.com/prometheus/prometheus/releases/download/v3.7.3/prometheus-3.7.3.linux-{{ arch }}.tar.gz"
    dest: /tmp
    remote_src: yes

- name: Copy Prometheus and Promtool Binaries
  become: yes
  become_user: jingrow
  copy:
    src: "/tmp/prometheus-3.7.3.linux-{{ arch }}/{{ item }}"
    dest: "/home/jingrow/prometheus/{{ item }}"
    mode: 0755
    remote_src: yes
  with_items:
    - prometheus
    - promtool

- name: Configure Prometheus Service Discovery for Monitor Server
  become: yes
  become_user: jingrow
  template:
    src: self.yml
    dest: /home/jingrow/prometheus/file_sd/self.yml
    force: true
    mode: 0600

- name: Configure Prometheus Service Discovery for Jcloude Server
  become: yes
  become_user: jingrow
  template:
    src: jcloude.yml.j2
    dest: /home/jingrow/prometheus/file_sd/jcloude.yml
    force: true
    mode: 0600

- name: Set JSON Variables
  set_fact:
    registries: "{{ registries_json | from_json }}"
    clusters: "{{ clusters_json | from_json }}"
    log_servers: "{{ log_servers_json | from_json }}"

- name: Configure Prometheus Service Discovery for Registries
  become: yes
  become_user: jingrow
  template:
    src: registry.yml
    dest: /home/jingrow/prometheus/file_sd/registry.{{ item.name }}.yml
    force: true
    mode: 0600
  loop: "{{ registries }}"

- name: Configure Prometheus Service Discovery for Log Servers
  become: yes
  become_user: jingrow
  template:
    src: log.yml
    dest: /home/jingrow/prometheus/file_sd/log.{{ item.name }}.yml
    force: true
    mode: 0600
  loop: "{{ log_servers }}"

- name: Configure Prometheus
  template:
    src: prometheus.yml
    dest: /home/jingrow/prometheus/prometheus.yml
    validate: "/home/jingrow/prometheus/promtool check config %s"

- name: Configure ownership permissions on prometheus.yml
  file:
    path: /home/jingrow/prometheus/prometheus.yml
    owner: jingrow
    group: jingrow
    mode: 0600

- name: Create Prometheus Systemd Service File
  template:
    src: prometheus.service
    dest: /etc/systemd/system/prometheus.service
    owner: root
    group: root
    mode: 0644

- name: Restart Prometheus Service
  systemd:
    daemon_reload: true
    name: prometheus
    enabled: yes
    state: restarted

- name: Setup Agent Based Discovery
  become: yes
  become_user: jingrow
  command: "/home/jingrow/agent/env/bin/agent setup monitor --url {{ jcloude_url }} --token {{ monitor_token }}"
  args:
    chdir: /home/jingrow/agent

- name: Setup Cronjob for Agent Based Discovery
  become: yes
  become_user: jingrow
  cron:
    name: agent discovery
    job: cd /home/jingrow/agent && /home/jingrow/agent/env/bin/agent discover
