Use salt support to generate grains

Change-Id: I7cc5f00608127e0bf4df5bbd58db4cae9816a402
diff --git a/metadata/service/collector.yml b/metadata/service/collector.yml
new file mode 100644
index 0000000..03f8ecc
--- /dev/null
+++ b/metadata/service/collector.yml
@@ -0,0 +1,6 @@
+applications:
+  - prometheus
+parameters:
+  prometheus:
+    collector:
+      enabled: true
diff --git a/prometheus/alertmanager.sls b/prometheus/alertmanager.sls
index 576c349..6f83334 100644
--- a/prometheus/alertmanager.sls
+++ b/prometheus/alertmanager.sls
@@ -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:
diff --git a/prometheus/collector.sls b/prometheus/collector.sls
index c9c839c..2528ec9 100644
--- a/prometheus/collector.sls
+++ b/prometheus/collector.sls
@@ -1,43 +1,10 @@
-{%- from "prometheus/map.jinja" import server as prometheus_server with context %}
+{%- if pillar.prometheus.collector.get('enabled', True) %}
 
-{%- 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 %}
+{#-
+  Grains are now managed from salt.minion.grains so we will just include it
+#}
 
-prometheus_grains_dir:
-  file.directory:
-  - name: /etc/salt/grains.d
-  - mode: 700
-  - makedirs: true
-  - user: root
+include:
+  - salt.minion.grains
 
-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
+{%- endif %}
diff --git a/prometheus/files/prometheus.grain b/prometheus/files/prometheus.grain
deleted file mode 100644
index 3e3b373..0000000
--- a/prometheus/files/prometheus.grain
+++ /dev/null
@@ -1 +0,0 @@
-{{ service_grains|yaml(False) }}
diff --git a/prometheus/init.sls b/prometheus/init.sls
index e69de29..3141a49 100644
--- a/prometheus/init.sls
+++ b/prometheus/init.sls
@@ -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 %}
diff --git a/prometheus/meta/salt.yml b/prometheus/meta/salt.yml
new file mode 100644
index 0000000..9343f83
--- /dev/null
+++ b/prometheus/meta/salt.yml
@@ -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
+-#}
diff --git a/prometheus/server.sls b/prometheus/server.sls
index fc0a181..e4dfb5c 100644
--- a/prometheus/server.sls
+++ b/prometheus/server.sls
@@ -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: