Class: Puppet::Parser::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/parser/collector.rb

Overview

An object that collects stored objects from the central cache and returns them to the current host, yo.

API:

  • public

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, type, equery, vquery, form) ⇒ Collector

Returns a new instance of Collector.

API:

  • public



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puppet/parser/collector.rb', line 68

def initialize(scope, type, equery, vquery, form)
  @scope  = scope
  @vquery = vquery
  @equery = equery

  # initialisation
  @collected = {}

  # Canonize the type
  @type = Puppet::Resource.new(type, "whatever").type

  unless [:exported, :virtual].include?(form)
    raise ArgumentError, "Invalid query form #{form}"
  end
  @form = form
end

Instance Attribute Details

#collectedObject

API:

  • public



5
6
7
# File 'lib/puppet/parser/collector.rb', line 5

def collected
  @collected
end

#equeryObject

API:

  • public



4
5
6
# File 'lib/puppet/parser/collector.rb', line 4

def equery
  @equery
end

#formObject

API:

  • public



4
5
6
# File 'lib/puppet/parser/collector.rb', line 4

def form
  @form
end

#overridesObject

API:

  • public



5
6
7
# File 'lib/puppet/parser/collector.rb', line 5

def overrides
  @overrides
end

#resourcesObject

API:

  • public



5
6
7
# File 'lib/puppet/parser/collector.rb', line 5

def resources
  @resources
end

#scopeObject

API:

  • public



4
5
6
# File 'lib/puppet/parser/collector.rb', line 4

def scope
  @scope
end

#typeObject

API:

  • public



4
5
6
# File 'lib/puppet/parser/collector.rb', line 4

def type
  @type
end

#vqueryObject

API:

  • public



4
5
6
# File 'lib/puppet/parser/collector.rb', line 4

def vquery
  @vquery
end

Instance Method Details

#add_override(hash) ⇒ Object

add a resource override to the soon to be exported/realized resources

Raises:

API:

  • public



86
87
88
89
90
91
# File 'lib/puppet/parser/collector.rb', line 86

def add_override(hash)
  raise ArgumentError, "Exported resource try to override without parameters" unless hash[:parameters]

  # schedule an override for an upcoming collection
  @overrides = hash
end

#evaluateObject

Call the collection method, mark all of the returned objects as non-virtual, optionally applying parameter overrides. The collector can also delete himself from the compiler if there is no more resources to collect (valid only for resource fixed-set collector which get their resources from collect_resources and not from the catalog)

API:

  • public



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet/parser/collector.rb', line 12

def evaluate
  # Shortcut if we're not using storeconfigs and they're trying to collect
  # exported resources.
  if form == :exported and Puppet[:storeconfigs] != true
    Puppet.warning "Not collecting exported resources without storeconfigs"
    return false
  end

  if self.resources
    unless objects = collect_resources and ! objects.empty?
      return false
    end
  else
    method = "collect_#{@form.to_s}"
    objects = send(method).each do |obj|
      obj.virtual = false
    end
    return false if objects.empty?
  end

  # we have an override for the collected resources
  if @overrides and !objects.empty?
    # force the resource to be always child of any other resource
    overrides[:source].meta_def(:child_of?) do |klass|
      true
    end

    # tell the compiler we have some override for him unless we already
    # overrided those resources
    objects.each do |res|
      unless @collected.include?(res.ref)
        newres = Puppet::Parser::Resource.
          new(res.type, res.title,
              :parameters => overrides[:parameters],
              :file       => overrides[:file],
              :line       => overrides[:line],
              :source     => overrides[:source],
              :scope      => overrides[:scope])

        scope.compiler.add_override(newres)
      end
    end
  end

  # filter out object that this collector has previously found.
  objects.reject! { |o| @collected.include?(o.ref) }

  return false if objects.empty?

  # keep an eye on the resources we have collected
  objects.inject(@collected) { |c,o| c[o.ref]=o; c }

  # return our newly collected resources
  objects
end