#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

- name: "download grafana rpm"
  get_url:
  args:
    url: https://dl.grafana.com/oss/release/{{ grafana_rpm }}
    dest: /tmp/{{ grafana_rpm }}
    checksum: "{{ grafana_checksum }}"
    force: no
- name: "Import GPG key for CentOS 8.0 and later"
  become: true
  rpm_key:
    key: https://grafanarel.s3.amazonaws.com/RPM-GPG-KEY-grafana
    state: present
  when: ansible_facts['distribution_version'] is version('8.0', '>=')
- name: "ensure grafana is installed"
  yum: name=/tmp/{{ grafana_rpm }} state=present
- name: "configure grafana"
  template: src=grafana.ini dest=/etc/grafana/grafana.ini
- name: "config file to load dashboards into Grafana"
  template: src=dashboards.yaml dest=/etc/grafana/provisioning/dashboards
- name: "ensure dashboards directory exists"
  file: path=/etc/grafana/dashboards state=directory owner=grafana group=grafana
- name: "add cluster dashboard configuration"
  template: src=cluster-dashboard.json dest=/etc/grafana/dashboards/cluster-dashboard.json owner=grafana group=grafana
- name: "configure fluo dashboards"
  get_url: url=https://raw.githubusercontent.com/apache/fluo/main/contrib/grafana/{{ item }} dest=/etc/grafana/dashboards/{{ item }}
  with_items:
    - fluo-recent-dashboard.json
    - fluo-historical-dashboard.json
- name: "configure accumulo dashboards"
  get_url: url=https://raw.githubusercontent.com/apache/fluo-uno/main/plugins/influx-metrics/accumulo-dashboard.json dest=/etc/grafana/dashboards/accumulo-dashboard.json
- name: "ensure owner and group are correct for /etc/grafana"
  file: path=/etc/grafana/ recurse=yes owner=grafana group=grafana
- name: "ensure grafana is running (and enable it at boot)"
  service: name=grafana-server state=started enabled=yes
  notify:
    - add datasources
