Ori.livneh has uploaded a new change for review.

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

Change subject: Add ini() to stdlib
......................................................................

Add ini() to stdlib

Like php_ini(), but uses top-level keys as [section headings].

Change-Id: I0b4828a2a6bc310fe3262189513c488a00226efa
---
M modules/grafana2/manifests/init.pp
M modules/wmflib/README.md
A modules/wmflib/lib/puppet/parser/functions/ini.rb
3 files changed, 62 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/puppet 
refs/changes/83/245883/1

diff --git a/modules/grafana2/manifests/init.pp 
b/modules/grafana2/manifests/init.pp
index 249e06d..8e6a755 100644
--- a/modules/grafana2/manifests/init.pp
+++ b/modules/grafana2/manifests/init.pp
@@ -25,7 +25,7 @@
     require_package('grafana')
 
     file { '/etc/grafana/grafana.ini':
-        content => php_ini($config),
+        content => ini($config),
         owner   => 'root',
         group   => 'root',
         mode    => '0444',
diff --git a/modules/wmflib/README.md b/modules/wmflib/README.md
index 3ffd781..0e4c192 100644
--- a/modules/wmflib/README.md
+++ b/modules/wmflib/README.md
@@ -91,6 +91,25 @@
     }
 
 
+## ini
+
+`ini( hash $ini_settings [, hash $... ] )`
+
+Serialize a hash into the .ini-style format expected by Python's
+ConfigParser. Takes one or more hashes as arguments. If the argument
+list contains more than one hash, they are merged together. In case of
+duplicate keys, hashes to the right win.
+
+### Example
+
+    ini({'server' => {'port' => 80}})
+
+will produce:
+
+    [server]
+    port = 80
+
+
 ## ordered_json
 
 `ordered_json( hash $data [, hash $... ] )`
diff --git a/modules/wmflib/lib/puppet/parser/functions/ini.rb 
b/modules/wmflib/lib/puppet/parser/functions/ini.rb
new file mode 100644
index 0000000..9821f6d
--- /dev/null
+++ b/modules/wmflib/lib/puppet/parser/functions/ini.rb
@@ -0,0 +1,42 @@
+# == Function: ini( hash $ini_settings [, hash $... ] )
+#
+# Serialize a hash into the .ini-style format expected by Python's
+# ConfigParser. Takes one or more hashes as arguments. If the argument
+# list contains more than one hash, they are merged together. In case of
+# duplicate keys, hashes to the right win.
+#
+# === Example
+#
+#   ini({'server' => {'port' => 80}})
+#
+# will produce:
+#
+#   [server]
+#   port = 80
+#
+def ini_flatten(map, prefix = nil)
+  map.reduce({}) do |flat, (k, v)|
+    k = [prefix, k].compact.join('.')
+    flat.merge! v.is_a?(Hash) ? ini_flatten(v, k) : Hash[k, v]
+  end
+end
+
+def ini_cast(v)
+  v.include?('.') ? Float(v) : Integer(v) rescue v
+end
+
+module Puppet::Parser::Functions
+  newfunction(:ini, :type => :rvalue, :arity => -2) do |args|
+    if args.map(&:class).uniq != [Hash]
+      fail(ArgumentError, 'ini(): hash arguments required')
+    end
+    args.reduce(&:merge).map do |section,items|
+      ini_flatten(items).map do |k, vs|
+        case vs
+        when Array then vs.map { |v| "#{k}[#{v}] = #{ini_cast(v)}" }
+        else "#{k} = #{ini_cast(vs)}"
+        end
+      end.flatten.sort.push('').unshift("[#{section}]").join("\n")
+    end.flatten.push('').join("\n")
+  end
+end

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0b4828a2a6bc310fe3262189513c488a00226efa
Gerrit-PatchSet: 1
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Ori.livneh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to