Merge "Remove salt-master, reclass from pkg dependencis"
diff --git a/.kitchen.yml b/.kitchen.yml
index e516907..9ef697b 100644
--- a/.kitchen.yml
+++ b/.kitchen.yml
@@ -18,6 +18,9 @@
     - name: linux
       repo: git
       source: https://github.com/salt-formulas/salt-formula-linux.git
+    - name: salt
+      repo: git
+      source: https://github.com/salt-formulas/salt-formula-salt.git
   state_top:
     base:
       "*":
diff --git a/fluentd/agent.sls b/fluentd/agent.sls
index d1ed8d8..9fb4d86 100644
--- a/fluentd/agent.sls
+++ b/fluentd/agent.sls
@@ -47,6 +47,24 @@
     - require:
       - pkg: fluentd_packages_agent
 
+{%- if grains.get('init') == 'systemd' %}
+
+fluentd_config_service:
+  file.managed:
+    - name: /etc/systemd/system/td-agent.service.d/override.conf
+    - source: salt://fluentd/files/override.conf
+    - makedirs: true
+    - user: root
+    - group: root
+    - mode: 644
+    - template: jinja
+    - require:
+      - pkg: fluentd_packages_agent
+    - context:
+      fluentd_agent: {{ fluentd_agent }}
+
+{%- else %}
+
 fluentd_config_service:
   file.managed:
     - name: /etc/default/td-agent
@@ -60,6 +78,8 @@
     - context:
       fluentd_agent: {{ fluentd_agent }}
 
+{%- endif %}
+
 fluentd_config_agent:
   file.managed:
     - name: {{ fluentd_agent.dir.config }}/td-agent.conf
@@ -210,6 +230,7 @@
     {%- endif %}
     - watch:
       - file: fluentd_config_agent
+      - file: fluentd_config_service
     - require:
       - file: fluentd_positiondb_dir
 
diff --git a/fluentd/files/filter/_generate.conf b/fluentd/files/filter/_generate.conf
index aaee7e9..338c4e2 100644
--- a/fluentd/files/filter/_generate.conf
+++ b/fluentd/files/filter/_generate.conf
@@ -1,4 +1,4 @@
-{%- for name, values in values.iteritems() %}
+{%- for name, values in salt['modelutils.order_by_requisites'](values).iteritems() %}
 {%- if values.get('enabled', True) %}
 # Filter {{ name }}
 {% include 'fluentd/files/filter/' + values.get('type') + '.conf' %}
diff --git a/fluentd/files/filter/prometheus.conf b/fluentd/files/filter/prometheus.conf
index 0496d8d..7f8388f 100644
--- a/fluentd/files/filter/prometheus.conf
+++ b/fluentd/files/filter/prometheus.conf
@@ -3,11 +3,7 @@
   {%- if values.get('label') %}
   <labels>
     {%- for label in values.label %}
-      {%- if label.type == 'variable' %}
-    {{ label.name }} {%raw%}${{%-endraw%}{{ label.value }}{%raw%}}{%-endraw%}
-      {%- else %}
     {{ label.name }} {{ label.value }}
-      {%- endif %}
     {%- endfor %}
   </labels>
   {%- endif %}
diff --git a/fluentd/files/input/_generate.conf b/fluentd/files/input/_generate.conf
index 17e6b13..62ca096 100644
--- a/fluentd/files/input/_generate.conf
+++ b/fluentd/files/input/_generate.conf
@@ -1,4 +1,4 @@
-{%- for name, values in values.iteritems() %}
+{%- for name, values in salt['modelutils.order_by_requisites'](values).iteritems() %}
 {%- if values.get('enabled', True) %}
 # Input {{ name }}
 {% include ['fluentd/files/input/' + values.get('type') + '.conf', 'fluentd/files/input/generic.conf'] %}
diff --git a/fluentd/files/match/_generate.conf b/fluentd/files/match/_generate.conf
index 8cf9579..23a2dfa 100644
--- a/fluentd/files/match/_generate.conf
+++ b/fluentd/files/match/_generate.conf
@@ -1,6 +1,8 @@
-{%- for name, values in values.iteritems() %}
+{%- for name, values in salt['modelutils.order_by_requisites'](values).iteritems() %}
 {%- if values.get('enabled', True) %}
 # Output {{ name }}
+<match {{ values.tag }}>
 {% include 'fluentd/files/match/' + values.get('type') + '.conf' %}
+</match>
 {%- endif %}
 {%- endfor %}
diff --git a/fluentd/files/match/copy.conf b/fluentd/files/match/copy.conf
new file mode 100644
index 0000000..f59b977
--- /dev/null
+++ b/fluentd/files/match/copy.conf
@@ -0,0 +1,11 @@
+  @type copy
+  {%- for store in values.store %}
+  <store>
+  {%- with values=store %}
+    {%- set content %}
+      {%- include 'fluentd/files/match/' + store.get('type') + '.conf' %}
+    {%- endset %}
+  {{ content | indent(2, False) }}
+  {%- endwith %}
+  </store>
+  {%- endfor %}
diff --git a/fluentd/files/match/elasticsearch.conf b/fluentd/files/match/elasticsearch.conf
index 7e2b160..e26a313 100644
--- a/fluentd/files/match/elasticsearch.conf
+++ b/fluentd/files/match/elasticsearch.conf
@@ -1,4 +1,3 @@
-<match {{ values.tag }}>
   @type elasticsearch
   host {{ values.host }}
   port {{ values.get("port", 9200) }}
@@ -19,4 +18,3 @@
   {%- endwith %}
   </buffer>
 {%- endif %}
-</match>
diff --git a/fluentd/files/match/file.conf b/fluentd/files/match/file.conf
index 86e87ff..5eae9a7 100644
--- a/fluentd/files/match/file.conf
+++ b/fluentd/files/match/file.conf
@@ -1,4 +1,2 @@
-<match {{ values.tag }}>
   @type file
   path {{ values.path }}
-</match>
diff --git a/fluentd/files/match/null.conf b/fluentd/files/match/null.conf
new file mode 100644
index 0000000..c851b5a
--- /dev/null
+++ b/fluentd/files/match/null.conf
@@ -0,0 +1 @@
+  @type null
diff --git a/fluentd/files/match/relabel.conf b/fluentd/files/match/relabel.conf
index 88ca0c7..051cd43 100644
--- a/fluentd/files/match/relabel.conf
+++ b/fluentd/files/match/relabel.conf
@@ -1,4 +1,2 @@
-<match {{ values.tag }}>
   @type relabel
   @label @{{ values.label }}
-</match>
diff --git a/fluentd/files/match/rewrite_tag_filter.conf b/fluentd/files/match/rewrite_tag_filter.conf
index b62f24f..911ead3 100644
--- a/fluentd/files/match/rewrite_tag_filter.conf
+++ b/fluentd/files/match/rewrite_tag_filter.conf
@@ -1,6 +1,8 @@
-<match {{ values.tag }}>
   @type rewrite_tag_filter
   {%- for rule in values.rule %}
-  rewriterule{{ loop.index }} {{ rule.name }} {{ rule.regexp }} {{ rule.result }}
+  <rule>
+    key {{ rule.name }}
+    pattern {{ rule.regexp }}
+    tag {{ rule.result }}
+  </rule>
   {%- endfor %}
-</match>
diff --git a/fluentd/files/override.conf b/fluentd/files/override.conf
new file mode 100644
index 0000000..c5fa878
--- /dev/null
+++ b/fluentd/files/override.conf
@@ -0,0 +1,3 @@
+[Service]
+User={{ fluentd_agent.get('user', 'root') }}
+Group={{ fluentd_agent.get('group', 'root') }}
diff --git a/fluentd/meta/prometheus.yml b/fluentd/meta/prometheus.yml
new file mode 100644
index 0000000..3c56a49
--- /dev/null
+++ b/fluentd/meta/prometheus.yml
@@ -0,0 +1,19 @@
+{%- from "fluentd/map.jinja" import fluentd_agent with context %}
+{%- if fluentd_agent.get('enabled', False) and fluentd_agent.get('config', {}).get('input', {}).get('prometheus') is defined %}
+server:
+  {%- set fqdn_ip4_addresses = [] %}
+  {%- for addr in grains['fqdn_ip4'] %}
+    {%- if not addr.startswith('127.') %}
+      {%- do fqdn_ip4_addresses.append(addr) %}
+    {%- endif %}
+  {%- endfor %}
+  {%- set address = fqdn_ip4_addresses[0] %}
+  {%- if address is defined %}
+  target:
+    static:
+      fluentd:
+        endpoint:
+          - address: {{ address }}
+            port: 24231
+  {%- endif %}
+{%- endif %}
diff --git a/metadata.yml b/metadata.yml
index da0562d..01b4ee2 100644
--- a/metadata.yml
+++ b/metadata.yml
@@ -1,3 +1,6 @@
 name: "fluentd"
 version: "1.0"
 source: "https://github.com/salt-formulas/salt-formula-fluentd"
+dependencies:
+- name: salt
+  source: "https://github.com/salt-formulas/salt-formula-salt"
diff --git a/metadata/service/support.yml b/metadata/service/support.yml
index 1fc7796..12a793d 100644
--- a/metadata/service/support.yml
+++ b/metadata/service/support.yml
@@ -1,4 +1,5 @@
 parameters:
   fluentd:
     _support:
-      enabled: true
+      prometheus:
+        enabled: true