Class: Host::Base

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Foreman::STI
Defined in:
app/models/host/base.rb

Direct Known Subclasses

Managed

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Foreman::STI

included, #save_with_type

Class Method Details

.attributes_protected_by_defaultObject


54
55
56
# File 'app/models/host/base.rb', line 54

def self.attributes_protected_by_default
  super - [ inheritance_column ]
end

.importHostAndFacts(yaml) ⇒ Object


58
59
# File 'app/models/host/base.rb', line 58

def self.importHostAndFacts yaml
end

Instance Method Details

#attributes_to_import_from_factsObject


137
138
139
# File 'app/models/host/base.rb', line 137

def attributes_to_import_from_facts
  attrs = [:model]
end

#facts_hashObject


163
164
165
166
167
168
169
# File 'app/models/host/base.rb', line 163

def facts_hash
  hash = {}
  fact_values.all(:include => :fact_name).collect do |fact|
    hash[fact.fact_name.name] = fact.value
  end
  hash
end

#importFacts(name, facts) ⇒ Object

import host facts, required when running without storeconfigs. expect a Puppet::Node::Facts


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
89
90
# File 'app/models/host/base.rb', line 63

def importFacts name, facts

  # we are not importing facts for hosts in build state (e.g. waiting for a re-installation)
  raise ::Foreman::Exception.new(N_("Host is pending for Build")) if build
  time = facts[:_timestamp]
  time = time.to_time if time.is_a?(String)

  # we are not doing anything we already processed this fact (or a newer one)
  if time
    return true unless last_compile.nil? or (last_compile + 1.minute < time)
    self.last_compile = time
  end
  # save all other facts
  merge_facts(facts)
  save(:validate => false)

  populateFieldsFromFacts(facts)

  # we are saving here with no validations, as we want this process to be as fast
  # as possible, assuming we already have all the right settings in Foreman.
  # If we don't (e.g. we never install the server via Foreman, we populate the fields from facts
  # TODO: if it was installed by Foreman and there is a mismatch,
  # we should probably send out an alert.
  return self.save(:validate => false)

rescue Exception => e
  logger.warn "Failed to save #{name}: #{e}"
end

#is_name_downcased?Boolean


158
159
160
161
# File 'app/models/host/base.rb', line 158

def is_name_downcased?
  return unless name.present?
  errors.add(:name, _("must be lowercase")) unless name == name.downcase
end

#merge_facts(facts) ⇒ Object

Inspired from Puppet::Rails:Host


93
94
95
96
97
98
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
126
127
128
129
130
131
132
133
134
135
# File 'app/models/host/base.rb', line 93

def merge_facts(facts)
  db_facts = {}

  deletions = []
  fact_values.includes(: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|
    db_values = value_hashes.collect { |v| v['value'] }
    value = facts[name]
    values = value.is_a?(Array) ? value : [value.to_s]

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

  FactValue.delete(deletions) unless deletions.empty?

  # Get FactNames in one call
  fact_names = FactName.maximum(:id, :group => 'name')

  # Create any needed new FactNames
  facts['_timestamp'] = facts.delete(:_timestamp) if facts.include?(:_timestamp)
  facts.each do |name, value|
    next if db_facts.include?(name)
    values = value.is_a?(Array) ? value : [value]

    values.each do |v|
      next if v.nil?
      fact_values.build(:value => v,
                        :fact_name_id => fact_names[name] || FactName.create!(:name => name).id)
    end
  end
end

#populateFieldsFromFacts(facts = self.facts_hash) ⇒ Object


141
142
143
144
145
146
147
148
149
# File 'app/models/host/base.rb', line 141

def populateFieldsFromFacts facts = self.facts_hash
  # we don't import facts for host in build mode
  return if build?

  importer = Facts::Importer.new facts

  set_non_empty_values importer, attributes_to_import_from_facts
  importer
end

#set_non_empty_values(importer, methods) ⇒ Object


151
152
153
154
155
156
# File 'app/models/host/base.rb', line 151

def set_non_empty_values importer, methods
  methods.each do |attr|
    value = importer.send(attr)
    self.send("#{attr}=", value) unless value.blank?
  end
end

#to_paramObject


171
172
173
# File 'app/models/host/base.rb', line 171

def to_param
  name
end