facter 4.0.51 → 4.0.52

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/lib/docs/generate_cli.rb +7 -0
  3. data/lib/facter.rb +57 -44
  4. data/lib/facter/custom_facts/core/aggregate.rb +9 -0
  5. data/lib/facter/custom_facts/core/resolvable.rb +11 -0
  6. data/lib/facter/custom_facts/util/resolution.rb +2 -0
  7. data/lib/facter/facts/amzn/lsbdistcodename.rb +16 -0
  8. data/lib/facter/facts/amzn/lsbdistdescription.rb +16 -0
  9. data/lib/facter/facts/amzn/lsbdistid.rb +16 -0
  10. data/lib/facter/facts/amzn/os/distro/codename.rb +24 -0
  11. data/lib/facter/facts/amzn/os/distro/description.rb +21 -0
  12. data/lib/facter/facts/amzn/os/distro/id.rb +21 -0
  13. data/lib/facter/facts/amzn/os/distro/release.rb +32 -0
  14. data/lib/facter/facts/linux/az_metadata.rb +27 -0
  15. data/lib/facter/facts/linux/cloud/provider.rb +17 -0
  16. data/lib/facter/facts/linux/dhcp_servers.rb +2 -2
  17. data/lib/facter/facts/linux/interfaces.rb +1 -1
  18. data/lib/facter/facts/linux/ipaddress6_interfaces.rb +1 -1
  19. data/lib/facter/facts/linux/ipaddress_interfaces.rb +1 -1
  20. data/lib/facter/facts/linux/macaddress_interfaces.rb +1 -1
  21. data/lib/facter/facts/linux/mtu_interfaces.rb +1 -1
  22. data/lib/facter/facts/linux/netmask6_interfaces.rb +1 -1
  23. data/lib/facter/facts/linux/netmask_interfaces.rb +1 -1
  24. data/lib/facter/facts/linux/network6_interfaces.rb +1 -1
  25. data/lib/facter/facts/linux/network_interfaces.rb +1 -1
  26. data/lib/facter/facts/linux/networking/dhcp.rb +1 -1
  27. data/lib/facter/facts/linux/networking/domain.rb +1 -1
  28. data/lib/facter/facts/linux/networking/fqdn.rb +1 -1
  29. data/lib/facter/facts/linux/networking/hostname.rb +1 -1
  30. data/lib/facter/facts/linux/networking/interfaces.rb +1 -1
  31. data/lib/facter/facts/linux/networking/ip.rb +1 -1
  32. data/lib/facter/facts/linux/networking/ip6.rb +1 -1
  33. data/lib/facter/facts/linux/networking/mac.rb +1 -1
  34. data/lib/facter/facts/linux/networking/mtu.rb +1 -1
  35. data/lib/facter/facts/linux/networking/netmask.rb +1 -1
  36. data/lib/facter/facts/linux/networking/netmask6.rb +1 -1
  37. data/lib/facter/facts/linux/networking/network.rb +1 -1
  38. data/lib/facter/facts/linux/networking/network6.rb +1 -1
  39. data/lib/facter/facts/linux/networking/primary.rb +1 -1
  40. data/lib/facter/facts/linux/networking/scope6.rb +1 -1
  41. data/lib/facter/facts/linux/scope6_interfaces.rb +1 -1
  42. data/lib/facter/facts/rhel/lsbdistcodename.rb +16 -0
  43. data/lib/facter/facts/rhel/lsbdistdescription.rb +16 -0
  44. data/lib/facter/facts/rhel/lsbdistid.rb +16 -0
  45. data/lib/facter/facts/rhel/os/distro/codename.rb +2 -4
  46. data/lib/facter/facts/rhel/os/distro/description.rb +2 -4
  47. data/lib/facter/facts/rhel/os/distro/id.rb +2 -4
  48. data/lib/facter/facts/rhel/os/distro/release.rb +13 -12
  49. data/lib/facter/facts/sles/lsbdistcodename.rb +16 -0
  50. data/lib/facter/facts/sles/lsbdistdescription.rb +16 -0
  51. data/lib/facter/facts/sles/lsbdistid.rb +16 -0
  52. data/lib/facter/facts/sles/os/distro/codename.rb +3 -4
  53. data/lib/facter/facts/sles/os/distro/description.rb +2 -5
  54. data/lib/facter/facts/sles/os/distro/id.rb +6 -5
  55. data/lib/facter/facts/sles/os/distro/release.rb +17 -12
  56. data/lib/facter/facts/ubuntu/lsbdistrelease.rb +2 -2
  57. data/lib/facter/facts/windows/az_metadata.rb +27 -0
  58. data/lib/facter/facts/windows/cloud/provider.rb +17 -0
  59. data/lib/facter/framework/cli/cli.rb +5 -5
  60. data/lib/facter/framework/config/config_reader.rb +2 -0
  61. data/lib/facter/framework/config/fact_groups.rb +25 -2
  62. data/lib/facter/framework/core/cache_manager.rb +5 -1
  63. data/lib/facter/framework/core/fact_filter.rb +3 -3
  64. data/lib/facter/framework/core/fact_loaders/fact_loader.rb +0 -1
  65. data/lib/facter/framework/core/fact_manager.rb +5 -3
  66. data/lib/facter/framework/core/options.rb +1 -2
  67. data/lib/facter/framework/core/options/option_store.rb +1 -3
  68. data/lib/facter/framework/parsers/query_parser.rb +13 -6
  69. data/lib/facter/resolvers/az.rb +39 -0
  70. data/lib/facter/resolvers/linux/hostname.rb +115 -0
  71. data/lib/facter/resolvers/linux/networking.rb +106 -0
  72. data/lib/facter/resolvers/redhat_release.rb +28 -12
  73. data/lib/facter/resolvers/solaris/ffi/functions.rb +1 -1
  74. data/lib/facter/templates/man.erb +8 -0
  75. data/lib/facter/util/linux/dhcp.rb +86 -0
  76. data/lib/facter/util/linux/routing_table.rb +60 -0
  77. data/lib/facter/util/linux/socket_parser.rb +114 -0
  78. data/lib/facter/util/resolvers/ffi/hostname.rb +70 -0
  79. data/lib/facter/version.rb +1 -1
  80. metadata +29 -7
  81. data/lib/facter/facts/linux/cloud.rb +0 -15
  82. data/lib/facter/resolvers/cloud.rb +0 -39
  83. data/lib/facter/resolvers/networking_linux.rb +0 -296
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Facter
4
+ module Resolvers
5
+ module Linux
6
+ class Networking < BaseResolver
7
+ init_resolver
8
+
9
+ class << self
10
+ private
11
+
12
+ def post_resolve(fact_name, _options)
13
+ @fact_list.fetch(fact_name) { retrieve_network_info(fact_name) }
14
+
15
+ @fact_list[fact_name]
16
+ end
17
+
18
+ def retrieve_network_info(fact_name)
19
+ add_info_from_socket_reader
20
+ add_info_from_routing_table
21
+ retrieve_primary_interface
22
+ Facter::Util::Resolvers::Networking.expand_main_bindings(@fact_list)
23
+ @fact_list[fact_name]
24
+ end
25
+
26
+ def add_info_from_socket_reader
27
+ @fact_list[:interfaces] = Facter::Util::Linux::SocketParser.retrieve_interfaces(log)
28
+ mtu_and_indexes = interfaces_mtu_and_index
29
+
30
+ @fact_list[:interfaces].keys.each do |interface_name|
31
+ mtu(interface_name, mtu_and_indexes)
32
+ dhcp(interface_name, mtu_and_indexes)
33
+
34
+ @log.debug("Found interface #{interface_name} with #{@fact_list[:interfaces][interface_name]}")
35
+ end
36
+ end
37
+
38
+ def interfaces_mtu_and_index
39
+ mtu_and_indexes = {}
40
+ output = Facter::Core::Execution.execute('ip link show', logger: log)
41
+ output.each_line do |line|
42
+ next unless line.include?('mtu')
43
+
44
+ parse_ip_command_line(line, mtu_and_indexes)
45
+ end
46
+ log.debug("Associated MTU and index in ip command: #{mtu_and_indexes}")
47
+ mtu_and_indexes
48
+ end
49
+
50
+ def parse_ip_command_line(line, mtu_and_indexes)
51
+ mtu = line.match(/mtu (\d+)/)&.captures&.first&.to_i
52
+ index_tokens = line.split(':')
53
+ index = index_tokens[0].strip
54
+ # vlans are displayed as <vlan_name>@<physical_interface>
55
+ name = index_tokens[1].split('@').first.strip
56
+ mtu_and_indexes[name] = { index: index, mtu: mtu }
57
+ end
58
+
59
+ def mtu(interface_name, mtu_and_indexes)
60
+ mtu = mtu_and_indexes.dig(interface_name, :mtu)
61
+ @fact_list[:interfaces][interface_name][:mtu] = mtu unless mtu.nil?
62
+ end
63
+
64
+ def dhcp(interface_name, mtu_and_indexes)
65
+ dhcp = Facter::Util::Linux::Dhcp.dhcp(interface_name, mtu_and_indexes.dig(interface_name, :index), log)
66
+ @fact_list[:interfaces][interface_name][:dhcp] = dhcp unless dhcp.nil?
67
+ end
68
+
69
+ def add_info_from_routing_table
70
+ routes4, routes6 = Facter::Util::Linux::RoutingTable.read_routing_table(log)
71
+ compare_ips(routes4, :bindings)
72
+ compare_ips(routes6, :bindings6)
73
+ end
74
+
75
+ def compare_ips(routes, binding_key)
76
+ routes.each do |route|
77
+ next unless @fact_list[:interfaces].key?(route[:interface])
78
+
79
+ interface_data = @fact_list[:interfaces][route[:interface]]
80
+ add_binding_if_missing(interface_data, binding_key, route)
81
+ end
82
+ end
83
+
84
+ def add_binding_if_missing(interface_data, binding_key, route)
85
+ interface_binding = interface_data[binding_key]
86
+
87
+ if interface_binding.nil?
88
+ interface_data[binding_key] = [{ address: route[:ip] }]
89
+ elsif interface_binding.none? { |binding| binding[:address] == route[:ip] }
90
+ interface_binding << { address: route[:ip] }
91
+ end
92
+ end
93
+
94
+ def retrieve_primary_interface
95
+ primary_helper = Facter::Util::Resolvers::Networking::PrimaryInterface
96
+ primary_interface = primary_helper.read_from_proc_route
97
+ primary_interface ||= primary_helper.read_from_ip_route
98
+ primary_interface ||= primary_helper.find_in_interfaces(@fact_list[:interfaces])
99
+
100
+ @fact_list[:primary_interface] = primary_interface
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -6,6 +6,8 @@ module Facter
6
6
  # :name
7
7
  # :version
8
8
  # :codename
9
+ # :description
10
+ # :distributor_id
9
11
 
10
12
  init_resolver
11
13
 
@@ -26,29 +28,43 @@ module Facter
26
28
  end
27
29
 
28
30
  def build_fact_list(output)
31
+ @fact_list[:description] = output.strip
29
32
  output_strings = output.split('release')
30
33
  output_strings.map!(&:strip)
31
- version_codename = output_strings[1].split(' ')
32
-
33
- @fact_list[:name] = name(output_strings[0])
34
- @fact_list[:version] = version_codename[0]&.strip
35
-
36
- codename = version_codename[1]&.strip
37
- @fact_list[:codename] = codename ? codename.gsub(/[()]/, '') : nil
38
34
 
35
+ @fact_list[:codename] = codename(output)
36
+ @fact_list[:distributor_id] = distributor_id(output_strings[0])
37
+ @fact_list[:name] = release_name(output_strings[0])
38
+ @fact_list[:version] = version(output_strings)
39
39
  @fact_list[:identifier] = identifier(@fact_list[:name])
40
40
  end
41
41
 
42
- def name(name)
43
- name.strip.split(' ')[0..1].join
42
+ def release_name(value)
43
+ value.split.reject { |el| el.casecmp('linux').zero? }[0..1].join
44
44
  end
45
45
 
46
- def identifier(name)
47
- identifier = name.strip.downcase
48
- identifier = 'rhel' if @fact_list[:name].strip.casecmp('Red Hat Enterprise Linux')
46
+ def identifier(value)
47
+ identifier = value.downcase
48
+ identifier = 'rhel' if @fact_list[:name].casecmp('Red Hat Enterprise Linux')
49
49
 
50
50
  identifier
51
51
  end
52
+
53
+ def codename(value)
54
+ matched_data = value.match(/.*release.*(\(.*\)).*/)
55
+ return unless matched_data
56
+
57
+ codename = (matched_data[1] || '').gsub(/\(|\)/, '')
58
+ codename.empty? ? nil : codename
59
+ end
60
+
61
+ def version(value)
62
+ value[1].split.first
63
+ end
64
+
65
+ def distributor_id(value)
66
+ value.split.reject { |el| el.casecmp('linux').zero? }.join
67
+ end
52
68
  end
53
69
  end
54
70
  end
@@ -6,7 +6,7 @@ module Facter
6
6
  module FFI
7
7
  module Ioctl
8
8
  extend ::FFI::Library
9
- ffi_lib ::FFI::Library::LIBC, '/usr/lib/libsocket.so'
9
+ ffi_lib ::FFI::Library::LIBC, 'socket'
10
10
 
11
11
  attach_function :ioctl_base, :ioctl, %i[int int pointer], :int
12
12
  attach_function :open_socket, :socket, %i[int int int], :int
@@ -20,6 +20,14 @@ OPTIONS
20
20
 
21
21
  <% end -%>
22
22
 
23
+ <% @Facter::Cli.commands.select { |_k, command_class| command_class.instance_of?(Thor::Command) }.each do |_, command| -%>
24
+ * `<%= command.usage %>`:
25
+
26
+ <%= command.description %>
27
+
28
+
29
+ <% end -%>
30
+
23
31
  FILES
24
32
  -----
25
33
  <em>/etc/puppetlabs/facter/facter.conf</em>
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Facter
4
+ module Util
5
+ module Linux
6
+ class Dhcp
7
+ class << self
8
+ DIRS = %w[/var/lib/dhclient/
9
+ /var/lib/dhcp/
10
+ /var/lib/dhcp3/
11
+ /var/lib/NetworkManager/
12
+ /var/db/].freeze
13
+
14
+ def dhcp(interface_name, interface_index, logger)
15
+ @log = logger
16
+ @log.debug("Get DHCP for interface #{interface_name}")
17
+
18
+ dhcp = search_systemd_netif_leases(interface_index, interface_name)
19
+ dhcp ||= search_dhclient_leases(interface_name)
20
+ dhcp ||= search_internal_leases(interface_name)
21
+ dhcp ||= search_with_dhcpcd_command(interface_name)
22
+ dhcp
23
+ end
24
+
25
+ private
26
+
27
+ def search_systemd_netif_leases(index, interface_name)
28
+ return if index.nil?
29
+
30
+ @log.debug("Attempt to get DHCP for interface #{interface_name}, from systemd/netif/leases")
31
+
32
+ file_content = Facter::Util::FileHelper.safe_read("/run/systemd/netif/leases/#{index}", nil)
33
+ dhcp = file_content.match(/SERVER_ADDRESS=(.*)/) if file_content
34
+ dhcp[1] if dhcp
35
+ end
36
+
37
+ def search_dhclient_leases(interface_name)
38
+ @log.debug("Attempt to get DHCP for interface #{interface_name}, from dhclient leases")
39
+
40
+ DIRS.each do |dir|
41
+ next unless File.readable?(dir)
42
+
43
+ lease_files = Dir.entries(dir).select { |file| file =~ /dhclient.*\.lease/ }
44
+ next if lease_files.empty?
45
+
46
+ lease_files.select do |file|
47
+ content = Facter::Util::FileHelper.safe_read("#{dir}#{file}", nil)
48
+ next unless content =~ /interface.*#{interface_name}/
49
+
50
+ dhcp = content.match(/dhcp-server-identifier ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/)
51
+ return dhcp[1] if dhcp
52
+ end
53
+ end
54
+
55
+ nil
56
+ end
57
+
58
+ def search_internal_leases(interface_name)
59
+ return unless File.readable?('/var/lib/NetworkManager/')
60
+
61
+ @log.debug("Attempt to get DHCP for interface #{interface_name}, from NetworkManager leases")
62
+
63
+ files = Dir.entries('/var/lib/NetworkManager/').reject { |dir| dir =~ /^\.+$/ }
64
+ lease_file = files.find { |file| file =~ /internal.*#{interface_name}\.lease/ }
65
+ return unless lease_file
66
+
67
+ dhcp = Facter::Util::FileHelper.safe_read("/var/lib/NetworkManager/#{lease_file}", nil)
68
+
69
+ return unless dhcp
70
+
71
+ dhcp = dhcp.match(/SERVER_ADDRESS=(.*)/)
72
+ dhcp[1] if dhcp
73
+ end
74
+
75
+ def search_with_dhcpcd_command(interface_name)
76
+ @log.debug("Attempt to get DHCP for interface #{interface_name}, from dhcpcd command")
77
+
78
+ output = Facter::Core::Execution.execute("dhcpcd -U #{interface_name}", logger: @log)
79
+ dhcp = output.match(/dhcp_server_identifier='(.*)'/)
80
+ dhcp[1] if dhcp
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Facter
4
+ module Util
5
+ module Linux
6
+ class RoutingTable
7
+ class << self
8
+ ROUTE_TYPES = %w[anycast
9
+ unicast
10
+ broadcast
11
+ local
12
+ nat
13
+ unreachable
14
+ prohibit
15
+ blackhole
16
+ throw].freeze
17
+
18
+ def read_routing_table(logger)
19
+ ipv4_output = Facter::Core::Execution.execute('ip route show', logger: logger)
20
+ ipv6_output = Facter::Core::Execution.execute('ip -6 route show', logger: logger)
21
+ routes4 = parse_routes(ipv4_output, true)
22
+ routes6 = parse_routes(ipv6_output, false)
23
+ [routes4, routes6]
24
+ end
25
+
26
+ private
27
+
28
+ def parse_routes(output, ipv4_type)
29
+ routes = []
30
+ output.each_line do |line|
31
+ parts = line.split(' ').compact
32
+ next if parts.include?('linkdown')
33
+
34
+ delete_invalid_route_type(parts)
35
+ next if !ipv4_type && !parts[0].include?(':')
36
+
37
+ route = construct_route(parts)
38
+ routes << route unless route[:ip].nil?
39
+ end
40
+ routes.uniq
41
+ end
42
+
43
+ def delete_invalid_route_type(parts)
44
+ route_type = parts[0]
45
+ parts.delete_at(0) if ROUTE_TYPES.include?(route_type)
46
+ end
47
+
48
+ def construct_route(parts)
49
+ route = {}
50
+ dev_index = parts.find_index { |elem| elem == 'dev' }
51
+ src_index = parts.find_index { |elem| elem == 'src' }
52
+ route[:interface] = parts[dev_index + 1] if dev_index
53
+ route[:ip] = parts[src_index + 1] if src_index
54
+ route
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Facter
4
+ module Util
5
+ module Linux
6
+ class SocketParser
7
+ class << self
8
+ def retrieve_interfaces(logger)
9
+ require 'socket'
10
+ @interfaces = {}
11
+ @log = logger
12
+ Socket.getifaddrs.each do |ifaddr|
13
+ populate_interface_info(ifaddr)
14
+ end
15
+
16
+ @interfaces
17
+ end
18
+
19
+ private
20
+
21
+ def populate_interface_info(ifaddr)
22
+ interface_name = ifaddr.name
23
+ @interfaces[interface_name] = {} if @interfaces[interface_name].nil?
24
+
25
+ mac(ifaddr)
26
+ ip_info_of(ifaddr)
27
+ end
28
+
29
+ def mac(ifaddr)
30
+ return unless @interfaces[ifaddr.name][:mac].nil?
31
+
32
+ mac = search_for_mac(ifaddr)
33
+ @interfaces[ifaddr.name][:mac] = mac if mac
34
+ end
35
+
36
+ def search_for_mac(ifaddr)
37
+ mac = mac_from_bonded_interface(ifaddr.name)
38
+ mac ||= mac_from(ifaddr)
39
+ mac if !mac.nil? && mac != '00:00:00:00:00:00' && mac =~ /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/
40
+ end
41
+
42
+ def mac_from_bonded_interface(interface_name)
43
+ master = bond_master_of(interface_name)
44
+ return unless master
45
+
46
+ output = Facter::Util::FileHelper.safe_read("/proc/net/bonding/#{master}", nil)
47
+ return unless output
48
+
49
+ found_match = false
50
+ output.each_line do |line|
51
+ if line.strip == "Slave Interface: #{interface_name}"
52
+ found_match = true
53
+ elsif line.include? 'Slave Interface'
54
+ # if we reached the data block of another interface belonging to the bonded interface
55
+ found_match = false
56
+ end
57
+ return Regexp.last_match(1) if found_match && line =~ /Permanent HW addr: (\S*)/
58
+ end
59
+ end
60
+
61
+ def bond_master_of(interface_name)
62
+ content = Facter::Core::Execution.execute("ip link show #{interface_name}", logger: @log)
63
+ content.match(/master (\S*) /)&.captures&.first
64
+ end
65
+
66
+ def mac_from(ifaddr)
67
+ if Socket.const_defined? :PF_LINK
68
+ ifaddr.addr&.getnameinfo&.first # sometimes it returns localhost or ip
69
+ elsif Socket.const_defined? :PF_PACKET
70
+ return if ifaddr.addr.nil?
71
+
72
+ mac_from_sockaddr_of(ifaddr)
73
+ end
74
+ rescue StandardError => e
75
+ @log.debug("Could not read mac for interface #{ifaddr.name}, got #{e}")
76
+ end
77
+
78
+ def mac_from_sockaddr_of(ifaddr)
79
+ result = ifaddr.addr.inspect_sockaddr
80
+ result&.match(/hwaddr=([\h:]+)/)&.captures&.first
81
+ end
82
+
83
+ def ip_info_of(ifaddr)
84
+ return if ifaddr.addr.nil? || ifaddr.netmask.nil?
85
+
86
+ add_binding(ifaddr.name, ifaddr)
87
+ rescue StandardError => e
88
+ @log.debug("Could not read binding data, got #{e}")
89
+ end
90
+
91
+ def add_binding(interface_name, ifaddr)
92
+ ip, netmask, binding_key = binding_data(ifaddr)
93
+ binding = Facter::Util::Resolvers::Networking.build_binding(ip, netmask)
94
+ return if binding.nil?
95
+
96
+ @interfaces[interface_name][binding_key] = [] if @interfaces[interface_name][binding_key].nil?
97
+ @interfaces[interface_name][binding_key] << binding
98
+
99
+ @log.debug("Adding to interface #{interface_name}, binding:\n#{binding}")
100
+ end
101
+
102
+ def binding_data(ifaddr)
103
+ # ipv6 ips are retrieved as <ip>%<interface_name>
104
+ ip = ifaddr.addr.ip_address.split('%').first
105
+ netmask = ifaddr.netmask.ip_address
106
+ binding_key = ifaddr.addr.ipv4? ? :bindings : :bindings6
107
+
108
+ [ip, netmask, binding_key]
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end