Class: Facts::Importer

Inherits:
Object
  • Object
show all
Defined in:
app/models/facts_importer.rb

Constant Summary collapse

EXCLUDED_INTERFACES =
%w[lo usb0]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(facts) ⇒ Importer


5
6
7
# File 'app/models/facts_importer.rb', line 5

def initialize facts
  @facts = HashWithIndifferentAccess.new(facts)
end

Instance Attribute Details

#factsObject (readonly)

Returns the value of attribute facts


3
4
5
# File 'app/models/facts_importer.rb', line 3

def facts
  @facts
end

Instance Method Details

#architectureObject


53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/facts_importer.rb', line 53

def architecture
  # On solaris architecture fact is harwareisa
  name = case os_name
           when /(sunos|solaris)/i
             facts[:hardwareisa]
           else
             facts[:architecture] || facts[:hardwareisa]
           end
  # ensure that we convert debian legacy to standard
  name = "x86_64" if name == "amd64"
  Architecture.find_or_create_by_name name unless name.blank?
end

#certnameObject


111
112
113
# File 'app/models/facts_importer.rb', line 111

def certname
  facts[:clientcert]
end

#domainObject


73
74
75
76
# File 'app/models/facts_importer.rb', line 73

def domain
  name = facts[:domain]
  Domain.find_or_create_by_name name unless name.blank?
end

#environmentObject


47
48
49
50
51
# File 'app/models/facts_importer.rb', line 47

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

#interfacesObject


89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/facts_importer.rb', line 89

def interfaces
  ifs = facts[:interfaces]
  return {} if ifs.empty? or (ifs=ifs.split(",")).empty?
  interfaces = HashWithIndifferentAccess.new

  (ifs - EXCLUDED_INTERFACES).each do |int|
    if (ip = facts["ipaddress_#{int}".to_sym]) and (mac = facts["macaddress_#{int}".to_sym])
      interfaces[int] = { :ip => ip, :mac => mac }
    end
  end
  interfaces
end

#ipObject


107
108
109
# File 'app/models/facts_importer.rb', line 107

def ip
  facts[:ipaddress]
end

#macObject

TODO: Remove these method once interfaces management is enabled


103
104
105
# File 'app/models/facts_importer.rb', line 103

def mac
  facts[:macaddress] == "00:00:00:00:00:00" ? nil : facts[:macaddress]
end

#modelObject


66
67
68
69
70
71
# File 'app/models/facts_importer.rb', line 66

def model
  name = facts[:productname] || facts[:model]
  # 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

#operatingsystemObject


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/facts_importer.rb', line 9

def operatingsystem
  orel    = case os_name
              when /(suse|sles|gentoo)/i
                facts[:operatingsystemrelease]
              else
                facts[:lsbdistrelease] || facts[:operatingsystemrelease]
            end

  if os_name == "Archlinux"
    # Archlinux is rolling release, so it has no release. We use 1.0 always
    args = { :name => os_name, :major => "1", :minor => "0" }
    Operatingsystem.where(args).first || Operatingsystem.create!(args)
  elsif orel.present?
    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
    end
    major, minor = orel.split(".")
    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 }
    os = Operatingsystem.where(args).first || Operatingsystem.create!(args)
    if os_name[/debian|ubuntu/i] or os.family == 'Debian'
      os.release_name = facts[:lsbdistcodename]
      os.save
    end
    os
  else
    Operatingsystem.find_by_name(os_name) || Operatingsystem.create!(:name => os_name)
  end
end

#primary_interfaceObject


78
79
80
81
82
83
84
85
# File 'app/models/facts_importer.rb', line 78

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