---
- 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: Download Prometheus Archive
  become: yes
  become_user: jingrow
  unarchive:
    src: https://github.com/prometheus/prometheus/releases/download/v2.49.1/prometheus-2.49.1.linux-amd64.tar.gz
    dest: /tmp
    remote_src: yes

- name: Copy Prometheus Console Templates
  become: yes
  become_user: jingrow
  copy:
    src: '/tmp/prometheus-2.49.1.linux-amd64/{{ item }}/'
    dest: '/home/jingrow/prometheus/{{ item }}/'
    mode: 0755
    remote_src: yes
  with_items:
    - console_libraries
    - consoles

- name: Copy Prometheus and Promtool Binaries
  become: yes
  become_user: jingrow
  copy:
    src: '/tmp/prometheus-2.49.1.linux-amd64/{{ 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 Jcloud Server
  become: yes
  become_user: jingrow
  template:
    src: jcloud.yml.j2
    dest: /home/jingrow/prometheus/file_sd/jcloud.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 {{ jcloud_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
