Class: Puppet::Resource::TypeCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/resource/type_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ TypeCollection

Returns a new instance of TypeCollection.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/puppet/resource/type_collection.rb', line 10

def initialize(env)
  @environment = env.is_a?(String) ? Puppet::Node::Environment.new(env) : env
  @hostclasses = {}
  @definitions = {}
  @nodes = {}

  # So we can keep a list and match the first-defined regex
  @node_list = []

  @watched_files = {}
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



2
3
4
# File 'lib/puppet/resource/type_collection.rb', line 2

def environment
  @environment
end

Instance Method Details

#<<(thing) ⇒ Object



26
27
28
29
# File 'lib/puppet/resource/type_collection.rb', line 26

def <<(thing)
  add(thing)
  self
end

#add(instance) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/puppet/resource/type_collection.rb', line 31

def add(instance)
  if instance.type == :hostclass and other = @hostclasses[instance.name] and other.type == :hostclass
    other.merge(instance)
    return other
  end
  method = "add_#{instance.type}"
  send(method, instance)
  instance.resource_type_collection = self
  instance
end

#add_definition(instance) ⇒ Object



89
90
91
92
93
# File 'lib/puppet/resource/type_collection.rb', line 89

def add_definition(instance)
  dupe_check(instance, @hostclasses) { |dupe| "'#{instance.name}' is already defined#{dupe.error_context} as a class; cannot redefine as a definition" }
  dupe_check(instance, @definitions) { |dupe| "Definition '#{instance.name}' is already defined#{dupe.error_context}; cannot be redefined" }
  @definitions[instance.name] = instance
end

#add_hostclass(instance) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/puppet/resource/type_collection.rb', line 42

def add_hostclass(instance)
  dupe_check(instance, @hostclasses) { |dupe| "Class '#{instance.name}' is already defined#{dupe.error_context}; cannot redefine" }
  dupe_check(instance, @definitions) { |dupe| "Definition '#{instance.name}' is already defined#{dupe.error_context}; cannot be redefined as a class" }

  @hostclasses[instance.name] = instance
  instance
end

#add_node(instance) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/puppet/resource/type_collection.rb', line 54

def add_node(instance)
  dupe_check(instance, @nodes) { |dupe| "Node '#{instance.name}' is already defined#{dupe.error_context}; cannot redefine" }

  @node_list << instance
  @nodes[instance.name] = instance
  instance
end

#clearObject



4
5
6
7
8
# File 'lib/puppet/resource/type_collection.rb', line 4

def clear
  @hostclasses.clear
  @definitions.clear
  @nodes.clear
end

#definition(name) ⇒ Object



95
96
97
# File 'lib/puppet/resource/type_collection.rb', line 95

def definition(name)
  @definitions[munge_name(name)]
end

#find(namespaces, name, type) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/puppet/resource/type_collection.rb', line 99

def find(namespaces, name, type)
  #Array("") == [] for some reason
  namespaces = [namespaces] unless namespaces.is_a?(Array)

  if name =~ /^::/
    return send(type, name.sub(/^::/, ''))
  end

  namespaces.each do |namespace|
    ary = namespace.split("::")

    while ary.length > 0
      tmp_namespace = ary.join("::")
      if r = find_partially_qualified(tmp_namespace, name, type)
        return r
      end

      # Delete the second to last object, which reduces our namespace by one.
      ary.pop
    end

    if result = send(type, name)
      return result
    end
  end
  nil
end

#find_definition(namespaces, name) ⇒ Object



149
150
151
# File 'lib/puppet/resource/type_collection.rb', line 149

def find_definition(namespaces, name)
  find_or_load(namespaces, name, :definition)
end

#find_hostclass(namespaces, name) ⇒ Object



145
146
147
# File 'lib/puppet/resource/type_collection.rb', line 145

def find_hostclass(namespaces, name)
  find_or_load(namespaces, name, :hostclass)
end

#find_node(namespaces, name) ⇒ Object



141
142
143
# File 'lib/puppet/resource/type_collection.rb', line 141

def find_node(namespaces, name)
  find("", name, :node)
end

#find_or_load(namespaces, name, type) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/puppet/resource/type_collection.rb', line 127

def find_or_load(namespaces, name, type)
  name      = name.downcase
  namespaces = [namespaces] unless namespaces.is_a?(Array)
  namespaces = namespaces.collect { |ns| ns.downcase }

  # This could be done in the load_until, but the knowledge seems to
  # belong here.
  if r = find(namespaces, name, type)
    return r
  end

  loader.load_until(namespaces, name) { find(namespaces, name, type) }
end

#hostclass(name) ⇒ Object



50
51
52
# File 'lib/puppet/resource/type_collection.rb', line 50

def hostclass(name)
  @hostclasses[munge_name(name)]
end

#inspectObject



22
23
24
# File 'lib/puppet/resource/type_collection.rb', line 22

def inspect
  "TypeCollection" + { :hostclasses => @hostclasses.keys, :definitions => @definitions.keys, :nodes => @nodes.keys }.inspect
end

#loaderObject



62
63
64
65
# File 'lib/puppet/resource/type_collection.rb', line 62

def loader
  require 'puppet/parser/type_loader'
  @loader ||= Puppet::Parser::TypeLoader.new(environment)
end

#node(name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/puppet/resource/type_collection.rb', line 67

def node(name)
  name = munge_name(name)

  if node = @nodes[name]
    return node
  end

  @node_list.each do |node|
    next unless node.name_is_regex?
    return node if node.match(name)
  end
  nil
end

#node_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/puppet/resource/type_collection.rb', line 81

def node_exists?(name)
  @nodes[munge_name(name)]
end

#nodes?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/puppet/resource/type_collection.rb', line 85

def nodes?
  @nodes.length > 0
end

#perform_initial_importObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/puppet/resource/type_collection.rb', line 159

def perform_initial_import
  parser = Puppet::Parser::Parser.new(environment)
  if code = Puppet.settings.uninterpolated_value(:code, environment.to_s) and code != ""
    parser.string = code
  else
    file = Puppet.settings.value(:manifest, environment.to_s)
    parser.file = file
  end
  parser.parse
rescue => detail
  @parse_failed = true

  msg = "Could not parse for environment #{environment}: #{detail}"
  error = Puppet::Error.new(msg)
  error.set_backtrace(detail.backtrace)
  raise error
end

#require_reparse?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/puppet/resource/type_collection.rb', line 177

def require_reparse?
  @parse_failed || stale?
end

#stale?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/puppet/resource/type_collection.rb', line 181

def stale?
  @watched_files.values.detect { |file| file.changed? }
end

#versionObject



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/puppet/resource/type_collection.rb', line 185

def version
  return @version if defined?(@version)

  if environment[:config_version] == ""
    @version = Time.now.to_i
    return @version
  end

  @version = Puppet::Util.execute([environment[:config_version]]).strip

rescue Puppet::ExecutionFailure => e
  raise Puppet::ParseError, "Unable to set config_version: #{e.message}"
end

#watch_file(file) ⇒ Object



199
200
201
# File 'lib/puppet/resource/type_collection.rb', line 199

def watch_file(file)
  @watched_files[file] = Puppet::Util::LoadedFile.new(file)
end

#watching_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/puppet/resource/type_collection.rb', line 203

def watching_file?(file)
  @watched_files.include?(file)
end