Support remote_collector and aggregator in cluster

When deployed in a cluster, the remote_collector and aggregator
services are only started when the node holds the virtual IP address.
diff --git a/heka/_service.sls b/heka/_service.sls
index b990341..30bde22 100644
--- a/heka/_service.sls
+++ b/heka/_service.sls
@@ -29,28 +29,29 @@
   - clean: true
 
 {%- if grains.get('init', None) == 'systemd' %}
+{%- set systemd_enabled = True %}
+{%- else %}
+{%- set systemd_enabled = False %}
+{%- endif %}
 
 heka_{{ service_name }}_service_file:
   file.managed:
+{%- if systemd_enabled %}
   - name: /etc/systemd/system/{{ service_name }}.service
+{%- else %}
+  - name: /etc/init/{{ service_name }}.conf
+{%- endif %}
   - source: salt://heka/files/heka.service
   - user: root
   - mode: 644
   - defaults:
     service_name: {{ service_name }}
+    systemd_enabled: {{ systemd_enabled|lower }}
+    max_open_files: 102400
+    automatic_starting: {{ server.automatic_starting }}
   - template: jinja
 
-{%- else %}
-
-heka_{{ service_name }}_service_file:
-  file.managed:
-  - name: /etc/init/{{ service_name }}.conf
-  - source: salt://heka/files/heka.service
-  - user: root
-  - mode: 644
-  - defaults:
-      service_name: {{ service_name }}
-  - template: jinja
+{%- if not systemd_enabled %}
 
 heka_{{ service_name }}_service_wrapper:
   file.managed:
@@ -59,17 +60,22 @@
   - user: root
   - mode: 755
   - defaults:
-      service_name: {{ service_name }}
+    service_name: {{ service_name }}
   - template: jinja
 
 {%- endif %}
 
 heka_{{ service_name }}_service:
+{%- if server.automatic_starting %}
   service.running:
-  - name: {{ service_name }}
   - enable: True
   - watch:
-    - file: /usr/share/lma_collector
+    - file: /usr/share/lma_collector/*
+    - file: /etc/{{ service_name }}/*
+{%- else %}
+  service.disabled:
+{%- endif %}
+  - name: {{ service_name }}
 
 {# Setup basic structure for all roles so updates can apply #}
 
@@ -190,8 +196,6 @@
     - file: heka_{{ service_name }}_conf_dir
   - require_in:
     - file: heka_{{ service_name }}_conf_dir_clean
-  - watch_in:
-    - service: heka_{{ service_name }}_service
 
 {%- set service_metadata = service_grains.get(service_name) %}
 
@@ -207,8 +211,6 @@
     - file: heka_{{ service_name }}_conf_dir
   - require_in:
     - file: heka_{{ service_name }}_conf_dir_clean
-  - watch_in:
-    - service: heka_{{ service_name }}_service
   - defaults:
       decoder_name: {{ decoder_name }}
       decoder: {{ decoder|yaml }}
@@ -227,8 +229,6 @@
     - file: heka_{{ service_name }}_conf_dir
   - require_in:
     - file: heka_{{ service_name }}_conf_dir_clean
-  - watch_in:
-    - service: heka_{{ service_name }}_service
   - defaults:
       input_name: {{ input_name }}
       input: {{ input|yaml }}
@@ -247,8 +247,6 @@
     - file: heka_{{ service_name }}_conf_dir
   - require_in:
     - file: heka_{{ service_name }}_conf_dir_clean
-  - watch_in:
-    - service: heka_{{ service_name }}_service
   - defaults:
       alarm_name: {{ alarm_name }}
       alarm: {{ alarm|yaml }}
@@ -262,8 +260,6 @@
   - group: heka
   - require:
     - file: /usr/share/lma_collector
-  - watch_in:
-    - service: heka_{{ service_name }}_service
   - defaults:
       alarm_name: {{ alarm_name }}
       alarm: {{ alarm|yaml }}
@@ -281,8 +277,6 @@
   - group: heka
   - require:
     - file: /usr/share/lma_collector
-  - watch_in:
-    - service: heka_{{ service_name }}_service
   - defaults:
     policy: {{ policy|yaml }}
 {%- endif %}
@@ -299,8 +293,6 @@
     - file: heka_{{ service_name }}_conf_dir
   - require_in:
     - file: heka_{{ service_name }}_conf_dir_clean
-  - watch_in:
-    - service: heka_{{ service_name }}_service
   - defaults:
       alarm_cluster_name: {{ alarm_cluster_name }}
       alarm_cluster: {{ alarm_cluster|yaml }}
@@ -313,8 +305,6 @@
   - group: heka
   - require:
     - file: /usr/share/lma_collector
-  - watch_in:
-    - service: heka_{{ service_name }}_service
   - defaults:
       alarm_cluster_name: {{ alarm_cluster_name }}
       alarm_cluster: {{ alarm_cluster|yaml }}
@@ -333,8 +323,6 @@
     - file: heka_{{ service_name }}_conf_dir
   - require_in:
     - file: heka_{{ service_name }}_conf_dir_clean
-  - watch_in:
-    - service: heka_{{ service_name }}_service
   - defaults:
       filter_name: {{ filter_name }}
       filter: {{ filter|yaml }}
@@ -353,8 +341,6 @@
     - file: heka_{{ service_name }}_conf_dir
   - require_in:
     - file: heka_{{ service_name }}_conf_dir_clean
-  - watch_in:
-    - service: heka_{{ service_name }}_service
   - defaults:
       splitter_name: {{ splitter_name }}
       splitter: {{ splitter|yaml }}
@@ -373,8 +359,6 @@
     - file: heka_{{ service_name }}_conf_dir
   - require_in:
     - file: heka_{{ service_name }}_conf_dir_clean
-  - watch_in:
-    - service: heka_{{ service_name }}_service
   - defaults:
       encoder_name: {{ encoder_name }}
       encoder: {{ encoder|yaml }}
@@ -393,8 +377,6 @@
     - file: heka_{{ service_name }}_conf_dir
   - require_in:
     - file: heka_{{ service_name }}_conf_dir_clean
-  - watch_in:
-    - service: heka_{{ service_name }}_service
   - defaults:
       output_name: {{ output_name }}
       output: {{ output|yaml }}
diff --git a/heka/files/heka.service b/heka/files/heka.service
index 935360a..02f6aed 100644
--- a/heka/files/heka.service
+++ b/heka/files/heka.service
@@ -1,4 +1,4 @@
-{%- if grains.get('init', None) == 'systemd' %}
+{%- if systemd_enabled %}
 
 [Unit]
 Description=heka {{ service_name }} - data collector and processor daemon
@@ -9,7 +9,7 @@
 EnvironmentFile=-/etc/default/{{ service_name }}
 User=heka
 Group=heka
-LimitNOFILE=102400
+LimitNOFILE={{ max_open_files }}
 ExecStart=/usr/bin/hekad -config=/etc/{{ service_name }}
 # NOT SURE HEKA doesn't support reloading by signal
 # ExecReload=/bin/kill -HUP $MAINPID
@@ -17,8 +17,10 @@
 Restart=on-failure
 StandardError=inherit
 
+{%- if automatic_starting %}
 [Install]
 WantedBy=multi-user.target
+{%- endif %}
 
 {%- else %}
 
@@ -26,8 +28,10 @@
 
 description     "{{ service_name }}"
 
+{%- if automatic_starting %}
 start on runlevel [2345]
 stop on runlevel [!2345]
+{%- endif %}
 
 respawn
 
@@ -38,7 +42,7 @@
 
 script
     # https://bugs.launchpad.net/lma-toolchain/+bug/1543289
-    ulimit -n 102400
+    ulimit -n {{ max_open_files }}
     exec start-stop-daemon --start  --chuid heka --exec /usr/local/bin/{{ service_name }}_wrapper >> /var/log/{{ service_name }}.log 2>&1
 end script
 
diff --git a/heka/files/service_wrapper b/heka/files/service_wrapper
index df3b5c9..f3532f1 100644
--- a/heka/files/service_wrapper
+++ b/heka/files/service_wrapper
@@ -2,5 +2,4 @@
 
 HEKAD="/usr/bin/hekad"
 
-ulimit -n 102400
 exec $HEKAD -config=/etc/{{ service_name }}
diff --git a/heka/map.jinja b/heka/map.jinja
index 7658623..3995eb2 100644
--- a/heka/map.jinja
+++ b/heka/map.jinja
@@ -37,11 +37,13 @@
 {% set default_aggregator_port = 5565 %}
 {% set default_nagios_port = 8001 %}
 {% set default_nagios_host_alarm_clusters = '00-clusters' %}
+{% set default_automatic_starting = True %}
 
 {% set log_collector = salt['grains.filter_by']({
   'default': {
     'elasticsearch_port': default_elasticsearch_port,
     'poolsize': 100,
+    'automatic_starting': default_automatic_starting,
   }
 }, merge=salt['pillar.get']('heka:log_collector')) %}
 
@@ -53,6 +55,7 @@
     'aggregator_port': default_aggregator_port,
     'nagios_port': default_nagios_port,
     'poolsize': 100,
+    'automatic_starting': default_automatic_starting,
   }
 }, merge=salt['pillar.get']('heka:metric_collector')) %}
 
@@ -63,6 +66,7 @@
     'influxdb_timeout': default_influxdb_timeout,
     'aggregator_port': default_aggregator_port,
     'poolsize': 100,
+    'automatic_starting': default_automatic_starting,
   }
 }, merge=salt['pillar.get']('heka:remote_collector')) %}
 
@@ -74,5 +78,6 @@
     'nagios_port': default_nagios_port,
     'nagios_default_host_alarm_clusters': default_nagios_host_alarm_clusters,
     'poolsize': 100,
+    'automatic_starting': default_automatic_starting,
   }
 }, merge=salt['pillar.get']('heka:aggregator')) %}
diff --git a/heka/meta/heka.yml b/heka/meta/heka.yml
index 4ce6298..7ff0aad 100644
--- a/heka/meta/heka.yml
+++ b/heka/meta/heka.yml
@@ -170,7 +170,7 @@
   input:
     heka_collectd:
       engine: http
-      address: 127.0.0.1
+      address: 0.0.0.0
       port: 8326
       decoder: collectd_decoder
       splitter: NullSplitter
@@ -201,7 +201,7 @@
       module_file: /usr/share/lma_collector/filters/influxdb_accumulator.lua
       module_dir: /usr/share/lma_collector/common;/usr/share/heka/lua_modules
       preserve_data: false
-      message_matcher: "Type == 'heka.sandbox.afd_metric'"
+      message_matcher: "Type =~ /metric$/"
       ticker_interval: 1
       config:
         tag_fields: "deployment_id environment_label tenant_id user_id"
diff --git a/metadata/service/aggregator/cluster.yml b/metadata/service/aggregator/cluster.yml
new file mode 100644
index 0000000..e95243e
--- /dev/null
+++ b/metadata/service/aggregator/cluster.yml
@@ -0,0 +1,13 @@
+applications:
+- heka
+classes:
+- service.heka.support
+parameters:
+  _param:
+    aggregator_poolsize: 100
+  heka:
+    aggregator:
+      automatic_starting: false
+      enabled: true
+      influxdb_time_precision: ms
+      poolsize: ${_param:aggregator_poolsize}
diff --git a/metadata/service/aggregator/single.yml b/metadata/service/aggregator/single.yml
index 476536c..159e5bd 100644
--- a/metadata/service/aggregator/single.yml
+++ b/metadata/service/aggregator/single.yml
@@ -8,6 +8,7 @@
     nagios_host_dimension_key: nagios_host
   heka:
     aggregator:
+      automatic_starting: true
       enabled: true
       influxdb_time_precision: ms
       poolsize: ${_param:aggregator_poolsize}
diff --git a/metadata/service/remote_collector/cluster.yml b/metadata/service/remote_collector/cluster.yml
new file mode 100644
index 0000000..9e60e86
--- /dev/null
+++ b/metadata/service/remote_collector/cluster.yml
@@ -0,0 +1,13 @@
+applications:
+- heka
+classes:
+- service.heka.support
+parameters:
+  _param:
+    remote_collector_poolsize: 100
+  heka:
+    remote_collector:
+      automatic_starting: false
+      enabled: true
+      influxdb_time_precision: ms
+      poolsize: ${_param:remote_collector_poolsize}
diff --git a/metadata/service/remote_collector/single.yml b/metadata/service/remote_collector/single.yml
index 120414c..8533ad2 100644
--- a/metadata/service/remote_collector/single.yml
+++ b/metadata/service/remote_collector/single.yml
@@ -7,6 +7,7 @@
     remote_collector_poolsize: 100
   heka:
     remote_collector:
+      automatic_starting: true
       enabled: true
       influxdb_time_precision: ms
       poolsize: ${_param:remote_collector_poolsize}