From a40cdd3e14f129bfe4a3e6f87d38a14662185eb2 Mon Sep 17 00:00:00 2001
From: Leo-Paul Geneau <leo-paul.geneau@nexedi.com>
Date: Thu, 30 Sep 2021 15:06:58 +0200
Subject: [PATCH] software/fluentd: add port-listening promises

Add port-listening promises for all sources found in fluentd configuration file
---
 software/fluentd/buildout.hash.cfg    | 21 +++++++++++
 software/fluentd/instance-fluentd.cfg | 28 ++++++++++++++
 software/fluentd/instance.cfg         | 54 +++++++++++++++++++++++++++
 software/fluentd/instance.cfg.in      | 45 ----------------------
 software/fluentd/software.cfg         | 21 +++++++----
 5 files changed, 116 insertions(+), 53 deletions(-)
 create mode 100644 software/fluentd/buildout.hash.cfg
 create mode 100644 software/fluentd/instance-fluentd.cfg
 create mode 100644 software/fluentd/instance.cfg
 delete mode 100644 software/fluentd/instance.cfg.in

diff --git a/software/fluentd/buildout.hash.cfg b/software/fluentd/buildout.hash.cfg
new file mode 100644
index 000000000..7acf17a47
--- /dev/null
+++ b/software/fluentd/buildout.hash.cfg
@@ -0,0 +1,21 @@
+# THIS IS NOT A BUILDOUT FILE, despite purposedly using a compatible syntax.
+# The only allowed lines here are (regexes):
+# - "^#" comments, copied verbatim
+# - "^[" section beginings, copied verbatim
+# - lines containing an "=" sign which must fit in the following categorie.
+#   - "^\s*filename\s*=\s*path\s*$" where "path" is relative to this file
+#     Copied verbatim.
+#   - "^\s*hashtype\s*=.*" where "hashtype" is one of the values supported
+#     by the re-generation script.
+#     Re-generated.
+# - other lines are copied verbatim
+# Substitution (${...:...}), extension ([buildout] extends = ...) and
+# section inheritance (< = ...) are NOT supported (but you should really
+# not need these here).
+[instance-profile]
+filename = instance.cfg
+md5sum = c265bf7ec199190ba9f77960cac5da38
+
+[template-fluentd]
+filename = instance-fluentd.cfg
+md5sum = cceaa5e32d77659163c211d6bfbe87d6
diff --git a/software/fluentd/instance-fluentd.cfg b/software/fluentd/instance-fluentd.cfg
new file mode 100644
index 000000000..21cb72df4
--- /dev/null
+++ b/software/fluentd/instance-fluentd.cfg
@@ -0,0 +1,28 @@
+[service-fluentd]
+recipe = slapos.cookbook:wrapper
+wrapper-path = {{ directory['service'] }}/fluentd-service
+command-line = ${fluentd:location}/bin/fluentd -v -c {{ fluentd_agent_conf }}
+environment =
+  GEM_PATH=${fluentd:location}/lib/ruby/gems/1.8/
+
+{% set part_list = [] -%}
+
+{% for port in port_list -%}
+{%   set promise_section_title = 'fluentd-port-' ~ port ~ '-listening' -%}
+{%   do part_list.append(promise_section_title) -%}
+[{{ promise_section_title }}]
+<= monitor-promise-base
+module = check_socket_listening
+name = {{ promise_section_title }}.py
+config-host = $${slap-configuration:ipv6-random}
+config-port = {{ port }}
+{% endfor %}
+
+[buildout]
+parts =
+  service-fluentd
+{%- for part in part_list %}
+  {{ part }}
+{%- endfor %}
+
+extends = ${monitor-template:output}
diff --git a/software/fluentd/instance.cfg b/software/fluentd/instance.cfg
new file mode 100644
index 000000000..2f01759ce
--- /dev/null
+++ b/software/fluentd/instance.cfg
@@ -0,0 +1,54 @@
+[buildout]
+parts =
+  switch-softwaretype
+
+eggs-directory = ${buildout:eggs-directory}
+develop-eggs-directory = ${buildout:develop-eggs-directory}
+offline = true
+
+[switch-softwaretype]
+recipe = slapos.cookbook:switch-softwaretype
+default = dynamic-template-fluentd:rendered
+RootSoftwareInstance = $${:default}
+
+[directory]
+recipe = slapos.cookbook:mkdirectory
+home = $${buildout:directory}
+etc = $${:home}/etc
+var = $${:home}/var
+service = $${:etc}/service
+bin = $${:home}/bin
+
+[slap-configuration]
+recipe = slapos.cookbook:slapconfiguration
+computer = $${slap_connection:computer_id}
+partition = $${slap_connection:partition_id}
+url = $${slap_connection:server_url}
+key = $${slap_connection:key_file}
+cert = $${slap_connection:cert_file}
+
+[dynamic-template-fluentd]
+recipe = slapos.recipe.template:jinja2
+template = ${template-fluentd:output}
+rendered = $${buildout:directory}/instance-fluentd.cfg
+extensions = jinja2.ext.do
+context =
+  key fluentd_agent_conf fluentd-agent-conf:rendered
+  key port_list fluentd-conf:port-list
+  section directory directory
+
+[fluentd-conf]
+recipe = slapos.recipe.build
+slapparameter-dict = $${slap-configuration:configuration}
+init =
+  import re
+  options['text'] = options['slapparameter-dict'].get('conf_text') or ''
+  options['port-list'] = re.findall(r'<source>.*port (\d+).*<\/source>', options['text'], re.DOTALL)
+
+[fluentd-agent-conf]
+recipe  = slapos.recipe.template:jinja2
+template = inline:{{ conf }}
+rendered = $${directory:etc}/fluentd-agent.conf
+mode = 0644
+context =
+  key conf fluentd-conf:text
diff --git a/software/fluentd/instance.cfg.in b/software/fluentd/instance.cfg.in
deleted file mode 100644
index fcb4d6bbc..000000000
--- a/software/fluentd/instance.cfg.in
+++ /dev/null
@@ -1,45 +0,0 @@
-[buildout]
-parts =
-  service-fluentd
-
-eggs-directory = {{ buildout['eggs-directory'] }}
-develop-eggs-directory = {{ buildout['develop-eggs-directory'] }}
-offline = true
-
-[instance-parameter]
-recipe = slapos.cookbook:slapconfiguration.serialised
-computer = ${slap_connection:computer_id}
-partition = ${slap_connection:partition_id}
-url = ${slap_connection:server_url}
-key = ${slap_connection:key_file}
-cert = ${slap_connection:cert_file}
-
-[directory]
-recipe = slapos.cookbook:mkdirectory
-home = ${buildout:directory}
-etc = ${:home}/etc
-var = ${:home}/var
-script = ${:etc}/run/
-service = ${:etc}/service
-promise = ${:etc}/promise/
-log = ${:var}/log
-bin = ${:home}/bin
-
-[service-fluentd]
-recipe  = slapos.cookbook:wrapper
-wrapper-path    = ${directory:service}/fluentd-service
-command-line    = {{ fluentd_location }}/bin/fluentd
-    -v
-    -c ${fluentd-agent-conf:rendered}
-environment =
-  GEM_PATH={{ fluentd_location }}/lib/ruby/gems/1.8/
-
-[fluentd-agent-conf]
-recipe  = slapos.recipe.template:jinja2
-template = inline:{% raw -%}
-  {{ slapparameter_dict.get('conf_text', '') }}
-  {%- endraw %}
-rendered = ${directory:etc}/fluentd-agent.conf
-mode = 0644
-context =
-  key slapparameter_dict instance-parameter:configuration
diff --git a/software/fluentd/software.cfg b/software/fluentd/software.cfg
index 70d7c9f7e..b0916b870 100644
--- a/software/fluentd/software.cfg
+++ b/software/fluentd/software.cfg
@@ -1,7 +1,9 @@
 [buildout]
 extends =
+  buildout.hash.cfg
   ../../component/fluentd/buildout.cfg
   ../../stack/slapos.cfg
+  ../../stack/monitor/buildout.cfg
 
 parts =
   instance-profile
@@ -10,15 +12,18 @@ parts =
 [python]
 part = python3
 
-[instance-profile]
-recipe = slapos.recipe.template:jinja2
-template = ${:_profile_base_location_}/instance.cfg.in
-rendered = ${buildout:directory}/instance.cfg
+[template-base]
+recipe = slapos.recipe.template
+url = ${:_profile_base_location_}/${:filename}
 mode = 0644
-extensions = jinja2.ext.do
-context =
-  section buildout  buildout
-  key fluentd_location fluentd:location
+
+[instance-profile]
+< = template-base
+output = ${buildout:directory}/template.cfg
+
+[template-fluentd]
+< = template-base
+output = ${buildout:directory}/template-fluentd.cfg
 
 [fluentd]
 gems +=
-- 
2.30.9