Revision 031aec3e
Added by Ohad Levy about 12 years ago
- ID 031aec3e296fdbe671a52e514caea2459ed34b3a
app/models/host.rb | ||
---|---|---|
save(:validate => false)
|
||
|
||
# we want to import other information only if this host was never installed via Foreman
|
||
populateFieldsFromFacts if installed_at.nil?
|
||
populateFieldsFromFacts(facts.values) if installed_at.nil?
|
||
|
||
# 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.
|
||
... | ... | |
logger.warn "Failed to save #{facts.name}: #{e}"
|
||
end
|
||
|
||
def fv name
|
||
v=fact_values.first(:select => "fact_values.value", :joins => :fact_name,
|
||
:conditions => "fact_names.name = '#{name}'")
|
||
v.value unless v.nil?
|
||
end
|
||
def populateFieldsFromFacts facts
|
||
importer = Facts::Importer.new facts
|
||
|
||
def populateFieldsFromFacts
|
||
unless Setting[:ignore_puppet_facts_for_provisioning]
|
||
self.mac = fv(:macaddress).downcase unless fv(:macaddress).blank?
|
||
self.ip = fv(:ipaddress) if ip.nil?
|
||
end
|
||
self.domain = Domain.find_or_create_by_name fv(:domain) unless fv(:domain).empty?
|
||
# On solaris architecture fact is harwareisa
|
||
if myarch=fv(:architecture) || fv(:hardwareisa)
|
||
self.arch=Architecture.find_or_create_by_name myarch unless myarch.empty?
|
||
end
|
||
set_non_empty_values importer, [:domain, :architecture, :operatingsystem, :model]
|
||
set_non_empty_values importer, [:mac, :ip] unless Setting[:ignore_puppet_facts_for_provisioning]
|
||
|
||
# by default, puppet doesn't store an env name in the database
|
||
env = fv(:environment) || Setting[:default_puppet_environment]
|
||
if Setting[:update_environment_from_facts]
|
||
self.environment = Environment.find_or_create_by_name env
|
||
else
|
||
self.environment ||= Environment.find_or_create_by_name env
|
||
end
|
||
|
||
os_name = fv(:operatingsystem)
|
||
case os_name
|
||
when /(suse|sles)/i
|
||
orel = fv(:operatingsystemrelease)
|
||
set_non_empty_values importer, [:environment]
|
||
else
|
||
orel = fv(:lsbdistrelease) || fv(:operatingsystemrelease)
|
||
self.environment ||= importer.environment unless importer.environment.blank?
|
||
end
|
||
|
||
if orel.present?
|
||
major, minor = orel.split(".")
|
||
minor ||= ""
|
||
self.os = Operatingsystem.find_or_create_by_name_and_major_and_minor os_name, major, minor
|
||
end
|
||
|
||
unless self.model
|
||
modelname = fv(:productname) || fv(:model) || (fv(:is_virtual) == "true" ? fv(:virtual) : nil)
|
||
self.model = Model.find_or_create_by_name(modelname.strip) unless modelname.empty?
|
||
end
|
||
|
||
# again we are saving without validations as input is required (e.g. partition tables)
|
||
self.save(:validate => false)
|
||
end
|
||
|
||
# Called by build link in the list
|
||
... | ... | |
domain.resolver.getaddress(name_or_ip).to_s
|
||
end
|
||
|
||
def set_non_empty_values importer, methods
|
||
methods.each do |attr|
|
||
value = importer.send(attr)
|
||
self.send("#{attr}=", value) unless value.blank?
|
||
end
|
||
end
|
||
end
|
Also available in: Unified diff
refactor - added an importing class for puppet facts