Class: Puppet::Rails::Host

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
Benchmark
Includes:
Benchmark, Util, Util::CollectionMerger
Defined in:
lib/vendor/puppet/rails/host.rb

Constant Summary

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Benchmark

accumulate_benchmark, debug_benchmark, log_accumulated_marks, railsmark, time_debug?, write_benchmarks

Methods included from Util::CollectionMerger

#ar_hash_merge

Methods included from Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, #execfail, #execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, uri_to_path, wait_for_output, which, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Class Method Details

.from_puppet(node) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vendor/puppet/rails/host.rb', line 20

def self.from_puppet(node)
  host = find_by_name(node.name) || new(:name => node.name)

  {"ipaddress" => "ip", "environment" => "environment"}.each do |myparam, itsparam|
    if value = node.send(myparam)
      host.send(itsparam + "=", value)
    end
  end

  host
end

Instance Method Details

#add_new_resources(additions) ⇒ Object



149
150
151
152
153
# File 'lib/vendor/puppet/rails/host.rb', line 149

def add_new_resources(additions)
  additions.each do |resource|
    Puppet::Rails::Resource.from_parser_resource(self, resource)
  end
end

#build_rails_resource_from_parser_resource(resource) ⇒ Object

Turn a parser resource into a Rails resource.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/vendor/puppet/rails/host.rb', line 156

def build_rails_resource_from_parser_resource(resource)
  db_resource = nil
  accumulate_benchmark("Added resources", :initialization) {
    args = Puppet::Rails::Resource.rails_resource_initial_args(resource)

    db_resource = self.resources.build(args)

    # Our file= method does the name to id conversion.
    db_resource.file = resource.file
  }


  accumulate_benchmark("Added resources", :parameters) {
    resource.each do |param, value|
      Puppet::Rails::ParamValue.from_parser_param(param, value).each do |value_hash|
        db_resource.param_values.build(value_hash)
      end
    end
  }

  accumulate_benchmark("Added resources", :tags) {
    resource.tags.each { |tag| db_resource.add_resource_tag(tag) }
  }

  db_resource.save

  db_resource
end

#compare_to_catalog(existing, list) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/vendor/puppet/rails/host.rb', line 123

def compare_to_catalog(existing, list)
  compiled = list.inject({}) do |hash, resource|
    hash[resource.ref] = resource
    hash
  end

  resources = nil
  debug_benchmark("Resource removal") {
    resources = remove_unneeded_resources(compiled, existing)
  }

  # Now for all resources in the catalog but not in the db, we're pretty easy.
  additions = nil
  debug_benchmark("Resource merger") {
    additions = perform_resource_merger(compiled, resources)
  }

  debug_benchmark("Resource addition") {
    additions.each do |resource|
      build_rails_resource_from_parser_resource(resource)
    end

    log_accumulated_marks "Added resources"
  }
end

#environment=(value) ⇒ Object

Override the setter for environment to force it to be a string, lest it be YAML encoded. See #4487.



34
35
36
# File 'lib/vendor/puppet/rails/host.rb', line 34

def environment=(value)
  super value.to_s
end

#find_resourcesObject



109
110
111
112
113
114
115
116
# File 'lib/vendor/puppet/rails/host.rb', line 109

def find_resources
  condition = { :exported => true } if Puppet.settings[:thin_storeconfigs]

  resources.find(:all, :include => :source_file, :conditions => condition || {}).inject({}) do | hash, resource |
    hash[resource.id] = resource
    hash
  end
end

#find_resources_parameters(resources) ⇒ Object



231
232
233
234
235
236
237
238
# File 'lib/vendor/puppet/rails/host.rb', line 231

def find_resources_parameters(resources)
  params = Puppet::Rails::ParamValue.find_all_params_from_host(self)

  # assign each loaded parameters/tags to the resource it belongs to
  params.each do |param|
    resources[param['resource_id']].add_param_to_list(param) if resources.include?(param['resource_id'])
  end
end

#find_resources_parameters_tags(resources) ⇒ Object



118
119
120
121
# File 'lib/vendor/puppet/rails/host.rb', line 118

def find_resources_parameters_tags(resources)
  find_resources_parameters(resources)
  find_resources_tags(resources)
end

#find_resources_tags(resources) ⇒ Object



240
241
242
243
244
245
246
# File 'lib/vendor/puppet/rails/host.rb', line 240

def find_resources_tags(resources)
  tags = Puppet::Rails::ResourceTag.find_all_tags_from_host(self)

  tags.each do |tag|
    resources[tag['resource_id']].add_tag_to_list(tag) if resources.include?(tag['resource_id'])
  end
end

#get_facts_hashObject

returns a hash of fact_names.name => [ fact_values ] for this host. Note that ‘fact_values’ is actually a list of the value instances, not just actual values.



41
42
43
44
45
46
47
48
# File 'lib/vendor/puppet/rails/host.rb', line 41

def get_facts_hash
  fact_values = self.fact_values.find(:all, :include => :fact_name)
  return fact_values.inject({}) do | hash, value |
    hash[value.fact_name.name] ||= []
    hash[value.fact_name.name] << value
    hash
  end
end

#merge_facts(facts) ⇒ Object

This is very similar to the merge_parameters method of Puppet::Rails::Resource.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vendor/puppet/rails/host.rb', line 53

def merge_facts(facts)
  db_facts = {}

  deletions = []
  self.fact_values.find(:all, :include => :fact_name).each do |value|
    deletions << value['id'] and next unless facts.include?(value['name'])
    # Now store them for later testing.
    db_facts[value['name']] ||= []
    db_facts[value['name']] << value
  end

  # Now get rid of any parameters whose value list is different.
  # This might be extra work in cases where an array has added or lost
  # a single value, but in the most common case (a single value has changed)
  # this makes sense.
  db_facts.each do |name, value_hashes|
    values = value_hashes.collect { |v| v['value'] }

    unless values == facts[name]
      value_hashes.each { |v| deletions << v['id'] }
    end
  end

  # Perform our deletions.
  Puppet::Rails::FactValue.delete(deletions) unless deletions.empty?

  # Lastly, add any new parameters.
  facts.each do |name, value|
    next if db_facts.include?(name)
    values = value.is_a?(Array) ? value : [value]

    values.each do |v|
      fact_values.build(:value => v, :fact_name => Puppet::Rails::FactName.find_or_create_by_name(name))
    end
  end
end

#merge_resources(list) ⇒ Object

Set our resources.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/vendor/puppet/rails/host.rb', line 91

def merge_resources(list)
  # keep only exported resources in thin_storeconfig mode
  list = list.select { |r| r.exported? } if Puppet.settings[:thin_storeconfigs]

  resources_by_id = nil
  debug_benchmark("Searched for resources") {
    resources_by_id = find_resources
  }

  debug_benchmark("Searched for resource params and tags") {
    find_resources_parameters_tags(resources_by_id)
  } if id

  debug_benchmark("Performed resource comparison") {
    compare_to_catalog(resources_by_id, list)
  }
end

#perform_resource_merger(compiled, resources) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/vendor/puppet/rails/host.rb', line 186

def perform_resource_merger(compiled, resources)
  return compiled.values if resources.empty?

  # Now for all resources in the catalog but not in the db, we're pretty easy.
  additions = []
  compiled.each do |ref, resource|
    if db_resource = resources[ref]
      db_resource.merge_parser_resource(resource)
    else
      additions << resource
    end
  end
  log_accumulated_marks "Resource merger"

  additions
end

#remove_unneeded_resources(compiled, existing) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/vendor/puppet/rails/host.rb', line 203

def remove_unneeded_resources(compiled, existing)
  deletions = []
  resources = {}
  existing.each do |id, resource|
    # it seems that it can happen (see bug #2010) some resources are duplicated in the
    # database (ie logically corrupted database), in which case we remove the extraneous
    # entries.
    if resources.include?(resource.ref)
      deletions << id
      next
    end

    # If the resource is in the db but not in the catalog, mark it
    # for removal.
    unless compiled.include?(resource.ref)
      deletions << id
      next
    end

    resources[resource.ref] = resource
  end
  # We need to use 'destroy' here, not 'delete', so that all
  # dependent objects get removed, too.
  Puppet::Rails::Resource.destroy(deletions) unless deletions.empty?

  resources
end

#to_puppetObject



248
249
250
251
252
253
254
255
256
257
# File 'lib/vendor/puppet/rails/host.rb', line 248

def to_puppet
  node = Puppet::Node.new(self.name)
  {"ip" => "ipaddress", "environment" => "environment"}.each do |myparam, itsparam|
    if value = send(myparam)
      node.send(itsparam + "=", value)
    end
  end

  node
end