Class: Puppet::Resource::TypeCollection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Warnings

clear_warnings, debug_once, notice_once, warnonce

Constructor Details

#initialize(env) ⇒ TypeCollection

Returns a new instance of TypeCollection.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/puppet/resource/type_collection.rb', line 19

def initialize(env)
  @environment = env
  @hostclasses = {}
  @definitions = {}
  @nodes = {}
  @notfound = {}

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

  @watched_files = Puppet::Util::FileWatcher.new
end

Instance Attribute Details

#environmentObject (readonly)



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

def environment
  @environment
end

#parse_failedObject



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

def parse_failed
  @parse_failed
end

Instance Method Details

#<<(thing) ⇒ Object



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

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

#add(instance) ⇒ Object



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

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



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

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



58
59
60
61
62
63
64
# File 'lib/puppet/resource/type_collection.rb', line 58

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



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

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



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

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

#definition(name) ⇒ Object



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

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

#find_definition(namespaces, name) ⇒ Object



122
123
124
# File 'lib/puppet/resource/type_collection.rb', line 122

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

#find_hostclass(namespaces, name, options = {}) ⇒ Object



118
119
120
# File 'lib/puppet/resource/type_collection.rb', line 118

def find_hostclass(namespaces, name, options = {})
  find_or_load(namespaces, name, :hostclass, options)
end

#find_node(namespaces, name) ⇒ Object



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

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

#hostclass(name) ⇒ Object



66
67
68
# File 'lib/puppet/resource/type_collection.rb', line 66

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

#import_ast(ast, modname) ⇒ Object



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

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

#inspectObject



38
39
40
# File 'lib/puppet/resource/type_collection.rb', line 38

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

#loaderObject



78
79
80
# File 'lib/puppet/resource/type_collection.rb', line 78

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

#node(name) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/puppet/resource/type_collection.rb', line 82

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)


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

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

#nodes?Boolean

Returns:

  • (Boolean)


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

def nodes?
  @nodes.length > 0
end

#require_reparse?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/puppet/resource/type_collection.rb', line 132

def require_reparse?
  @parse_failed || stale?
end

#stale?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/puppet/resource/type_collection.rb', line 136

def stale?
  @watched_files.changed?
end

#versionObject



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/puppet/resource/type_collection.rb', line 140

def version
  if !defined?(@version)
    if environment.config_version.nil? || environment.config_version == ""
      @version = Time.now.to_i
    else
      @version = Puppet::Util::Execution.execute([environment.config_version]).strip
    end
  end

  @version
rescue Puppet::ExecutionFailure => e
  raise Puppet::ParseError, "Execution of config_version command `#{environment.config_version}` failed: #{e.message}", e.backtrace
end

#watch_file(filename) ⇒ Object



154
155
156
# File 'lib/puppet/resource/type_collection.rb', line 154

def watch_file(filename)
  @watched_files.watch(filename)
end

#watching_file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/puppet/resource/type_collection.rb', line 158

def watching_file?(filename)
  @watched_files.watching?(filename)
end