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,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ffi'
4
+
5
+ module Facter
6
+ module Util
7
+ module Resolvers
8
+ module Ffi
9
+ class AddrInfo < ::FFI::Struct
10
+ layout :ai_flags, :int,
11
+ :ai_family, :int,
12
+ :ai_socketype, :int,
13
+ :ai_protocol, :int,
14
+ :ai_addrlen, :uint,
15
+ :ai_addr, :pointer,
16
+ :ai_canonname, :string,
17
+ :ai_next, :pointer
18
+ end
19
+
20
+ module Hostname
21
+ HOST_NAME_MAX = 64
22
+
23
+ extend ::FFI::Library
24
+ ffi_lib ::FFI::Library::LIBC
25
+
26
+ attach_function :getaddrinfo, %i[string string pointer pointer], :int
27
+ attach_function :gethostname, %i[pointer int], :int
28
+ attach_function :freeaddrinfo, [:pointer], :void
29
+
30
+ def self.getffihostname
31
+ raw_hostname = ::FFI::MemoryPointer.new(:char, HOST_NAME_MAX)
32
+
33
+ res = Hostname.gethostname(raw_hostname, HOST_NAME_MAX)
34
+ return unless res.zero?
35
+
36
+ raw_hostname.read_string
37
+ end
38
+
39
+ def self.getffiaddrinfo(hostname) # rubocop:disable Metrics/AbcSize
40
+ ret = FFI::MemoryPointer.new(:pointer)
41
+
42
+ hints = Facter::Util::Resolvers::Ffi::AddrInfo.new
43
+ hints[:ai_family] = Socket::AF_UNSPEC
44
+ hints[:ai_socketype] = Socket::SOCK_STREAM
45
+ hints[:ai_flags] = Socket::AI_CANONNAME
46
+
47
+ res = Hostname.getaddrinfo(hostname, '', hints.to_ptr, ret)
48
+ log = Log.new(self)
49
+ log.debug("FFI Getaddrinfo finished with exit status: #{res}")
50
+ log.debug("FFI Getaddrinfo returned an AddrInfo struct at address: #{ret}")
51
+ return unless res.zero?
52
+
53
+ return if ret == FFI::Pointer::NULL || ret.read_pointer == FFI::Pointer::NULL
54
+
55
+ begin
56
+ addr = Facter::Util::Resolvers::Ffi::AddrInfo.new(ret.read_pointer)
57
+ name = addr[:ai_canonname]
58
+ log.debug("FFI AddrInfo struct has the fqdn: #{name}")
59
+ return if !name || hostname == name
60
+
61
+ name
62
+ ensure
63
+ Hostname.freeaddrinfo(ret.read_pointer)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Facter
4
- VERSION = '4.0.51' unless defined?(VERSION)
4
+ VERSION = '4.0.52' unless defined?(VERSION)
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: facter
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.51
4
+ version: 4.0.52
5
5
  platform: ruby
6
6
  authors:
7
7
  - Puppet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-05 00:00:00.000000000 Z
11
+ date: 2021-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -166,6 +166,7 @@ extra_rdoc_files: []
166
166
  files:
167
167
  - bin/facter
168
168
  - lib/docs/generate.rb
169
+ - lib/docs/generate_cli.rb
169
170
  - lib/docs/template.erb
170
171
  - lib/facter.rb
171
172
  - lib/facter/config.rb
@@ -277,6 +278,13 @@ files:
277
278
  - lib/facter/facts/aix/system_uptime/uptime.rb
278
279
  - lib/facter/facts/aix/timezone.rb
279
280
  - lib/facter/facts/alpine/os/release.rb
281
+ - lib/facter/facts/amzn/lsbdistcodename.rb
282
+ - lib/facter/facts/amzn/lsbdistdescription.rb
283
+ - lib/facter/facts/amzn/lsbdistid.rb
284
+ - lib/facter/facts/amzn/os/distro/codename.rb
285
+ - lib/facter/facts/amzn/os/distro/description.rb
286
+ - lib/facter/facts/amzn/os/distro/id.rb
287
+ - lib/facter/facts/amzn/os/distro/release.rb
280
288
  - lib/facter/facts/amzn/os/release.rb
281
289
  - lib/facter/facts/bsd/kernelmajversion.rb
282
290
  - lib/facter/facts/bsd/kernelversion.rb
@@ -379,7 +387,8 @@ files:
379
387
  - lib/facter/facts/gentoo/os/release.rb
380
388
  - lib/facter/facts/linux/aio_agent_version.rb
381
389
  - lib/facter/facts/linux/augeas/version.rb
382
- - lib/facter/facts/linux/cloud.rb
390
+ - lib/facter/facts/linux/az_metadata.rb
391
+ - lib/facter/facts/linux/cloud/provider.rb
383
392
  - lib/facter/facts/linux/dhcp_servers.rb
384
393
  - lib/facter/facts/linux/disks.rb
385
394
  - lib/facter/facts/linux/dmi/bios/release_date.rb
@@ -613,6 +622,9 @@ files:
613
622
  - lib/facter/facts/openwrt/os/release.rb
614
623
  - lib/facter/facts/ovs/os/release.rb
615
624
  - lib/facter/facts/photon/os/release.rb
625
+ - lib/facter/facts/rhel/lsbdistcodename.rb
626
+ - lib/facter/facts/rhel/lsbdistdescription.rb
627
+ - lib/facter/facts/rhel/lsbdistid.rb
616
628
  - lib/facter/facts/rhel/os/distro/codename.rb
617
629
  - lib/facter/facts/rhel/os/distro/description.rb
618
630
  - lib/facter/facts/rhel/os/distro/id.rb
@@ -621,6 +633,9 @@ files:
621
633
  - lib/facter/facts/rhel/os/name.rb
622
634
  - lib/facter/facts/rhel/os/release.rb
623
635
  - lib/facter/facts/slackware/os/release.rb
636
+ - lib/facter/facts/sles/lsbdistcodename.rb
637
+ - lib/facter/facts/sles/lsbdistdescription.rb
638
+ - lib/facter/facts/sles/lsbdistid.rb
624
639
  - lib/facter/facts/sles/os/distro/codename.rb
625
640
  - lib/facter/facts/sles/os/distro/description.rb
626
641
  - lib/facter/facts/sles/os/distro/id.rb
@@ -729,6 +744,8 @@ files:
729
744
  - lib/facter/facts/ubuntu/os/release.rb
730
745
  - lib/facter/facts/windows/aio_agent_version.rb
731
746
  - lib/facter/facts/windows/augeas/version.rb
747
+ - lib/facter/facts/windows/az_metadata.rb
748
+ - lib/facter/facts/windows/cloud/provider.rb
732
749
  - lib/facter/facts/windows/dhcp_servers.rb
733
750
  - lib/facter/facts/windows/dmi/manufacturer.rb
734
751
  - lib/facter/facts/windows/dmi/product/name.rb
@@ -867,10 +884,10 @@ files:
867
884
  - lib/facter/resolvers/aix/processors.rb
868
885
  - lib/facter/resolvers/aix/serialnumber.rb
869
886
  - lib/facter/resolvers/augeas.rb
887
+ - lib/facter/resolvers/az.rb
870
888
  - lib/facter/resolvers/base_resolver.rb
871
889
  - lib/facter/resolvers/bsd/ffi/ffi_helper.rb
872
890
  - lib/facter/resolvers/bsd/processors.rb
873
- - lib/facter/resolvers/cloud.rb
874
891
  - lib/facter/resolvers/containers.rb
875
892
  - lib/facter/resolvers/debian_version.rb
876
893
  - lib/facter/resolvers/disk.rb
@@ -893,7 +910,9 @@ files:
893
910
  - lib/facter/resolvers/hostname.rb
894
911
  - lib/facter/resolvers/identity.rb
895
912
  - lib/facter/resolvers/linux/docker_uptime.rb
913
+ - lib/facter/resolvers/linux/hostname.rb
896
914
  - lib/facter/resolvers/linux/load_averages.rb
915
+ - lib/facter/resolvers/linux/networking.rb
897
916
  - lib/facter/resolvers/load_averages.rb
898
917
  - lib/facter/resolvers/lpar.rb
899
918
  - lib/facter/resolvers/lsb_release.rb
@@ -909,7 +928,6 @@ files:
909
928
  - lib/facter/resolvers/memory.rb
910
929
  - lib/facter/resolvers/mountpoints.rb
911
930
  - lib/facter/resolvers/networking.rb
912
- - lib/facter/resolvers/networking_linux.rb
913
931
  - lib/facter/resolvers/open_vz.rb
914
932
  - lib/facter/resolvers/os_release.rb
915
933
  - lib/facter/resolvers/partitions.rb
@@ -987,8 +1005,12 @@ files:
987
1005
  - lib/facter/util/facts/virtual_detector.rb
988
1006
  - lib/facter/util/facts/windows_release_finder.rb
989
1007
  - lib/facter/util/file_helper.rb
1008
+ - lib/facter/util/linux/dhcp.rb
1009
+ - lib/facter/util/linux/routing_table.rb
1010
+ - lib/facter/util/linux/socket_parser.rb
990
1011
  - lib/facter/util/macosx/system_profile_executor.rb
991
1012
  - lib/facter/util/resolvers/aws_token.rb
1013
+ - lib/facter/util/resolvers/ffi/hostname.rb
992
1014
  - lib/facter/util/resolvers/ffi/load_averages.rb
993
1015
  - lib/facter/util/resolvers/filesystem_helper.rb
994
1016
  - lib/facter/util/resolvers/fingerprint.rb
@@ -1002,7 +1024,7 @@ files:
1002
1024
  - lib/facter/util/utils.rb
1003
1025
  - lib/facter/util/windows/win32ole.rb
1004
1026
  - lib/facter/version.rb
1005
- homepage:
1027
+ homepage: https://github.com/puppetlabs/facter
1006
1028
  licenses:
1007
1029
  - MIT
1008
1030
  metadata: {}
@@ -1021,7 +1043,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
1021
1043
  - !ruby/object:Gem::Version
1022
1044
  version: '0'
1023
1045
  requirements: []
1024
- rubygems_version: 3.0.8
1046
+ rubygems_version: 3.0.0
1025
1047
  signing_key:
1026
1048
  specification_version: 4
1027
1049
  summary: New version of Facter
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Facts
4
- module Linux
5
- class Cloud
6
- FACT_NAME = 'cloud'
7
-
8
- def call_the_resolver
9
- cloud_provider = Facter::Resolvers::Cloud.resolve(:cloud_provider)
10
-
11
- Facter::ResolvedFact.new(FACT_NAME, cloud_provider)
12
- end
13
- end
14
- end
15
- end
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Facter
4
- module Resolvers
5
- class Cloud < BaseResolver
6
- # cloud_provider
7
-
8
- init_resolver
9
-
10
- class << self
11
- private
12
-
13
- def post_resolve(fact_name, _options)
14
- @fact_list.fetch(fact_name) { detect_azure(fact_name) }
15
- end
16
-
17
- def detect_azure(fact_name)
18
- search_dirs = %w[/var/lib/dhcp /var/lib/NetworkManager]
19
- search_dirs.each do |path|
20
- next if !File.readable?(path) || !File.directory?(path)
21
-
22
- files = Dir.entries(path)
23
- files.select! { |filename| filename =~ /^dhclient.*lease.*$/ }
24
- files.each do |file|
25
- path = File.join([path, file])
26
- output = Facter::Util::FileHelper.safe_read(path)
27
-
28
- if output.include?('option unknown-245') || output.include?('option 245')
29
- @fact_list[:cloud_provider] = 'azure'
30
- return @fact_list[fact_name]
31
- end
32
- end
33
- end
34
- nil
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,296 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Facter
4
- module Resolvers
5
- class NetworkingLinux < BaseResolver
6
- init_resolver
7
-
8
- DIRS = ['/var/lib/dhclient/', '/var/lib/dhcp/', '/var/lib/dhcp3/', '/var/lib/NetworkManager/', '/var/db/'].freeze
9
- ROUTE_TYPES = %w[anycast
10
- unicast
11
- broadcast
12
- local
13
- nat
14
- unreachable
15
- prohibit
16
- blackhole
17
- throw].freeze
18
-
19
- class << self
20
- private
21
-
22
- def post_resolve(fact_name, _options)
23
- @fact_list.fetch(fact_name) { retrieve_network_info(fact_name) }
24
-
25
- @fact_list[fact_name]
26
- end
27
-
28
- def retrieve_network_info(fact_name)
29
- mtu_and_indexes = interfaces_mtu_and_index
30
- retrieve_interfaces_with_socket(mtu_and_indexes)
31
- add_info_from_routing_table
32
- retrieve_primary_interface
33
- Facter::Util::Resolvers::Networking.expand_main_bindings(@fact_list)
34
- @fact_list[fact_name]
35
- end
36
-
37
- def interfaces_mtu_and_index
38
- mtu_and_indexes = {}
39
- output = Facter::Core::Execution.execute('ip link show', logger: log)
40
- output.each_line do |line|
41
- next unless line.include?('mtu')
42
-
43
- parse_ip_command_line(line, mtu_and_indexes)
44
- end
45
- log.debug("Associated MTU and index in ip command: #{mtu_and_indexes}")
46
- mtu_and_indexes
47
- end
48
-
49
- def parse_ip_command_line(line, mtu_and_indexes)
50
- mtu = line.match(/mtu (\d+)/)&.captures&.first&.to_i
51
- index_tokens = line.split(':')
52
- index = index_tokens[0].strip
53
- # vlans are displayed as <vlan_name>@<physical_interface>
54
- name = index_tokens[1].split('@').first.strip
55
- mtu_and_indexes[name] = { index: index, mtu: mtu }
56
- end
57
-
58
- def retrieve_interfaces_with_socket(mtu_and_indexes)
59
- require 'socket'
60
- interfaces = {}
61
- Socket.getifaddrs.each do |ifaddr|
62
- populate_interface_info(ifaddr, interfaces, mtu_and_indexes)
63
- end
64
-
65
- @fact_list[:interfaces] = interfaces
66
- end
67
-
68
- def populate_interface_info(ifaddr, interfaces, mtu_and_indexes)
69
- interface_name = ifaddr.name
70
- interfaces[interface_name] = {} if interfaces[interface_name].nil?
71
- interface_data = interfaces[interface_name]
72
-
73
- mac(ifaddr, interfaces)
74
- mtu(ifaddr, interfaces, mtu_and_indexes)
75
- ip, netmask, ipv4_type = ip_info_of(ifaddr)
76
- add_binding(interface_data, interface_name, ip, netmask, ipv4_type)
77
- dhcp(interface_name, mtu_and_indexes[interface_name], interface_data) if interface_data[:dhcp].nil?
78
- log.debug("Found interface #{interface_name} with #{interface_data}")
79
- end
80
-
81
- def mac(ifaddr, interfaces)
82
- return unless interfaces[ifaddr.name][:mac].nil?
83
-
84
- mac = search_for_mac(ifaddr)
85
- interfaces[ifaddr.name][:mac] = mac if mac
86
- end
87
-
88
- def search_for_mac(ifaddr)
89
- mac = mac_from_bonded_interface(ifaddr.name)
90
- mac ||= mac_from(ifaddr)
91
- mac if !mac.nil? && mac != '00:00:00:00:00:00' && mac =~ /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/
92
- end
93
-
94
- def mac_from_bonded_interface(interface_name)
95
- master = bond_master_of(interface_name)
96
- return unless master
97
-
98
- output = Facter::Util::FileHelper.safe_read("/proc/net/bonding/#{master}", nil)
99
- return unless output
100
-
101
- found_match = false
102
- output.each_line do |line|
103
- if line.strip == "Slave Interface: #{interface_name}"
104
- found_match = true
105
- elsif line.include? 'Slave Interface'
106
- # if we reached the data block of another interface belonging to the bonded interface
107
- found_match = false
108
- end
109
- return Regexp.last_match(1) if found_match && line =~ /Permanent HW addr: (\S*)/
110
- end
111
- end
112
-
113
- def bond_master_of(interface_name)
114
- content = Facter::Core::Execution.execute("ip link show #{interface_name}", logger: log)
115
- content.match(/master (\S*) /)&.captures&.first
116
- end
117
-
118
- def mac_from(ifaddr)
119
- if Socket.const_defined? :PF_LINK
120
- ifaddr.addr&.getnameinfo&.first # sometimes it returns localhost or ip
121
- elsif Socket.const_defined? :PF_PACKET
122
- return if ifaddr.addr.nil?
123
-
124
- search_mac_in_sockaddr(ifaddr)
125
- end
126
- rescue StandardError => e
127
- log.debug("Could not read mac, got #{e}")
128
- end
129
-
130
- def search_mac_in_sockaddr(ifaddr)
131
- result = ifaddr.addr.inspect_sockaddr
132
- result&.match(/hwaddr=([\h:]+)/)&.captures&.first
133
- end
134
-
135
- def mtu(ifaddr, interfaces, mtu_and_indexes)
136
- return unless interfaces[ifaddr.name][:mtu].nil?
137
-
138
- mtu = mtu_and_indexes.dig(ifaddr.name, :mtu)
139
- interfaces[ifaddr.name][:mtu] = mtu unless mtu.nil?
140
- end
141
-
142
- def ip_info_of(ifaddr)
143
- return if ifaddr.addr.nil? || ifaddr.netmask.nil?
144
-
145
- # ipv6 ips are retrieved as <ip>%<interface_name>
146
- ip = ifaddr.addr.ip_address.split('%').first
147
- netmask = ifaddr.netmask.ip_address
148
- [ip, netmask, ifaddr.addr.ipv4?]
149
- rescue StandardError => e
150
- log.debug("Could not read binding data, got #{e}")
151
- end
152
-
153
- def add_binding(interface_data, interface_name, ip, netmask, ip_v4_type)
154
- binding = Facter::Util::Resolvers::Networking.build_binding(ip, netmask)
155
- return if binding.nil?
156
-
157
- log.debug("Adding to interface #{interface_name}, binding:\n#{binding}")
158
- binding_key = ip_v4_type == true ? :bindings : :bindings6
159
- interface_data[binding_key] = [] if interface_data[binding_key].nil?
160
- interface_data[binding_key] << binding
161
- end
162
-
163
- def add_info_from_routing_table
164
- routes4, routes6 = read_routing_table
165
- compare_ips(routes4, :bindings)
166
- compare_ips(routes6, :bindings6)
167
- end
168
-
169
- def read_routing_table
170
- ipv4_output = Facter::Core::Execution.execute('ip route show', logger: log)
171
- ipv6_output = Facter::Core::Execution.execute('ip -6 route show', logger: log)
172
- routes4 = parse_routes(ipv4_output, true)
173
- routes6 = parse_routes(ipv6_output, false)
174
- [routes4, routes6]
175
- end
176
-
177
- def parse_routes(output, ipv4_type)
178
- routes = []
179
- output.each_line do |line|
180
- parts = line.split(' ').compact
181
- next if parts.include?('linkdown')
182
-
183
- delete_invalid_route_type(parts)
184
- next if !ipv4_type && !parts[0].include?(':')
185
-
186
- route = construct_route(parts)
187
- routes << route unless route[:ip].nil?
188
- end
189
- routes.uniq
190
- end
191
-
192
- def delete_invalid_route_type(parts)
193
- route_type = parts[0]
194
- parts.delete_at(0) if ROUTE_TYPES.include?(route_type)
195
- end
196
-
197
- def construct_route(parts)
198
- route = {}
199
- dev_index = parts.find_index { |elem| elem == 'dev' }
200
- src_index = parts.find_index { |elem| elem == 'src' }
201
- route[:interface] = parts[dev_index + 1] if dev_index
202
- route[:ip] = parts[src_index + 1] if src_index
203
- route
204
- end
205
-
206
- def compare_ips(routes, binding_key)
207
- routes.each do |route|
208
- next unless @fact_list[:interfaces].key?(route[:interface])
209
-
210
- interface_data = @fact_list[:interfaces][route[:interface]]
211
- add_binding_if_missing(interface_data, binding_key, route)
212
- end
213
- end
214
-
215
- def add_binding_if_missing(interface_data, binding_key, route)
216
- interface_binding = interface_data[binding_key]
217
-
218
- if interface_binding.nil?
219
- interface_data[binding_key] = [{ address: route[:ip] }]
220
- elsif interface_binding.none? { |binding| binding[:address] == route[:ip] }
221
- interface_binding << { address: route[:ip] }
222
- end
223
- end
224
-
225
- def dhcp(interface_name, index_and_mtu, interface_data)
226
- log.debug("Get DHCP for interface #{interface_name}")
227
-
228
- search_systemd_netif_leases(interface_data, index_and_mtu)
229
- search_dhclient_leases(interface_data, interface_name)
230
- search_internal_leases(interface_data, interface_name)
231
- search_with_dhcpcd_command(interface_data, interface_name)
232
- end
233
-
234
- def search_systemd_netif_leases(interface_data, index_and_mtu)
235
- return if index_and_mtu.nil?
236
-
237
- index = index_and_mtu[:index]
238
- file_content = Facter::Util::FileHelper.safe_read("/run/systemd/netif/leases/#{index}", nil)
239
- dhcp = file_content.match(/SERVER_ADDRESS=(.*)/) if file_content
240
- interface_data[:dhcp] = dhcp[1] if dhcp
241
- end
242
-
243
- def search_dhclient_leases(interface_data, interface_name)
244
- return unless interface_data[:dhcp].nil?
245
-
246
- DIRS.each do |dir|
247
- next unless File.readable?(dir)
248
-
249
- lease_files = Dir.entries(dir).select { |file| file =~ /dhclient.*\.lease/ }
250
- next if lease_files.empty?
251
-
252
- lease_files.select do |file|
253
- content = Facter::Util::FileHelper.safe_read("#{dir}#{file}", nil)
254
- next unless content =~ /interface.*#{interface_name}/
255
-
256
- dhcp = content.match(/dhcp-server-identifier ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/)
257
- return interface_data[:dhcp] = dhcp[1] if dhcp
258
- end
259
- end
260
- end
261
-
262
- def search_internal_leases(interface_data, interface_name)
263
- return if !interface_data[:dhcp].nil? || !File.readable?('/var/lib/NetworkManager/')
264
-
265
- files = Dir.entries('/var/lib/NetworkManager/').reject { |dir| dir =~ /^\.+$/ }
266
- lease_file = files.find { |file| file =~ /internal.*#{interface_name}\.lease/ }
267
- return unless lease_file
268
-
269
- dhcp = Facter::Util::FileHelper.safe_read("/var/lib/NetworkManager/#{lease_file}", nil)
270
-
271
- return unless dhcp
272
-
273
- dhcp = dhcp.match(/SERVER_ADDRESS=(.*)/)
274
- interface_data[:dhcp] = dhcp[1] if dhcp
275
- end
276
-
277
- def search_with_dhcpcd_command(interface_data, interface_name)
278
- return unless interface_data[:dhcp].nil?
279
-
280
- output = Facter::Core::Execution.execute("dhcpcd -U #{interface_name}", logger: log)
281
- dhcp = output.match(/dhcp_server_identifier='(.*)'/)
282
- interface_data[:dhcp] = dhcp[1] if dhcp
283
- end
284
-
285
- def retrieve_primary_interface
286
- primary_helper = Facter::Util::Resolvers::Networking::PrimaryInterface
287
- primary_interface = primary_helper.read_from_proc_route
288
- primary_interface ||= primary_helper.read_from_ip_route
289
- primary_interface ||= primary_helper.find_in_interfaces(@fact_list[:interfaces])
290
-
291
- @fact_list[:primary_interface] = primary_interface
292
- end
293
- end
294
- end
295
- end
296
- end