Class: PuppetFactParser

Inherits:
FactParser show all
Defined in:
app/services/puppet_fact_parser.rb

Constant Summary

Constants inherited from FactParser

FactParser::ALIASES, FactParser::BONDS, FactParser::BRIDGES, FactParser::VIRTUAL, FactParser::VIRTUAL_NAMES, FactParser::VLANS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FactParser

#class_name_humanized, #comment, #has_comment?, #hostgroup, #initialize, #parse_interfaces?, parser_for, parsers, register_fact_parser

Constructor Details

This class inherits a constructor from FactParser

Instance Attribute Details

#factsObject (readonly)

Returns the value of attribute facts


2
3
4
# File 'app/services/puppet_fact_parser.rb', line 2

def facts
  @facts
end

Instance Method Details

#architectureObject


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/services/puppet_fact_parser.rb', line 49

def architecture
  # On solaris and junos architecture fact is hardwareisa
  name = case os_name
           when /(sunos|solaris|junos)/i
             facts[:hardwareisa]
           else
             facts[:architecture] || facts[:hardwareisa]
         end
  # ensure that we convert debian legacy to standard
  name = "x86_64" if name == "amd64"
  name = "aarch64" if name == "arm64"
  Architecture.where(:name => name).first_or_create if name.present?
end

#boot_timestampObject


117
118
119
120
121
# File 'app/services/puppet_fact_parser.rb', line 117

def boot_timestamp
  # system_uptime::seconds is Facter 3, we also fallback to Facter 2 uptime_seconds
  uptime_seconds = facts.fetch('system_uptime', {}).fetch('seconds', nil) || facts[:uptime_seconds]
  uptime_seconds.nil? ? nil : (Time.zone.now.to_i - uptime_seconds.to_i)
end

#certnameObject


109
110
111
# File 'app/services/puppet_fact_parser.rb', line 109

def certname
  facts[:clientcert]
end

#coresObject


139
140
141
# File 'app/services/puppet_fact_parser.rb', line 139

def cores
  facts.dig('processors', 'count') || facts['processorcount']
end

#disks_totalObject


143
144
145
# File 'app/services/puppet_fact_parser.rb', line 143

def disks_total
  facts['disks']&.values&.sum { |disk| disk&.fetch('size_bytes', 0).to_i }
end

#domainObject


70
71
72
73
# File 'app/services/puppet_fact_parser.rb', line 70

def domain
  name = facts[:domain]
  Domain.unscoped.where(:name => name).first_or_create if name.present?
end

#environmentObject


43
44
45
46
47
# File 'app/services/puppet_fact_parser.rb', line 43

def environment
  # by default, puppet doesn't store an env name in the database
  name = facts[:environment] || facts[:agent_specified_environment] || Setting[:default_puppet_environment]
  Environment.unscoped.where(:name => name).first_or_create
end

#interfacesObject


80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/services/puppet_fact_parser.rb', line 80

def interfaces
  interfaces = super
  return interfaces unless use_legacy_facts?
  underscore_device_regexp = /\A([^_]*)_(\d+)\z/
  interfaces.clone.each do |identifier, _|
    matches = identifier.match(underscore_device_regexp)
    next unless matches
    new_name = "#{matches[1]}.#{matches[2]}"
    interfaces[new_name] = interfaces.delete(identifier)
  end
  interfaces
end

#interfaces_attribute_map(attribute) ⇒ Object


93
94
95
96
97
98
99
100
# File 'app/services/puppet_fact_parser.rb', line 93

def interfaces_attribute_map(attribute)
  map = {
    'mac' => 'macaddress',
    'ip' => 'ipaddress',
    'ip6' => 'ipaddress6',
  }
  map.has_key?(attribute) ? map[attribute] : attribute
end

#ipmi_interfaceObject


75
76
77
78
# File 'app/services/puppet_fact_parser.rb', line 75

def ipmi_interface
  ipmi = facts.select { |name, _| name =~ /\Aipmi_(.*)\Z/ }.map { |name, value| [name.sub(/\Aipmi_/, ''), value] }
  Hash[ipmi].with_indifferent_access
end

#modelObject


63
64
65
66
67
68
# File 'app/services/puppet_fact_parser.rb', line 63

def model
  name = facts[:productname] || facts[:model] || facts[:boardproductname]
  # if its a virtual machine and we didn't get a model name, try using that instead.
  name ||= facts[:virtual] if virtual
  Model.where(:name => name.strip).first_or_create if name.present?
end

#operatingsystemObject


4
5
6
7
8
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
# File 'app/services/puppet_fact_parser.rb', line 4

def operatingsystem
  orel = os_release.dup

  if orel.present?
    major, minor = orel.split('.', 2)
    major = major.to_s.gsub(/\D/, '')
    minor = minor.to_s.gsub(/[^\d\.]/, '')
    args = {:name => os_name, :major => major, :minor => minor}
    os = Operatingsystem.find_or_initialize_by(args)
    if os_name[/debian|ubuntu/i] || os.family == 'Debian'
      if facts.dig(:os, :distro, :codename).presence || facts[:lsbdistcodename]
        os.release_name = facts.dig(:os, :distro, :codename).presence || facts[:lsbdistcodename]
      elsif os.release_name.blank?
        os.release_name = 'unknown'
      end
    end
  else
    os = Operatingsystem.find_or_initialize_by(:name => os_name)
  end

  if os.description.blank?
    if os_name == 'SLES'
      os.description = os_name + ' ' + orel.gsub('.', ' SP')
    elsif facts.dig(:os, :distro, :description).presence || facts[:lsbdistdescription]
      family = os.deduce_family || 'Operatingsystem'
      os = os.becomes(family.constantize)
      os.description = os.shorten_description(facts.dig(:os, :distro, :description).presence || facts[:lsbdistdescription])
    end
  end

  if os.new_record?
    os.save!
    Operatingsystem.find_by_id(os.id) # complete reload to be an instance of the STI subclass
  else
    os.save!
    os
  end
end

#ramObject


127
128
129
130
131
132
133
# File 'app/services/puppet_fact_parser.rb', line 127

def ram
  if (value = facts.dig('memory', 'system', 'total_bytes'))
    value / 1.megabyte
  else
    facts['memorysize_mb']
  end
end

#socketsObject


135
136
137
# File 'app/services/puppet_fact_parser.rb', line 135

def sockets
  facts.dig('processors', 'physicalcount') || facts['physicalprocessorcount']
end

#suggested_primary_interface(host) ⇒ Object


102
103
104
105
106
107
# File 'app/services/puppet_fact_parser.rb', line 102

def suggested_primary_interface(host)
  # facter 3.x: find 'primary' fact in 'networking' structure
  facter3_primary = facts.try(:fetch, "networking", nil).try(:fetch, "primary", nil)
  return [facter3_primary, interfaces[facter3_primary]] if facter3_primary
  super
end

#support_interfaces_parsing?Boolean

Returns:

  • (Boolean)

113
114
115
# File 'app/services/puppet_fact_parser.rb', line 113

def support_interfaces_parsing?
  true
end

#virtualObject


123
124
125
# File 'app/services/puppet_fact_parser.rb', line 123

def virtual
  facts['is_virtual']
end