summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser
diff options
context:
space:
mode:
Diffstat (limited to 'lib/puppet/parser')
-rw-r--r--lib/puppet/parser/ast/leaf.rb9
-rw-r--r--lib/puppet/parser/compiler.rb14
-rw-r--r--lib/puppet/parser/functions.rb5
-rw-r--r--lib/puppet/parser/functions/extlookup.rb12
-rw-r--r--lib/puppet/parser/functions/fqdn_rand.rb2
-rw-r--r--lib/puppet/parser/functions/versioncmp.rb6
-rw-r--r--lib/puppet/parser/resource.rb5
-rw-r--r--lib/puppet/parser/scope.rb75
-rw-r--r--lib/puppet/parser/templatewrapper.rb7
9 files changed, 79 insertions, 56 deletions
diff --git a/lib/puppet/parser/ast/leaf.rb b/lib/puppet/parser/ast/leaf.rb
index c8ebc9483..3efb52f63 100644
--- a/lib/puppet/parser/ast/leaf.rb
+++ b/lib/puppet/parser/ast/leaf.rb
@@ -124,10 +124,11 @@ class Puppet::Parser::AST
# not include syntactical constructs, like '$' and '{}').
def evaluate(scope)
parsewrap do
- if (var = scope.lookupvar(@value, :file => file, :line => line)) == :undefined
- var = :undef
+ if ! scope.include?(@value)
+ :undef
+ else
+ scope[@value, {:file => file, :line => line}]
end
- var
end
end
@@ -141,7 +142,7 @@ class Puppet::Parser::AST
def evaluate_container(scope)
container = variable.respond_to?(:evaluate) ? variable.safeevaluate(scope) : variable
- (container.is_a?(Hash) or container.is_a?(Array)) ? container : scope.lookupvar(container, :file => file, :line => line)
+ (container.is_a?(Hash) or container.is_a?(Array)) ? container : scope[container, {:file => file, :line => line}]
end
def evaluate_key(scope)
diff --git a/lib/puppet/parser/compiler.rb b/lib/puppet/parser/compiler.rb
index c1daade4c..06cd80a1e 100644
--- a/lib/puppet/parser/compiler.rb
+++ b/lib/puppet/parser/compiler.rb
@@ -139,19 +139,21 @@ class Puppet::Parser::Compiler
# 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
- param_classes = nil
+ class_parameters = nil
# if we are a param class, save the classes hash
# and transform classes to be the keys
if classes.class == Hash
- param_classes = classes
+ class_parameters = classes
classes = classes.keys
end
classes.each do |name|
# If we can find the class, then make a resource that will evaluate it.
if klass = scope.find_hostclass(name)
- if param_classes
- resource = klass.ensure_in_catalog(scope, param_classes[name] || {})
+ # If parameters are passed, then attempt to create a duplicate resource
+ # so the appropriate error is thrown.
+ if class_parameters
+ resource = klass.ensure_in_catalog(scope, class_parameters[name] || {})
else
next if scope.class_scope(klass)
resource = klass.ensure_in_catalog(scope)
@@ -450,7 +452,7 @@ class Puppet::Parser::Compiler
# Set the node's parameters into the top-scope as variables.
def set_node_parameters
node.parameters.each do |param, value|
- @topscope.setvar(param, value)
+ @topscope[param] = value
end
# These might be nil.
@@ -473,7 +475,7 @@ class Puppet::Parser::Compiler
Puppet.settings.each do |name, setting|
next if name.to_s == "name"
- scope.setvar name.to_s, environment[name]
+ scope[name.to_s] = environment[name]
end
end
diff --git a/lib/puppet/parser/functions.rb b/lib/puppet/parser/functions.rb
index e19ac127f..22eee70d7 100644
--- a/lib/puppet/parser/functions.rb
+++ b/lib/puppet/parser/functions.rb
@@ -29,8 +29,11 @@ module Puppet::Parser::Functions
Environment = Puppet::Node::Environment
def self.environment_module(env = nil)
+ if env and ! env.is_a?(Puppet::Node::Environment)
+ env = Puppet::Node::Environment.new(env)
+ end
@modules.synchronize {
- @modules[ env || Environment.current || Environment.root ] ||= Module.new
+ @modules[ (env || Environment.current || Environment.root).name ] ||= Module.new
}
end
diff --git a/lib/puppet/parser/functions/extlookup.rb b/lib/puppet/parser/functions/extlookup.rb
index 5fbf26cec..9ffca59a7 100644
--- a/lib/puppet/parser/functions/extlookup.rb
+++ b/lib/puppet/parser/functions/extlookup.rb
@@ -91,9 +91,9 @@ This is for back compatibility to interpolate variables with %. % interpolation
raise Puppet::ParseError, ("extlookup(): wrong number of arguments (#{args.length}; must be <= 3)") if args.length > 3
- extlookup_datadir = undef_as('',lookupvar('::extlookup_datadir'))
+ extlookup_datadir = undef_as('',self['::extlookup_datadir'])
- extlookup_precedence = undef_as([],lookupvar('::extlookup_precedence')).collect { |var| var.gsub(/%\{(.+?)\}/) { lookupvar("::#{$1}") } }
+ extlookup_precedence = undef_as([],self['::extlookup_precedence']).collect { |var| var.gsub(/%\{(.+?)\}/) { self["::#{$1}"] } }
datafiles = Array.new
@@ -121,9 +121,9 @@ This is for back compatibility to interpolate variables with %. % interpolation
if result[0].length == 2
val = result[0][1].to_s
- # parse %{}'s in the CSV into local variables using lookupvar()
+ # parse %{}'s in the CSV into local variables using the current scope
while val =~ /%\{(.+?)\}/
- val.gsub!(/%\{#{$1}\}/, lookupvar($1))
+ val.gsub!(/%\{#{$1}\}/, self[$1])
end
desired = val
@@ -134,9 +134,9 @@ This is for back compatibility to interpolate variables with %. % interpolation
# Individual cells in a CSV result are a weird data type and throws
# puppets yaml parsing, so just map it all to plain old strings
desired = cells.map do |c|
- # parse %{}'s in the CSV into local variables using lookupvar()
+ # parse %{}'s in the CSV into local variables using the current scope
while c =~ /%\{(.+?)\}/
- c.gsub!(/%\{#{$1}\}/, lookupvar($1))
+ c.gsub!(/%\{#{$1}\}/, self[$1])
end
c.to_s
diff --git a/lib/puppet/parser/functions/fqdn_rand.rb b/lib/puppet/parser/functions/fqdn_rand.rb
index 93ab98bcd..668802e73 100644
--- a/lib/puppet/parser/functions/fqdn_rand.rb
+++ b/lib/puppet/parser/functions/fqdn_rand.rb
@@ -7,6 +7,6 @@ Puppet::Parser::Functions::newfunction(:fqdn_rand, :type => :rvalue, :doc =>
$random_number_seed = fqdn_rand(30,30)") do |args|
require 'digest/md5'
max = args.shift
- srand(Digest::MD5.hexdigest([lookupvar('::fqdn'),args].join(':')).hex)
+ srand(Digest::MD5.hexdigest([self['::fqdn'],args].join(':')).hex)
rand(max).to_s
end
diff --git a/lib/puppet/parser/functions/versioncmp.rb b/lib/puppet/parser/functions/versioncmp.rb
index 6091e0923..e4edb151e 100644
--- a/lib/puppet/parser/functions/versioncmp.rb
+++ b/lib/puppet/parser/functions/versioncmp.rb
@@ -1,10 +1,8 @@
require 'puppet/util/package'
- Puppet::Parser::Functions::newfunction(
- :versioncmp, :type => :rvalue,
-
- :doc => "Compares two versions
+Puppet::Parser::Functions::newfunction( :versioncmp, :type => :rvalue,
+:doc => "Compares two versions
Prototype:
diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb
index 3bb5f8601..56887c357 100644
--- a/lib/puppet/parser/resource.rb
+++ b/lib/puppet/parser/resource.rb
@@ -173,7 +173,7 @@ class Puppet::Parser::Resource < Puppet::Resource
:name => param, :value => value, :source => self.source
)
elsif ! param.is_a?(Puppet::Parser::Resource::Param)
- raise ArgumentError, "Must pass a parameter or all necessary values"
+ raise ArgumentError, "Received incomplete information - no value provided for parameter #{param}"
end
tag(*param.value) if param.name == :tag
@@ -258,7 +258,8 @@ class Puppet::Parser::Resource < Puppet::Resource
def add_backward_compatible_relationship_param(name)
# Skip metaparams for which we get no value.
- return unless val = scope.lookupvar(name.to_s) and val != :undefined
+ return unless scope.include?(name.to_s)
+ val = scope[name.to_s]
# The default case: just set the value
set_parameter(name, val) and return unless @parameters[name]
diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb
index ed67cd141..9d84c7e65 100644
--- a/lib/puppet/parser/scope.rb
+++ b/lib/puppet/parser/scope.rb
@@ -48,13 +48,42 @@ class Puppet::Parser::Scope
end
end
+ def [](name, options = {})
+ table = ephemeral?(name) ? @ephemeral.last : @symtable
+ # If the variable is qualified, then find the specified scope and look the variable up there instead.
+ if name =~ /^(.*)::(.+)$/
+ begin
+ qualified_scope($1)[$2,options]
+ rescue RuntimeError => e
+ location = (options[:file] && options[:line]) ? " at #{options[:file]}:#{options[:line]}" : ''
+ warning "Could not look up qualified variable '#{name}'; #{e.message}#{location}"
+ nil
+ end
+ elsif ephemeral_include?(name) or table.include?(name)
+ # We can't use "if table[name]" here because the value might be false
+ if options[:dynamic] and self != compiler.topscope
+ location = (options[:file] && options[:line]) ? " at #{options[:file]}:#{options[:line]}" : ''
+ Puppet.deprecation_warning "Dynamic lookup of $#{name}#{location} is deprecated. Support will be removed in Puppet 2.8. Use a fully-qualified variable name (e.g., $classname::variable) or parameterized classes."
+ end
+ table[name]
+ elsif parent
+ parent[name,options.merge(:dynamic => (dynamic || options[:dynamic]))]
+ else
+ nil
+ end
+ end
+
+ def []=(var, value)
+ setvar(var, value)
+ end
+
# A demeterific shortcut to the catalog.
def catalog
compiler.catalog
end
- def environment
- compiler.environment
+ def each
+ to_hash.each { |name, value| yield(name, value) }
end
# Proxy accessors
@@ -62,6 +91,10 @@ class Puppet::Parser::Scope
@compiler.node.name
end
+ def include?(name)
+ ! self[name].nil?
+ end
+
# Is the value true? This allows us to control the definition of truth
# in one place.
def self.true?(value)
@@ -101,7 +134,7 @@ class Puppet::Parser::Scope
# Remove this when rebasing
def environment
- compiler ? compiler.environment : nil
+ compiler ? compiler.environment : Puppet::Node::Environment.new
end
def find_hostclass(name)
@@ -211,7 +244,11 @@ class Puppet::Parser::Scope
end
def undef_as(x,v)
- (v == :undefined) ? x : (v == :undef) ? x : v
+ if v.nil? or v == :undef
+ x
+ else
+ v
+ end
end
def qualified_scope(classname)
@@ -223,29 +260,9 @@ class Puppet::Parser::Scope
private :qualified_scope
# Look up a variable. The simplest value search we do.
+ # This method is effectively deprecated - use self[] instead.
def lookupvar(name, options = {})
- table = ephemeral?(name) ? @ephemeral.last : @symtable
- # If the variable is qualified, then find the specified scope and look the variable up there instead.
- if name =~ /^(.*)::(.+)$/
- begin
- qualified_scope($1).lookupvar($2,options)
- rescue RuntimeError => e
- location = (options[:file] && options[:line]) ? " at #{options[:file]}:#{options[:line]}" : ''
- warning "Could not look up qualified variable '#{name}'; #{e.message}#{location}"
- :undefined
- end
- elsif ephemeral_include?(name) or table.include?(name)
- # We can't use "if table[name]" here because the value might be false
- if options[:dynamic] and self != compiler.topscope
- location = (options[:file] && options[:line]) ? " at #{options[:file]}:#{options[:line]}" : ''
- Puppet.deprecation_warning "Dynamic lookup of $#{name}#{location} is deprecated. Support will be removed in Puppet 2.8. Use a fully-qualified variable name (e.g., $classname::variable) or parameterized classes."
- end
- table[name]
- elsif parent
- parent.lookupvar(name,options.merge(:dynamic => (dynamic || options[:dynamic])))
- else
- :undefined
- end
+ self[name, options]
end
# Return a hash containing our variables and their values, optionally (and
@@ -312,6 +329,8 @@ class Puppet::Parser::Scope
# Set a variable in the current scope. This will override settings
# in scopes above, but will not allow variables in the current scope
# to be reassigned.
+ # It's preferred that you use self[]= instead of this; only use this
+ # when you need to set options.
def setvar(name,value, options = {})
table = options[:ephemeral] ? @ephemeral.last : @symtable
if table.include?(name)
@@ -329,7 +348,7 @@ class Puppet::Parser::Scope
table[name] = value
else # append case
# lookup the value in the scope if it exists and insert the var
- table[name] = undef_as('',lookupvar(name))
+ table[name] = undef_as('',self[name])
# concatenate if string, append if array, nothing for other types
case value
when Array
@@ -443,6 +462,6 @@ class Puppet::Parser::Scope
def extend_with_functions_module
extend Puppet::Parser::Functions.environment_module(Puppet::Node::Environment.root)
- extend Puppet::Parser::Functions.environment_module(environment)
+ extend Puppet::Parser::Functions.environment_module(environment) if environment != Puppet::Node::Environment.root
end
end
diff --git a/lib/puppet/parser/templatewrapper.rb b/lib/puppet/parser/templatewrapper.rb
index 27d75bf92..9336e704d 100644
--- a/lib/puppet/parser/templatewrapper.rb
+++ b/lib/puppet/parser/templatewrapper.rb
@@ -25,7 +25,7 @@ class Puppet::Parser::TemplateWrapper
# Should return true if a variable is defined, false if it is not
def has_variable?(name)
- scope.lookupvar(name.to_s, :file => file, :line => script_line) != :undefined
+ scope.include?(name.to_s)
end
# Allow templates to access the defined classes
@@ -56,9 +56,8 @@ class Puppet::Parser::TemplateWrapper
# the missing_method definition here until we declare the syntax finally
# dead.
def method_missing(name, *args)
- value = scope.lookupvar(name.to_s,:file => file,:line => script_line)
- if value != :undefined
- return value
+ if scope.include?(name.to_s)
+ return scope[name.to_s, {:file => file,:line => script_line}]
else
# Just throw an error immediately, instead of searching for
# other missingmethod things or whatever.