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
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)
9
11
# Shortcut if we're not using storeconfigs and they're trying to collect
10
12
# exported resources.
32
# we have an override for the collected resources
33
if @overrides and !objects.empty?
35
# force the resource to be always child of any other resource
36
overrides[:source].meta_def(:child_of?) do
40
# tell the compiler we have some override for him unless we already
41
# overrided those resources
43
unless @collected.include?(res.ref)
44
res = Puppet::Parser::Resource.new(
47
:params => overrides[:params],
48
:file => overrides[:file],
49
:line => overrides[:line],
50
:source => overrides[:source],
51
:scope => overrides[:scope]
54
scope.compiler.add_override(res)
59
# filter out object that already have been collected by ourself
60
objects.reject! { |o| @collected.include?(o.ref) }
62
return false if objects.empty?
64
# keep an eye on the resources we have collected
65
objects.inject(@collected) { |c,o| c[o.ref]=o; c }
67
# return our newly collected resources
34
71
def initialize(scope, type, equery, vquery, form)
37
77
# Canonize the type
38
@type = Puppet::ResourceReference.new(type, "whatever").type
78
@type = Puppet::Resource::Reference.new(type, "whatever").type
86
# add a resource override to the soon to be exported/realized resources
87
def add_override(hash)
89
raise ArgumentError, "Exported resource try to override without parameters"
92
# schedule an override for an upcoming collection
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)
55
query = {:include => {:param_values => :param_name}}
57
105
search = "(exported=? AND restype=?)"
58
106
values = [true, @type]
60
108
search += " AND (%s)" % @equery if @equery
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
120
query = {:joins => {:resource_tags => :puppet_tag}}
122
query = {:joins => {:param_values => :param_name}}
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)
88
151
resources << resource
133
196
# Collect just virtual objects, from our local compiler.
134
197
def collect_virtual(exported = false)
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)