Use salt support to generate grains

Change-Id: I7cc5f00608127e0bf4df5bbd58db4cae9816a402
This commit is contained in:
Filip Pytloun 2017-05-09 09:45:44 +02:00
parent c9e81f45db
commit d92d2d7ce8
7 changed files with 46 additions and 43 deletions

View file

@ -0,0 +1,6 @@
applications:
- prometheus
parameters:
prometheus:
collector:
enabled: true

View file

@ -1,7 +1,7 @@
{% from "prometheus/map.jinja" import alertmanager with context %}
{%- if alertmanager.enabled %}
{%- if pillar.docker.host is defined %}
{%- if pillar.docker is defined and pillar.docker.host is defined %}
{{alertmanager.dir.config}}/alertmanager.yml:
file.managed:

View file

@ -1,43 +1,10 @@
{%- from "prometheus/map.jinja" import server as prometheus_server with context %}
{%- if pillar.prometheus.collector.get('enabled', True) %}
{#-
Grains are now managed from salt.minion.grains so we will just include it
#}
include:
- salt.minion.grains
{%- set service_grains = {'prometheus': {'server': {'alert': {}, 'recording': {}, 'target': {}}}} %}
{%- for service_name, service in pillar.items() %}
{%- if service.get('_support', {}).get('prometheus', {}).get('enabled', False) %}
{%- set grains_fragment_file = service_name+'/meta/prometheus.yml' %}
{%- macro load_grains_file() %}{% include grains_fragment_file ignore missing %}{% endmacro %}
{%- set grains_yaml = load_grains_file()|load_yaml %}
{%- if grains_yaml is mapping %}
{%- set service_grains = salt['grains.filter_by']({'default': service_grains}, merge={'prometheus': grains_yaml}) %}
{%- endif %}
{%- endif %}
{%- endfor %}
prometheus_grains_dir:
file.directory:
- name: /etc/salt/grains.d
- mode: 700
- makedirs: true
- user: root
prometheus_grain:
file.managed:
- name: /etc/salt/grains.d/prometheus
- source: salt://prometheus/files/prometheus.grain
- template: jinja
- mode: 600
- defaults:
service_grains: {{ service_grains|yaml }}
- require:
- file: prometheus_grains_dir
prometheus_grains_file:
cmd.wait:
- name: cat /etc/salt/grains.d/* > /etc/salt/grains
- watch:
- file: prometheus_grain
prometheus_grains_publish:
module.run:
- name: mine.update
- watch:
- cmd: prometheus_grains_file

View file

@ -1 +0,0 @@
{{ service_grains|yaml(False) }}

View file

@ -0,0 +1,10 @@
{%- if pillar.prometheus.server is defined or
pillar.prometheus.alertmanager is defined %}
include:
{%- if pillar.prometheus.server is defined %}
- prometheus.server
{%- endif %}
{%- if pillar.prometheus.alertmanager is defined %}
- prometheus.alertmanager
{%- endif %}
{%- endif %}

21
prometheus/meta/salt.yml Normal file
View file

@ -0,0 +1,21 @@
grain:
{%- if pillar.prometheus is defined and pillar.prometheus.collector is defined %}
{%- from "prometheus/map.jinja" import server as prometheus_server with context %}
prometheus:
{%- set service_grains = {'prometheus': {'server': {'alert': {}, 'recording': {}, 'target': {}}}} %}
{%- for service_name, service in pillar.items() %}
{%- if service.get('_support', {}).get('prometheus', {}).get('enabled', False) %}
{%- set grains_fragment_file = service_name+'/meta/prometheus.yml' %}
{%- macro load_grains_file() %}{% include grains_fragment_file ignore missing %}{% endmacro %}
{%- set grains_yaml = load_grains_file()|load_yaml %}
{%- if grains_yaml is mapping %}
{%- set service_grains = salt['grains.filter_by']({'default': service_grains}, merge={'prometheus': grains_yaml}) %}
{%- endif %}
{%- endif %}
{%- endfor %}
{{ service_grains|yaml(False)|indent(4) }}
{%- endif %}
{#-
vim: syntax=jinja
-#}

View file

@ -1,7 +1,7 @@
{% from "prometheus/map.jinja" import server with context %}
{%- if server.enabled %}
{%- if pillar.docker.host is defined %}
{%- if pillar.docker is defined and pillar.docker.host is defined %}
{{server.dir.config}}/prometheus.yml:
file.managed: