Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #744 from mchf/network-ng
Implemented reading of routes via SCR agents (dropped Yast::Routing.Read)
- Loading branch information
Showing
13 changed files
with
256 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,4 @@ testsuite/run/ | |
.yardoc | ||
/coverage | ||
*.pot | ||
config.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/lib/y2network/config_reader/sysconfig_routes_reader.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# Copyright (c) [2019] SUSE LLC | ||
# | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can redistribute it and/or modify it | ||
# under the terms of version 2 of the GNU General Public License as published | ||
# by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
# more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along | ||
# with this program; if not, contact SUSE LLC. | ||
# | ||
# To contact SUSE LLC about this file by physical or electronic mail, you may | ||
# find current contact information at www.suse.com. | ||
require "y2network/routing_table" | ||
|
||
require "yast" | ||
require "y2network/interface" | ||
|
||
module Y2Network | ||
module ConfigReader | ||
DEFAULT_ROUTES_FILE = "/etc/sysconfig/network/routes".freeze | ||
|
||
# This class reads the current configuration from a file in routes format | ||
# (@see man routes) | ||
class SysconfigRoutesReader | ||
# @param routes_file [<String>] full path to a file in routes format, when | ||
# not defined, then /etc/sysconfig/network/routes is used | ||
def initialize(routes_file: DEFAULT_ROUTES_FILE) | ||
@routes_file = Yast::Path.new(".routes") if routes_file == DEFAULT_ROUTES_FILE | ||
end | ||
|
||
# Load routing tables | ||
# | ||
# Loads content of the routes file specified in the initialization | ||
# | ||
# @return [Y2Network::RoutingTable] | ||
def config | ||
routes = load_routes.map { |r| build_route(r) } | ||
Y2Network::RoutingTable.new(routes) | ||
end | ||
|
||
private | ||
|
||
MISSING_VALUE = "-".freeze | ||
private_constant :MISSING_VALUE | ||
DEFAULT_DEST = "default".freeze | ||
private_constant :DEFAULT_DEST | ||
|
||
# Loads routes from system | ||
# | ||
# @return [Array<Hash<String, String>>] list of hashes representing routes | ||
# as provided by SCR agent. | ||
# keys: destination, gateway, netmask, [device, [extrapara]] | ||
def load_routes | ||
routes = Yast::SCR.Read(@routes_file) || [] | ||
normalize_routes(routes.uniq) | ||
end | ||
|
||
# Converts routes config as read from system into well-defined format | ||
# | ||
# Expects list of hashes as param. Hash should contain keys "destination", | ||
# "gateway", "netmask", "device", "extrapara" | ||
# | ||
# Currently it converts "destination" in CIDR format (<ip>/<prefix_len>) | ||
# and keeps just <ip> part in "destination" and puts "/<prefix_len>" into | ||
# "netmask" | ||
# | ||
# @param routes [Array<Hash>] in quad or CIDR flavors (see {#Routes}) | ||
# @return [Array<Hash>] in quad or slash flavor | ||
def normalize_routes(routes) | ||
return routes if routes.nil? || routes.empty? | ||
|
||
routes.map do |route| | ||
subnet, prefix = route["destination"].split("/") | ||
|
||
next route if prefix.nil? | ||
|
||
route["destination"] = subnet | ||
route["netmask"] = "/#{prefix}" | ||
|
||
route | ||
end | ||
end | ||
|
||
# Given an IP and a netmask, returns a valid IPAddr object | ||
# | ||
# @param ip_str [String] IP address; {MISSING_VALUE} means that the IP is not defined | ||
# @param netmask_str [String] Netmask; {MISSING_VALUE} means than no netmask was specified | ||
# @return [IPAddr,nil] The IP address or `nil` if the IP is missing | ||
def build_ip(ip_str, netmask_str = MISSING_VALUE) | ||
return nil if ip_str == MISSING_VALUE | ||
|
||
ip = IPAddr.new(ip_str) | ||
netmask_str == MISSING_VALUE ? ip : ip.mask(netmask_str) | ||
end | ||
|
||
# Build a route given a hash from the SCR agent | ||
# | ||
# @param hash [Hash] Hash from the `.routes` SCR agent | ||
# @return Route | ||
def build_route(hash) | ||
# TODO: check whether the iface is configured in the system | ||
iface = Interface.new(hash["device"]) | ||
# normalized SCR output contains either subnet mask or /<prefix length> under | ||
# "netmask" key | ||
# TODO: this should be improved in normalize_routes | ||
mask = hash["netmask"] =~ /\/[0-9]+/ ? hash["netmask"][1..-1] : hash["netmask"] | ||
|
||
Y2Network::Route.new( | ||
to: hash["destination"] != DEFAULT_DEST ? build_ip(hash["destination"], mask) : :default, | ||
interface: iface, | ||
gateway: build_ip(hash["gateway"], MISSING_VALUE) | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright (c) [2019] SUSE LLC | ||
# | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can redistribute it and/or modify it | ||
# under the terms of version 2 of the GNU General Public License as published | ||
# by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
# more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along | ||
# with this program; if not, contact SUSE LLC. | ||
# | ||
# To contact SUSE LLC about this file by physical or electronic mail, you may | ||
# find current contact information at www.suse.com. | ||
module Y2Network | ||
# General routing configuration storage (routing tables, forwarding setup, ...) | ||
class Routing | ||
# @return [Array<RoutingTable>] | ||
attr_reader :tables | ||
# @return [Boolean] whether IPv4 forwarding is enabled | ||
attr_reader :forward_ipv4 | ||
# @return [Boolean] whether IPv6 forwarding is enabled | ||
attr_reader :forward_ipv6 | ||
|
||
def initialize(tables:) | ||
@tables = tables | ||
end | ||
|
||
# Routes in the configuration | ||
# | ||
# Convenience method to iterate through the routes in all routing tables. | ||
# | ||
# @return [Array<Route>] List of routes which are defined in the configuration | ||
def routes | ||
tables.flat_map(&:to_a) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
10.29.7.0 10.29.129.1 255.255.255.0 eth0 | ||
10.192.0.0/10 10.29.129.1 - - | ||
default 10.29.129.3 - |
51 changes: 51 additions & 0 deletions
51
test/y2network/config_reader/sysconfig_routes_reader_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright (c) [2019] SUSE LLC | ||
# | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can redistribute it and/or modify it | ||
# under the terms of version 2 of the GNU General Public License as published | ||
# by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
# more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along | ||
# with this program; if not, contact SUSE LLC. | ||
# | ||
# To contact SUSE LLC about this file by physical or electronic mail, you may | ||
# find current contact information at www.suse.com. | ||
require_relative "../../test_helper" | ||
|
||
require "y2network/config_reader/sysconfig_routes_reader" | ||
require "yast/rspec" | ||
|
||
describe Y2Network::ConfigReader::SysconfigRoutesReader do | ||
subject(:reader) { described_class.new } | ||
|
||
describe "#config" do | ||
around { |example| change_scr_root("#{DATA_PATH}/scr_read/", &example) } | ||
|
||
it "returns a RoutingTable with routes" do | ||
routing_table = reader.config | ||
|
||
expect(routing_table).to be_instance_of Y2Network::RoutingTable | ||
expect(routing_table.routes).not_to be_empty | ||
end | ||
|
||
it "contains default gw definition" do | ||
expect(reader.config.routes.any?(&:default?)).to be_truthy | ||
end | ||
|
||
it "accepts prefix from gateway field" do | ||
route = reader.config.routes.find { |r| r.to == "10.192.0.0" } | ||
|
||
expect(route.to.prefix).to eql 10 | ||
end | ||
|
||
it "stores device when set" do | ||
expect(reader.config.routes.any? { |r| r.interface.name == "eth0" }).to be_truthy | ||
end | ||
end | ||
end |
Oops, something went wrong.