diff options
-rw-r--r-- | lib/puppet.rb | 4 | ||||
-rw-r--r-- | lib/puppet/parser/ast.rb | 101 | ||||
-rw-r--r-- | lib/puppet/parser/grammar.ra | 8 | ||||
-rw-r--r-- | lib/puppet/parser/parser.rb | 242 | ||||
-rw-r--r-- | lib/puppet/parser/scope.rb | 113 | ||||
-rwxr-xr-x | test/language/tc_ast.rb | 62 | ||||
-rwxr-xr-x | test/language/tc_scope.rb | 4 |
7 files changed, 313 insertions, 221 deletions
diff --git a/lib/puppet.rb b/lib/puppet.rb index 9d2f6d4c7..0679f477f 100644 --- a/lib/puppet.rb +++ b/lib/puppet.rb @@ -29,10 +29,10 @@ module Puppet def to_s str = nil - if defined? @file and defined? @line + if defined? @file and defined? @line and @file and @line str = "%s in file %s at line %s" % [@message, @file, @line] - elsif defined? @line + elsif defined? @line and @line str = "%s at line %s" % [@message, @line] else diff --git a/lib/puppet/parser/ast.rb b/lib/puppet/parser/ast.rb index 5d1b5c47d..7fa1a156d 100644 --- a/lib/puppet/parser/ast.rb +++ b/lib/puppet/parser/ast.rb @@ -1208,7 +1208,7 @@ module Puppet # specified node, and this parse tree is only ever looked up when # a client connects. class NodeDef < AST::Branch - attr_accessor :names, :code + attr_accessor :names, :code, :parentclass def each [@names,@code].each { |child| yield child } @@ -1221,15 +1221,21 @@ module Puppet unless names.is_a?(Array) names = [names] end - Puppet.debug("defining hosts '%s'" % [names.join(", ")]) - + names.each { |name| + Puppet.debug("defining host '%s'" % name) + arghash = { + :name => name, + :code => @code + } + + if @parentclass + arghash[:parentclass] = @parentclass.safeevaluate(scope) + end + begin scope.setnode(name, - Node.new( - :name => name, - :code => @code - ) + Node.new(arghash) ) rescue Puppet::ParseError => except except.line = self.line @@ -1245,16 +1251,21 @@ module Puppet } end + def initialize(hash) + @parentclass = nil + super + end + def tree(indent = 0) return [ @names.tree(indent + 1), - ((@@indline * 4 * indent) + self.typewrap("host")), + ((@@indline * 4 * indent) + self.typewrap("node")), @code.tree(indent + 1), ].join("\n") end def to_s - return "host %s {\n%s }" % [@name, @code] + return "node %s {\n%s }" % [@name, @code] end end @@ -1333,7 +1344,24 @@ module Puppet Puppet.debug "%s class already evaluated" % @name return nil end + + self.evalparent(scope, hash, objname) + + # just use the Component evaluate method, but change the type + # to our own type + retval = super(scope,hash,@name,objname) + + # Set the mark after we evaluate, so we don't record it but + # then encounter an error + scope.setclass(@name) + return retval + end + + # Evaluate our parent class. + def evalparent(scope, args, name) if @parentclass + parentobj = nil + begin parentobj = scope.lookuptype(@parentclass) rescue Puppet::ParseError => except @@ -1365,17 +1393,8 @@ module Puppet error.line = self.line raise error end - parentobj.safeevaluate(scope,hash,@parentclass,objname) + parentobj.safeevaluate(scope,args,@parentclass,name) end - - # just use the Component evaluate method, but change the type - # to our own type - retval = super(scope,hash,@name,objname) - - # Set the mark after we evaluate, so we don't record it but - # then encounter an error - scope.setclass(@name) - return retval end def initialize(hash) @@ -1403,11 +1422,55 @@ module Puppet scope.setvar(var, value) } + self.evalparent(scope) + # And then evaluate our code. @code.safeevaluate(scope) return scope end + + # Evaluate our parent class. + def evalparent(scope) + if @parentclass + # This is pretty messed up. I don't know if this will + # work in the long term, but we need to evaluate the node + # in our own scope, even though our parent node has + # a scope associated with it, because otherwise we 1) won't + # get our facts defined, and 2) we won't actually get the + # objects returned, based on how nodes work. + + # We also can't just evaluate the node itself, because + # it would create a node scope within this scope, + # and that would cause mass havoc. + hash = nil + unless hash = scope.node(@parentclass) + raise Puppet::ParseError, + "Could not find parent node %s" % + @parentclass + end + + begin + code = hash[:node].code + code.safeevaluate(scope) + rescue Puppet::ParseError => except + except.line = self.line + except.file = self.file + raise except + rescue => detail + error = Puppet::ParseError.new(detail) + error.line = self.line + error.file = self.file + raise error + end + end + end + + def initialize(hash) + @parentclass = nil + super + + end end #--------------------------------------------------------------- end diff --git a/lib/puppet/parser/grammar.ra b/lib/puppet/parser/grammar.ra index bbc679f78..78fb9aa47 100644 --- a/lib/puppet/parser/grammar.ra +++ b/lib/puppet/parser/grammar.ra @@ -494,7 +494,7 @@ hostclass: CLASS NAME parent LBRACE statements RBRACE { result = AST::ClassDef.new(args) } -nodedef: NODE names LBRACE statements RBRACE { +nodedef: NODE names parent LBRACE statements RBRACE { unless val[1].is_a?(AST::ASTArray) val[1] = AST::ASTArray.new( :line => val[1].line, @@ -508,9 +508,9 @@ nodedef: NODE names LBRACE statements RBRACE { :names => val[1], :code => val[4] } - #if val[3].is_a?(AST::Name) - # args[:parentclass] = val[2] - #end + if val[2].is_a?(AST::Name) + args[:parentclass] = val[2] + end result = AST::NodeDef.new(args) } diff --git a/lib/puppet/parser/parser.rb b/lib/puppet/parser/parser.rb index 94fd83cfb..df92e9c2e 100644 --- a/lib/puppet/parser/parser.rb +++ b/lib/puppet/parser/parser.rb @@ -32,7 +32,7 @@ module Puppet class Parser < Racc::Parser -module_eval <<'..end grammar.ra modeval..id9e2c7fc335', 'grammar.ra', 638 +module_eval <<'..end grammar.ra modeval..id4705e629f1', 'grammar.ra', 638 attr_reader :file, :files # Create an AST array out of all of the args @@ -144,7 +144,7 @@ end def string=(string) @lexer.string = string end -..end grammar.ra modeval..id9e2c7fc335 +..end grammar.ra modeval..id4705e629f1 ##### racc 1.4.4 generates ### @@ -221,7 +221,7 @@ racc_reduce_table = [ 2, 47, :_reduce_69, 6, 49, :_reduce_70, 6, 50, :_reduce_71, - 5, 51, :_reduce_72, + 6, 51, :_reduce_72, 1, 81, :_reduce_none, 2, 81, :_reduce_74, 0, 82, :_reduce_75, @@ -243,159 +243,155 @@ racc_reduce_table = [ racc_reduce_n = 91 -racc_shift_n = 151 +racc_shift_n = 152 racc_action_table = [ - 88, 123, 88, 118, 96, 31, 31, 52, 88, 117, - 88, 120, 107, 5, 5, 100, 88, 106, 88, 101, - 102, 55, 112, 58, 67, 122, 50, 104, 35, 106, - 35, 86, 87, 86, 87, 90, 35, 90, 35, 86, - 87, 86, 87, 90, 35, 90, 35, 86, 87, 86, - 87, 90, 133, 90, 5, 144, 114, 101, 102, 104, - 116, 40, 119, 55, 54, 108, 126, 50, 138, 128, - 4, 7, 61, 11, 14, 130, 18, 28, 29, 61, - 2, 5, 66, 9, 48, 15, 4, 7, 134, 11, - 14, 5, 18, 28, 29, 136, 2, 5, 61, 9, - 5, 15, 31, 28, 29, 28, 29, 5, 41, 2, - 5, 141, 40, 81, 28, 29, 28, 29, 31, 39, - 24, 147, 35, 65, 23, 2, 5, 61, 31, 69, - 31, nil, 35, 146, 35, 2, 5, 2, 5, 31, - nil, 31, nil, 35, 28, 29, 2, 5, 2, 72, - nil, 4, 7, nil, 11, 14, nil, 18, 28, 29, - nil, 2, 5, nil, 9, nil, 15, nil, nil, 31, - nil, nil, nil, 35, 150, nil, 2, 5, nil, 28, - 29, 28, 29, 31, nil, nil, nil, 35, nil, nil, - 2, 5, 4, 7, nil, 11, 14, nil, 18, nil, - nil, nil, 2, 5, 31, 9, 31, 15, 35, nil, - 35, 2, 5, 2, 5, 4, 7, nil, 11, 14, - nil, 18, nil, nil, nil, 2, 5, nil, 9, nil, - 15, 4, 7, nil, 11, 14, nil, 18, nil, nil, - nil, 2, 5, nil, 9, nil, 15, 4, 7, nil, - 11, 14, nil, 18, nil, nil, nil, 2, 5, nil, - 9, nil, 15, 4, 7, nil, 11, 14, nil, 18, + 88, 123, 88, 118, 144, 130, 101, 102, 88, 117, + 96, 4, 7, 88, 11, 14, 120, 18, 88, 31, + 40, 2, 5, 88, 9, 122, 15, 5, 35, 110, + 35, 86, 87, 86, 87, 90, 35, 90, 55, 86, + 87, 35, 112, 90, 86, 87, 35, 138, 90, 86, + 87, 35, 104, 90, 86, 87, 28, 29, 90, 28, + 29, 28, 29, 106, 65, 4, 7, 114, 11, 14, + 5, 18, 28, 29, 58, 2, 5, 50, 9, 31, + 15, 31, 28, 29, 31, 35, 31, 5, 2, 5, + 35, 2, 5, 2, 5, 100, 104, 31, 146, 101, + 102, 35, 116, 107, 2, 5, 119, 31, 28, 29, + 55, 35, 54, 61, 2, 5, 4, 7, 126, 11, + 14, 50, 18, 128, 151, 5, 2, 5, 108, 9, + 61, 15, 66, 31, 28, 29, 28, 29, 81, 134, + 2, 72, 4, 7, 48, 11, 14, 136, 18, 61, + 5, 5, 2, 5, 147, 9, 65, 15, 141, 31, + 41, 31, 106, 35, 40, 35, 2, 5, 2, 5, + 28, 29, 4, 7, 39, 11, 14, 24, 18, 28, + 29, 148, 2, 5, 69, 9, 23, 15, 61, 52, + nil, nil, nil, nil, nil, 31, nil, nil, nil, 35, + nil, nil, 2, 5, 31, nil, nil, nil, 35, nil, + nil, 2, 5, 4, 7, nil, 11, 14, nil, 18, nil, nil, nil, 2, 5, nil, 9, nil, 15, 4, 7, nil, 11, 14, nil, 18, nil, nil, nil, 2, 5, nil, 9, nil, 15, 4, 7, nil, 11, 14, nil, 18, nil, nil, nil, 2, 5, nil, 9, nil, - 15 ] + 15, 4, 7, nil, 11, 14, nil, 18, nil, nil, + nil, 2, 5, nil, 9, nil, 15, 4, 7, nil, + 11, 14, nil, 18, nil, nil, nil, 2, 5, nil, + 9, nil, 15 ] racc_action_check = [ - 143, 93, 55, 83, 55, 15, 66, 24, 91, 83, - 96, 91, 63, 15, 66, 57, 54, 62, 123, 57, - 57, 70, 71, 38, 46, 93, 38, 72, 143, 73, - 55, 143, 143, 55, 55, 143, 91, 55, 96, 91, - 91, 96, 96, 91, 54, 96, 123, 54, 54, 123, - 123, 54, 110, 123, 46, 125, 76, 125, 125, 61, - 82, 33, 85, 26, 25, 65, 97, 21, 115, 103, - 110, 110, 40, 110, 110, 105, 110, 112, 112, 106, - 110, 110, 43, 110, 19, 110, 115, 115, 111, 115, - 115, 18, 115, 119, 119, 113, 115, 115, 114, 115, - 52, 115, 112, 104, 104, 126, 126, 118, 14, 112, - 112, 122, 12, 51, 9, 9, 50, 50, 119, 11, - 7, 137, 119, 41, 4, 119, 119, 147, 104, 48, - 126, nil, 104, 132, 126, 104, 104, 126, 126, 9, - nil, 50, nil, 9, 28, 28, 9, 9, 50, 50, - nil, 132, 132, nil, 132, 132, nil, 132, 58, 58, - nil, 132, 132, nil, 132, nil, 132, nil, nil, 28, - nil, nil, nil, 28, 148, nil, 28, 28, nil, 39, - 39, 99, 99, 58, nil, nil, nil, 58, nil, nil, - 58, 58, 148, 148, nil, 148, 148, nil, 148, nil, - nil, nil, 148, 148, 39, 148, 99, 148, 39, nil, - 99, 39, 39, 99, 99, 107, 107, nil, 107, 107, - nil, 107, nil, nil, nil, 107, 107, nil, 107, nil, - 107, 0, 0, nil, 0, 0, nil, 0, nil, nil, - nil, 0, 0, nil, 0, nil, 0, 141, 141, nil, - 141, 141, nil, 141, nil, nil, nil, 141, 141, nil, - 141, nil, 141, 67, 67, nil, 67, 67, nil, 67, - nil, nil, nil, 67, 67, nil, 67, nil, 67, 20, - 20, nil, 20, 20, nil, 20, nil, nil, nil, 20, - 20, nil, 20, nil, 20, 81, 81, nil, 81, 81, + 143, 93, 54, 83, 125, 105, 125, 125, 55, 83, + 55, 107, 107, 91, 107, 107, 91, 107, 96, 66, + 33, 107, 107, 123, 107, 93, 107, 66, 143, 67, + 54, 143, 143, 54, 54, 143, 55, 54, 70, 55, + 55, 91, 71, 55, 91, 91, 96, 115, 91, 96, + 96, 123, 72, 96, 123, 123, 104, 104, 123, 112, + 112, 119, 119, 73, 46, 115, 115, 76, 115, 115, + 46, 115, 126, 126, 38, 115, 115, 38, 115, 15, + 115, 104, 99, 99, 112, 104, 119, 15, 104, 104, + 119, 112, 112, 119, 119, 57, 61, 126, 132, 57, + 57, 126, 82, 63, 126, 126, 85, 99, 50, 50, + 26, 99, 25, 40, 99, 99, 132, 132, 97, 132, + 132, 21, 132, 103, 149, 52, 132, 132, 65, 132, + 106, 132, 43, 50, 28, 28, 58, 58, 51, 111, + 50, 50, 149, 149, 19, 149, 149, 113, 149, 114, + 18, 118, 149, 149, 133, 149, 41, 149, 122, 28, + 14, 58, 62, 28, 12, 58, 28, 28, 58, 58, + 39, 39, 133, 133, 11, 133, 133, 7, 133, 9, + 9, 137, 133, 133, 48, 133, 4, 133, 148, 24, + nil, nil, nil, nil, nil, 39, nil, nil, nil, 39, + nil, nil, 39, 39, 9, nil, nil, nil, 9, nil, + nil, 9, 9, 0, 0, nil, 0, 0, nil, 0, + nil, nil, nil, 0, 0, nil, 0, nil, 0, 141, + 141, nil, 141, 141, nil, 141, nil, nil, nil, 141, + 141, nil, 141, nil, 141, 81, 81, nil, 81, 81, nil, 81, nil, nil, nil, 81, 81, nil, 81, nil, - 81 ] + 81, 110, 110, nil, 110, 110, nil, 110, nil, nil, + nil, 110, 110, nil, 110, nil, 110, 20, 20, nil, + 20, 20, nil, 20, nil, nil, nil, 20, 20, nil, + 20, nil, 20 ] racc_action_pointer = [ - 207, nil, nil, nil, 121, nil, nil, 85, nil, 112, - nil, 107, 107, nil, 73, -22, nil, nil, 56, 84, - 255, 62, nil, nil, -7, 59, 50, nil, 142, nil, - nil, nil, nil, 56, nil, nil, nil, nil, 21, 177, - 37, 94, nil, 73, nil, nil, 19, nil, 129, nil, - 114, 108, 65, nil, 13, -1, nil, 11, 156, nil, - nil, 51, 8, 7, nil, 30, -21, 239, nil, nil, - 8, -14, 19, 20, nil, nil, 23, nil, nil, nil, - nil, 271, 45, -6, nil, 50, nil, nil, nil, nil, - nil, 5, nil, -8, nil, nil, 7, 58, nil, 179, - nil, nil, nil, 65, 101, 69, 44, 191, nil, nil, - 46, 82, 75, 89, 63, 62, nil, nil, 72, 91, - nil, nil, 106, 15, nil, 49, 103, nil, nil, nil, - nil, nil, 127, nil, nil, nil, nil, 112, nil, nil, - nil, 223, nil, -3, nil, nil, nil, 92, 168, nil, - nil ] + 189, nil, nil, nil, 183, nil, nil, 142, nil, 177, + nil, 162, 159, nil, 125, 52, nil, nil, 115, 144, + 253, 116, nil, nil, 175, 107, 97, nil, 132, nil, + nil, nil, nil, 15, nil, nil, nil, nil, 72, 168, + 78, 127, nil, 123, nil, nil, 35, nil, 184, nil, + 106, 133, 90, nil, -1, 5, nil, 91, 134, nil, + nil, 88, 153, 98, nil, 93, -8, 24, nil, nil, + 25, 6, 44, 54, nil, nil, 34, nil, nil, nil, + nil, 221, 87, -6, nil, 94, nil, nil, nil, nil, + nil, 10, nil, -8, nil, nil, 15, 110, nil, 80, + nil, nil, nil, 119, 54, -1, 95, -13, nil, nil, + 237, 133, 57, 141, 114, 41, nil, nil, 116, 59, + nil, nil, 153, 20, nil, -2, 70, nil, nil, nil, + nil, nil, 92, 148, nil, nil, nil, 172, nil, nil, + nil, 205, nil, -3, nil, nil, nil, nil, 153, 118, + nil, nil ] racc_action_default = [ -91, -4, -26, -5, -91, -25, -6, -91, -7, -91, -8, -91, -91, -9, -91, -91, -10, -11, -91, -91, -1, -91, -2, -69, -75, -91, -46, -45, -91, -49, -43, -85, -48, -42, -40, -50, -44, -47, -41, -91, - -34, -75, -16, -12, -13, -15, -91, -73, -91, -3, + -34, -75, -16, -12, -13, -15, -75, -73, -91, -3, -34, -91, -75, -76, -91, -91, -38, -91, -91, -33, - -35, -91, -89, -91, -83, -91, -91, -91, -74, 151, + -35, -91, -89, -91, -83, -91, -91, -91, -74, 152, -31, -23, -25, -89, -29, -21, -91, -27, -30, -32, -28, -91, -91, -91, -79, -82, -65, -64, -66, -68, -67, -91, -53, -91, -56, -59, -91, -91, -58, -91, -86, -87, -88, -91, -91, -91, -90, -91, -84, -14, -91, -91, -24, -91, -34, -91, -77, -78, -91, -91, -52, -54, -91, -91, -61, -91, -91, -39, -51, -37, - -19, -36, -91, -72, -17, -22, -18, -20, -70, -80, - -81, -91, -57, -91, -60, -63, -71, -91, -91, -62, - -55 ] + -19, -36, -91, -91, -17, -22, -18, -20, -70, -80, + -81, -91, -57, -91, -60, -63, -71, -72, -91, -91, + -62, -55 ] racc_goto_table = [ - 20, 49, 38, 62, 27, 25, 42, 99, 45, 94, - 84, 47, 75, 73, 33, 44, 125, 77, 78, 89, - 89, 38, 79, 27, 56, 92, 105, 131, 95, 57, - 53, 111, 38, 33, 27, 59, 98, 113, 43, 68, - 91, 70, 51, 80, 33, 85, 94, 64, 63, 46, - 71, 38, 83, 27, 103, 74, 89, 42, 82, 45, - 19, 89, 121, 33, nil, nil, 109, 110, 131, 124, - nil, nil, nil, nil, 135, 143, 139, 137, 142, 77, - 78, 115, nil, nil, 79, nil, nil, nil, 89, nil, - nil, 49, 38, nil, 27, 127, 49, 38, nil, 27, - 129, nil, nil, 70, 33, 80, nil, 132, 89, 33, - nil, 85, 38, 49, 27, 140, 149, 74, nil, 38, - nil, 27, 145, nil, 33, nil, nil, nil, nil, 49, - nil, 33, nil, nil, nil, nil, nil, nil, nil, nil, - nil, 148 ] + 20, 49, 38, 62, 99, 27, 42, 84, 45, 94, + 131, 47, 75, 73, 25, 44, 92, 125, 77, 89, + 89, 38, 78, 79, 27, 105, 63, 53, 95, 111, + 33, 67, 38, 56, 98, 27, 113, 43, 57, 68, + 51, 70, 91, 80, 59, 85, 94, 46, 71, 33, + 83, 38, 131, 121, 27, 82, 89, 42, 19, 45, + 33, 89, nil, 103, nil, nil, 109, nil, nil, 124, + nil, 74, 143, 139, 135, nil, nil, 137, 142, 33, + 77, 115, nil, nil, 78, 79, nil, nil, 89, nil, + nil, nil, 38, nil, nil, 27, 49, 38, nil, nil, + 27, nil, nil, 70, 127, 80, nil, 132, 89, 129, + 133, 85, 38, 49, 49, 27, 150, nil, nil, 38, + 33, nil, 27, nil, 140, 33, nil, nil, nil, nil, + 49, 145, nil, 74, nil, nil, nil, nil, nil, nil, + 33, 149, nil, nil, nil, nil, nil, 33 ] racc_goto_check = [ - 2, 3, 14, 18, 4, 26, 15, 29, 14, 35, - 44, 14, 21, 18, 20, 13, 38, 23, 24, 30, - 30, 14, 25, 4, 26, 33, 19, 27, 37, 28, - 42, 17, 14, 20, 4, 26, 36, 19, 12, 14, - 32, 15, 39, 14, 20, 14, 35, 42, 40, 41, - 16, 14, 43, 4, 26, 20, 30, 15, 42, 14, - 1, 30, 33, 20, nil, nil, 13, 2, 27, 37, - nil, nil, nil, nil, 21, 29, 44, 18, 35, 23, - 24, 2, nil, nil, 25, nil, nil, nil, 30, nil, - nil, 3, 14, nil, 4, 26, 3, 14, nil, 4, - 26, nil, nil, 15, 20, 14, nil, 2, 30, 20, - nil, 14, 14, 3, 4, 26, 37, 20, nil, 14, - nil, 4, 26, nil, 20, nil, nil, nil, nil, 3, - nil, 20, nil, nil, nil, nil, nil, nil, nil, nil, - nil, 2 ] + 2, 3, 14, 18, 29, 4, 15, 44, 14, 35, + 27, 14, 21, 18, 26, 13, 33, 38, 23, 30, + 30, 14, 24, 25, 4, 19, 40, 42, 37, 17, + 20, 40, 14, 26, 36, 4, 19, 12, 28, 14, + 39, 15, 32, 14, 26, 14, 35, 41, 16, 20, + 43, 14, 27, 33, 4, 42, 30, 15, 1, 14, + 20, 30, nil, 26, nil, nil, 13, nil, nil, 37, + nil, 20, 29, 44, 21, nil, nil, 18, 35, 20, + 23, 2, nil, nil, 24, 25, nil, nil, 30, nil, + nil, nil, 14, nil, nil, 4, 3, 14, nil, nil, + 4, nil, nil, 15, 26, 14, nil, 2, 30, 26, + 2, 14, 14, 3, 3, 4, 37, nil, nil, 14, + 20, nil, 4, nil, 26, 20, nil, nil, nil, nil, + 3, 26, nil, 20, nil, nil, nil, nil, nil, nil, + 20, 2, nil, nil, nil, nil, nil, 20 ] racc_goto_pointer = [ - nil, 60, 0, -19, -5, nil, nil, nil, nil, nil, - nil, nil, 23, 0, -7, -9, 0, -40, -37, -36, - 5, -38, nil, -33, -32, -28, -4, -79, 1, -50, - -35, nil, -14, -29, nil, -45, -19, -27, -80, 18, - 7, 31, 6, 0, -42 ] + nil, 58, 0, -19, -4, nil, nil, nil, nil, nil, + nil, nil, 22, 0, -7, -9, -2, -42, -37, -37, + 21, -38, nil, -32, -28, -27, 5, -96, 10, -53, + -35, nil, -12, -38, nil, -45, -21, -27, -79, 16, + -15, 29, 3, -2, -45 ] racc_goto_default = [ nil, nil, nil, 22, 1, 3, 6, 8, 10, 13, 16, 17, nil, nil, 21, 26, nil, nil, nil, nil, 12, nil, 76, 34, 36, 37, nil, 60, nil, nil, 30, 32, nil, nil, 93, 97, nil, nil, nil, nil, - nil, nil, nil, nil, nil ] + nil, nil, 64, nil, nil ] racc_token_table = { false => 0, @@ -1191,9 +1187,9 @@ module_eval <<'.,.,', 'grammar.ra', 515 :names => val[1], :code => val[4] } - #if val[3].is_a?(AST::Name) - # args[:parentclass] = val[2] - #end + if val[2].is_a?(AST::Name) + args[:parentclass] = val[2] + end result = AST::NodeDef.new(args) result end diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb index ef78bf218..3730af606 100644 --- a/lib/puppet/parser/scope.rb +++ b/lib/puppet/parser/scope.rb @@ -10,9 +10,9 @@ module Puppet attr_accessor :parent, :level, :interp attr_accessor :name, :type - # The global host table. This will likely be changed to be scoped, - # eventually, but for now it's not. - @@hosttable = {} + # This is probably not all that good of an idea, but... + # This way a parent can share its node table with all of its children. + attr_writer :nodetable # Whether we behave declaratively. Note that it's a class variable, # so all scopes behave the same. @@ -30,6 +30,12 @@ module Puppet # Create a new child scope. def child=(scope) @children.push(scope) + + if defined? @nodetable + scope.nodetable = @nodetable + else + raise Puppet::DevError, "No nodetable has been defined" + end end # Test whether a given scope is declarative. Even though it's @@ -89,16 +95,17 @@ module Puppet # appropriate scope. def evalnode(names, facts) scope = code = nil + + # Find a node that matches one of our names names.each { |node| - scope = self.findnode(node) - if scope - code = scope.node(node) - end - if scope and code + if hash = @nodetable[node] + code = hash[:node] + scope = hash[:scope] break end } + # And fail if we don't find one. unless scope and code raise Puppet::Error, "Could not find configuration for %s" % names.join(" or ") @@ -124,30 +131,32 @@ module Puppet return self.to_trans end - # Find a given node's definition; searches downward recursively. - def findnode(node) - if @nodetable.include?(node) - return self - else - scope = nil - self.reject { |child| - ! child.is_a?(Scope) - }.each { |child| - if scope = child.findnode(node) - break - end - } - - return scope - end - end - # Retrieve a specific node. This is basically only used from within # 'findnode'. def node(name) @nodetable[name] end + # Store a host in the site node table. + def setnode(name,code) + unless defined? @nodetable + raise Puppet::DevError, "No node table defined" + end + if @nodetable.include?(name) + raise Puppet::ParseError, "Host %s is already defined" % name + else + #Puppet.warning "Setting node %s at level %s" % [name, @level] + + # We have to store both the scope that's setting the node and + # the node itself, so that the node gets evaluated in the correct + # scope. + @nodetable[name] = { + :scope => self, + :node => code + } + end + end + # Evaluate normally, with no node definitions def evaluate(objects, facts = {}) facts.each { |var, value| @@ -167,7 +176,16 @@ module Puppet if @parent.nil? @level = 1 + @@declarative = declarative + + # A table for storing nodes. + @nodetable = Hash.new(nil) + + # Eventually, if we support sites, this will allow definitions + # of nodes with the same name in different sites. For now + # the top-level scope is always the only site scope. + @sitescope = true else @parent.child = self @level = @parent.level + 1 @@ -187,9 +205,6 @@ module Puppet # be used by top scopes and node scopes. @classtable = Hash.new(nil) - # A table for storing nodes. - @nodetable = Hash.new(nil) - # All of the defaults set for types. It's a hash of hashes, # with the first key being the type, then the second key being # the parameter. @@ -397,46 +412,6 @@ module Puppet } end - # Check whether a node is already defined. - # FIXME Should this system replace the 'UseNodes' flags and such? - def nodedefined?(name) - if defined? @nodemarkers - return @nodemarkers[name] - else - if @parent - return @parent.nodedefined?(name) - else - return false - end - end - end - - # Mark that a node is defined. We don't want to allow more than one - # node definition per name, because, well, that would make things not - # work any more. - def marknode(name) - if @parent - @parent.marknode(name) - else - unless defined? @nodemarkers - @nodemarkers = {} - end - @nodemarkers[name] = true - end - end - - # Store a host in the global table. - def setnode(name,code) - if self.nodedefined?(name) - raise Puppet::ParseError, "Host %s is already defined" % name - else - @nodetable[name] = code - self.marknode(name) - end - - #self.nodescope = true - end - # Define our type. def settype(name,ltype) @typetable[name] = ltype diff --git a/test/language/tc_ast.rb b/test/language/tc_ast.rb index c8cd2f5d2..ab336c9e1 100755 --- a/test/language/tc_ast.rb +++ b/test/language/tc_ast.rb @@ -283,7 +283,7 @@ class TestAST < TestPuppet assert(! scope.lookupclass("parent"), "Found parent class in top scope") # verify we can find our node - assert(scope.findnode(nodename), "Could not find node") + assert(scope.node(nodename), "Could not find node") # And verify that we can evaluate it okay objects = nil @@ -399,7 +399,7 @@ class TestAST < TestPuppet end # Test that we can 'include' variables, not just normal strings. - def test_zincludevars + def test_includevars children = [] # Create our class for testin @@ -438,4 +438,62 @@ class TestAST < TestPuppet assert_equal("/%s" % klassname, objects[0][0][:name]) } end + + # Test that node inheritance works correctly + def test_nodeinheritance + children = [] + + # create the base node + name = "basenode" + children << nodeobj(name) + + # and the sub node + name = "subnode" + children << AST::NodeDef.new( + :names => nameobj(name), + :parentclass => nameobj("basenode"), + :code => AST::ASTArray.new( + :children => [ + varobj("%svar" % name, "%svalue" % name), + fileobj("/%s" % name) + ] + ) + ) + #subnode = nodeobj(name) + #subnode.parentclass = "basenode" + + #children << subnode + + # and the top object + top = nil + assert_nothing_raised("Could not create top object") { + top = AST::ASTArray.new( + :children => children + ) + } + + # Evaluate the parse tree + scope = nil + assert_nothing_raised("Could not evaluate node") { + scope = Puppet::Parser::Scope.new() + top.evaluate(scope) + } + + # Verify we can find the node via a search list + objects = nil + assert_nothing_raised("Could not retrieve node definition") { + objects = scope.evalnode([name], {}) + } + assert(objects, "Could not retrieve node definition") + + # And now verify that we got the subnode file + assert_nothing_raised("Could not find basenode file") { + assert_equal("/basenode", objects[0][0][:name]) + } + + # and the parent node file + assert_nothing_raised("Could not find subnode file") { + assert_equal("/subnode", objects[0][1][:name]) + } + end end diff --git a/test/language/tc_scope.rb b/test/language/tc_scope.rb index 192b74b61..5b1918707 100755 --- a/test/language/tc_scope.rb +++ b/test/language/tc_scope.rb @@ -294,8 +294,8 @@ class TestScope < TestPuppet # Now verify we can find the host again host = nil assert_nothing_raised("Host lookup failed") { - scope = top.findnode("testing") - host = scope.node("testing") + hash = top.node("testing") + host = hash[:node] } assert(host, "Could not find host") |