From 49059568d0e4e00bbc35d6f9b2a6cd23e7d00f46 Mon Sep 17 00:00:00 2001 From: Dan Bode Date: Wed, 16 Mar 2011 14:26:04 -0500 Subject: (5909) Function to dyncamically generate resources. This function allows you to dynamically generate resources, passing them as a hash to the create_resources function. This was originally written to be used together with an ENC. Resources can be programitally generated as yaml and passed to a class. classes: webserver::instances: instances: instance1: foo: bar instance2: foo: blah Then puppet code can consume the hash parameters and convert then into resources class webserver::instances ( $instances = {} ) { create_resources('webserver::instance', $instances) } Now I can dynamically determine how webserver instances are deployed to nodes by updating the YAML files. --- lib/puppet/parser/functions/create_resources.rb | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 lib/puppet/parser/functions/create_resources.rb (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/create_resources.rb b/lib/puppet/parser/functions/create_resources.rb new file mode 100644 index 000000000..430f110b4 --- /dev/null +++ b/lib/puppet/parser/functions/create_resources.rb @@ -0,0 +1,47 @@ +Puppet::Parser::Functions::newfunction(:create_resources, :doc => ' +Converts a hash into a set of resources and adds them to the catalog. +Takes two parameters: + create_resource($type, $resources) + Creates resources of type $type from the $resources hash. Assumes that + hash is in the following form: + {title=>{parameters}} + This is currently tested for defined resources, classes, as well as native types +') do |args| + raise ArgumentError, ("create_resources(): wrong number of arguments (#{args.length}; must be 2)") if args.length != 2 + #raise ArgumentError, 'requires resource type and param hash' if args.size < 2 + # figure out what kind of resource we are + type_of_resource = nil + type_name = args[0].downcase + if type_name == 'class' + type_of_resource = :class + else + if resource = Puppet::Type.type(type_name.to_sym) + type_of_resource = :type + elsif resource = find_definition(type_name.downcase) + type_of_resource = :define + else + raise ArgumentError, "could not create resource of unknown type #{type_name}" + end + end + # iterate through the resources to create + args[1].each do |title, params| + raise ArgumentError, 'params should not contain title' if(params['title']) + case type_of_resource + when :type + res = resource.hash2resource(params.merge(:title => title)) + catalog.add_resource(res) + when :define + p_resource = Puppet::Parser::Resource.new(type_name, title, :scope => self, :source => resource) + params.merge(:name => title).each do |k,v| + p_resource.set_parameter(k,v) + end + resource.instantiate_resource(self, p_resource) + compiler.add_resource(self, p_resource) + when :class + klass = find_hostclass(title) + raise ArgumentError, "could not find hostclass #{title}" unless klass + klass.ensure_in_catalog(self, params) + compiler.catalog.add_class([title]) + end + end +end -- cgit From ade4efea6d2d4dbee7ccfec62114df0c5dcf33e2 Mon Sep 17 00:00:00 2001 From: Matt Robinson Date: Mon, 28 Mar 2011 11:52:11 -0700 Subject: (#6830) Fix MD5 handling to work with Ruby 1.9 In Ruby 1.9 you have to require 'digest/md5' instead of just 'md5'. The following irb sessions from each version of Ruby should explain the changes in how MD5 is used between Ruby versions and why this patch was made. ruby-1.9.2-p136 :001 > require 'md5' LoadError: no such file to load -- md5 ruby-1.9.2-p136 :002 > require 'digest/md5' => true ruby-1.9.2-p136 :003 > Digest::MD5.hexdigest('mystring') => "169319501261c644a58610f967e8f9d0" ruby-1.9.2-p136 :004 > Digest::MD5.new('mystring') => # ruby-1.8.7-p330 :001 > require 'digest/md5' => [] ruby-1.8.7-p330 :002 > require 'md5' => ["MD5"] ruby-1.8.7-p330 :003 > Digest::MD5.hexdigest('mystring') => "169319501261c644a58610f967e8f9d0" ruby-1.8.7-p330 :004 > MD5.new('mystring') => # ruby-1.8.7-p330 :005 > MD5.new('mystring').to_s => "169319501261c644a58610f967e8f9d0" ruby-1.8.7-p330 :006 > Digest::MD5.new('mystring') ArgumentError: wrong number of arguments (1 for 0) Reviewed-by: Jesse Wolfe --- lib/puppet/parser/functions/fqdn_rand.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/fqdn_rand.rb b/lib/puppet/parser/functions/fqdn_rand.rb index 52946f2c1..91157a148 100644 --- a/lib/puppet/parser/functions/fqdn_rand.rb +++ b/lib/puppet/parser/functions/fqdn_rand.rb @@ -5,8 +5,8 @@ Puppet::Parser::Functions::newfunction(:fqdn_rand, :type => :rvalue, :doc => $random_number = fqdn_rand(30) $random_number_seed = fqdn_rand(30,30)") do |args| - require 'md5' + require 'digest/md5' max = args.shift - srand MD5.new([lookupvar('fqdn'),args].join(':')).to_s.hex + srand(Digest::MD5.hexdigest([lookupvar('fqdn'),args].join(':')).hex) rand(max).to_s end -- cgit