---

- block:
  - name: create node_exporter dir
    file:
      path: /opt/prometheus/metrics
      state: directory
      owner: root
      group: root
      mode: 0755

  - name: download prometheus_node_exporter archive
    get_url:
      checksum: sha256:3369b76cd2b0ba678b6d618deab320e565c3d93ccb5c2a0d5db51a53857768ae
      url: "https://s3-us-west-1.amazonaws.com/zoox-third-party/prometheus_node_exporter/node_exporter-1.0.1.linux-amd64.tar.gz"
      dest: "/tmp/node_exporter-1.0.1.linux-amd64.tar.gz"
      mode: 0777

  - name: unpack node_exporter
    unarchive:
      src: "/tmp/node_exporter-1.0.1.linux-amd64.tar.gz"
      dest: "/opt/prometheus/"
      mode: 0750
      owner: root
      group: root
      remote_src: yes
      extra_opts:
      - '--strip=1'
    notify: restart node_exporter

  - name: install upstart config
    copy:
      src: "prometheus.conf"
      dest: "/etc/init/prometheus.conf"
      owner: root
      group: root
      mode: u=rw,g=r,o=r
    notify: restart node_exporter
    when: ansible_distribution_version == '14.04'

  - name: install systemd service
    template:
      src: "prometheus.service"
      dest: "/etc/systemd/system/prometheus.service"
      owner: root
      group: root
      mode: 0755
    notify: systemd restart node_exporter
    when: ansible_distribution_version == '18.04'

  - block:
    - name: gpu_metrics cronjob
      cron:
        name: get gpu metrics
        user: root
        job: "run-one python /opt/prometheus/gpu_metrics.py > /opt/prometheus/metrics/gpu_metrics.prom"

    - name: copy gpu metric collection script
      copy:
        src: gpu_metrics.py
        dest: /opt/prometheus/gpu_metrics.py
        mode: 0750
        owner: root
        group: root
    when: gpus is defined and (gpus|int) > 0

  become: yes
