linux_admin 3.0.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 55478e58c9aaa8793bc1ba5df8216e89e68bbb30cc81faee45393b2ccf61bce9
4
- data.tar.gz: f41a819b7bc0cd0ec5c6ba8a89bec5a779b3a3ca4884e49fb9e792f25d8c5599
3
+ metadata.gz: 5a68c70db030c6c422243f781a3500324a224ae653c7e04e6ebac3f5ad902d08
4
+ data.tar.gz: 2d1d363cd64e3c2ebf64616a0376752f2e04aa7b74c143856d89c1aa2bcd0a7e
5
5
  SHA512:
6
- metadata.gz: e661c3f97fe45c92730cc9afefb0c98ea7d70f6ee2c5d7a492319cded40e5e963a7c014c1179b1a9dce17392e1ff6150dde248e9103b5b62b3a6bfb982c481a2
7
- data.tar.gz: 83772108b1940c6c1aba6e7c17f87ebd657505b0ea38960adef8d319e61d2d14b858c9d99d32a5d20f2216196df4f02c04236cdcbff2b9d192f4e57a0fbe4828
6
+ metadata.gz: 24c97db1e87afbbb873c963eb0e5c45f54dd1de468b46e301ec44563bcc459ecc2a97c9219a4c221ec8703258328c0c2f476b6e27f91da4c6bb811945ea3cfe7
7
+ data.tar.gz: 9b1a31fbf403c5b0873dced35a8308870a382c907ae8ef3919a285151c878bebf9297d71179435c09bc4c4a182fffee053e3e1ee8b76b2a50f2e8e8bd2e3d1ee
@@ -12,7 +12,15 @@ module LinuxAdmin
12
12
  def initialize(interface)
13
13
  @interface_file = self.class.path_to_interface_config_file(interface)
14
14
  super
15
+ end
16
+
17
+ # Gathers current network information for this interface
18
+ #
19
+ # @return [Boolean] true if network information was gathered successfully
20
+ def reload
21
+ super
15
22
  parse_conf
23
+ true
16
24
  end
17
25
 
18
26
  # Parses the interface configuration file into the @interface_config hash
@@ -1,7 +1,8 @@
1
- require 'ipaddr'
2
-
3
1
  module LinuxAdmin
4
2
  class NetworkInterface
3
+ require "ipaddr"
4
+ require "json"
5
+
5
6
  # Cached class instance variable for what distro we are running on
6
7
  @dist_class = nil
7
8
 
@@ -20,13 +21,28 @@ module LinuxAdmin
20
21
  end
21
22
  end
22
23
 
24
+ def self.list
25
+ ip_link.pluck("ifname").map { |iface| new(iface) }
26
+ rescue AwesomeSpawn::CommandResultError => e
27
+ raise NetworkInterfaceError.new(e.message, e.result)
28
+ end
29
+
30
+ private_class_method def self.ip_link
31
+ require "json"
32
+
33
+ result = Common.run!(Common.cmd("ip"), :params => ["--json", "link"])
34
+ JSON.parse(result.output)
35
+ rescue AwesomeSpawn::CommandResultError, JSON::ParserError => e
36
+ raise NetworkInterfaceError.new(e.message, e.result)
37
+ end
38
+
23
39
  # Creates an instance of the correct NetworkInterface subclass for the local distro
24
40
  def self.new(*args)
25
41
  self == LinuxAdmin::NetworkInterface ? dist_class.new(*args) : super
26
42
  end
27
43
 
28
44
  # @return [String] the interface for networking operations
29
- attr_reader :interface
45
+ attr_reader :interface, :link_type
30
46
 
31
47
  # @param interface [String] Name of the network interface to manage
32
48
  def initialize(interface)
@@ -45,18 +61,25 @@ module LinuxAdmin
45
61
  return false
46
62
  end
47
63
 
48
- parse_ip4(ip_output)
49
- parse_ip6(ip_output, :global)
50
- parse_ip6(ip_output, :link)
64
+ @link_type = ip_output["link_type"]
65
+ addr_info = ip_output["addr_info"]
66
+
67
+ parse_ip4(addr_info)
68
+ parse_ip6(addr_info, "global")
69
+ parse_ip6(addr_info, "link")
51
70
 
52
- @network_conf[:mac] = parse_ip_output(ip_output, %r{link/ether}, 1)
71
+ @network_conf[:mac] = ip_output["address"]
53
72
 
54
73
  [4, 6].each do |version|
55
- @network_conf["gateway#{version}".to_sym] = parse_ip_output(ip_route(version), /^default/, 2)
74
+ @network_conf["gateway#{version}".to_sym] = ip_route(version, "default")&.dig("gateway")
56
75
  end
57
76
  true
58
77
  end
59
78
 
79
+ def loopback?
80
+ @link_type == "loopback"
81
+ end
82
+
60
83
  # Retrieve the IPv4 address assigned to the interface
61
84
  #
62
85
  # @return [String] IPv4 address for the managed interface
@@ -153,23 +176,15 @@ module LinuxAdmin
153
176
 
154
177
  private
155
178
 
156
- # Parses the output of `ip addr show`
157
- #
158
- # @param output [String] The command output
159
- # @param regex [Regexp] Regular expression to match the desired output line
160
- # @param col [Fixnum] The whitespace delimited column to be returned
161
- # @return [String] The parsed data
162
- def parse_ip_output(output, regex, col)
163
- the_line = output.split("\n").detect { |l| l =~ regex }
164
- the_line.nil? ? nil : the_line.strip.split(' ')[col]
165
- end
166
-
167
179
  # Runs the command `ip addr show <interface>`
168
180
  #
169
181
  # @return [String] The command output
170
182
  # @raise [NetworkInterfaceError] if the command fails
171
183
  def ip_show
172
- Common.run!(Common.cmd("ip"), :params => ["addr", "show", @interface]).output
184
+ output = Common.run!(Common.cmd("ip"), :params => ["--json", "addr", "show", @interface]).output
185
+ return {} if output.blank?
186
+
187
+ JSON.parse(output).first
173
188
  rescue AwesomeSpawn::CommandResultError => e
174
189
  raise NetworkInterfaceError.new(e.message, e.result)
175
190
  end
@@ -179,8 +194,11 @@ module LinuxAdmin
179
194
  # @param version [Fixnum] Version of IP protocol (4 or 6)
180
195
  # @return [String] The command output
181
196
  # @raise [NetworkInterfaceError] if the command fails
182
- def ip_route(version)
183
- Common.run!(Common.cmd("ip"), :params => ["-#{version}", 'route']).output
197
+ def ip_route(version, route = "default")
198
+ output = Common.run!(Common.cmd("ip"), :params => ["--json", "-#{version}", "route", "show", route]).output
199
+ return {} if output.blank?
200
+
201
+ JSON.parse(output).first
184
202
  rescue AwesomeSpawn::CommandResultError => e
185
203
  raise NetworkInterfaceError.new(e.message, e.result)
186
204
  end
@@ -188,26 +206,24 @@ module LinuxAdmin
188
206
  # Parses the IPv4 information from the output of `ip addr show <device>`
189
207
  #
190
208
  # @param ip_output [String] The command output
191
- def parse_ip4(ip_output)
192
- cidr_ip = parse_ip_output(ip_output, /inet /, 1)
193
- return unless cidr_ip
209
+ def parse_ip4(addr_info)
210
+ inet = addr_info&.detect { |addr| addr["family"] == "inet" }
211
+ return if inet.nil?
194
212
 
195
- parts = cidr_ip.split('/')
196
- @network_conf[:address] = parts[0]
197
- @network_conf[:prefix] = parts[1].to_i
213
+ @network_conf[:address] = inet["local"]
214
+ @network_conf[:prefix] = inet["prefixlen"]
198
215
  end
199
216
 
200
217
  # Parses the IPv6 information from the output of `ip addr show <device>`
201
218
  #
202
219
  # @param ip_output [String] The command output
203
220
  # @param scope [Symbol] The IPv6 scope (either `:global` or `:local`)
204
- def parse_ip6(ip_output, scope)
205
- cidr_ip = parse_ip_output(ip_output, /inet6 .* scope #{scope}/, 1)
206
- return unless cidr_ip
221
+ def parse_ip6(addr_info, scope)
222
+ inet6 = addr_info&.detect { |addr| addr["family"] == "inet6" && addr["scope"] == scope }
223
+ return if inet6.nil?
207
224
 
208
- parts = cidr_ip.split('/')
209
- @network_conf["address6_#{scope}".to_sym] = parts[0]
210
- @network_conf["prefix6_#{scope}".to_sym] = parts[1].to_i
225
+ @network_conf["address6_#{scope}".to_sym] = inet6["local"]
226
+ @network_conf["prefix6_#{scope}".to_sym] = inet6["prefixlen"]
211
227
  end
212
228
  end
213
229
  end
@@ -1,3 +1,3 @@
1
1
  module LinuxAdmin
2
- VERSION = "3.0.0".freeze
2
+ VERSION = "4.0.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux_admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Dunne
@@ -30,7 +30,7 @@ authors:
30
30
  autorequire:
31
31
  bindir: bin
32
32
  cert_chain: []
33
- date: 2024-04-30 00:00:00.000000000 Z
33
+ date: 2024-08-13 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: manageiq-style
@@ -343,7 +343,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
343
343
  - !ruby/object:Gem::Version
344
344
  version: '0'
345
345
  requirements: []
346
- rubygems_version: 3.5.9
346
+ rubygems_version: 3.3.27
347
347
  signing_key:
348
348
  specification_version: 4
349
349
  summary: LinuxAdmin is a module to simplify management of linux systems.