Manage grains using support metadata

Change-Id: Ieb404dfec50d5d7c5e766d44856ebcc8f7e851df
diff --git a/heka/_common.sls b/heka/_common.sls
index 9f71c31..a46b820 100644
--- a/heka/_common.sls
+++ b/heka/_common.sls
@@ -46,10 +46,3 @@
 
 /etc/init.d/heka:
   file.absent
-
-heka_grains_dir:
-  file.directory:
-  - name: /etc/salt/grains.d
-  - mode: 700
-  - makedirs: true
-  - user: root
diff --git a/heka/_service.sls b/heka/_service.sls
index eb54952..d7600f9 100644
--- a/heka/_service.sls
+++ b/heka/_service.sls
@@ -1,3 +1,4 @@
+{%- from "heka/map.jinja" import service_grains with context %}
 {%- macro load_grains_file(grains_fragment_file) %}{% include grains_fragment_file ignore missing %}{% endmacro %}
 
 {%- if server.enabled is defined and server.enabled %}
@@ -91,72 +92,6 @@
 {%- endif %}
   - name: {{ service_name }}
 
-{# Setup basic structure for all roles so updates can apply #}
-
-{%- set service_grains = {
-  'log_collector': {
-    'decoder': {},
-    'input': {},
-    'trigger': {},
-    'filter': {},
-    'splitter': {},
-    'encoder': {},
-    'output': {},
-  },
-  'metric_collector': {
-    'decoder': {},
-    'input': {},
-    'trigger': {},
-    'alarm': {},
-    'filter': {},
-    'splitter': {},
-    'encoder': {},
-    'output': {},
-  },
-  'remote_collector': {
-    'decoder': {},
-    'input': {},
-    'trigger': {},
-    'alarm': {},
-    'filter': {},
-    'splitter': {},
-    'encoder': {},
-    'output': {},
-  },
-  'aggregator': {
-    'decoder': {},
-    'input': {},
-    'trigger': {},
-    'alarm_cluster': {},
-    'filter': {},
-    'splitter': {},
-    'encoder': {},
-    'output': {},
-  },
-  'ceilometer_collector': {
-    'decoder': {},
-    'input': {},
-    'filter': {},
-    'splitter': {},
-    'encoder': {},
-    'output': {},
-  }
-} %}
-
-
-
-{# Loading the other services' support metadata for local roles #}
-
-{%- for service_name, service in pillar.iteritems() %}
-{%- if service.get('_support', {}).get('heka', {}).get('enabled', False) %}
-
-{%- set grains_fragment_file = service_name+'/meta/heka.yml' %}
-{%- set grains_yaml = load_grains_file(grains_fragment_file)|load_yaml %}
-{%- set service_grains = salt['grains.filter_by']({'default': service_grains}, merge=grains_yaml) %}
-
-{%- endif %}
-{%- endfor %}
-
 {%- if service_name in ('remote_collector', 'aggregator') %}
 
 {# Load the other services' support metadata from salt-mine #}
@@ -191,19 +126,6 @@
 {%- endif %}
 {%- endfor %}
 
-heka_{{ service_name }}_grain:
-  file.managed:
-  - name: /etc/salt/grains.d/heka
-  - source: salt://heka/files/heka.grain
-  - template: jinja
-  - user: root
-  - mode: 600
-  - defaults:
-    service_grains:
-      heka: {{ salt['heka_alarming.grains_for_mine'](service_grains)|yaml }}
-  - require:
-    - file: heka_grains_dir
-
 /etc/{{ service_name }}/global.toml:
   file.managed:
   - source: salt://heka/files/toml/global.toml
diff --git a/heka/files/heka.grain b/heka/files/heka.grain
deleted file mode 100644
index 3e3b373..0000000
--- a/heka/files/heka.grain
+++ /dev/null
@@ -1 +0,0 @@
-{{ service_grains|yaml(False) }}
diff --git a/heka/map.jinja b/heka/map.jinja
index 14c5e4e..4357d2f 100644
--- a/heka/map.jinja
+++ b/heka/map.jinja
@@ -130,3 +130,63 @@
     'sensu_watchdog_ttl': default_sensu_watchdog_ttl,
   }
 }, merge=salt['pillar.get']('heka:ceilometer_collector')) %}
+
+{# Setup basic structure for all roles so updates can apply #}
+{%- set service_grains = {
+  'log_collector': {
+    'decoder': {},
+    'input': {},
+    'trigger': {},
+    'filter': {},
+    'splitter': {},
+    'encoder': {},
+    'output': {},
+  },
+  'metric_collector': {
+    'decoder': {},
+    'input': {},
+    'trigger': {},
+    'alarm': {},
+    'filter': {},
+    'splitter': {},
+    'encoder': {},
+    'output': {},
+  },
+  'remote_collector': {
+    'decoder': {},
+    'input': {},
+    'trigger': {},
+    'alarm': {},
+    'filter': {},
+    'splitter': {},
+    'encoder': {},
+    'output': {},
+  },
+  'aggregator': {
+    'decoder': {},
+    'input': {},
+    'trigger': {},
+    'alarm_cluster': {},
+    'filter': {},
+    'splitter': {},
+    'encoder': {},
+    'output': {},
+  },
+  'ceilometer_collector': {
+    'decoder': {},
+    'input': {},
+    'filter': {},
+    'splitter': {},
+    'encoder': {},
+    'output': {},
+  }
+} %}
+
+{# Loading the other services' support metadata for local roles #}
+{%- for service_name, service in pillar.iteritems() %}
+  {%- if service.get('_support', {}).get('heka', {}).get('enabled', False) %}
+    {%- set grains_fragment_file = service_name+'/meta/heka.yml' %}
+    {%- set grains_yaml = load_grains_file(grains_fragment_file)|load_yaml %}
+    {%- set service_grains = salt['grains.filter_by']({'default': service_grains}, merge=grains_yaml) %}
+  {%- endif %}
+{%- endfor %}
diff --git a/heka/meta/salt.yml b/heka/meta/salt.yml
new file mode 100644
index 0000000..42f51ae
--- /dev/null
+++ b/heka/meta/salt.yml
@@ -0,0 +1,4 @@
+grain:
+  heka:
+    {%- from "heka/map.jinja" import service_grains with context %}
+    {{ service_grains|yaml(False)|indent(4) }}