Support remote_collector in container

This change updates the formula to support the deployment of the
remote_collector service as a container (eg in Docker Swarm).

Change-Id: If54cbf04af0e994e82887cb049f916111416ec48
diff --git a/heka/_container.sls b/heka/_container.sls
new file mode 100644
index 0000000..337485c
--- /dev/null
+++ b/heka/_container.sls
@@ -0,0 +1,24 @@
+{%- from "heka/map.jinja" import server as root_server with context %}
+
+{{ server.prefix_dir }}:
+  file.directory:
+  - mode: 750
+  - makedirs: true
+
+{{ server.prefix_dir }}/usr/share/lma_collector:
+  file.recurse:
+  - source: salt://heka/files/lua
+  - file_mode: 640
+  - dir_mode: 750
+  - require:
+    - file: {{ server.prefix_dir }}
+
+{{ server.prefix_dir }}/usr/share/lma_collector/common/extra_fields.lua:
+  file.managed:
+  - source: salt://heka/files/extra_fields.lua
+  - mode: 640
+  - defaults:
+      extra_fields: {{ root_server.extra_fields }}
+  - template: jinja
+  - require:
+    - file: {{ server.prefix_dir }}/usr/share/lma_collector
diff --git a/heka/_service.sls b/heka/_service.sls
index dfe3dbe..dd6c8c1 100644
--- a/heka/_service.sls
+++ b/heka/_service.sls
@@ -2,11 +2,12 @@
 
 heka_{{ service_name }}_conf_dir:
   file.directory:
-  - name: /etc/{{ service_name }}
+  - name: {{ server.prefix_dir }}/etc/{{ service_name }}
   - user: heka
   - mode: 750
   - makedirs: true
 
+{%- if not server.container_mode %}
 heka_{{ service_name }}_cache_dir:
   file.directory:
   - name: /var/cache/{{ service_name }}
@@ -14,14 +15,17 @@
   - group: heka
   - mode: 750
   - makedirs: true
+{% endif %}
 
 heka_{{ service_name }}_conf_dir_clean:
   file.directory:
-  - name: /etc/{{ service_name }}
+  - name: {{ server.prefix_dir }}/etc/{{ service_name }}
   - clean: true
   - watch_in:
     - service: heka_{{ service_name }}_service
 
+{%- if not server.container_mode %}
+
 {%- if grains.get('init', None) == 'systemd' %}
 {%- set systemd_enabled = True %}
 {%- else %}
@@ -81,14 +85,21 @@
   service.running:
   - enable: True
   - watch:
-    - file: /usr/share/lma_collector
-    - file: /usr/share/lma_collector/*
-    - file: /etc/{{ service_name }}/*
+    - file: {{ server.prefix_dir }}/usr/share/lma_collector
+    - file: {{ server.prefix_dir }}/usr/share/lma_collector/*
+    - file: {{ server.prefix_dir }}/etc/{{ service_name }}/*
 {%- else %}
   service.disabled:
 {%- endif %}
   - name: {{ service_name }}
 
+{%- else %}
+{# Fake service state to satisfy the requisites #}
+heka_{{ service_name }}_service:
+  test.nop
+
+{%- endif %}
+
 {# Setup basic structure for all roles so updates can apply #}
 {%- set service_grains = {
   'log_collector': {
@@ -200,7 +211,7 @@
 {%- endif %}
 {%- endfor %}
 
-/etc/{{ service_name }}/global.toml:
+{{ server.prefix_dir }}/etc/{{ service_name }}/global.toml:
   file.managed:
   - source: salt://heka/files/toml/global.toml
   - template: jinja
@@ -218,7 +229,7 @@
 
 {%- for decoder_name, decoder in service_metadata.get('decoder', {}).iteritems() %}
 
-/etc/{{ service_name }}/decoder_{{ decoder_name }}.toml:
+{{ server.prefix_dir }}/etc/{{ service_name }}/decoder_{{ decoder_name }}.toml:
   file.managed:
   - source: salt://heka/files/toml/decoder/{{ decoder.engine }}.toml
   - template: jinja
@@ -236,7 +247,7 @@
 
 {%- for input_name, input in service_metadata.get('input', {}).iteritems() %}
 
-/etc/{{ service_name }}/input_{{ input_name }}.toml:
+{{ server.prefix_dir }}/etc/{{ service_name }}/input_{{ input_name }}.toml:
   file.managed:
   - source: salt://heka/files/toml/input/{{ input.engine }}.toml
   - template: jinja
@@ -255,7 +266,7 @@
 {%- for alarm_name, alarm in service_metadata.get('alarm', {}).iteritems() %}
 
 {%- if alarm.get('enabled', True) %}
-/etc/{{ service_name }}/filter_afd_{{ alarm_name }}.toml:
+{{ server.prefix_dir }}/etc/{{ service_name }}/filter_afd_{{ alarm_name }}.toml:
   file.managed:
   - source: salt://heka/files/toml/filter/afd_alarm.toml
   - template: jinja
@@ -273,14 +284,14 @@
       hostname: {{ grains.host }}
       {%- endif %}
 
-/usr/share/lma_collector/common/lma_{{ alarm_name|replace('-', '_') }}.lua:
+{{ server.prefix_dir }}/usr/share/lma_collector/common/lma_{{ alarm_name|replace('-', '_') }}.lua:
   file.managed:
   - source: salt://heka/files/lma_alarm.lua
   - template: jinja
   - mode: 640
   - group: heka
   - require:
-    - file: /usr/share/lma_collector
+    - file: {{ server.prefix_dir }}/usr/share/lma_collector
   - defaults:
       alarm_name: {{ alarm_name }}
       alarm: {{ alarm|yaml }}
@@ -291,14 +302,14 @@
 
 {%- set policy = service_metadata.get('policy') %}
 {%- if policy %}
-/usr/share/lma_collector/common/gse_policies.lua:
+{{ server.prefix_dir }}/usr/share/lma_collector/common/gse_policies.lua:
   file.managed:
   - source: salt://heka/files/gse_policies.lua
   - template: jinja
   - mode: 640
   - group: heka
   - require:
-    - file: /usr/share/lma_collector
+    - file: {{ server.prefix_dir }}/usr/share/lma_collector
   - defaults:
     policy: {{ policy|yaml }}
 {%- endif %}
@@ -306,7 +317,7 @@
 {%- for alarm_cluster_name, alarm_cluster in service_metadata.get('alarm_cluster', {}).iteritems() %}
 
 {%- if alarm_cluster.get('enabled', True) %}
-/etc/{{ service_name }}/filter_gse_{{ alarm_cluster_name }}.toml:
+{{ server.prefix_dir }}/etc/{{ service_name }}/filter_gse_{{ alarm_cluster_name }}.toml:
   file.managed:
   - source: salt://heka/files/toml/filter/gse_alarm_cluster.toml
   - template: jinja
@@ -320,14 +331,14 @@
       alarm_cluster_name: {{ alarm_cluster_name }}
       alarm_cluster: {{ alarm_cluster|yaml }}
 
-/usr/share/lma_collector/common/gse_{{ alarm_cluster_name|replace('-', '_') }}_topology.lua:
+{{ server.prefix_dir }}/usr/share/lma_collector/common/gse_{{ alarm_cluster_name|replace('-', '_') }}_topology.lua:
   file.managed:
   - source: salt://heka/files/gse_topology.lua
   - template: jinja
   - mode: 640
   - group: heka
   - require:
-    - file: /usr/share/lma_collector
+    - file: {{ server.prefix_dir }}/usr/share/lma_collector
   - defaults:
       alarm_cluster_name: {{ alarm_cluster_name }}
       alarm_cluster: {{ alarm_cluster|yaml }}
@@ -337,7 +348,7 @@
 
 {%- for filter_name, filter in service_metadata.get('filter', {}).iteritems() %}
 
-/etc/{{ service_name }}/filter_{{ filter_name }}.toml:
+{{ server.prefix_dir }}/etc/{{ service_name }}/filter_{{ filter_name }}.toml:
   file.managed:
   - source: salt://heka/files/toml/filter/{{ filter.engine }}.toml
   - template: jinja
@@ -355,7 +366,7 @@
 
 {%- for splitter_name, splitter in service_metadata.get('splitter', {}).iteritems() %}
 
-/etc/{{ service_name }}/splitter_{{ splitter_name }}.toml:
+{{ server.prefix_dir }}/etc/{{ service_name }}/splitter_{{ splitter_name }}.toml:
   file.managed:
   - source: salt://heka/files/toml/splitter/{{ splitter.engine }}.toml
   - template: jinja
@@ -373,7 +384,7 @@
 
 {%- for encoder_name, encoder in service_metadata.get('encoder', {}).iteritems() %}
 
-/etc/{{ service_name }}/encoder_{{ encoder_name }}.toml:
+{{ server.prefix_dir }}/etc/{{ service_name }}/encoder_{{ encoder_name }}.toml:
   file.managed:
   - source: salt://heka/files/toml/encoder/{{ encoder.engine }}.toml
   - template: jinja
@@ -391,7 +402,7 @@
 
 {%- for output_name, output in service_metadata.get('output', {}).iteritems() %}
 
-/etc/{{ service_name }}/output_{{ output_name }}.toml:
+{{ server.prefix_dir }}/etc/{{ service_name }}/output_{{ output_name }}.toml:
   file.managed:
   - source: salt://heka/files/toml/output/{{ output.engine }}.toml
   - template: jinja
diff --git a/heka/map.jinja b/heka/map.jinja
index f1eba2a..be11040 100644
--- a/heka/map.jinja
+++ b/heka/map.jinja
@@ -45,9 +45,12 @@
 {% set default_sensu_notification_handler = 'default' %}
 {% set default_sensu_port = 3030 %}
 {% set default_sensu_watchdog_ttl = 120 %}
+{% set default_prefix_dir = '' %}
 
 {% set log_collector = salt['grains.filter_by']({
   'default': {
+    'container_mode': False,
+    'prefix_dir': default_prefix_dir,
     'elasticsearch_port': default_elasticsearch_port,
     'poolsize': 100,
     'automatic_starting': default_automatic_starting,
@@ -62,6 +65,8 @@
 
 {% set metric_collector = salt['grains.filter_by']({
   'default': {
+    'container_mode': False,
+    'prefix_dir': default_prefix_dir,
     'influxdb_port': default_influxdb_port,
     'influxdb_time_precision': default_influxdb_time_precision,
     'influxdb_timeout': default_influxdb_timeout,
@@ -79,6 +84,8 @@
 
 {% set remote_collector = salt['grains.filter_by']({
   'default': {
+    'container_mode': False,
+    'prefix_dir': default_prefix_dir,
     'amqp_port': default_amqp_port,
     'amqp_vhost': '',
     'elasticsearch_port': default_elasticsearch_port,
@@ -98,6 +105,8 @@
 
 {% set aggregator = salt['grains.filter_by']({
   'default': {
+    'container_mode': False,
+    'prefix_dir': default_prefix_dir,
     'influxdb_port': default_influxdb_port,
     'influxdb_time_precision': default_influxdb_time_precision,
     'influxdb_timeout': default_influxdb_timeout,
@@ -115,6 +124,8 @@
 
 {% set ceilometer_collector = salt['grains.filter_by']({
   'default': {
+    'container_mode': False,
+    'prefix_dir': default_prefix_dir,
     'influxdb_port': default_influxdb_port,
     'influxdb_timeout': default_influxdb_timeout,
     'influxdb_time_precision': default_influxdb_time_precision,
diff --git a/heka/remote_collector.sls b/heka/remote_collector.sls
index 35533c3..5681d7e 100644
--- a/heka/remote_collector.sls
+++ b/heka/remote_collector.sls
@@ -1,11 +1,15 @@
 {%- if pillar.heka.remote_collector is defined %}
 
+{%- from "heka/map.jinja" import remote_collector with context %}
+{%- set service_name = "remote_collector" %}
+{%- set server = remote_collector %}
+
+{%- if remote_collector.container_mode %}
+{%- include "heka/_container.sls" %}
+{%- else %}
 include:
 - heka._common
-
-{%- from "heka/map.jinja" import remote_collector with context %}
-{%- set server = remote_collector %}
-{%- set service_name = "remote_collector" %}
+{%- endif %}
 
 {%- include "heka/_service.sls" %}