Skip to content

Commit

Permalink
Merge pull request #773 from yast/network-ng-drop-routing
Browse files Browse the repository at this point in the history
Remove Yast::Routing module
  • Loading branch information
imobachgs committed Apr 10, 2019
2 parents f0d4a89 + 577856c commit a994558
Show file tree
Hide file tree
Showing 28 changed files with 428 additions and 1,107 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ script:
# the "yast-travis-ruby" script is included in the base yastdevel/ruby image
# see https://github.com/yast/docker-yast-ruby/blob/master/yast-travis-ruby
- docker run -it -e TRAVIS=1 -e TRAVIS_JOB_ID="$TRAVIS_JOB_ID" yast-network-image yast-travis-ruby
- docker run -it -e TRAVIS=1 -e TRAVIS_JOB_ID="$TRAVIS_JOB_ID" yast-network-image rake check:doc
6 changes: 3 additions & 3 deletions src/include/network/lan/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def initialize_network_lan_address(include_target)
Yast.import "NetworkInterfaces"
Yast.import "Popup"
Yast.import "ProductFeatures"
Yast.import "Routing"
Yast.import "String"
Yast.import "Wizard"
Yast.import "Map"
Expand Down Expand Up @@ -1367,13 +1366,14 @@ def AddressDialog
# "No IP address" case, then default gw must stay (#460262)
# and also: don't delete default GW for usb/pcmcia devices (#307102)
if LanItems.isCurrentDHCP && !LanItems.isCurrentHotplug
Routing.RemoveDefaultGw
yast_config = Y2Network::Config.find(:yast)
yast_config.routing.remove_default_routes if yast_config
end
end

# When virtual interfaces are added the list of routing devices needs
# to be updated to offer them
LanItems.update_routing_devices! if LanItems.update_routing_devices?
LanItems.add_current_device_to_routing if LanItems.update_routing_devices?
end

if LanItems.type == "vlan"
Expand Down
2 changes: 0 additions & 2 deletions src/include/network/lan/complex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def initialize_network_lan_complex(include_target)
Yast.import "Mode"
Yast.import "NetworkConfig"
Yast.import "NetworkService"
Yast.import "Routing"
Yast.import "Wizard"
Yast.import "Popup"
Yast.import "Label"
Expand Down Expand Up @@ -464,7 +463,6 @@ def handleOverview(_key, event)
end

LanItems.DeleteItem
LanItems.update_routing_devices!
initOverview("")
end
end
Expand Down
7 changes: 1 addition & 6 deletions src/lib/network/edit_nic_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Yast
Yast.import "UI"
Yast.import "LanItems"
Yast.import "Popup"
Yast.import "Routing"

# The class represents a simple dialog which allows user to input new NIC
# name. It also allows to select a device attribute (MAC, Bus id, ...) which will
Expand Down Expand Up @@ -74,11 +73,7 @@ def run
# FIXME: it changes udev key used for device identification
# and / or its value only, name is changed elsewhere
LanItems.update_item_udev_rule!(udev_type)

if new_name != old_name
LanItems.update_routing_devices!
LanItems.update_routes!(old_name) if update_routes?(old_name)
end
LanItems.rename_current_device_in_routing(old_name) if new_name != old_name
end

close
Expand Down
1 change: 0 additions & 1 deletion src/lib/network/network_autoyast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def initialize
Yast.import "LanItems"
Yast.import "Linuxrc"
Yast.import "Host"
Yast.import "Routing"
Yast.import "AutoInstall"
end

Expand Down
28 changes: 28 additions & 0 deletions src/lib/y2network/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ def from(source, opts = {})
reader = ConfigReader.for(source, opts)
reader.config
end

# Adds the configuration to the register
#
# @param id [Symbol] Configuration ID
# @param config [Y2Network::Config] Network configuration
def add(id, config)
configs[id] = config
end

# Finds the configuration in the register
#
# @param id [Symbol] Configuration ID
# @return [Config,nil] Configuration with the given ID or nil if not found
def find(id)
configs[id]
end

# Resets the configuration register
def reset
configs.clear
end

private

# Configuration register
def configs
@configs ||= {}
end
end

# Constructor
Expand Down
23 changes: 20 additions & 3 deletions src/lib/y2network/config_reader/sysconfig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def initialize(_opts = {})
# @return [Y2Network::Config] Network configuration
def config
interfaces = find_interfaces
routing_tables = find_routing_tables
routing_tables = find_routing_tables(interfaces)
routing = Routing.new(
tables: routing_tables, forward_ipv4: forward_ipv4?, forward_ipv6: forward_ipv6?
)
Expand Down Expand Up @@ -67,13 +67,15 @@ def find_interfaces
# Merges routes from /etc/sysconfig/network/routes and /etc/sysconfig/network/ifroute-*
# TODO: currently it implicitly loads main/default routing table
#
# return [RoutingTable] an object with routes
def find_routing_tables
# @param interfaces [Array<Interface>] Existing interfaces
# @return [RoutingTable] an object with routes
def find_routing_tables(interfaces)
main_routes = load_routes_from
iface_routes = find_interfaces.flat_map do |iface|
load_routes_from("/etc/sysconfig/network/ifroute-#{iface.name}")
end
all_routes = main_routes + iface_routes
link_routes_to_interfaces(all_routes, interfaces)
[Y2Network::RoutingTable.new(all_routes.uniq)]
end

Expand All @@ -97,6 +99,21 @@ def forward_ipv4?
def forward_ipv6?
Yast::SCR.Read(Yast::Path.new(SYSCTL_IPV6_PATH)) == "1"
end

# Links routes to interfaces objects
#
# {Y2Network::SysconfigRoutesFile} knows nothing about the already detected interfaces, so it
# instantiates a new object for each interface found. This method links the routes
# with the interfaces found in #interfaces.
#
# @param routes [Array<Route>] Routes to link
# @param interfaces [Array<Interface>] Interfaces
def link_routes_to_interfaces(routes, interfaces)
routes.each do |route|
interface = interfaces.find { |i| route.interface.name == i.name }
route.interface = interface if interface
end
end
end
end
end
2 changes: 1 addition & 1 deletion src/lib/y2network/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Y2Network
# Network interface.
class Interface
# @return [String] Device name (eth0, wlan0, etc.)
attr_reader :name
attr_accessor :name

# Constructor
#
Expand Down
59 changes: 51 additions & 8 deletions src/lib/y2network/presenters/routing_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,72 @@
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "yast"
Yast.import "Summary"
Yast.import "NetHwDetection"

module Y2Network
module Presenters
# This class converts a routing configuration object into a hash to be used
# in an AutoYaST summary
class RoutingSummary
# @return [Y2Network::Config]
attr_reader :config
include Yast::I18n

# @return [Y2Network::Routing]
attr_reader :routing

# Constructor
#
# @param config [Y2Network::Config] Network configuration to represent
def initialize(config)
@config = config
# @param routing [Y2Network::Routing] Network configuration to represent
def initialize(routing)
textdomain "network"
@routing = routing
end

# Returns the summary of network configuration settings in text form
#
# @todo Implement the real summary.
#
# @param mode [Symbol] Summary mode (:summary or :proposal)
# @return [String]
def text(mode:)
"Config summary in #{mode} mode"
def text
summary = ""
return summary if routing.nil? || routing.routes.empty?

gateway_string = gateway_string_from(routing)
if gateway_string
summary = Yast::Summary.AddListItem(summary, _("Gateway: %s") % gateway_string)
end
summary = Yast::Summary.AddListItem(
summary, format(_("IP Forwarding for IPv4: %s"), boolean_to_human(routing.forward_ipv4))
)
summary = Yast::Summary.AddListItem(
summary, format(_("IP Forwarding for IPv6: %s"), boolean_to_human(routing.forward_ipv6))
)

"<ul>#{summary}</ul>"
end

private

# Returns a text representation of the gateway
#
# @param routing [Y2Network::Routing] Routing configuration
# @return [String,nil] Text representation of the gateway IP; nil if no gateway is found
def gateway_string_from(routing)
default_route = routing.default_route
return nil if default_route.nil?
gateway = default_route.gateway.to_s
hostname = Yast::NetHwDetection.ResolveIP(gateway)
return gateway if hostname.empty?
"#{gateway} (#{hostname})"
end

# Converts a boolean into a on/off string
#
# @param value [Boolean] Value to convert
# @return [String] "on" if +value+ is true; false otherwise
def boolean_to_human(value)
value ? _("on") : _("off")
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/lib/y2network/proposal_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def default_backend
default
end

# Adds the NetworkManager package to the {Yast::PackagesProposal} and sets
# Adds the NetworkManager package to the Yast::PackagesProposal and sets
# NetworkManager as the backend to be used
def enable_network_manager!
log.info "Enabling NetworkManager"
Expand All @@ -61,7 +61,7 @@ def enable_network_manager!
selected_backend
end

# Add the wicked package to the {Yast::PackagesProposal} and sets wicked
# Add the wicked package to the Yast::PackagesProposal and sets wicked
# as the backend to be used
def enable_wicked!
log.info "Enabling Wicked"
Expand Down
14 changes: 14 additions & 0 deletions src/lib/y2network/routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ def routes
tables.flat_map(&:to_a)
end

# Returns the default route
#
# @todo Should we implement a `default_routes` which returns an array instead?
#
# @return [Route] Returns the first found default route
def default_route
routes.find(&:default?)
end

# Remove default routes from routing tables
def remove_default_routes
tables.each(&:remove_default_routes)
end

# Determines whether two set of routing settings are equal
#
# @param other [Routing] Routing settings to compare with
Expand Down
5 changes: 5 additions & 0 deletions src/lib/y2network/routing_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ def concat(routing_table)
self
end

# Returns the default route
def remove_default_routes
@routes.reject!(&:default?)
end

# Determines whether two routing tables are equal
#
# @param other [RoutingTable] Routing table to compare with
Expand Down
2 changes: 0 additions & 2 deletions src/lib/y2network/widgets/routing_buttons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
require "y2network/dialogs/route"
require "y2network/route"

Yast.import "NetworkService"

module Y2Network
module Widgets
class AddRoute < CWM::PushButton
Expand Down
17 changes: 9 additions & 8 deletions src/modules/Lan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
require "y2firewall/firewalld"
require "y2network/autoinst_profile/networking_section"
require "y2network/config"
require "y2network/presenters/routing_summary"

require "shellwords"

Expand Down Expand Up @@ -811,9 +812,9 @@ def Export
def Summary(mode)
case mode
when "summary"
"#{LanItems.BuildLanOverview.first}#{DNS.Summary}#{routing_summary(mode)}"
"#{LanItems.BuildLanOverview.first}#{DNS.Summary}#{routing_summary}"
when "proposal"
"#{LanItems.summary(:proposal)}#{DNS.Summary}#{routing_summary(mode)}"
"#{LanItems.summary(:proposal)}#{DNS.Summary}#{routing_summary}"
else
LanItems.BuildLanOverview.first
end
Expand Down Expand Up @@ -974,20 +975,20 @@ def dhcp_ntp_servers
# @param id [Symbol] Network configuration ID
# @return [Y2Network::Config,nil] Network configuration with the given ID or nil if not found
def find_config(id)
configs[id]
Y2Network::Config.find(id)
end

# Adds the configuration
#
# @param id [Symbol] Configuration ID
# @param config [Y2Network::Config] Network configuration
def add_config(id, config)
configs[id] = config
Y2Network::Config.add(id, config)
end

# Clears the network configurations list
def clear_configs
configs.clear
Y2Network::Config.reset
end

# Returns the system configuration
Expand Down Expand Up @@ -1132,12 +1133,12 @@ def refresh_lan_items

# Returns the routing summary
#
# @param mode [String,Symbol] Summary mode
# @return [String]
def routing_summary(mode)
def routing_summary
config = find_config(:yast)
return "" unless config && config.routing
presenter = Y2Network::Presenters::RoutingSummary.new(config.routing)
presenter.text(mode: mode.to_sym)
presenter.text
end

def firewalld
Expand Down

0 comments on commit a994558

Please sign in to comment.