summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser
diff options
context:
space:
mode:
authorMax Martin <max@puppetlabs.com>2011-03-30 13:02:19 -0700
committerMax Martin <max@puppetlabs.com>2011-03-30 13:02:19 -0700
commit9c06fbd762cddcc41a7185a36f2a8e72879125eb (patch)
treebfd37ac5887307857d0b0d79a1db034010eed424 /lib/puppet/parser
parent4196699f5fbb90ceecbb709c8502622eaad39062 (diff)
parent11309a214148abb2167c1bd0adba0e035b2d8384 (diff)
downloadpuppet-9c06fbd762cddcc41a7185a36f2a8e72879125eb.tar.gz
puppet-9c06fbd762cddcc41a7185a36f2a8e72879125eb.tar.xz
puppet-9c06fbd762cddcc41a7185a36f2a8e72879125eb.zip
Merge branch 'next'
* next: (39 commits) (#5908) Add support for new update-rc.d disable API (#6830) Fix tests that depended on special inherited behavior (#6830) Fix overly stubbed tests (#6830) Fix instance_variables now comes back as symbols (#6830) Fix badly stubbed Time object in test (#6830) Fix MD5 handling to work with Ruby 1.9 (#6830) Fix File class scoping (#6830) Handle case where array is actually a string (#6830) Fix case where instance_variables returns symbols in Ruby 1.9 (#6862) Add a default subject for the mail_patches rake task Fixed #6256 - Creation of rrd directory. (#6855) ResourceType#search now accepts a regex (#5477) Allow watch_file to watch non-existent files, especially site.pp (#5477) Allow watch_file to watch non-existent files, especially site.pp Fixing #6851 - ResourceType#find/search loads types Fixing Module#path detection Fixed #6850 - Clean up ResourceType#to_pson (#6830) Fix stat method calls to not use an unneeded argument (#4576) Raise an error when a node is classified into a non-existent class Update CHANGELOG for 2.6.7 ...
Diffstat (limited to 'lib/puppet/parser')
-rw-r--r--lib/puppet/parser/compiler.rb16
-rw-r--r--lib/puppet/parser/functions/create_resources.rb47
-rw-r--r--lib/puppet/parser/functions/fqdn_rand.rb4
-rw-r--r--lib/puppet/parser/lexer.rb5
-rw-r--r--lib/puppet/parser/parser_support.rb1
-rw-r--r--lib/puppet/parser/type_loader.rb28
6 files changed, 86 insertions, 15 deletions
diff --git a/lib/puppet/parser/compiler.rb b/lib/puppet/parser/compiler.rb
index fdabd05c9..a891f1a11 100644
--- a/lib/puppet/parser/compiler.rb
+++ b/lib/puppet/parser/compiler.rb
@@ -20,7 +20,7 @@ class Puppet::Parser::Compiler
puts detail.backtrace if Puppet[:trace]
raise Puppet::Error, "#{detail} on node #{node.name}"
ensure
- # We get these from the environment and only cache them in a thread
+ # We get these from the environment and only cache them in a thread
# variable for the duration of the compilation.
Thread.current[:known_resource_types] = nil
Thread.current[:env_module_directories] = nil
@@ -133,12 +133,11 @@ class Puppet::Parser::Compiler
end
# Evaluate each specified class in turn. If there are any classes we can't
- # find, just tag the catalog and move on. This method really just
- # creates resource objects that point back to the classes, and then the
- # resources are themselves evaluated later in the process.
+ # find, raise an error. This method really just creates resource objects
+ # that point back to the classes, and then the resources are themselves
+ # evaluated later in the process.
def evaluate_classes(classes, scope, lazy_evaluate = true)
raise Puppet::DevError, "No source for scope passed to evaluate_classes" unless scope.source
- found = []
param_classes = nil
# if we are a param class, save the classes hash
# and transform classes to be the keys
@@ -153,20 +152,17 @@ class Puppet::Parser::Compiler
if param_classes
resource = klass.ensure_in_catalog(scope, param_classes[name] || {})
else
- found << name and next if scope.class_scope(klass)
+ next if scope.class_scope(klass)
resource = klass.ensure_in_catalog(scope)
end
# If they've disabled lazy evaluation (which the :include function does),
# then evaluate our resource immediately.
resource.evaluate unless lazy_evaluate
- found << name
else
- Puppet.info "Could not find class #{name} for #{node.name}"
- @catalog.tag(name)
+ raise Puppet::Error, "Could not find class #{name} for #{node.name}"
end
end
- found
end
def evaluate_relationships
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
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
diff --git a/lib/puppet/parser/lexer.rb b/lib/puppet/parser/lexer.rb
index 4050adeb8..2f416615e 100644
--- a/lib/puppet/parser/lexer.rb
+++ b/lib/puppet/parser/lexer.rb
@@ -312,7 +312,8 @@ class Puppet::Parser::Lexer
def file=(file)
@file = file
@line = 1
- @scanner = StringScanner.new(File.read(file))
+ contents = File.exists?(file) ? File.read(file) : ""
+ @scanner = StringScanner.new(contents)
end
def shift_token
@@ -547,7 +548,7 @@ class Puppet::Parser::Lexer
value,terminator = slurpstring('"$')
token_queue << [TOKENS[token_type[terminator]],preamble+value]
if terminator != '$' or @scanner.scan(/\{/)
- token_queue.shift
+ token_queue.shift
elsif var_name = @scanner.scan(%r{(\w*::)*\w+|[0-9]})
token_queue << [TOKENS[:VARIABLE],var_name]
tokenize_interpolated_string(DQ_continuation_token_types)
diff --git a/lib/puppet/parser/parser_support.rb b/lib/puppet/parser/parser_support.rb
index 746aa0f90..7888fe1dc 100644
--- a/lib/puppet/parser/parser_support.rb
+++ b/lib/puppet/parser/parser_support.rb
@@ -79,7 +79,6 @@ class Puppet::Parser::Parser
unless file =~ /\.pp$/
file = file + ".pp"
end
- raise Puppet::Error, "Could not find file #{file}" unless FileTest.exist?(file)
end
raise Puppet::AlreadyImportedError, "Import loop detected" if known_resource_types.watching_file?(file)
diff --git a/lib/puppet/parser/type_loader.rb b/lib/puppet/parser/type_loader.rb
index 140c9f2ca..1fba73d0b 100644
--- a/lib/puppet/parser/type_loader.rb
+++ b/lib/puppet/parser/type_loader.rb
@@ -92,6 +92,34 @@ class Puppet::Parser::TypeLoader
end
end
+ def import_all
+ require 'find'
+
+ module_names = []
+ # Collect the list of all known modules
+ environment.modulepath.each do |path|
+ Dir.chdir(path) do
+ Dir.glob("*").each do |dir|
+ next unless FileTest.directory?(dir)
+ module_names << dir
+ end
+ end
+ end
+
+ module_names.uniq!
+ # And then load all files from each module, but (relying on system
+ # behavior) only load files from the first module of a given name. E.g.,
+ # given first/foo and second/foo, only files from first/foo will be loaded.
+ module_names.each do |name|
+ mod = Puppet::Module.new(name, environment)
+ Find.find(File.join(mod.path, "manifests")) do |path|
+ if path =~ /\.pp$/ or path =~ /\.rb$/
+ import(path)
+ end
+ end
+ end
+ end
+
def known_resource_types
environment.known_resource_types
end