From 16b06320cd3bb3121446717c05b6bc13ae2ff133 Mon Sep 17 00:00:00 2001 From: mh Date: Wed, 27 Jul 2011 18:41:27 +0200 Subject: init of trocla module --- lib/puppet/util/trocla_helper.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lib/puppet/util/trocla_helper.rb (limited to 'lib/puppet/util/trocla_helper.rb') diff --git a/lib/puppet/util/trocla_helper.rb b/lib/puppet/util/trocla_helper.rb new file mode 100644 index 0000000..8187d08 --- /dev/null +++ b/lib/puppet/util/trocla_helper.rb @@ -0,0 +1,31 @@ +module Puppet::Util::TroclaHelper + def trocla(trocla_func,has_options,*args) + # Functions called from puppet manifests that look like this: + # lookup("foo", "bar") + # internally in puppet are invoked: func(["foo", "bar"]) + # + # where as calling from templates should work like this: + # scope.function_lookup("foo", "bar") + # + # Therefore, declare this function with args '*args' to accept any number + # of arguments and deal with puppet's special calling mechanism now: + if args[0].is_a?(Array) + args = args[0] + end + + key = args[0] || raise(Puppet::ParseError, "You need to pass at least a key as an argument!") + format = args[1] || 'plain' + options = args[2] || {} + + configfile = File.join(File.dirname(Puppet.settings[:config]), "trocla.yaml") + + raise(Puppet::ParseError, "Trocla config file #{configfile} not readable") unless File.exist?(configfile) + raise(Puppet::ParseError, "You need rubygems to use Trocla") unless Puppet.features.rubygems? + + require 'rubygems' + require 'trocla' + + has_options ? Trocla.new(configfile).send(trocla_func, key, format, options) : Trocla.new(configfile).send(trocla_func, key, format) + end + module_function :trocla +end \ No newline at end of file -- cgit v1.2.3 From 8ffd1eab30b4bed765457c73ea40b994bff38b7e Mon Sep 17 00:00:00 2001 From: mh Date: Wed, 27 Jul 2011 19:45:54 +0200 Subject: bring config filename in line with what trocla uses --- lib/puppet/util/trocla_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/util/trocla_helper.rb') diff --git a/lib/puppet/util/trocla_helper.rb b/lib/puppet/util/trocla_helper.rb index 8187d08..ea4edee 100644 --- a/lib/puppet/util/trocla_helper.rb +++ b/lib/puppet/util/trocla_helper.rb @@ -17,7 +17,7 @@ module Puppet::Util::TroclaHelper format = args[1] || 'plain' options = args[2] || {} - configfile = File.join(File.dirname(Puppet.settings[:config]), "trocla.yaml") + configfile = File.join(File.dirname(Puppet.settings[:config]), "troclarc.yaml") raise(Puppet::ParseError, "Trocla config file #{configfile} not readable") unless File.exist?(configfile) raise(Puppet::ParseError, "You need rubygems to use Trocla") unless Puppet.features.rubygems? @@ -28,4 +28,4 @@ module Puppet::Util::TroclaHelper has_options ? Trocla.new(configfile).send(trocla_func, key, format, options) : Trocla.new(configfile).send(trocla_func, key, format) end module_function :trocla -end \ No newline at end of file +end -- cgit v1.2.3 From abc6919245bca4c1965376fec4744df2329211a5 Mon Sep 17 00:00:00 2001 From: mh Date: Mon, 8 Aug 2011 00:22:23 +0200 Subject: wording --- lib/puppet/util/trocla_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/util/trocla_helper.rb') diff --git a/lib/puppet/util/trocla_helper.rb b/lib/puppet/util/trocla_helper.rb index ea4edee..374d538 100644 --- a/lib/puppet/util/trocla_helper.rb +++ b/lib/puppet/util/trocla_helper.rb @@ -17,9 +17,9 @@ module Puppet::Util::TroclaHelper format = args[1] || 'plain' options = args[2] || {} - configfile = File.join(File.dirname(Puppet.settings[:config]), "troclarc.yaml") + configfile = File.join(File.dirname(Puppet.settings[:config]), 'troclarc.yaml') - raise(Puppet::ParseError, "Trocla config file #{configfile} not readable") unless File.exist?(configfile) + raise(Puppet::ParseError, "Trocla config file #{configfile} is not readable") unless File.exist?(configfile) raise(Puppet::ParseError, "You need rubygems to use Trocla") unless Puppet.features.rubygems? require 'rubygems' -- cgit v1.2.3 From 64205c93a957bafdfb255a991844f2d7744f11b5 Mon Sep 17 00:00:00 2001 From: mh Date: Tue, 6 Sep 2011 15:02:51 +0200 Subject: as setting a hash first is a bit cumbersome, we provide the possibility to pass a yaml string --- lib/puppet/util/trocla_helper.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib/puppet/util/trocla_helper.rb') diff --git a/lib/puppet/util/trocla_helper.rb b/lib/puppet/util/trocla_helper.rb index ea4edee..9a41c0e 100644 --- a/lib/puppet/util/trocla_helper.rb +++ b/lib/puppet/util/trocla_helper.rb @@ -16,7 +16,11 @@ module Puppet::Util::TroclaHelper key = args[0] || raise(Puppet::ParseError, "You need to pass at least a key as an argument!") format = args[1] || 'plain' options = args[2] || {} - + if options.is_a?(String) + require 'yaml' + options = YAML.load(options) + end + configfile = File.join(File.dirname(Puppet.settings[:config]), "troclarc.yaml") raise(Puppet::ParseError, "Trocla config file #{configfile} not readable") unless File.exist?(configfile) -- cgit v1.2.3 From 02bbc22223f53d979c1ab5724e26aa2ac4c22c7a Mon Sep 17 00:00:00 2001 From: Michael Franz Aigner Date: Thu, 28 Feb 2013 08:17:37 -0500 Subject: Removing calls to Puppet.features.rubygems? This makes the Gem usable in the latest Puppet versions. The handling of RubyGems got revised in Puppet 3.0.1-rc1: http://projects.puppetlabs.com/issues/16757 The new policy is that either bundler and/or rubygems are guaranteed to be loaded and initialized when the Puppet manifest is evaluated, making it unnecessary for Puppet modules to load rubygems. This new policy broke the puppet-trocla module. This is because 'Puppet.features.rubygems?' always evaluates to false now, which causes the module to abort the manifest compilation with a message informing about the necessity of RubyGems to be present. --- lib/puppet/util/trocla_helper.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'lib/puppet/util/trocla_helper.rb') diff --git a/lib/puppet/util/trocla_helper.rb b/lib/puppet/util/trocla_helper.rb index 58ccf9c..64ef859 100644 --- a/lib/puppet/util/trocla_helper.rb +++ b/lib/puppet/util/trocla_helper.rb @@ -25,9 +25,7 @@ module Puppet::Util::TroclaHelper configfile = File.join(File.dirname(Puppet.settings[:config]), "troclarc.yaml") raise(Puppet::ParseError, "Trocla config file #{configfile} is not readable") unless File.exist?(configfile) - raise(Puppet::ParseError, "You need rubygems to use Trocla") unless Puppet.features.rubygems? - require 'rubygems' require 'trocla' has_options ? Trocla.new(configfile).send(trocla_func, key, format, options) : Trocla.new(configfile).send(trocla_func, key, format) -- cgit v1.2.3 From 0e320508328db93fc2ca8aa29eb1ed22e0f0f22a Mon Sep 17 00:00:00 2001 From: mh Date: Wed, 12 Aug 2015 08:42:35 +0200 Subject: whitespace cleanup --- lib/puppet/util/trocla_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/util/trocla_helper.rb') diff --git a/lib/puppet/util/trocla_helper.rb b/lib/puppet/util/trocla_helper.rb index 64ef859..94670ae 100644 --- a/lib/puppet/util/trocla_helper.rb +++ b/lib/puppet/util/trocla_helper.rb @@ -16,7 +16,7 @@ module Puppet::Util::TroclaHelper key = args[0] || raise(Puppet::ParseError, "You need to pass at least a key as an argument!") format = args[1] || 'plain' options = args[2] || {} - + if options.is_a?(String) require 'yaml' options = YAML.load(options) @@ -27,7 +27,7 @@ module Puppet::Util::TroclaHelper raise(Puppet::ParseError, "Trocla config file #{configfile} is not readable") unless File.exist?(configfile) require 'trocla' - + has_options ? Trocla.new(configfile).send(trocla_func, key, format, options) : Trocla.new(configfile).send(trocla_func, key, format) end module_function :trocla -- cgit v1.2.3 From bbedb788a7951e2f69c1c2815a5c3c669ff02ae6 Mon Sep 17 00:00:00 2001 From: mh Date: Tue, 7 Jun 2016 21:59:33 +0200 Subject: keep trocla object around - addresses #18 Per puppet function call we now created a new Trocla object. This is a) a very naive approach and b) obviously can lead to a lot of inefficiency as we for example need to build up trocla each time again. Also this means that we are running into problems like opening a connection to a database system each time a trocla lookup is done (and we never close the connection :-/). The proper way to solve this is to make sure we don't create too many trocla objects. With this change, we should now create once a global trocla object PER puppet(-master/-server) process and keep it around for the life time of such a process. --- lib/puppet/util/trocla_helper.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'lib/puppet/util/trocla_helper.rb') diff --git a/lib/puppet/util/trocla_helper.rb b/lib/puppet/util/trocla_helper.rb index 94670ae..ce583f5 100644 --- a/lib/puppet/util/trocla_helper.rb +++ b/lib/puppet/util/trocla_helper.rb @@ -22,13 +22,22 @@ module Puppet::Util::TroclaHelper options = YAML.load(options) end - configfile = File.join(File.dirname(Puppet.settings[:config]), "troclarc.yaml") + has_options ? store.send(trocla_func, key, format, options) : store.send(trocla_func, key, format) + end + module_function :trocla + + private - raise(Puppet::ParseError, "Trocla config file #{configfile} is not readable") unless File.exist?(configfile) + def store + @store ||= begin + require 'trocla' + configfile = File.join(File.dirname(Puppet.settings[:config]), "troclarc.yaml") - require 'trocla' + raise(Puppet::ParseError, "Trocla config file #{configfile} is not readable") unless File.exist?(configfile) - has_options ? Trocla.new(configfile).send(trocla_func, key, format, options) : Trocla.new(configfile).send(trocla_func, key, format) + Trocla.new(configfile) + end end - module_function :trocla + module_function :store + end -- cgit v1.2.3