~ubuntu-branches/ubuntu/lucid/puppet/lucid-security

« back to all changes in this revision

Viewing changes to lib/puppet/parser/compiler.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#  Copyright (c) 2007. All rights reserved.
3
3
 
4
4
require 'puppet/node'
5
 
require 'puppet/node/catalog'
 
5
require 'puppet/resource/catalog'
6
6
require 'puppet/util/errors'
7
7
 
8
8
# Maintain a graph of scopes, along with a bunch of data
10
10
class Puppet::Parser::Compiler
11
11
    include Puppet::Util
12
12
    include Puppet::Util::Errors
13
 
    attr_reader :parser, :node, :facts, :collections, :catalog, :node_scope
 
13
    attr_reader :parser, :node, :facts, :collections, :catalog, :node_scope, :resources
14
14
 
15
15
    # Add a collection to the global list.
16
16
    def add_collection(coll)
31
31
 
32
32
    # Store a resource in our resource table.
33
33
    def add_resource(scope, resource)
 
34
        @resources << resource
 
35
 
34
36
        # Note that this will fail if the resource is not unique.
35
37
        @catalog.add_resource(resource)
36
38
 
37
39
        # And in the resource graph.  At some point, this might supercede
38
40
        # the global resource table, but the table is a lot faster
39
41
        # so it makes sense to maintain for now.
40
 
        @catalog.add_edge(scope.resource, resource)
 
42
        if resource.type.to_s.downcase == "class" and main = @catalog.resource(:class, :main)
 
43
            @catalog.add_edge(main, resource)
 
44
        else
 
45
            @catalog.add_edge(scope.resource, resource)
 
46
        end
41
47
    end
42
48
 
43
49
    # Do we use nodes found in the code, vs. the external node sources?
44
50
    def ast_nodes?
45
 
        parser.nodes.length > 0
 
51
        parser.nodes?
46
52
    end
47
53
 
48
54
    # Store the fact that we've evaluated a class, and store a reference to
95
101
 
96
102
        fail_on_unevaluated()
97
103
 
98
 
        if Puppet[:storeconfigs]
99
 
            store()
100
 
        end
101
 
 
102
104
        return @catalog
103
105
    end
104
106
 
135
137
        found = []
136
138
        classes.each do |name|
137
139
            # If we can find the class, then make a resource that will evaluate it.
138
 
            if klass = scope.findclass(name)
 
140
            if klass = scope.find_hostclass(name)
139
141
                found << name and next if class_scope(klass)
140
142
 
141
143
                resource = klass.evaluate(scope)
204
206
        @resource_overrides[resource.ref]
205
207
    end
206
208
 
207
 
    # Return a list of all resources.
208
 
    def resources
209
 
        @catalog.vertices
210
 
    end
211
 
 
212
209
    # The top scope is usually the top-level scope, but if we're using AST nodes,
213
210
    # then it is instead the node's scope.
214
211
    def topscope
224
221
        # Now see if we can find the node.
225
222
        astnode = nil
226
223
        @node.names.each do |name|
227
 
            break if astnode = @parser.nodes[name.to_s.downcase]
 
224
            break if astnode = @parser.node(name.to_s.downcase)
228
225
        end
229
226
 
230
 
        unless (astnode ||= @parser.nodes["default"])
 
227
        unless (astnode ||= @parser.node("default"))
231
228
            raise Puppet::ParseError, "Could not find default node or by name with '%s'" % node.names.join(", ")
232
229
        end
233
230
 
305
302
 
306
303
    # Find and evaluate our main object, if possible.
307
304
    def evaluate_main
308
 
        @main = @parser.findclass("", "") || @parser.newclass("")
 
305
        @main = @parser.find_hostclass("", "") || @parser.newclass("")
309
306
        @topscope.source = @main
310
307
        @main_resource = Puppet::Parser::Resource.new(:type => "class", :title => :main, :scope => @topscope, :source => @main)
311
308
        @topscope.resource = @main_resource
312
309
 
 
310
        @resources << @main_resource
313
311
        @catalog.add_resource(@main_resource)
314
312
 
315
313
        @main_resource.evaluate
366
364
    # Make sure all of our resources and such have done any last work
367
365
    # necessary.
368
366
    def finish
369
 
        @catalog.vertices.each do |resource|
 
367
        resources.each do |resource|
370
368
            # Add in any resource overrides.
371
369
            if overrides = resource_overrides(resource)
372
370
                overrides.each do |over|
412
410
        @scope_graph = Puppet::SimpleGraph.new
413
411
 
414
412
        # For maintaining the relationship between scopes and their resources.
415
 
        @catalog = Puppet::Node::Catalog.new(@node.name)
 
413
        @catalog = Puppet::Resource::Catalog.new(@node.name)
416
414
        @catalog.version = @parser.version
 
415
 
 
416
        # local resource array to maintain resource ordering
 
417
        @resources = []
 
418
 
 
419
        # Make sure any external node classes are in our class list
 
420
        @catalog.add_class(*@node.classes)
417
421
    end
418
422
 
419
423
    # Set the node's parameters into the top-scope as variables.
421
425
        node.parameters.each do |param, value|
422
426
            @topscope.setvar(param, value)
423
427
        end
424
 
    end
425
 
 
426
 
    # Store the catalog into the database.
427
 
    def store
428
 
        unless Puppet.features.rails?
429
 
            raise Puppet::Error,
430
 
                "storeconfigs is enabled but rails is unavailable"
431
 
        end
432
 
 
433
 
        unless ActiveRecord::Base.connected?
434
 
            Puppet::Rails.connect
435
 
        end
436
 
 
437
 
        # We used to have hooks here for forking and saving, but I don't
438
 
        # think it's worth retaining at this point.
439
 
        store_to_active_record(@node, @catalog.vertices)
440
 
    end
441
 
 
442
 
    # Do the actual storage.
443
 
    def store_to_active_record(node, resources)
444
 
        begin
445
 
            # We store all of the objects, even the collectable ones
446
 
            benchmark(:info, "Stored catalog for #{node.name}") do
447
 
                Puppet::Rails::Host.transaction do
448
 
                    Puppet::Rails::Host.store(node, resources)
449
 
                end
450
 
            end
451
 
        rescue => detail
452
 
            if Puppet[:trace]
453
 
                puts detail.backtrace
454
 
            end
455
 
            Puppet.err "Could not store configs: %s" % detail.to_s
456
 
        end
 
428
 
 
429
        # These might be nil.
 
430
        catalog.client_version = node.parameters["clientversion"]
 
431
        catalog.server_version = node.parameters["serverversion"]
457
432
    end
458
433
 
459
434
    # Return an array of all of the unevaluated resources.  These will be definitions,
460
435
    # which need to get evaluated into native resources.
461
436
    def unevaluated_resources
462
 
        ary = @catalog.vertices.reject { |resource| resource.builtin? or resource.evaluated?  }
 
437
        ary = resources.reject { |resource| resource.builtin? or resource.evaluated?  }
463
438
 
464
439
        if ary.empty?
465
440
            return nil