Module: ForemanInventoryUpload::Generators::FactHelpers

Extended by:
ActiveSupport::Concern
Included in:
Metadata, Slice
Defined in:
lib/foreman_inventory_upload/generators/fact_helpers.rb

Constant Summary collapse

CLOUD_AMAZON =
'aws'
CLOUD_GOOGLE =
'google'
CLOUD_AZURE =
'azure'
CLOUD_ALIBABA =
'alibaba'
UUID_REGEX =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i

Instance Method Summary collapse

Instance Method Details

#account_id(organization) ⇒ Object



26
27
28
29
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 26

def (organization)
  @organization_accounts ||= {}
  @organization_accounts[organization.id] ||= organization.pools.where.not(account_number: nil).pluck(:account_number).first
end

#bios_uuid(host) ⇒ Object



124
125
126
127
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 124

def bios_uuid(host)
  value = fact_value(host, 'dmi::system::uuid') || ''
  uuid_value(value)
end

#cloud_provider(host) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 39

def cloud_provider(host)
  bios_version = fact_value(host, 'dmi::bios::version')

  if bios_version
    return CLOUD_AMAZON if bios_version.downcase['amazon']
    return CLOUD_GOOGLE if bios_version.downcase['google']
  end

  chassis_asset_tag = fact_value(host, 'dmi::chassis::asset_tag')
  return CLOUD_AZURE if chassis_asset_tag && chassis_asset_tag['7783-7084-3265-9085-8269-3286-77']

  system_manufacturer = fact_value(host, 'dmi::system::manufacturer')
  return CLOUD_ALIBABA if system_manufacturer && system_manufacturer.downcase['alibaba cloud']

  product_name = fact_value(host, 'dmi::system::product_name')
  return CLOUD_ALIBABA if product_name && product_name.downcase['alibaba cloud ecs']

  nil
end

#fact_value(host, fact_name) ⇒ Object



15
16
17
18
19
20
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 15

def fact_value(host, fact_name)
  value_record = host.fact_values.find do |fact_value|
    fact_value.fact_name_id == ForemanInventoryUpload::Generators::Queries.fact_names[fact_name]
  end
  value_record&.value
end

#fqdn(host) ⇒ Object



67
68
69
70
71
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 67

def fqdn(host)
  return host.fqdn unless obfuscate_hostname?(host)

  fact_value(host, 'insights_client::hostname') || obfuscate_fqdn(host.fqdn)
end

#golden_ticket?(organization) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 31

def golden_ticket?(organization)
  result = organization.try(:golden_ticket?)
  result = organization.content_access_mode == 'org_environment' if result.nil?

  @organization_golden_tickets ||= {}
  @organization_golden_tickets[organization.id] ||= result
end

#host_ips(host) ⇒ Object



85
86
87
88
89
90
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 85

def host_ips(host)
  return obfuscated_ips(host) if obfuscate_ips?(host)

  # return a pass through proxy hash in case no obfuscation needed
  Hash.new { |h, k| k }
end

#hostname_matchObject



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 112

def hostname_match
  bash_hostname = `uname -n`.chomp
  foreman_hostname = ForemanRhCloud.foreman_host&.name
  if bash_hostname == foreman_hostname
    fqdn(ForemanRhCloud.foreman_host)
  elsif Setting[:obfuscate_inventory_hostnames]
    obfuscate_fqdn(bash_hostname)
  else
    bash_hostname
  end
end

#kilobytes_to_bytes(kilobytes) ⇒ Object



22
23
24
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 22

def kilobytes_to_bytes(kilobytes)
  kilobytes * 1024
end

#obfuscate_fqdn(fqdn) ⇒ Object



73
74
75
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 73

def obfuscate_fqdn(fqdn)
  "#{Digest::SHA1.hexdigest(fqdn)}.example.com"
end

#obfuscate_hostname?(host) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 59

def obfuscate_hostname?(host)
  insights_client_setting = fact_value(host, 'insights_client::obfuscate_hostname_enabled')
  insights_client_setting = ActiveModel::Type::Boolean.new.cast(insights_client_setting)
  return insights_client_setting unless insights_client_setting.nil?

  Setting[:obfuscate_inventory_hostnames]
end

#obfuscate_ip(ip, ips_dict) ⇒ Object



106
107
108
109
110
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 106

def obfuscate_ip(ip, ips_dict)
  max_obfuscated = ips_dict.values.map { |v| IPAddr.new(v).to_i }.max || IPAddr.new('10.230.230.0').to_i

  IPAddr.new(max_obfuscated + 1, Socket::AF_INET).to_s
end

#obfuscate_ips?(host) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 77

def obfuscate_ips?(host)
  insights_client_setting = fact_value(host, 'insights_client::obfuscate_ip_enabled')
  insights_client_setting = ActiveModel::Type::Boolean.new.cast(insights_client_setting)
  return insights_client_setting unless insights_client_setting.nil?

  Setting[:obfuscate_inventory_ips]
end

#obfuscated_ips(host) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 92

def obfuscated_ips(host)
  insights_client_ips = JSON.parse(fact_value(host, 'insights_client::ips') || '[]')

  obfuscated_ips = Hash[
    insights_client_ips.map { |ip_record| [ip_record['original'], ip_record['obfuscated']] }
  ]

  obfuscated_ips.default_proc = proc do |hash, key|
    hash[key] = obfuscate_ip(key, hash)
  end

  obfuscated_ips
end

#uuid_value(value) ⇒ Object



129
130
131
132
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 129

def uuid_value(value)
  uuid_match = UUID_REGEX.match(value)
  uuid_match&.to_s
end

#uuid_value!(value) ⇒ Object

Raises:

  • (Foreman::Exception)


134
135
136
137
138
139
# File 'lib/foreman_inventory_upload/generators/fact_helpers.rb', line 134

def uuid_value!(value)
  uuid = uuid_value(value)
  raise Foreman::Exception.new(N_('Value %{value} is not a valid UUID') % {value: value}) if value && uuid.empty?

  uuid
end