Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/154035

Change subject: wmflib: add ensure_final_newline()
......................................................................

wmflib: add ensure_final_newline()

If the argument is a string, ensure it ends with a final newline.
If it is not a string, pass it through unmodified.

This function is designed to make it easier to write custom Puppet
resource types that delegate to file resources and that take
either a "content" or a "source" parameter.

Change-Id: Ib1f88b11b6308900a0bf0279eaca863793f614cd
---
M modules/apache/manifests/conf.pp
M modules/eventlogging/manifests/plugin.pp
M modules/logstash/manifests/conf.pp
M modules/rsyslog/manifests/conf.pp
M modules/sysctl/manifests/conffile.pp
A modules/wmflib/lib/puppet/parser/functions/ensure_final_newline.rb
6 files changed, 33 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/puppet 
refs/changes/35/154035/1

diff --git a/modules/apache/manifests/conf.pp b/modules/apache/manifests/conf.pp
index 0b5aab9..2f8c6f5 100644
--- a/modules/apache/manifests/conf.pp
+++ b/modules/apache/manifests/conf.pp
@@ -61,14 +61,10 @@
     $title_safe  = regsubst($title, '[\W_]', '-', 'G')
     $file_ext    = $conf_type ? { env => 'sh', default => 'conf' }
     $conf_file   = sprintf('%02d-%s.%s', $priority, $title_safe, $file_ext)
-    $content_formatted = $content ? {
-        undef   => undef,
-        default => regsubst($content, "\n?$", "\n"),
-    }
 
     file { "/etc/apache2/${conf_type}-available/${conf_file}":
         ensure  => $ensure,
-        content => $content_formatted,
+        content => ensure_final_newline($content),
         source  => $source,
         owner   => 'root',
         group   => 'root',
diff --git a/modules/eventlogging/manifests/plugin.pp 
b/modules/eventlogging/manifests/plugin.pp
index d7ed4cf..40ca902 100644
--- a/modules/eventlogging/manifests/plugin.pp
+++ b/modules/eventlogging/manifests/plugin.pp
@@ -42,7 +42,7 @@
 
     file { "/usr/local/lib/eventlogging/${basename}.py":
         ensure  => $ensure,
-        content => $content,
+        content => ensure_final_newline($content),
         source  => $source,
         require => File['/usr/local/lib/eventlogging'],
         notify  => Service['eventlogging/init'],
diff --git a/modules/logstash/manifests/conf.pp 
b/modules/logstash/manifests/conf.pp
index 542bbff..ef94a11 100644
--- a/modules/logstash/manifests/conf.pp
+++ b/modules/logstash/manifests/conf.pp
@@ -30,7 +30,7 @@
 
     file { "/etc/logstash/conf.d/${priority}-${config_name}.conf":
         ensure  => $ensure,
-        content => $content,
+        content => ensure_final_newline($content),
         source  => $source,
         require => File['/etc/logstash/conf.d'],
         notify  => Service['logstash'],
diff --git a/modules/rsyslog/manifests/conf.pp 
b/modules/rsyslog/manifests/conf.pp
index 6f5f7c6..c6a95ad 100644
--- a/modules/rsyslog/manifests/conf.pp
+++ b/modules/rsyslog/manifests/conf.pp
@@ -41,15 +41,9 @@
     $basename = regsubst($title, '[\W_]', '-', 'G')
     $filename = sprintf('/etc/rsyslog.d/%02d-%s.conf', $priority, $basename)
 
-    # append a trailing newline if omitted
-    $content_formatted = $content ? {
-        undef   => undef,
-        default => regsubst($content, "\n?$", "\n")
-    }
-
     file { $filename:
         ensure  => $ensure,
-        content => $content_formatted,
+        content => ensure_final_newline($content),
         source  => $source,
         owner   => 'root',
         group   => 'root',
diff --git a/modules/sysctl/manifests/conffile.pp 
b/modules/sysctl/manifests/conffile.pp
index 1e80d13..0c4797a 100644
--- a/modules/sysctl/manifests/conffile.pp
+++ b/modules/sysctl/manifests/conffile.pp
@@ -47,7 +47,7 @@
 
     file { $filename:
         ensure  => $ensure,
-        content => $content,
+        content => ensure_final_newline($content),
         source  => $source,
         notify  => Exec['update_sysctl'],
     }
diff --git a/modules/wmflib/lib/puppet/parser/functions/ensure_final_newline.rb 
b/modules/wmflib/lib/puppet/parser/functions/ensure_final_newline.rb
new file mode 100644
index 0000000..a4bde14
--- /dev/null
+++ b/modules/wmflib/lib/puppet/parser/functions/ensure_final_newline.rb
@@ -0,0 +1,28 @@
+# == Function: ensure_final_newline
+#
+# If the argument is a string, ensure it ends with a final newline.
+# If it is not a string, pass it through unmodified.
+#
+# This function is designed to make it easier to write custom Puppet
+# resource types that delegate to file resources and that take
+# either a "content" or a "source" parameter.
+#
+module Puppet::Parser::Functions
+  newfunction(
+    :ensure_final_newline,
+    :arity => 1,
+    :type => :rvalue,
+    :doc  => <<-END
+      If the argument is a string, ensure it ends with a final newline.
+      If it is not a string, pass it through unmodified.
+
+      This function is designed to make it easier to write custom Puppet
+      resource types that delegate to file resources and that take
+      either a "content" or a "source" parameter.
+
+    END
+  ) do |args|
+    val = args.first
+    val.is_a?(String) && val[-1, 1] != "\n" ? val << "\n" : val
+  end
+end

-- 
To view, visit https://gerrit.wikimedia.org/r/154035
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib1f88b11b6308900a0bf0279eaca863793f614cd
Gerrit-PatchSet: 1
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Ori.livneh <o...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to