Class: Puppet::Resource::TypeCollection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ TypeCollection

Returns a new instance of TypeCollection.



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

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.



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

def environment
  @environment
end

#parse_failedObject

Returns the value of attribute parse_failed.



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

def parse_failed
  @parse_failed
end

Instance Method Details

#<<(thing) ⇒ Object



36
37
38
39
# File 'lib/vendor/puppet/resource/type_collection.rb', line 36

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

#add(instance) ⇒ Object



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

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



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

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



52
53
54
55
56
57
58
# File 'lib/vendor/puppet/resource/type_collection.rb', line 52

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



64
65
66
67
68
69
70
# File 'lib/vendor/puppet/resource/type_collection.rb', line 64

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



7
8
9
10
11
12
# File 'lib/vendor/puppet/resource/type_collection.rb', line 7

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

#definition(name) ⇒ Object



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

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

#find_definition(namespaces, name) ⇒ Object



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

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

#find_hostclass(namespaces, name) ⇒ Object



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

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

#find_node(namespaces, name) ⇒ Object



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

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

#hostclass(name) ⇒ Object



60
61
62
# File 'lib/vendor/puppet/resource/type_collection.rb', line 60

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

#import_ast(ast, modname) ⇒ Object



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

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

#inspectObject



32
33
34
# File 'lib/vendor/puppet/resource/type_collection.rb', line 32

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

#loaderObject



72
73
74
# File 'lib/vendor/puppet/resource/type_collection.rb', line 72

def loader
  @loader ||= Puppet::Parser::TypeLoader.new(environment)
end

#node(name) ⇒ Object



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

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)


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

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

#nodes?Boolean

Returns:

  • (Boolean)


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

def nodes?
  @nodes.length > 0
end

#require_reparse?Boolean

Returns:

  • (Boolean)


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

def require_reparse?
  @parse_failed || stale?
end

#stale?Boolean

Returns:

  • (Boolean)


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

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

#versionObject



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

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



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

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

#watching_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


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

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