summaryrefslogtreecommitdiffstats
path: root/lib/puppet
diff options
context:
space:
mode:
authorJames Turnbull <james@lovedthanlost.net>2008-02-13 20:29:00 +1100
committerJames Turnbull <james@lovedthanlost.net>2008-02-13 20:29:00 +1100
commitd3959c497f4c61995f664d88aa3386b54c4baf8d (patch)
tree1a83a85c0c6d626781cad5632db146aeae49d0c9 /lib/puppet
parentc3ead0331adba5f60ea7d508775a89de68e26caa (diff)
parentbcb9b564281003e22d72752d84fa9dc9c8c7107b (diff)
downloadpuppet-d3959c497f4c61995f664d88aa3386b54c4baf8d.tar.gz
puppet-d3959c497f4c61995f664d88aa3386b54c4baf8d.tar.xz
puppet-d3959c497f4c61995f664d88aa3386b54c4baf8d.zip
Merge branch '0.24.x' of git://reductivelabs.com/puppet into 0.24.x
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/node/catalog.rb68
-rw-r--r--lib/puppet/parser/ast.rb76
-rw-r--r--lib/puppet/parser/ast/astarray.rb5
-rw-r--r--lib/puppet/parser/ast/caseopt.rb8
-rw-r--r--lib/puppet/parser/ast/casestatement.rb9
-rw-r--r--lib/puppet/parser/ast/collection.rb8
-rw-r--r--lib/puppet/parser/ast/collexpr.rb8
-rw-r--r--lib/puppet/parser/ast/definition.rb310
-rw-r--r--lib/puppet/parser/ast/else.rb5
-rw-r--r--lib/puppet/parser/ast/function.rb13
-rw-r--r--lib/puppet/parser/ast/hostclass.rb120
-rw-r--r--lib/puppet/parser/ast/ifstatement.rb9
-rw-r--r--lib/puppet/parser/ast/leaf.rb12
-rw-r--r--lib/puppet/parser/ast/node.rb84
-rw-r--r--lib/puppet/parser/ast/resource.rb12
-rw-r--r--lib/puppet/parser/ast/resource_defaults.rb6
-rw-r--r--lib/puppet/parser/ast/resource_override.rb10
-rw-r--r--lib/puppet/parser/ast/resource_reference.rb6
-rw-r--r--lib/puppet/parser/ast/resourceparam.rb6
-rw-r--r--lib/puppet/parser/ast/selector.rb11
-rw-r--r--lib/puppet/parser/ast/tag.rb6
-rw-r--r--lib/puppet/parser/ast/vardef.rb7
-rw-r--r--lib/puppet/parser/collector.rb8
-rw-r--r--lib/puppet/parser/compiler.rb (renamed from lib/puppet/parser/compile.rb)167
-rw-r--r--lib/puppet/parser/functions.rb6
-rw-r--r--lib/puppet/parser/interpreter.rb4
-rw-r--r--lib/puppet/parser/resource.rb17
-rw-r--r--lib/puppet/parser/resource/param.rb2
-rw-r--r--lib/puppet/parser/resource/reference.rb10
-rw-r--r--lib/puppet/parser/scope.rb19
-rw-r--r--lib/puppet/parser/templatewrapper.rb2
-rw-r--r--lib/puppet/pgraph.rb14
-rw-r--r--lib/puppet/simple_graph.rb8
-rw-r--r--lib/puppet/transaction.rb2
-rw-r--r--lib/puppet/transportable.rb2
-rw-r--r--lib/puppet/type/pfile.rb2
-rw-r--r--lib/puppet/util/constant_inflector.rb14
-rw-r--r--lib/puppet/util/graph.rb2
-rw-r--r--lib/puppet/util/tagging.rb5
39 files changed, 482 insertions, 601 deletions
diff --git a/lib/puppet/node/catalog.rb b/lib/puppet/node/catalog.rb
index 9601309d8..b74947107 100644
--- a/lib/puppet/node/catalog.rb
+++ b/lib/puppet/node/catalog.rb
@@ -1,5 +1,7 @@
require 'puppet/indirector'
+require 'puppet/util/tagging'
+
# This class models a node catalog. It is the thing
# meant to be passed from server to client, and it contains all
# of the information in the catalog, including the resources
@@ -8,6 +10,8 @@ class Puppet::Node::Catalog < Puppet::PGraph
extend Puppet::Indirector
indirects :catalog, :terminus_class => :compiler
+ include Puppet::Util::Tagging
+
# The host name this is a catalog for.
attr_accessor :name
@@ -58,14 +62,14 @@ class Puppet::Node::Catalog < Puppet::PGraph
raise ArgumentError, "Can only add objects that respond to :ref"
end
+ fail_unless_unique(resource)
+
ref = resource.ref
- if @resource_table.include?(ref)
- raise ArgumentError, "Resource %s is already defined" % ref
- else
- @resource_table[ref] = resource
- end
- resource.catalog = self unless is_relationship_graph
- add_vertex!(resource)
+
+ @resource_table[ref] = resource
+
+ resource.catalog = self if resource.respond_to?(:catalog=) and ! is_relationship_graph
+ add_vertex(resource)
end
end
@@ -268,7 +272,6 @@ class Puppet::Node::Catalog < Puppet::PGraph
super()
@name = name if name
@extraction_format ||= :transportable
- @tags = []
@classes = []
@resource_table = {}
@transient_resources = []
@@ -313,9 +316,9 @@ class Puppet::Node::Catalog < Puppet::PGraph
# First create the dependency graph
self.vertices.each do |vertex|
- @relationship_graph.add_vertex! vertex
+ @relationship_graph.add_vertex vertex
vertex.builddepends.each do |edge|
- @relationship_graph.add_edge!(edge)
+ @relationship_graph.add_edge(edge)
end
end
@@ -325,7 +328,7 @@ class Puppet::Node::Catalog < Puppet::PGraph
unless @relationship_graph.edge?(edge.source, edge.target) # don't let automatic relationships conflict with manual ones.
unless @relationship_graph.edge?(edge.target, edge.source)
vertex.debug "Autorequiring %s" % [edge.source]
- @relationship_graph.add_edge!(edge)
+ @relationship_graph.add_edge(edge)
else
vertex.debug "Skipping automatic relationship with %s" % (edge.source == vertex ? edge.target : edge.source)
end
@@ -381,25 +384,6 @@ class Puppet::Node::Catalog < Puppet::PGraph
@resource_table.keys
end
- # Add a tag.
- def tag(*names)
- names.each do |name|
- name = name.to_s
- @tags << name unless @tags.include?(name)
- if name.include?("::")
- name.split("::").each do |sub|
- @tags << sub unless @tags.include?(sub)
- end
- end
- end
- nil
- end
-
- # Return the list of tags.
- def tags
- @tags.dup
- end
-
# Convert our catalog into a RAL catalog.
def to_ral
to_catalog :to_type
@@ -447,6 +431,28 @@ class Puppet::Node::Catalog < Puppet::PGraph
end
end
+ # Verify that the given resource isn't defined elsewhere.
+ def fail_unless_unique(resource)
+ # Short-curcuit the common case,
+ return unless existing_resource = @resource_table[resource.ref]
+
+ # Either it's a defined type, which are never
+ # isomorphic, or it's a non-isomorphic type, so
+ # we should throw an exception.
+ msg = "Duplicate definition: %s is already defined" % resource.ref
+
+ if existing_resource.file and existing_resource.line
+ msg << " in file %s at line %s" %
+ [existing_resource.file, existing_resource.line]
+ end
+
+ if resource.line or resource.file
+ msg << "; cannot redefine"
+ end
+
+ raise ArgumentError.new(msg)
+ end
+
# An abstracted method for converting one catalog into another type of catalog.
# This pretty much just converts all of the resources from one class to another, using
# a conversion method.
@@ -481,7 +487,7 @@ class Puppet::Node::Catalog < Puppet::PGraph
raise Puppet::DevError, "Could not find resource %s when converting %s resources" % [edge.target.ref, message]
end
- result.add_edge!(source, target, edge.label)
+ result.add_edge(source, target, edge.label)
end
map.clear
diff --git a/lib/puppet/parser/ast.rb b/lib/puppet/parser/ast.rb
index 14b686e2f..c9bd7c9e8 100644
--- a/lib/puppet/parser/ast.rb
+++ b/lib/puppet/parser/ast.rb
@@ -14,30 +14,6 @@ class Puppet::Parser::AST
include Puppet::Util::MethodHelper
attr_accessor :line, :file, :parent, :scope
- # Just used for 'tree', which is only used in debugging.
- @@pink = ""
- @@green = ""
- @@yellow = ""
- @@slate = ""
- @@reset = ""
-
- # Just used for 'tree', which is only used in debugging.
- @@indent = " " * 4
- @@indline = @@pink + ("-" * 4) + @@reset
- @@midline = @@slate + ("-" * 4) + @@reset
-
- @@settypes = {}
-
- # Just used for 'tree', which is only used in debugging.
- def AST.indention
- return @@indent * @@indention
- end
-
- # Just used for 'tree', which is only used in debugging.
- def AST.midline
- return @@midline
- end
-
# Does this ast object set something? If so, it gets evaluated first.
def self.settor?
if defined? @settor
@@ -47,16 +23,12 @@ class Puppet::Parser::AST
end
end
- # Evaluate the current object. Basically just iterates across all
+ # Evaluate the current object. Just a stub method, since the subclass
+ # should override this method.
# of the contained children and evaluates them in turn, returning a
# list of all of the collected values, rejecting nil values
- def evaluate(args)
- #Puppet.debug("Evaluating ast %s" % @name)
- value = self.collect { |obj|
- obj.safeevaluate(args)
- }.reject { |obj|
- obj.nil?
- }
+ def evaluate(*options)
+ raise Puppet::DevError, "Did not override #evaluate in %s" % self.class
end
# Throw a parse error.
@@ -75,11 +47,11 @@ class Puppet::Parser::AST
# correctly handles errors. It is critical to use this method because
# it can enable you to catch the error where it happens, rather than
# much higher up the stack.
- def safeevaluate(options)
+ def safeevaluate(*options)
# We duplicate code here, rather than using exceptwrap, because this
# is called so many times during parsing.
begin
- return self.evaluate(options)
+ return self.evaluate(*options)
rescue Puppet::Error => detail
raise adderrorcontext(detail)
rescue => detail
@@ -90,14 +62,6 @@ class Puppet::Parser::AST
end
end
- # Again, just used for printing out the parse tree.
- def typewrap(string)
- #return self.class.to_s.sub(/.+::/,'') +
- #"(" + @@green + string.to_s + @@reset + ")"
- return @@green + string.to_s + @@reset +
- "(" + self.class.to_s.sub(/.+::/,'') + ")"
- end
-
# Initialize the object. Requires a hash as the argument, and
# takes each of the parameters of the hash and calls the settor
# method for them. This is probably pretty inefficient and should
@@ -107,13 +71,27 @@ class Puppet::Parser::AST
@line = nil
set_options(args)
end
- #---------------------------------------------------------------
- # Now autoload everything.
- @autoloader = Puppet::Util::Autoload.new(self,
- "puppet/parser/ast"
- )
- @autoloader.loadall
end
+# And include all of the AST subclasses.
+require 'puppet/parser/ast/astarray'
+require 'puppet/parser/ast/branch'
+require 'puppet/parser/ast/caseopt'
+require 'puppet/parser/ast/casestatement'
+require 'puppet/parser/ast/collection'
+require 'puppet/parser/ast/collexpr'
+require 'puppet/parser/ast/definition'
+require 'puppet/parser/ast/else'
+require 'puppet/parser/ast/function'
+require 'puppet/parser/ast/hostclass'
+require 'puppet/parser/ast/ifstatement'
require 'puppet/parser/ast/leaf'
-
+require 'puppet/parser/ast/node'
+require 'puppet/parser/ast/resource'
+require 'puppet/parser/ast/resource_defaults'
+require 'puppet/parser/ast/resource_override'
+require 'puppet/parser/ast/resource_reference'
+require 'puppet/parser/ast/resourceparam'
+require 'puppet/parser/ast/selector'
+require 'puppet/parser/ast/tag'
+require 'puppet/parser/ast/vardef'
diff --git a/lib/puppet/parser/ast/astarray.rb b/lib/puppet/parser/ast/astarray.rb
index 5f1e838d0..b66fd6bba 100644
--- a/lib/puppet/parser/ast/astarray.rb
+++ b/lib/puppet/parser/ast/astarray.rb
@@ -15,8 +15,7 @@ class Puppet::Parser::AST
end
# Evaluate our children.
- def evaluate(hash)
- scope = hash[:scope]
+ def evaluate(scope)
rets = nil
# We basically always operate declaratively, and when we
# do we need to evaluate the settor-like statements first. This
@@ -51,7 +50,7 @@ class Puppet::Parser::AST
}
rets = [settors, others].flatten.collect { |child|
- child.safeevaluate(:scope => scope)
+ child.safeevaluate(scope)
}
return rets.reject { |o| o.nil? }
end
diff --git a/lib/puppet/parser/ast/caseopt.rb b/lib/puppet/parser/ast/caseopt.rb
index d1d9d0e9c..824bde853 100644
--- a/lib/puppet/parser/ast/caseopt.rb
+++ b/lib/puppet/parser/ast/caseopt.rb
@@ -44,17 +44,17 @@ class Puppet::Parser::AST
def eachvalue(scope)
if @value.is_a?(AST::ASTArray)
@value.each { |subval|
- yield subval.evaluate(:scope => scope)
+ yield subval.safeevaluate(scope)
}
else
- yield @value.evaluate(:scope => scope)
+ yield @value.safeevaluate(scope)
end
end
# Evaluate the actual statements; this only gets called if
# our option matched.
- def evaluate(hash)
- return @statements.safeevaluate(hash)
+ def evaluate(scope)
+ return @statements.safeevaluate(scope)
end
end
end
diff --git a/lib/puppet/parser/ast/casestatement.rb b/lib/puppet/parser/ast/casestatement.rb
index 3c6f9c7e2..aa03090de 100644
--- a/lib/puppet/parser/ast/casestatement.rb
+++ b/lib/puppet/parser/ast/casestatement.rb
@@ -8,9 +8,8 @@ class Puppet::Parser::AST
# Short-curcuit evaluation. Return the value of the statements for
# the first option that matches.
- def evaluate(hash)
- scope = hash[:scope]
- value = @test.safeevaluate(:scope => scope)
+ def evaluate(scope)
+ value = @test.safeevaluate(scope)
sensitive = Puppet[:casesensitive]
value = value.downcase if ! sensitive and value.respond_to?(:downcase)
@@ -30,7 +29,7 @@ class Puppet::Parser::AST
if found
# we found a matching option
- retvalue = option.safeevaluate(:scope => scope)
+ retvalue = option.safeevaluate(scope)
break
end
@@ -42,7 +41,7 @@ class Puppet::Parser::AST
# Unless we found something, look for the default.
unless found
if default
- retvalue = default.safeevaluate(:scope => scope)
+ retvalue = default.safeevaluate(scope)
else
Puppet.debug "No true answers and no default"
retvalue = nil
diff --git a/lib/puppet/parser/ast/collection.rb b/lib/puppet/parser/ast/collection.rb
index e05977a47..9e795a33c 100644
--- a/lib/puppet/parser/ast/collection.rb
+++ b/lib/puppet/parser/ast/collection.rb
@@ -9,18 +9,16 @@ class Collection < AST::Branch
attr_accessor :type, :query, :form
# We return an object that does a late-binding evaluation.
- def evaluate(hash)
- scope = hash[:scope]
-
+ def evaluate(scope)
if self.query
- str, code = self.query.safeevaluate :scope => scope
+ str, code = self.query.safeevaluate scope
else
str = code = nil
end
newcoll = Puppet::Parser::Collector.new(scope, @type, str, code, self.form)
- scope.compile.add_collection(newcoll)
+ scope.compiler.add_collection(newcoll)
newcoll
end
diff --git a/lib/puppet/parser/ast/collexpr.rb b/lib/puppet/parser/ast/collexpr.rb
index 4a96d9c61..3e13d9400 100644
--- a/lib/puppet/parser/ast/collexpr.rb
+++ b/lib/puppet/parser/ast/collexpr.rb
@@ -9,9 +9,7 @@ class CollExpr < AST::Branch
attr_accessor :test1, :test2, :oper, :form, :type, :parens
# We return an object that does a late-binding evaluation.
- def evaluate(hash)
- scope = hash[:scope]
-
+ def evaluate(scope)
# Make sure our contained expressions have all the info they need.
[@test1, @test2].each do |t|
if t.is_a?(self.class)
@@ -21,8 +19,8 @@ class CollExpr < AST::Branch
end
# The code is only used for virtual lookups
- str1, code1 = @test1.safeevaluate :scope => scope
- str2, code2 = @test2.safeevaluate :scope => scope
+ str1, code1 = @test1.safeevaluate scope
+ str2, code2 = @test2.safeevaluate scope
# First build up the virtual code.
# If we're a conjunction operator, then we're calling code. I did
diff --git a/lib/puppet/parser/ast/definition.rb b/lib/puppet/parser/ast/definition.rb
index 3d6d6188c..2b7506446 100644
--- a/lib/puppet/parser/ast/definition.rb
+++ b/lib/puppet/parser/ast/definition.rb
@@ -1,155 +1,148 @@
require 'puppet/parser/ast/branch'
-class Puppet::Parser::AST
- # Evaluate the stored parse tree for a given component. This will
- # receive the arguments passed to the component and also the type and
- # name of the component.
- class Definition < AST::Branch
- include Puppet::Util
- include Puppet::Util::Warnings
- include Puppet::Util::MethodHelper
- class << self
- attr_accessor :name
- end
+require 'puppet/util/warnings'
- # The class name
- @name = :definition
+# The AST class for defined types, which is also the base class
+# nodes and classes.
+class Puppet::Parser::AST::Definition < Puppet::Parser::AST::Branch
+ include Puppet::Util::Warnings
+ class << self
+ attr_accessor :name
+ end
- attr_accessor :classname, :arguments, :code, :scope, :keyword
- attr_accessor :exported, :namespace, :parser, :virtual
+ # The class name
+ @name = :definition
- # These are retrieved when looking up the superclass
- attr_accessor :name
+ attr_accessor :classname, :arguments, :code, :scope, :keyword
+ attr_accessor :exported, :namespace, :parser, :virtual, :name
- attr_reader :parentclass
+ attr_reader :parentclass
- def child_of?(klass)
- false
- end
+ def child_of?(klass)
+ false
+ end
- def evaluate(options)
- origscope = options[:scope]
- resource = options[:resource]
+ # Create a resource that knows how to evaluate our actual code.
+ def evaluate(scope)
+ # Do nothing if the resource already exists; this provides the singleton nature classes need.
+ return if scope.catalog.resource(self.class.name, self.classname)
- # Create a new scope.
- scope = subscope(origscope, resource)
+ resource = Puppet::Parser::Resource.new(:type => self.class.name, :title => self.classname, :scope => scope, :source => scope.source)
- # Additionally, add a tag for whatever kind of class
- # we are
- if @classname != "" and ! @classname.nil?
- @classname.split(/::/).each { |tag| scope.resource.tag(tag) }
- end
+ scope.catalog.tag(*resource.tags)
- [resource.name, resource.title].each do |str|
- unless str.nil? or str =~ /[^\w]/ or str == ""
- scope.resource.tag(str)
- end
- end
+ scope.compiler.add_resource(scope, resource)
- set_resource_parameters(scope, resource)
+ return resource
+ end
- if self.code
- return self.code.safeevaluate(:scope => scope)
- else
- return nil
- end
+ # Now evaluate the code associated with this class or definition.
+ def evaluate_code(resource)
+ # Create a new scope.
+ scope = subscope(resource.scope, resource)
+
+ set_resource_parameters(scope, resource)
+
+ if self.code
+ return self.code.safeevaluate(scope)
+ else
+ return nil
end
+ end
- def initialize(hash = {})
- @arguments = nil
- @parentclass = nil
- super
+ def initialize(hash = {})
+ @arguments = nil
+ @parentclass = nil
+ super
- # Convert the arguments to a hash for ease of later use.
- if @arguments
- unless @arguments.is_a? Array
- @arguments = [@arguments]
- end
- oldargs = @arguments
- @arguments = {}
- oldargs.each do |arg, val|
- @arguments[arg] = val
- end
- else
- @arguments = {}
+ # Convert the arguments to a hash for ease of later use.
+ if @arguments
+ unless @arguments.is_a? Array
+ @arguments = [@arguments]
end
-
- # Deal with metaparams in the argument list.
- @arguments.each do |arg, defvalue|
- next unless Puppet::Type.metaparamclass(arg)
- if defvalue
- warnonce "%s is a metaparam; this value will inherit to all contained resources" % arg
- else
- raise Puppet::ParseError, "%s is a metaparameter; please choose another parameter name in the %s definition" % [arg, self.classname]
- end
+ oldargs = @arguments
+ @arguments = {}
+ oldargs.each do |arg, val|
+ @arguments[arg] = val
end
+ else
+ @arguments = {}
end
- def find_parentclass
- @parser.findclass(namespace, parentclass)
+ # Deal with metaparams in the argument list.
+ @arguments.each do |arg, defvalue|
+ next unless Puppet::Type.metaparamclass(arg)
+ if defvalue
+ warnonce "%s is a metaparam; this value will inherit to all contained resources" % arg
+ else
+ raise Puppet::ParseError, "%s is a metaparameter; please choose another parameter name in the %s definition" % [arg, self.classname]
+ end
end
+ end
- # Set our parent class, with a little check to avoid some potential
- # weirdness.
- def parentclass=(name)
- if name == self.classname
- parsefail "Parent classes must have dissimilar names"
- end
+ def find_parentclass
+ @parser.findclass(namespace, parentclass)
+ end
- @parentclass = name
+ # Set our parent class, with a little check to avoid some potential
+ # weirdness.
+ def parentclass=(name)
+ if name == self.classname
+ parsefail "Parent classes must have dissimilar names"
end
- # Hunt down our class object.
- def parentobj
- if @parentclass
- # Cache our result, since it should never change.
- unless defined?(@parentobj)
- unless tmp = find_parentclass
- parsefail "Could not find %s %s" % [self.class.name, @parentclass]
- end
+ @parentclass = name
+ end
- if tmp == self
- parsefail "Parent classes must have dissimilar names"
- end
+ # Hunt down our class object.
+ def parentobj
+ return nil unless @parentclass
- @parentobj = tmp
- end
- @parentobj
- else
- nil
+ # Cache our result, since it should never change.
+ unless defined?(@parentobj)
+ unless tmp = find_parentclass
+ parsefail "Could not find %s parent %s" % [self.class.name, @parentclass]
+ end
+
+ if tmp == self
+ parsefail "Parent classes must have dissimilar names"
end
+
+ @parentobj = tmp
end
+ @parentobj
+ end
- # Create a new subscope in which to evaluate our code.
- def subscope(scope, resource)
- args = {
- :resource => resource,
- :keyword => self.keyword,
- :namespace => self.namespace,
- :source => self
- }
+ # Create a new subscope in which to evaluate our code.
+ def subscope(scope, resource)
+ args = {
+ :resource => resource,
+ :keyword => self.keyword,
+ :namespace => self.namespace,
+ :source => self
+ }
- oldscope = scope
- scope = scope.newscope(args)
- scope.source = self
+ oldscope = scope
+ scope = scope.newscope(args)
+ scope.source = self
- return scope
- end
+ return scope
+ end
- def to_s
- classname
- end
+ def to_s
+ classname
+ end
- # Check whether a given argument is valid. Searches up through
- # any parent classes that might exist.
- def validattr?(param)
- param = param.to_s
+ # Check whether a given argument is valid. Searches up through
+ # any parent classes that might exist.
+ def validattr?(param)
+ param = param.to_s
- if @arguments.include?(param)
- # It's a valid arg for us
- return true
- elsif param == "name"
- return true
+ if @arguments.include?(param)
+ # It's a valid arg for us
+ return true
+ elsif param == "name"
+ return true
# elsif defined? @parentclass and @parentclass
# # Else, check any existing parent
# if parent = @scope.lookuptype(@parentclass) and parent != []
@@ -160,53 +153,52 @@ class Puppet::Parser::AST
# raise Puppet::Error, "Could not find parent class %s" %
# @parentclass
# end
- elsif Puppet::Type.metaparam?(param)
- return true
- else
- # Or just return false
- return false
- end
+ elsif Puppet::Type.metaparam?(param)
+ return true
+ else
+ # Or just return false
+ return false
end
+ end
- private
-
- # Set any arguments passed by the resource as variables in the scope.
- def set_resource_parameters(scope, resource)
- args = symbolize_options(resource.to_hash || {})
-
- # Verify that all required arguments are either present or
- # have been provided with defaults.
- if self.arguments
- self.arguments.each { |arg, default|
- arg = symbolize(arg)
- unless args.include?(arg)
- if defined? default and ! default.nil?
- default = default.safeevaluate :scope => scope
- args[arg] = default
- #Puppet.debug "Got default %s for %s in %s" %
- # [default.inspect, arg.inspect, @name.inspect]
- else
- parsefail "Must pass %s to %s of type %s" %
- [arg, resource.title, @classname]
- end
+ private
+
+ # Set any arguments passed by the resource as variables in the scope.
+ def set_resource_parameters(scope, resource)
+ args = symbolize_options(resource.to_hash || {})
+
+ # Verify that all required arguments are either present or
+ # have been provided with defaults.
+ if self.arguments
+ self.arguments.each { |arg, default|
+ arg = arg.to_sym
+ unless args.include?(arg)
+ if defined? default and ! default.nil?
+ default = default.safeevaluate scope
+ args[arg] = default
+ #Puppet.debug "Got default %s for %s in %s" %
+ # [default.inspect, arg.inspect, @name.inspect]
+ else
+ parsefail "Must pass %s to %s of type %s" %
+ [arg, resource.title, @classname]
end
- }
- end
-
- # Set each of the provided arguments as variables in the
- # definition's scope.
- args.each { |arg,value|
- unless validattr?(arg)
- parsefail "%s does not accept attribute %s" % [@classname, arg]
- end
-
- exceptwrap do
- scope.setvar(arg.to_s, args[arg])
end
}
-
- scope.setvar("title", resource.title) unless args.include? :title
- scope.setvar("name", resource.name) unless args.include? :name
end
+
+ # Set each of the provided arguments as variables in the
+ # definition's scope.
+ args.each { |arg,value|
+ unless validattr?(arg)
+ parsefail "%s does not accept attribute %s" % [@classname, arg]
+ end
+
+ exceptwrap do
+ scope.setvar(arg.to_s, args[arg])
+ end
+ }
+
+ scope.setvar("title", resource.title) unless args.include? :title
+ scope.setvar("name", resource.name) unless args.include? :name
end
end
diff --git a/lib/puppet/parser/ast/else.rb b/lib/puppet/parser/ast/else.rb
index e76051372..affac625d 100644
--- a/lib/puppet/parser/ast/else.rb
+++ b/lib/puppet/parser/ast/else.rb
@@ -12,9 +12,8 @@ class Puppet::Parser::AST
# Evaluate the actual statements; this only gets called if
# our test was true matched.
- def evaluate(hash)
- scope = hash[:scope]
- return @statements.safeevaluate(:scope => scope)
+ def evaluate(scope)
+ return @statements.safeevaluate(scope)
end
end
end
diff --git a/lib/puppet/parser/ast/function.rb b/lib/puppet/parser/ast/function.rb
index 0cd1fff62..63d7c7abf 100644
--- a/lib/puppet/parser/ast/function.rb
+++ b/lib/puppet/parser/ast/function.rb
@@ -7,18 +7,11 @@ class Puppet::Parser::AST
@settor = true
- def evaluate(hash)
+ def evaluate(scope)
# We don't need to evaluate the name, because it's plaintext
+ args = @arguments.safeevaluate(scope)
- # Just evaluate the arguments
- scope = hash[:scope]
-
- args = @arguments.safeevaluate(:scope => scope)
-
- #exceptwrap :message => "Failed to execute %s" % @name,
- # :type => Puppet::ParseError do
- return scope.send("function_" + @name, args)
- #end
+ return scope.send("function_" + @name, args)
end
def initialize(hash)
diff --git a/lib/puppet/parser/ast/hostclass.rb b/lib/puppet/parser/ast/hostclass.rb
index 63900d0e3..8d4d01660 100644
--- a/lib/puppet/parser/ast/hostclass.rb
+++ b/lib/puppet/parser/ast/hostclass.rb
@@ -1,80 +1,80 @@
require 'puppet/parser/ast/definition'
-class Puppet::Parser::AST
- # The code associated with a class. This is different from definitions
- # in that each class is a singleton -- only one will exist for a given
- # node.
- class HostClass < AST::Definition
- @name = :class
+# The code associated with a class. This is different from definitions
+# in that each class is a singleton -- only one will exist for a given
+# node.
+class Puppet::Parser::AST::HostClass < Puppet::Parser::AST::Definition
+ @name = :class
- # Are we a child of the passed class? Do a recursive search up our
- # parentage tree to figure it out.
- def child_of?(klass)
- return false unless self.parentclass
+ # Are we a child of the passed class? Do a recursive search up our
+ # parentage tree to figure it out.
+ def child_of?(klass)
+ return false unless self.parentclass
- if klass == self.parentobj
- return true
- else
- return self.parentobj.child_of?(klass)
- end
+ if klass == self.parentobj
+ return true
+ else
+ return self.parentobj.child_of?(klass)
end
+ end
- # Evaluate the code associated with this class.
- def evaluate(options)
- scope = options[:scope]
- raise(ArgumentError, "Classes require resources") unless options[:resource]
- # Verify that we haven't already been evaluated. This is
- # what provides the singleton aspect.
- if existing_scope = scope.compile.class_scope(self)
- Puppet.debug "Class '%s' already evaluated; not evaluating again" % (classname == "" ? "main" : classname)
- return nil
- end
-
- scope.compile.catalog.tag(self.classname)
+ # Make sure our parent class has been evaluated, if we have one.
+ def evaluate(scope)
+ if parentclass and ! scope.catalog.resource(self.class.name, parentclass)
+ resource = parentobj.evaluate(scope)
+ end
- pnames = nil
- if pklass = self.parentobj
- pklass.safeevaluate :scope => scope, :resource => options[:resource]
+ super
+ end
- scope = parent_scope(scope, pklass)
- pnames = scope.namespaces
- end
+ # Evaluate the code associated with this class.
+ def evaluate_code(resource)
+ scope = resource.scope
+ # Verify that we haven't already been evaluated. This is
+ # what provides the singleton aspect.
+ if existing_scope = scope.compiler.class_scope(self)
+ Puppet.debug "Class '%s' already evaluated; not evaluating again" % (classname == "" ? "main" : classname)
+ return nil
+ end
- # Don't create a subscope for the top-level class, since it already
- # has its own scope.
- unless options[:resource].title == :main
- scope = subscope(scope, options[:resource])
- end
+ pnames = nil
+ if pklass = self.parentobj
+ parent_resource = resource.scope.compiler.catalog.resource(self.class.name, pklass.classname)
+ # This shouldn't evaluate if the class has already been evaluated.
+ pklass.evaluate_code(parent_resource)
- if pnames
- pnames.each do |ns|
- scope.add_namespace(ns)
- end
- end
+ scope = parent_scope(scope, pklass)
+ pnames = scope.namespaces
+ end
- # Set the class before we do anything else, so that it's set
- # during the evaluation and can be inspected.
- scope.compile.class_set(self.classname, scope)
+ # Don't create a subscope for the top-level class, since it already
+ # has its own scope.
+ scope = subscope(scope, resource) unless resource.title == :main
- # Now evaluate our code, yo.
- if self.code
- return self.code.evaluate(:scope => scope)
- else
- return nil
+ # Add the parent scope namespaces to our own.
+ if pnames
+ pnames.each do |ns|
+ scope.add_namespace(ns)
end
end
- def initialize(options)
- @parentclass = nil
- super
+ # Set the class before we evaluate the code, so that it's set during
+ # the evaluation and can be inspected.
+ scope.compiler.class_set(self.classname, scope)
+
+ # Now evaluate our code, yo.
+ if self.code
+ return self.code.safeevaluate(scope)
+ else
+ return nil
end
+ end
- def parent_scope(scope, klass)
- if s = scope.compile.class_scope(klass)
- return s
- else
- raise Puppet::DevError, "Could not find scope for %s" % klass.classname
- end
+ def parent_scope(scope, klass)
+ if s = scope.compiler.class_scope(klass)
+ return s
+ else
+ raise Puppet::DevError, "Could not find scope for %s" % klass.classname
end
end
end
diff --git a/lib/puppet/parser/ast/ifstatement.rb b/lib/puppet/parser/ast/ifstatement.rb
index 66a07b01f..afa2cd572 100644
--- a/lib/puppet/parser/ast/ifstatement.rb
+++ b/lib/puppet/parser/ast/ifstatement.rb
@@ -12,15 +12,14 @@ class Puppet::Parser::AST
# Short-curcuit evaluation. If we're true, evaluate our statements,
# else if there's an 'else' setting, evaluate it.
# the first option that matches.
- def evaluate(hash)
- scope = hash[:scope]
- value = @test.safeevaluate(:scope => scope)
+ def evaluate(scope)
+ value = @test.safeevaluate(scope)
if Puppet::Parser::Scope.true?(value)
- return @statements.safeevaluate(:scope => scope)
+ return @statements.safeevaluate(scope)
else
if defined? @else
- return @else.safeevaluate(:scope => scope)
+ return @else.safeevaluate(scope)
else
return nil
end
diff --git a/lib/puppet/parser/ast/leaf.rb b/lib/puppet/parser/ast/leaf.rb
index 225253061..c545c1e47 100644
--- a/lib/puppet/parser/ast/leaf.rb
+++ b/lib/puppet/parser/ast/leaf.rb
@@ -6,7 +6,7 @@ class Puppet::Parser::AST
attr_accessor :value, :type
# Return our value.
- def evaluate(hash)
+ def evaluate(scope)
return @value
end
@@ -35,14 +35,14 @@ class Puppet::Parser::AST
class String < AST::Leaf
# Interpolate the string looking for variables, and then return
# the result.
- def evaluate(hash)
- return hash[:scope].strinterp(@value, @file, @line)
+ def evaluate(scope)
+ return scope.strinterp(@value, @file, @line)
end
end
# An uninterpreted string.
class FlatString < AST::Leaf
- def evaluate(hash)
+ def evaluate(scope)
return @value
end
end
@@ -81,9 +81,9 @@ class Puppet::Parser::AST
class Variable < Name
# Looks up the value of the object in the scope tree (does
# not include syntactical constructs, like '$' and '{}').
- def evaluate(hash)
+ def evaluate(scope)
parsewrap do
- return hash[:scope].lookupvar(@value)
+ return scope.lookupvar(@value)
end
end
end
diff --git a/lib/puppet/parser/ast/node.rb b/lib/puppet/parser/ast/node.rb
index a296e43ba..2bf6c1882 100644
--- a/lib/puppet/parser/ast/node.rb
+++ b/lib/puppet/parser/ast/node.rb
@@ -1,67 +1,35 @@
require 'puppet/parser/ast/hostclass'
-class Puppet::Parser::AST
- # The specific code associated with a host. Nodes are annoyingly unlike
- # other objects. That's just the way it is, at least for now.
- class Node < AST::HostClass
- @name = :node
- attr_accessor :name
-
- def evaluate(options)
- scope = options[:scope]
-
- #pscope = if ! Puppet[:lexical] or options[:asparent]
- # @scope
- #else
- # origscope
- #end
-
- # We don't have to worry about the declarativeness of node parentage,
- # because the entry point is always a single node definition.
- if parent = self.parentobj
- scope = parent.safeevaluate :scope => scope, :resource => options[:resource]
- end
-
- scope = scope.newscope(
- :resource => options[:resource],
- :keyword => @keyword,
- :source => self,
- :namespace => "" # nodes are always in ""
- )
-
- # Mark our node name as a class, too, but strip it of the domain
- # name. Make the mark before we evaluate the code, so that it is
- # marked within the code itself.
- scope.compile.class_set(self.classname, scope)
-
- # And then evaluate our code if we have any
- if self.code
- @code.safeevaluate(:scope => scope)
- end
-
- return scope
+# The specific code associated with a host. Nodes are annoyingly unlike
+# other objects. That's just the way it is, at least for now.
+class Puppet::Parser::AST::Node < Puppet::Parser::AST::HostClass
+ @name = :node
+
+ def initialize(options)
+ @parentclass = nil
+ super
+
+ # Do some validation on the node name
+ if @name =~ /[^-\w.]/
+ raise Puppet::ParseError, "Invalid node name %s" % @name
end
+ end
- def initialize(options)
- @parentclass = nil
- super
+ def namespace
+ ""
+ end
- # Do some validation on the node name
- if @name =~ /[^-\w.]/
- raise Puppet::ParseError, "Invalid node name %s" % @name
- end
- end
+ # Make sure node scopes are marked as such.
+ def subscope(*args)
+ scope = super
+ scope.nodescope = true
+ scope
+ end
- # Make sure node scopes are marked as such.
- def subscope(*args)
- scope = super
- scope.nodescope = true
- end
+ private
- private
- # Search for the object matching our parent class.
- def find_parentclass
- @parser.findnode(parentclass)
- end
+ # Search for the object matching our parent class.
+ def find_parentclass
+ @parser.findnode(parentclass)
end
end
diff --git a/lib/puppet/parser/ast/resource.rb b/lib/puppet/parser/ast/resource.rb
index c53ab0a68..8a60522a3 100644
--- a/lib/puppet/parser/ast/resource.rb
+++ b/lib/puppet/parser/ast/resource.rb
@@ -9,15 +9,13 @@ class Resource < AST::ResourceReference
# Does not actually return an object; instead sets an object
# in the current scope.
- def evaluate(options)
- scope = options[:scope]
-
+ def evaluate(scope)
# Evaluate all of the specified params.
paramobjects = @params.collect { |param|
- param.safeevaluate(:scope => scope)
+ param.safeevaluate(scope)
}
- objtitles = @title.safeevaluate(:scope => scope)
+ objtitles = @title.safeevaluate(scope)
# it's easier to always use an array, even for only one name
unless objtitles.is_a?(Array)
@@ -50,10 +48,10 @@ class Resource < AST::ResourceReference
:scope => scope
)
- # And then store the resource in the compile.
+ # And then store the resource in the compiler.
# At some point, we need to switch all of this to return
# objects instead of storing them like this.
- scope.compile.store_resource(scope, obj)
+ scope.compiler.add_resource(scope, obj)
obj
end
}.reject { |obj| obj.nil? }
diff --git a/lib/puppet/parser/ast/resource_defaults.rb b/lib/puppet/parser/ast/resource_defaults.rb
index 8f9c1b8df..4856f0594 100644
--- a/lib/puppet/parser/ast/resource_defaults.rb
+++ b/lib/puppet/parser/ast/resource_defaults.rb
@@ -8,13 +8,11 @@ class Puppet::Parser::AST
# As opposed to ResourceDef, this stores each default for the given
# object type.
- def evaluate(hash)
- scope = hash[:scope]
-
+ def evaluate(scope)
# Use a resource reference to canonize the type
ref = Puppet::ResourceReference.new(@type, "whatever")
type = ref.type
- params = @params.safeevaluate(:scope => scope)
+ params = @params.safeevaluate(scope)
parsewrap do
scope.setdefaults(type, params)
diff --git a/lib/puppet/parser/ast/resource_override.rb b/lib/puppet/parser/ast/resource_override.rb
index 46c930902..f9464acda 100644
--- a/lib/puppet/parser/ast/resource_override.rb
+++ b/lib/puppet/parser/ast/resource_override.rb
@@ -17,17 +17,15 @@ class Puppet::Parser::AST
# Does not actually return an object; instead sets an object
# in the current scope.
- def evaluate(hash)
- scope = hash[:scope]
-
+ def evaluate(scope)
# Get our object reference.
- object = @object.safeevaluate(:scope => scope)
+ object = @object.safeevaluate(scope)
hash = {}
# Evaluate all of the specified params.
params = @params.collect { |param|
- param.safeevaluate(:scope => scope)
+ param.safeevaluate(scope)
}
# Now we just create a normal resource, but we call a very different
@@ -44,7 +42,7 @@ class Puppet::Parser::AST
# Now we tell the scope that it's an override, and it behaves as
# necessary.
- scope.compile.store_override(obj)
+ scope.compiler.add_override(obj)
obj
end
diff --git a/lib/puppet/parser/ast/resource_reference.rb b/lib/puppet/parser/ast/resource_reference.rb
index b06ea17be..4bb41165a 100644
--- a/lib/puppet/parser/ast/resource_reference.rb
+++ b/lib/puppet/parser/ast/resource_reference.rb
@@ -22,10 +22,8 @@ class Puppet::Parser::AST
# Evaluate our object, but just return a simple array of the type
# and name.
- def evaluate(hash)
- scope = hash[:scope]
-
- title = @title.safeevaluate(:scope => scope)
+ def evaluate(scope)
+ title = @title.safeevaluate(scope)
if @type.to_s.downcase == "class"
objtype = "class"
title = qualified_class(scope, title)
diff --git a/lib/puppet/parser/ast/resourceparam.rb b/lib/puppet/parser/ast/resourceparam.rb
index 8b1e7b367..c552a7ee5 100644
--- a/lib/puppet/parser/ast/resourceparam.rb
+++ b/lib/puppet/parser/ast/resourceparam.rb
@@ -10,12 +10,10 @@ class Puppet::Parser::AST
end
# Return the parameter and the value.
- def evaluate(hash)
- scope = hash[:scope]
-
+ def evaluate(scope)
return Puppet::Parser::Resource::Param.new(
:name => @param,
- :value => @value.safeevaluate(:scope => scope),
+ :value => @value.safeevaluate(scope),
:source => scope.source, :line => self.line, :file => self.file,
:add => self.add
)
diff --git a/lib/puppet/parser/ast/selector.rb b/lib/puppet/parser/ast/selector.rb
index d363ab7e4..399d405a3 100644
--- a/lib/puppet/parser/ast/selector.rb
+++ b/lib/puppet/parser/ast/selector.rb
@@ -11,13 +11,12 @@ class Puppet::Parser::AST
end
# Find the value that corresponds with the test.
- def evaluate(hash)
- scope = hash[:scope]
+ def evaluate(scope)
retvalue = nil
found = nil
# Get our parameter.
- paramvalue = @param.safeevaluate(:scope => scope)
+ paramvalue = @param.safeevaluate(scope)
sensitive = Puppet[:casesensitive]
@@ -33,13 +32,13 @@ class Puppet::Parser::AST
# Then look for a match in the options.
@values.each { |obj|
- param = obj.param.safeevaluate(:scope => scope)
+ param = obj.param.safeevaluate(scope)
if ! sensitive && param.respond_to?(:downcase)
param = param.downcase
end
if param == paramvalue
# we found a matching option
- retvalue = obj.value.safeevaluate(:scope => scope)
+ retvalue = obj.value.safeevaluate(scope)
found = true
break
elsif obj.param.is_a?(Default)
@@ -51,7 +50,7 @@ class Puppet::Parser::AST
# Unless we found something, look for the default.
unless found
if default
- retvalue = default.value.safeevaluate(:scope => scope)
+ retvalue = default.value.safeevaluate(scope)
else
self.fail Puppet::ParseError,
"No matching value for selector param '%s'" % paramvalue
diff --git a/lib/puppet/parser/ast/tag.rb b/lib/puppet/parser/ast/tag.rb
index e2882d2f0..2909504a7 100644
--- a/lib/puppet/parser/ast/tag.rb
+++ b/lib/puppet/parser/ast/tag.rb
@@ -8,10 +8,8 @@ class Puppet::Parser::AST
@name = :class
attr_accessor :type
- def evaluate(hash)
- scope = hash[:scope]
-
- types = @type.safeevaluate(:scope => scope)
+ def evaluate(scope)
+ types = @type.safeevaluate(scope)
types = [types] unless types.is_a? Array
diff --git a/lib/puppet/parser/ast/vardef.rb b/lib/puppet/parser/ast/vardef.rb
index 1e7f874bc..ee79159d7 100644
--- a/lib/puppet/parser/ast/vardef.rb
+++ b/lib/puppet/parser/ast/vardef.rb
@@ -9,10 +9,9 @@ class Puppet::Parser::AST
# Look up our name and value, and store them appropriately. The
# lexer strips off the syntax stuff like '$'.
- def evaluate(hash)
- scope = hash[:scope]
- name = @name.safeevaluate(:scope => scope)
- value = @value.safeevaluate(:scope => scope)
+ def evaluate(scope)
+ name = @name.safeevaluate(scope)
+ value = @value.safeevaluate(scope)
parsewrap do
scope.setvar(name,value, @file, @line)
diff --git a/lib/puppet/parser/collector.rb b/lib/puppet/parser/collector.rb
index b8165a84f..e0c37cd35 100644
--- a/lib/puppet/parser/collector.rb
+++ b/lib/puppet/parser/collector.rb
@@ -118,20 +118,20 @@ class Puppet::Parser::Collector
# If there are no more resources to find, delete this from the list
# of collections.
if @resources.empty?
- @scope.compile.delete_collection(self)
+ @scope.compiler.delete_collection(self)
end
return result
end
- # Collect just virtual objects, from our local compile.
+ # Collect just virtual objects, from our local compiler.
def collect_virtual(exported = false)
if exported
method = :exported?
else
method = :virtual?
end
- scope.compile.resources.find_all do |resource|
+ scope.compiler.resources.find_all do |resource|
resource.type == @type and resource.send(method) and match?(resource)
end
end
@@ -150,7 +150,7 @@ class Puppet::Parser::Collector
resource.exported = false
- scope.compile.store_resource(scope, resource)
+ scope.compiler.add_resource(scope, resource)
return resource
end
diff --git a/lib/puppet/parser/compile.rb b/lib/puppet/parser/compiler.rb
index e1e230d48..132ec15db 100644
--- a/lib/puppet/parser/compile.rb
+++ b/lib/puppet/parser/compiler.rb
@@ -7,7 +7,7 @@ require 'puppet/util/errors'
# Maintain a graph of scopes, along with a bunch of data
# about the individual catalog we're compiling.
-class Puppet::Parser::Compile
+class Puppet::Parser::Compiler
include Puppet::Util
include Puppet::Util::Errors
attr_reader :parser, :node, :facts, :collections, :catalog, :node_scope
@@ -17,6 +17,29 @@ class Puppet::Parser::Compile
@collections << coll
end
+ # Store a resource override.
+ def add_override(override)
+ # If possible, merge the override in immediately.
+ if resource = @catalog.resource(override.ref)
+ resource.merge(override)
+ else
+ # Otherwise, store the override for later; these
+ # get evaluated in Resource#finish.
+ @resource_overrides[override.ref] << override
+ end
+ end
+
+ # Store a resource in our resource table.
+ def add_resource(scope, resource)
+ # Note that this will fail if the resource is not unique.
+ @catalog.add_resource(resource)
+
+ # And in the resource graph. At some point, this might supercede
+ # the global resource table, but the table is a lot faster
+ # so it makes sense to maintain for now.
+ @catalog.add_edge(scope.resource, resource)
+ end
+
# Do we use nodes found in the code, vs. the external node sources?
def ast_nodes?
parser.nodes.length > 0
@@ -26,10 +49,10 @@ class Puppet::Parser::Compile
# the scope in which it was evaluated, so that we can look it up later.
def class_set(name, scope)
if existing = @class_scopes[name]
- if existing.nodescope? or scope.nodescope?
+ if existing.nodescope? != scope.nodescope?
raise Puppet::ParseError, "Cannot have classes, nodes, or definitions with the same name"
else
- raise Puppet::DevError, "Somehow evaluated the same class twice"
+ raise Puppet::DevError, "Somehow evaluated %s %s twice" % [ existing.nodescope? ? "node" : "class", name]
end
end
@class_scopes[name] = scope
@@ -54,7 +77,7 @@ class Puppet::Parser::Compile
return @catalog.classes
end
- # Compile our catalog. This mostly revolves around finding and evaluating classes.
+ # Compiler our catalog. This mostly revolves around finding and evaluating classes.
# This is the main entry into our catalog.
def compile
# Set the client's parameters into the top scope.
@@ -68,10 +91,10 @@ class Puppet::Parser::Compile
evaluate_generators()
- fail_on_unevaluated()
-
finish()
+ fail_on_unevaluated()
+
if Puppet[:storeconfigs]
store()
end
@@ -84,11 +107,6 @@ class Puppet::Parser::Compile
@collections.delete(coll) if @collections.include?(coll)
end
- # LAK:FIXME There are no tests for this.
- def delete_resource(resource)
- @resource_table.delete(resource.ref) if @resource_table.include?(resource.ref)
- end
-
# Return the node's environment.
def environment
unless defined? @environment
@@ -120,16 +138,11 @@ class Puppet::Parser::Compile
if klass = scope.findclass(name)
found << name and next if class_scope(klass)
- # Create a resource to model this class, and then add it to the list
- # of resources.
- resource = Puppet::Parser::Resource.new(:type => "class", :title => klass.classname, :scope => scope, :source => scope.source)
-
- store_resource(scope, resource)
+ resource = klass.evaluate(scope)
# If they've disabled lazy evaluation (which the :include function does),
# then evaluate our resource immediately.
resource.evaluate unless lazy_evaluate
- @catalog.tag(klass.classname)
found << name
else
Puppet.info "Could not find class %s for %s" % [name, node.name]
@@ -140,10 +153,8 @@ class Puppet::Parser::Compile
end
# Return a resource by either its ref or its type and title.
- def findresource(string, name = nil)
- string = "%s[%s]" % [string.capitalize, name] if name
-
- @resource_table[string]
+ def findresource(*args)
+ @catalog.resource(*args)
end
# Set up our compile. We require a parser
@@ -158,7 +169,7 @@ class Puppet::Parser::Compile
begin
send(param.to_s + "=", value)
rescue NoMethodError
- raise ArgumentError, "Compile objects do not accept %s" % param
+ raise ArgumentError, "Compiler objects do not accept %s" % param
end
end
@@ -171,10 +182,10 @@ class Puppet::Parser::Compile
# its parent to the graph.
def newscope(parent, options = {})
parent ||= topscope
- options[:compile] = self
+ options[:compiler] = self
options[:parser] ||= self.parser
scope = Puppet::Parser::Scope.new(options)
- @scope_graph.add_edge!(parent, scope)
+ @scope_graph.add_edge(parent, scope)
scope
end
@@ -195,35 +206,7 @@ class Puppet::Parser::Compile
# Return a list of all resources.
def resources
- @resource_table.values
- end
-
- # Store a resource override.
- def store_override(override)
- override.override = true
-
- # If possible, merge the override in immediately.
- if resource = @resource_table[override.ref]
- resource.merge(override)
- else
- # Otherwise, store the override for later; these
- # get evaluated in Resource#finish.
- @resource_overrides[override.ref] << override
- end
- end
-
- # Store a resource in our resource table.
- def store_resource(scope, resource)
- # This might throw an exception
- verify_uniqueness(resource)
-
- # Store it in the global table.
- @resource_table[resource.ref] = resource
-
- # And in the resource graph. At some point, this might supercede
- # the global resource table, but the table is a lot faster
- # so it makes sense to maintain for now.
- @catalog.add_edge!(scope.resource, resource)
+ @catalog.vertices
end
# The top scope is usually the top-level scope, but if we're using AST nodes,
@@ -250,9 +233,7 @@ class Puppet::Parser::Compile
# Create a resource to model this node, and then add it to the list
# of resources.
- resource = Puppet::Parser::Resource.new(:type => "node", :title => astnode.classname, :scope => topscope, :source => topscope.source)
- store_resource(topscope, resource)
- @catalog.tag(astnode.classname)
+ resource = astnode.evaluate(topscope)
resource.evaluate
@@ -285,11 +266,15 @@ class Puppet::Parser::Compile
def evaluate_definitions
exceptwrap do
if ary = unevaluated_resources
+ evaluated = false
ary.each do |resource|
- resource.evaluate
+ if not resource.virtual?
+ resource.evaluate
+ evaluated = true
+ end
end
# If we evaluated, let the loop know.
- return true
+ return evaluated
else
return false
end
@@ -325,9 +310,7 @@ class Puppet::Parser::Compile
@main_resource = Puppet::Parser::Resource.new(:type => "class", :title => :main, :scope => @topscope, :source => @main)
@topscope.resource = @main_resource
- @catalog.add_vertex!(@main_resource)
-
- @resource_table["Class[main]"] = @main_resource
+ @catalog.add_resource(@main_resource)
@main_resource.evaluate
end
@@ -383,14 +366,29 @@ class Puppet::Parser::Compile
# Make sure all of our resources and such have done any last work
# necessary.
def finish
- @resource_table.each { |name, resource| resource.finish if resource.respond_to?(:finish) }
+ @catalog.resources.each do |name|
+ resource = @catalog.resource(name)
+
+ # Add in any resource overrides.
+ if overrides = resource_overrides(resource)
+ overrides.each do |over|
+ resource.merge(over)
+ end
+
+ # Remove the overrides, so that the configuration knows there
+ # are none left.
+ overrides.clear
+ end
+
+ resource.finish if resource.respond_to?(:finish)
+ end
end
# Initialize the top-level scope, class, and resource.
def init_main
# Create our initial scope and a resource that will evaluate main.
- @topscope = Puppet::Parser::Scope.new(:compile => self, :parser => self.parser)
- @scope_graph.add_vertex!(@topscope)
+ @topscope = Puppet::Parser::Scope.new(:compiler => self, :parser => self.parser)
+ @scope_graph.add_vertex(@topscope)
end
# Set up all of our internal variables.
@@ -399,9 +397,6 @@ class Puppet::Parser::Compile
# be used by top scopes and node scopes.
@class_scopes = {}
- # The table for all defined resources.
- @resource_table = {}
-
# The list of objects that will available for export.
@exported_resources = {}
@@ -415,9 +410,6 @@ class Puppet::Parser::Compile
# but they each refer back to the scope that created them.
@collections = []
- # A list of tags we've generated; most class names.
- @tags = []
-
# A graph for maintaining scope relationships.
@scope_graph = Puppet::SimpleGraph.new
@@ -446,7 +438,7 @@ class Puppet::Parser::Compile
# We used to have hooks here for forking and saving, but I don't
# think it's worth retaining at this point.
- store_to_active_record(@node, @resource_table.values)
+ store_to_active_record(@node, @catalog.vertices)
end
# Do the actual storage.
@@ -469,9 +461,7 @@ class Puppet::Parser::Compile
# Return an array of all of the unevaluated resources. These will be definitions,
# which need to get evaluated into native resources.
def unevaluated_resources
- ary = @resource_table.find_all do |name, object|
- ! object.builtin? and ! object.evaluated?
- end.collect { |name, object| object }
+ ary = @catalog.vertices.reject { |resource| resource.builtin? or resource.evaluated? }
if ary.empty?
return nil
@@ -479,33 +469,4 @@ class Puppet::Parser::Compile
return ary
end
end
-
- # Verify that the given resource isn't defined elsewhere.
- def verify_uniqueness(resource)
- # Short-curcuit the common case,
- unless existing_resource = @resource_table[resource.ref]
- return true
- end
-
- if typeclass = Puppet::Type.type(resource.type) and ! typeclass.isomorphic?
- Puppet.info "Allowing duplicate %s" % typeclass.name
- return true
- end
-
- # Either it's a defined type, which are never
- # isomorphic, or it's a non-isomorphic type, so
- # we should throw an exception.
- msg = "Duplicate definition: %s is already defined" % resource.ref
-
- if existing_resource.file and existing_resource.line
- msg << " in file %s at line %s" %
- [existing_resource.file, existing_resource.line]
- end
-
- if resource.line or resource.file
- msg << "; cannot redefine"
- end
-
- raise Puppet::ParseError.new(msg)
- end
end
diff --git a/lib/puppet/parser/functions.rb b/lib/puppet/parser/functions.rb
index 34b38b809..e0b60e161 100644
--- a/lib/puppet/parser/functions.rb
+++ b/lib/puppet/parser/functions.rb
@@ -111,7 +111,7 @@ module Functions
vals = [vals] unless vals.is_a?(Array)
# The 'false' disables lazy evaluation.
- klasses = compile.evaluate_classes(vals, self, false)
+ klasses = compiler.evaluate_classes(vals, self, false)
missing = vals.find_all do |klass|
! klasses.include?(klass)
@@ -146,7 +146,7 @@ module Functions
tells you whether the current container is tagged with the specified tags.
The tags are ANDed, so that all of the specified tags must be included for
the function to return true.") do |vals|
- configtags = compile.catalog.tags
+ configtags = compiler.catalog.tags
resourcetags = resource.tags
retval = true
@@ -235,7 +235,7 @@ module Functions
vals = [vals] unless vals.is_a?(Array)
coll.resources = vals
- compile.add_collection(coll)
+ compiler.add_collection(coll)
end
newfunction(:search, :doc => "Add another namespace for this class to search.
diff --git a/lib/puppet/parser/interpreter.rb b/lib/puppet/parser/interpreter.rb
index e29e19944..1d93193dd 100644
--- a/lib/puppet/parser/interpreter.rb
+++ b/lib/puppet/parser/interpreter.rb
@@ -3,7 +3,7 @@ require 'timeout'
require 'puppet/rails'
require 'puppet/util/methodhelper'
require 'puppet/parser/parser'
-require 'puppet/parser/compile'
+require 'puppet/parser/compiler'
require 'puppet/parser/scope'
# The interpreter is a very simple entry-point class that
@@ -25,7 +25,7 @@ class Puppet::Parser::Interpreter
# evaluate our whole tree
def compile(node)
raise Puppet::ParseError, "Could not parse configuration; cannot compile" unless env_parser = parser(node.environment)
- return Puppet::Parser::Compile.new(node, env_parser).compile
+ return Puppet::Parser::Compiler.new(node, env_parser).compile
end
# create our interpreter
diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb
index 58667727f..fb0799011 100644
--- a/lib/puppet/parser/resource.rb
+++ b/lib/puppet/parser/resource.rb
@@ -58,8 +58,7 @@ class Puppet::Parser::Resource
def evaluate
if klass = @ref.definedtype
finish()
- scope.compile.delete_resource(self)
- return klass.evaluate(:scope => scope, :resource => self)
+ return klass.evaluate_code(self)
elsif builtin?
devfail "Cannot evaluate a builtin type"
else
@@ -83,7 +82,6 @@ class Puppet::Parser::Resource
# Do any finishing work on this object, called before evaluation or
# before storage/translation.
def finish
- add_overrides()
add_defaults()
add_metaparams()
add_scope_tags()
@@ -342,19 +340,6 @@ class Puppet::Parser::Resource
end
end
- # Add any overrides for this object.
- def add_overrides
- if overrides = scope.compile.resource_overrides(self)
- overrides.each do |over|
- self.merge(over)
- end
-
- # Remove the overrides, so that the configuration knows there
- # are none left.
- overrides.clear
- end
- end
-
def add_scope_tags
if scope_resource = scope.resource
tag(*scope_resource.tags)
diff --git a/lib/puppet/parser/resource/param.rb b/lib/puppet/parser/resource/param.rb
index 9352311d6..9dd3f26d2 100644
--- a/lib/puppet/parser/resource/param.rb
+++ b/lib/puppet/parser/resource/param.rb
@@ -12,7 +12,7 @@ class Puppet::Parser::Resource::Param
end
def inspect
- "#<#{self.class} @name => #{self.name}, @value => #{self.value}, @source => #{self.source.name}>"
+ "#<#{self.class} @name => #{name}, @value => #{value}, @source => #{source.name}>"
end
def line_to_i
diff --git a/lib/puppet/parser/resource/reference.rb b/lib/puppet/parser/resource/reference.rb
index 6e70d23b7..c59748049 100644
--- a/lib/puppet/parser/resource/reference.rb
+++ b/lib/puppet/parser/resource/reference.rb
@@ -37,10 +37,14 @@ class Puppet::Parser::Resource::Reference < Puppet::ResourceReference
if self.title == :main
tmp = @scope.findclass("")
else
- tmp = @scope.findclass(self.title)
+ unless tmp = @scope.findclass(self.title)
+ fail Puppet::ParseError, "Could not find class '%s'" % self.title
+ end
end
when "Node": # look for node definitions
- tmp = @scope.parser.nodes[self.title]
+ unless tmp = @scope.parser.nodes[self.title]
+ fail Puppet::ParseError, "Could not find node '%s'" % self.title
+ end
else # normal definitions
# We have to swap these variables around so the errors are right.
tmp = @scope.finddefine(self.type)
@@ -49,7 +53,7 @@ class Puppet::Parser::Resource::Reference < Puppet::ResourceReference
if tmp
@definedtype = tmp
else
- fail Puppet::ParseError, "Could not find resource '%s'" % self
+ fail Puppet::ParseError, "Could not find resource type '%s'" % self.type
end
end
diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb
index 028414cc0..a6e43e7b3 100644
--- a/lib/puppet/parser/scope.rb
+++ b/lib/puppet/parser/scope.rb
@@ -17,15 +17,20 @@ class Puppet::Parser::Scope
include Puppet::Util::Errors
attr_accessor :parent, :level, :parser, :source, :resource
attr_accessor :base, :keyword, :nodescope
- attr_accessor :top, :translated, :compile
+ attr_accessor :top, :translated, :compiler
+
+ # A demeterific shortcut to the catalog.
+ def catalog
+ compiler.catalog
+ end
# Proxy accessors
def host
- @compile.node.name
+ @compiler.node.name
end
def interpreter
- @compile.interpreter
+ @compiler.interpreter
end
# Is the value true? This allows us to control the definition of truth
@@ -72,7 +77,7 @@ class Puppet::Parser::Scope
end
def findresource(string, name = nil)
- compile.findresource(string, name)
+ compiler.findresource(string, name)
end
# Initialize our new scope. Defaults to having no parent.
@@ -147,7 +152,7 @@ class Puppet::Parser::Scope
unless klass
raise Puppet::ParseError, "Could not find class %s" % klassname
end
- unless kscope = compile.class_scope(klass)
+ unless kscope = compiler.class_scope(klass)
raise Puppet::ParseError, "Class %s has not been evaluated so its variables cannot be referenced" % klass.classname
end
return kscope.lookupvar(shortname, usestring)
@@ -184,7 +189,7 @@ class Puppet::Parser::Scope
# Create a new scope and set these options.
def newscope(options = {})
- compile.newscope(self, options)
+ compiler.newscope(self, options)
end
# Is this class for a node? This is used to make sure that
@@ -199,7 +204,7 @@ class Puppet::Parser::Scope
# than doing lots of queries.
def parent
unless defined?(@parent)
- @parent = compile.parent(self)
+ @parent = compiler.parent(self)
end
@parent
end
diff --git a/lib/puppet/parser/templatewrapper.rb b/lib/puppet/parser/templatewrapper.rb
index 13823d483..7a8f74156 100644
--- a/lib/puppet/parser/templatewrapper.rb
+++ b/lib/puppet/parser/templatewrapper.rb
@@ -7,7 +7,7 @@ class Puppet::Parser::TemplateWrapper
def initialize(scope, file)
@scope = scope
- @file = Puppet::Module::find_template(file, @scope.compile.environment)
+ @file = Puppet::Module::find_template(file, @scope.compiler.environment)
unless FileTest.exists?(@file)
raise Puppet::ParseError,
diff --git a/lib/puppet/pgraph.rb b/lib/puppet/pgraph.rb
index 54b815b45..71547802e 100644
--- a/lib/puppet/pgraph.rb
+++ b/lib/puppet/pgraph.rb
@@ -7,17 +7,14 @@ require 'puppet/simple_graph'
# This class subclasses a graph class in order to handle relationships
# among resources.
class Puppet::PGraph < Puppet::SimpleGraph
- # This is the type used for splicing.
- attr_accessor :container_type
-
include Puppet::Util
- def add_edge!(*args)
+ def add_edge(*args)
@reversal = nil
super
end
- def add_vertex!(*args)
+ def add_vertex(*args)
@reversal = nil
super
end
@@ -57,11 +54,6 @@ class Puppet::PGraph < Puppet::SimpleGraph
@reversal.tree_from_vertex(resource, :out).keys
end
- # Override this method to use our class instead.
- def edge_class()
- Puppet::Relationship
- end
-
# Determine all of the leaf nodes below a given vertex.
def leaves(vertex, direction = :out)
tree = tree_from_vertex(vertex, direction)
@@ -133,7 +125,7 @@ class Puppet::PGraph < Puppet::SimpleGraph
copy_label(s, t, edge.label)
next
end
- add_edge!(s, t, edge.label)
+ add_edge(s, t, edge.label)
end
# Now get rid of the edge, so remove_vertex! works correctly.
diff --git a/lib/puppet/simple_graph.rb b/lib/puppet/simple_graph.rb
index 11542ad53..503e4814c 100644
--- a/lib/puppet/simple_graph.rb
+++ b/lib/puppet/simple_graph.rb
@@ -100,10 +100,10 @@ class Puppet::SimpleGraph
# Return a reversed version of this graph.
def reversal
result = self.class.new
- vertices.each { |vertex| result.add_vertex!(vertex) }
+ vertices.each { |vertex| result.add_vertex(vertex) }
edges.each do |edge|
newedge = edge.class.new(edge.target, edge.source, edge.label)
- result.add_edge!(newedge)
+ result.add_edge(newedge)
end
result
end
@@ -150,7 +150,7 @@ class Puppet::SimpleGraph
end
# Add a new vertex to the graph.
- def add_vertex!(vertex)
+ def add_vertex(vertex)
return false if vertex?(vertex)
setup_vertex(vertex)
true # don't return the VertexWrapper instance.
@@ -176,7 +176,7 @@ class Puppet::SimpleGraph
# Add a new edge. The graph user has to create the edge instance,
# since they have to specify what kind of edge it is.
- def add_edge!(source, target = nil, label = nil)
+ def add_edge(source, target = nil, label = nil)
if target
edge = Puppet::Relationship.new(source, target, label)
else
diff --git a/lib/puppet/transaction.rb b/lib/puppet/transaction.rb
index f304cadc6..976bf7c68 100644
--- a/lib/puppet/transaction.rb
+++ b/lib/puppet/transaction.rb
@@ -173,7 +173,7 @@ class Transaction
relationship_graph.add_resource(gen_child) unless relationship_graph.resource(gen_child.ref)
unless relationship_graph.edge?(edge[1], edge[0])
- relationship_graph.add_edge!(*edge)
+ relationship_graph.add_edge(*edge)
else
resource.debug "Skipping automatic relationship to %s" % gen_child
end
diff --git a/lib/puppet/transportable.rb b/lib/puppet/transportable.rb
index c1d68a881..f686fbb78 100644
--- a/lib/puppet/transportable.rb
+++ b/lib/puppet/transportable.rb
@@ -193,7 +193,7 @@ module Puppet
next unless resource = child.to_type
config.add_resource resource
end
- config.add_edge!(container, resource)
+ config.add_edge(container, resource)
if child.is_a?(self.class)
delver.call(child)
end
diff --git a/lib/puppet/type/pfile.rb b/lib/puppet/type/pfile.rb
index 7d928d959..c32a4d474 100644
--- a/lib/puppet/type/pfile.rb
+++ b/lib/puppet/type/pfile.rb
@@ -650,7 +650,7 @@ module Puppet
# LAK:FIXME This shouldn't be necessary, but as long as we're
# modeling the relationship graph specifically, it is.
- catalog.relationship_graph.add_edge! self, child
+ catalog.relationship_graph.add_edge self, child
return child
end
diff --git a/lib/puppet/util/constant_inflector.rb b/lib/puppet/util/constant_inflector.rb
new file mode 100644
index 000000000..8b083951f
--- /dev/null
+++ b/lib/puppet/util/constant_inflector.rb
@@ -0,0 +1,14 @@
+# Created on 2008-02-12
+# Copyright Luke Kanies
+
+# A common module for converting between constants and
+# file names.
+module Puppet::Util::ConstantInflector
+ def file2constant(file)
+ file.split("/").collect { |name| name.capitalize }.join("::").gsub(/_+(.)/) { |term| $1.capitalize }
+ end
+
+ def constant2file(constant)
+ constant.to_s.gsub(/([a-z])([A-Z])/) { |term| $1 + "_" + $2 }.gsub("::", "/").downcase
+ end
+end
diff --git a/lib/puppet/util/graph.rb b/lib/puppet/util/graph.rb
index a9744578b..d1ef36f8e 100644
--- a/lib/puppet/util/graph.rb
+++ b/lib/puppet/util/graph.rb
@@ -16,7 +16,7 @@ module Puppet::Util::Graph
self.each do |child|
unless block_given? and ! yield(child)
- graph.add_edge!(self, child)
+ graph.add_edge(self, child)
if child.respond_to?(:to_graph)
child.to_graph(graph, &block)
diff --git a/lib/puppet/util/tagging.rb b/lib/puppet/util/tagging.rb
index 25d74c420..9abb3fb2b 100644
--- a/lib/puppet/util/tagging.rb
+++ b/lib/puppet/util/tagging.rb
@@ -19,6 +19,11 @@ module Puppet::Util::Tagging
qualified.collect { |name| name.split("::") }.flatten.each { |tag| @tags << tag unless @tags.include?(tag) }
end
+ # Are we tagged with the provided tag?
+ def tagged?(tag)
+ defined?(@tags) and @tags.include?(tag.to_s)
+ end
+
# Return a copy of the tag list, so someone can't ask for our tags
# and then modify them.
def tags