~nvalcarcel/ubuntu/lucid/puppet/fix-546677

« back to all changes in this revision

Viewing changes to lib/puppet/parser/collector.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:
1
1
# An object that collects stored objects from the central cache and returns
2
2
# them to the current host, yo.
3
3
class Puppet::Parser::Collector
4
 
    attr_accessor :type, :scope, :vquery, :equery, :form, :resources
 
4
    attr_accessor :type, :scope, :vquery, :equery, :form, :resources, :overrides, :collected
5
5
 
6
6
    # Call the collection method, mark all of the returned objects as non-virtual,
7
 
    # and then delete this object from the list of collections to evaluate.
 
7
    # optionally applying parameter overrides. The collector can also delete himself
 
8
    # from the compiler if there is no more resources to collect (valid only for resource fixed-set collector
 
9
    # which get their resources from +collect_resources+ and not from the catalog)
8
10
    def evaluate
9
11
        # Shortcut if we're not using storeconfigs and they're trying to collect
10
12
        # exported resources.
12
14
            Puppet.warning "Not collecting exported resources without storeconfigs"
13
15
            return false
14
16
        end
 
17
 
15
18
        if self.resources
16
 
            if objects = collect_resources and ! objects.empty?
17
 
                return objects
18
 
            else
 
19
            unless objects = collect_resources and ! objects.empty?
19
20
                return false
20
21
            end
21
22
        else
25
26
            end
26
27
            if objects.empty?
27
28
                return false
28
 
            else
29
 
                return objects
30
 
            end
31
 
        end
 
29
            end
 
30
        end
 
31
 
 
32
        # we have an override for the collected resources
 
33
        if @overrides and !objects.empty?
 
34
 
 
35
            # force the resource to be always child of any other resource
 
36
            overrides[:source].meta_def(:child_of?) do
 
37
                true
 
38
            end
 
39
 
 
40
            # tell the compiler we have some override for him unless we already
 
41
            # overrided those resources
 
42
            objects.each do |res|
 
43
                unless @collected.include?(res.ref)
 
44
                    res = Puppet::Parser::Resource.new(
 
45
                        :type => res.type,
 
46
                        :title => res.title,
 
47
                        :params => overrides[:params],
 
48
                        :file => overrides[:file],
 
49
                        :line => overrides[:line],
 
50
                        :source => overrides[:source],
 
51
                        :scope => overrides[:scope]
 
52
                    )
 
53
 
 
54
                    scope.compiler.add_override(res)
 
55
                end
 
56
            end
 
57
        end
 
58
 
 
59
        # filter out object that already have been collected by ourself
 
60
        objects.reject! { |o| @collected.include?(o.ref) }
 
61
 
 
62
        return false if objects.empty?
 
63
 
 
64
        # keep an eye on the resources we have collected
 
65
        objects.inject(@collected) { |c,o| c[o.ref]=o; c }
 
66
 
 
67
        # return our newly collected resources
 
68
        objects
32
69
    end
33
70
 
34
71
    def initialize(scope, type, equery, vquery, form)
35
72
        @scope = scope
36
73
 
 
74
        # initialisation
 
75
        @collected = {}
 
76
 
37
77
        # Canonize the type
38
 
        @type = Puppet::ResourceReference.new(type, "whatever").type
 
78
        @type = Puppet::Resource::Reference.new(type, "whatever").type
39
79
        @equery = equery
40
80
        @vquery = vquery
41
81
 
43
83
        @form = form
44
84
    end
45
85
 
 
86
    # add a resource override to the soon to be exported/realized resources
 
87
    def add_override(hash)
 
88
        unless hash[:params]
 
89
            raise ArgumentError, "Exported resource try to override without parameters"
 
90
        end
 
91
 
 
92
        # schedule an override for an upcoming collection
 
93
        @overrides = hash
 
94
    end
 
95
 
46
96
    private
47
97
 
48
98
    # Create our active record query.
52
102
        raise Puppet::DevError, "Cannot collect resources for a nil host" unless @scope.host
53
103
        host = Puppet::Rails::Host.find_by_name(@scope.host)
54
104
 
55
 
        query = {:include => {:param_values => :param_name}}
56
 
 
57
105
        search = "(exported=? AND restype=?)"
58
106
        values = [true, @type]
59
107
 
60
108
        search += " AND (%s)" % @equery if @equery
61
109
 
 
110
        # note:
 
111
        # we're not eagerly including any relations here because
 
112
        # it can creates so much objects we'll throw out later.
 
113
        # We used to eagerly include param_names/values but the way
 
114
        # the search filter is built ruined those efforts and we
 
115
        # were eagerly loading only the searched parameter and not
 
116
        # the other ones. 
 
117
        query = {}
 
118
        case search
 
119
        when /puppet_tags/
 
120
            query = {:joins => {:resource_tags => :puppet_tag}}
 
121
        when /param_name/
 
122
            query = {:joins => {:param_values => :param_name}}
 
123
        end
 
124
 
62
125
        # We're going to collect objects from rails, but we don't want any
63
126
        # objects from this host.
64
127
        search = ("host_id != ? AND %s" % search) and values.unshift(host.id) if host
82
145
        # and such, we'll need to vary the conditions, but this works with no
83
146
        # attributes, anyway.
84
147
        time = Puppet::Util.thinmark do
85
 
            Puppet::Rails::Resource.find(:all, @type, true, query).each do |obj|
 
148
            Puppet::Rails::Resource.find(:all, query).each do |obj|
86
149
                if resource = exported_resource(obj)
87
150
                    count += 1
88
151
                    resources << resource
132
195
 
133
196
    # Collect just virtual objects, from our local compiler.
134
197
    def collect_virtual(exported = false)
135
 
        if exported
136
 
            method = :exported?
137
 
        else
138
 
            method = :virtual?
139
 
        end
140
198
        scope.compiler.resources.find_all do |resource|
141
 
            resource.type == @type and resource.send(method) and match?(resource)
 
199
            resource.type == @type and (exported ? resource.exported? : true) and match?(resource)
142
200
        end
143
201
    end
144
202
 
155
213
        resource = obj.to_resource(self.scope)
156
214
 
157
215
        resource.exported = false
158
 
        
 
216
 
159
217
        scope.compiler.add_resource(scope, resource)
160
218
 
161
219
        return resource