diff --git a/manifests/unit.pp b/manifests/unit.pp index e1d985ba32cfc992b70853ec09eb62c105380828..9561700c6a3c4260bf968c72d6da9a5ae0bf52bc 100644 --- a/manifests/unit.pp +++ b/manifests/unit.pp @@ -1,4 +1,4 @@ -# Copyright © 2021 Thomas Bellman, Linköping, Sweden +# Copyright © 2021-2023 Thomas Bellman, Linköping, Sweden # Licensed under the GNU LGPL v3+; see the README file for more information. @@ -35,6 +35,11 @@ define systemd::unit( # support multi-value options, e.g. "ExecStart" in a service config, # or "ListenStream" in a socket config. # + # A section name (at the top level) or an option name (at the inner + # level) starting with a hash sign (#), e.g. "#Service" or "#User", + # takes a string or (nested) list of strings, which will become a + # comment in front of the corresponding section or option. + # $options=undef, # String to put in the unit file. This is equivalent to the diff --git a/manifests/unit_options.pp b/manifests/unit_options.pp index badfa2011d52b959a45a030d006ffe2749232180..683af00d046f671c5be52cc0ba65bc3bf1aa7f6b 100644 --- a/manifests/unit_options.pp +++ b/manifests/unit_options.pp @@ -1,4 +1,4 @@ -# Copyright © 2021 Thomas Bellman, Linköping, Sweden +# Copyright © 2021-2023 Thomas Bellman, Linköping, Sweden # Licensed under the GNU LGPL v3+; see the README file for more information. @@ -44,6 +44,11 @@ define systemd::unit_options( # reset that option to the empty list, as systemd would otherwise # just append the new value(s) to the existing list. # + # A section name (at the top level) or an option name (at the inner + # level) starting with a hash sign (#), e.g. "#Service" or "#User", + # takes a string or (nested) list of strings, which will become a + # comment in front of the corresponding section or option. + # $options=undef, # List of comment lines to add to the start of the parameter file. diff --git a/templates/unitfile.erb b/templates/unitfile.erb index 1aea7da3bd92ce6c395c5c83fbac7863b59a1b93..96cd880ef2a3d138864ceaea14c66da30fe08e1e 100644 --- a/templates/unitfile.erb +++ b/templates/unitfile.erb @@ -1,19 +1,31 @@ -<% - # Copyright © 2021 Thomas Bellman, Linköping, Sweden - # Licensed under the GNU LGPL v3+; see the README file for more information. +<%# + # Copyright © 2021-2023 Thomas Bellman, Linköping, Sweden + # Licensed under the GNU LGPL v3+; see the README file for more information. + # -%> <% resource_ref = "Systemd::%s[%s]" % [ @systemd_resource_type, @title, ] - comment_lines = [@comment].flatten.join("\n").split("\n") - if comment_lines.length() > 0 - comment_lines.each do |cline| + unit_comments = [@comment].flatten.join("\n").split("\n") + unit_comments.collect! { |cline| "# " + cline } + unit_comments << "\n" if unit_comments.length > 0 -%> -# <%= cline %> -<% end %> -<% end -%> -<% @options.sort.each do |section_name,section_options| -%> +<%= unit_comments.join("\n") -%> +<%# -%> +<%# -%> +<%# Loop over sections ('Unit', 'Service', et.c) -%> +<% @options.sort.each do |section_name, section_options| + next if section_name =~ /^#/ + section_comments = (@options['#'+section_name] or []).flatten(). + join("\n").split("\n") +-%> +<%= section_comments.collect { |cline| "# " + cline + "\n" }.join("") -%> [<%= section_name %>] +<%# # Loop over options in section -%> <% section_options.sort.each do |optname,value| -%> -<% if optname =~ /^-(.*)/ +<% + # Override instead of append to list option. + # This is only valid if called from systemd::unit_options, not when + # called from systemd::unit. + if optname =~ /^-(.*)/ if @systemd_resource_type == 'Unit_options' optname = $1 -%> @@ -27,6 +39,11 @@ -%> <% end -%> <% end -%> +<% next if optname =~ /^#/ -%> +<% opt_comments = (section_options['#'+optname] or []).flatten(). + join("\n").split("\n") +-%> +<%= opt_comments.collect { |cline| "# " + cline + "\n" }.join("") -%> <% [value].flatten.each do |v| -%> <%= optname %>=<%= v %> <% end -%>