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.



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

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

#parse_failedObject

Returns the value of attribute parse_failed.



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

def parse_failed
  @parse_failed
end

Instance Method Details

#<<(thing) ⇒ Object



34
35
36
37
# File 'lib/puppet/resource/type_collection.rb', line 34

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

#add(instance) ⇒ Object



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

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



97
98
99
100
101
# File 'lib/puppet/resource/type_collection.rb', line 97

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



50
51
52
53
54
55
56
# File 'lib/puppet/resource/type_collection.rb', line 50

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



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

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



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

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

#definition(name) ⇒ Object



103
104
105
# File 'lib/puppet/resource/type_collection.rb', line 103

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

#find_definition(namespaces, name) ⇒ Object



115
116
117
# File 'lib/puppet/resource/type_collection.rb', line 115

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

#find_hostclass(namespaces, name) ⇒ Object



111
112
113
# File 'lib/puppet/resource/type_collection.rb', line 111

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

#find_node(namespaces, name) ⇒ Object



107
108
109
# File 'lib/puppet/resource/type_collection.rb', line 107

def find_node(namespaces, name)
  @nodes[munge_name(name)]
end

#hostclass(name) ⇒ Object



58
59
60
# File 'lib/puppet/resource/type_collection.rb', line 58

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

#import_ast(ast, modname) ⇒ Object



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

def import_ast(ast, modname)
  ast.instantiate(modname).each do |instance|
    add(instance)
  end
end

#inspectObject



30
31
32
# File 'lib/puppet/resource/type_collection.rb', line 30

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

#loaderObject



70
71
72
73
# File 'lib/puppet/resource/type_collection.rb', line 70

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

#node(name) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/puppet/resource/type_collection.rb', line 75

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)


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

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

#nodes?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/puppet/resource/type_collection.rb', line 93

def nodes?
  @nodes.length > 0
end

#require_reparse?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/puppet/resource/type_collection.rb', line 125

def require_reparse?
  @parse_failed || stale?
end

#stale?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/puppet/resource/type_collection.rb', line 129

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

#versionObject



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/puppet/resource/type_collection.rb', line 133

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



147
148
149
# File 'lib/puppet/resource/type_collection.rb', line 147

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

#watching_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/puppet/resource/type_collection.rb', line 151

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