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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a68c70db030c6c422243f781a3500324a224ae653c7e04e6ebac3f5ad902d08
|
4
|
+
data.tar.gz: 2d1d363cd64e3c2ebf64616a0376752f2e04aa7b74c143856d89c1aa2bcd0a7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
49
|
-
|
50
|
-
|
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] =
|
71
|
+
@network_conf[:mac] = ip_output["address"]
|
53
72
|
|
54
73
|
[4, 6].each do |version|
|
55
|
-
@network_conf["gateway#{version}".to_sym] =
|
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}",
|
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(
|
192
|
-
|
193
|
-
return
|
209
|
+
def parse_ip4(addr_info)
|
210
|
+
inet = addr_info&.detect { |addr| addr["family"] == "inet" }
|
211
|
+
return if inet.nil?
|
194
212
|
|
195
|
-
|
196
|
-
@network_conf[:
|
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(
|
205
|
-
|
206
|
-
return
|
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
|
-
|
209
|
-
@network_conf["
|
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
|
data/lib/linux_admin/version.rb
CHANGED
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:
|
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-
|
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.
|
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.
|