foreman/app/services/facts_parser.rb @ 7e2880b6
031aec3e | Ohad Levy | module Facts
|
|
fb68fd22 | Ohad Levy | class Parser
|
|
031aec3e | Ohad Levy | attr_reader :facts
|
|
def initialize facts
|
|||
@facts = HashWithIndifferentAccess.new(facts)
|
|||
end
|
|||
def operatingsystem
|
|||
orel = case os_name
|
|||
ef3a1338 | Antony Perigault | when /(suse|sles|gentoo)/i
|
|
031aec3e | Ohad Levy | facts[:operatingsystemrelease]
|
|
else
|
|||
facts[:lsbdistrelease] || facts[:operatingsystemrelease]
|
|||
end
|
|||
ba71c565 | Greg Sutcliffe | if os_name == "Archlinux"
|
|
# Archlinux is rolling release, so it has no release. We use 1.0 always
|
|||
10130849 | Ohad Levy | args = { :name => os_name, :major => "1", :minor => "0" }
|
|
c2c32409 | Greg Sutcliffe | os = Operatingsystem.where(args).first || Operatingsystem.new(args)
|
|
ba71c565 | Greg Sutcliffe | elsif orel.present?
|
|
c604dec7 | Nicolas Dandrimont | if os_name == "Debian" and orel[/testing|unstable/i]
|
|
case facts[:lsbdistcodename]
|
|||
when /wheezy/i
|
|||
orel = "7"
|
|||
when /jessie/i
|
|||
orel = "8"
|
|||
when /sid/i
|
|||
orel = "99"
|
|||
end
|
|||
7e2880b6 | Ruediger Mueck | elsif os_name[/AIX/i]
|
|
majoraix, tlaix, spaix, yearaix = orel.split("-")
|
|||
orel = majoraix + "." + tlaix + spaix
|
|||
c604dec7 | Nicolas Dandrimont | end
|
|
031aec3e | Ohad Levy | major, minor = orel.split(".")
|
|
9628014a | Joseph Mitchell Magen | major.to_s.gsub!(/\D/,'') unless is_numeric? major
|
|
minor.to_s.gsub!(/\D/,'') unless is_numeric? minor
|
|||
args = { :name => os_name, :major => major.to_s, :minor => minor.to_s }
|
|||
10130849 | Ohad Levy | os = Operatingsystem.where(args).first || Operatingsystem.create!(args)
|
|
c2c32409 | Greg Sutcliffe | os.release_name = facts[:lsbdistcodename] if facts[:lsbdistcodename] && (os_name[/debian|ubuntu/i] || os.family == 'Debian')
|
|
031aec3e | Ohad Levy | else
|
|
c2c32409 | Greg Sutcliffe | os = Operatingsystem.find_by_name(os_name) || Operatingsystem.create!(:name => os_name)
|
|
end
|
|||
if facts[:lsbdistdescription] && os.description.blank?
|
|||
family = os.deduce_family || 'Operatingsystem'
|
|||
os.description = family.constantize.shorten_description facts[:lsbdistdescription]
|
|||
031aec3e | Ohad Levy | end
|
|
c2c32409 | Greg Sutcliffe | os.save!
|
|
os
|
|||
031aec3e | Ohad Levy | end
|
|
def environment
|
|||
# by default, puppet doesn't store an env name in the database
|
|||
name = facts[:environment] || Setting[:default_puppet_environment]
|
|||
Environment.find_or_create_by_name name
|
|||
end
|
|||
def architecture
|
|||
# On solaris architecture fact is harwareisa
|
|||
0813600a | theforemanuser123 | name = case os_name
|
|
when /(sunos|solaris)/i
|
|||
facts[:hardwareisa]
|
|||
else
|
|||
facts[:architecture] || facts[:hardwareisa]
|
|||
end
|
|||
0454b904 | Ohad Levy | # ensure that we convert debian legacy to standard
|
|
name = "x86_64" if name == "amd64"
|
|||
031aec3e | Ohad Levy | Architecture.find_or_create_by_name name unless name.blank?
|
|
end
|
|||
def model
|
|||
011049c6 | Michael Moll | name = facts[:productname] || facts[:model] || facts[:boardproductname]
|
|
031aec3e | Ohad Levy | # if its a virtual machine and we didn't get a model name, try using that instead.
|
|
name ||= facts[:is_virtual] == "true" ? facts[:virtual] : nil
|
|||
Model.find_or_create_by_name(name.strip) unless name.blank?
|
|||
end
|
|||
def domain
|
|||
name = facts[:domain]
|
|||
Domain.find_or_create_by_name name unless name.blank?
|
|||
end
|
|||
def primary_interface
|
|||
mac = facts[:macaddress]
|
|||
ip = facts[:ipaddress]
|
|||
interfaces.each do |int, values|
|
|||
return int.to_s if (values[:mac] == mac and values[:ip] == ip)
|
|||
end
|
|||
nil
|
|||
end
|
|||
41402094 | Ohad Levy | EXCLUDED_INTERFACES = %w[lo usb0] unless defined?(EXCLUDED_INTERFACES)
|
|
031aec3e | Ohad Levy | ||
def interfaces
|
|||
ifs = facts[:interfaces]
|
|||
return {} if ifs.empty? or (ifs=ifs.split(",")).empty?
|
|||
interfaces = HashWithIndifferentAccess.new
|
|||
(ifs - EXCLUDED_INTERFACES).each do |int|
|
|||
67799065 | Ohad Levy | if (ip = facts["ipaddress_#{int}".to_sym]) and (mac = facts["macaddress_#{int}".to_sym])
|
|
031aec3e | Ohad Levy | interfaces[int] = { :ip => ip, :mac => mac }
|
|
end
|
|||
end
|
|||
interfaces
|
|||
end
|
|||
# TODO: Remove these method once interfaces management is enabled
|
|||
def mac
|
|||
7a7a7a6b | Ohad Levy | facts[:macaddress] == "00:00:00:00:00:00" ? nil : facts[:macaddress]
|
|
031aec3e | Ohad Levy | end
|
|
def ip
|
|||
facts[:ipaddress]
|
|||
end
|
|||
370a7ac7 | Ohad Levy | ||
def certname
|
|||
facts[:clientcert]
|
|||
end
|
|||
0813600a | theforemanuser123 | ||
private
|
|||
def os_name
|
|||
01055e77 | Greg Sutcliffe | facts[:operatingsystem].blank? ? raise(::Foreman::Exception.new("invalid facts, missing operating system value")) : facts[:operatingsystem]
|
|
0813600a | theforemanuser123 | end
|
|
fe838fad | Greg Sutcliffe | ||
def is_numeric?(string)
|
|||
begin
|
|||
!!Integer(string)
|
|||
rescue ArgumentError, TypeError
|
|||
false
|
|||
end
|
|||
end
|
|||
031aec3e | Ohad Levy | end
|
|
end
|