Skip to content

Commit

Permalink
Merge e379179 into af26375
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Jun 12, 2019
2 parents af26375 + e379179 commit 6a95b71
Show file tree
Hide file tree
Showing 18 changed files with 974 additions and 26 deletions.
5 changes: 4 additions & 1 deletion src/lib/y2network/config.rb
Expand Up @@ -38,6 +38,8 @@ module Y2Network
class Config
# @return [InterfacesCollection]
attr_accessor :interfaces
# @return [Array<ConnectionConfig>]
attr_accessor :connections
# @return [Routing] Routing configuration
attr_accessor :routing
# @return [DNS] DNS configuration
Expand Down Expand Up @@ -89,8 +91,9 @@ def configs
# @param routing [Routing] Object with routing configuration
# @param dns [DNS] Object with DNS configuration
# @param source [Symbol] Configuration source
def initialize(interfaces: InterfacesCollection.new, routing: Routing.new, dns: DNS.new, source:)
def initialize(interfaces: InterfacesCollection.new, connections: [], routing: Routing.new, dns: DNS.new, source:)
@interfaces = interfaces
@connections = connections
@routing = routing
@dns = dns
@source = source
Expand Down
68 changes: 68 additions & 0 deletions src/lib/y2network/config_reader/connection_config/sysconfig.rb
@@ -0,0 +1,68 @@
# 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/sysconfig_interface_file"
Yast.import "NetworkInterfaces"

module Y2Network
module ConfigReader
module ConnectionConfig
class Sysconfig
# Constructor
#
# @param interface [String] Interface name
# @return [Y2Network::SysconfigInterfaceFile]
def read(interface)
type = find_type_for(interface)
handler_class = find_handler_class(type)
if handler_class.nil?
log.info "Unknown connection type: '#{type}'"
return nil
end
file = Y2Network::SysconfigInterfaceFile.new(interface.name)
handler_class.new(file).connection_config
end

private

# Determines the type of the connection using the name
#
# If the interface does not contain information about the type, it will rely on
# NetworkInterfaces#GetTypeFromSysfs.
#
# @todo Improve detection logic according to NetworkInterfaces#GetTypeFromIfcfgOrName.
#
# @param interface [Interface] Interface to seach the type for
# @return [Symbol]
def find_type_for(interface)
return interface.type if interface.type
type = Yast::NetworkInterfaces.GetTypeFromSysfs(interface.name)
type.nil? ? :eth : type.to_sym
end

def find_handler_class(type)
require "y2network/config_reader/connection_config/sysconfig_handlers/#{type}"
SysconfigHandlers.const_get(type.to_s.capitalize)
rescue LoadError
nil
end
end
end
end
end
@@ -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 "y2network/connection_config/ethernet"

module Y2Network
module ConfigReader
module ConnectionConfig
module SysconfigHandlers
# This class is able to build a ConnectionConfig::Ethernet object given a
# SysconfigInterfaceFile object.
class Eth
# @return [Y2Network::SysconfigInterfaceFile]
attr_reader :file

# Constructor
#
# @param file [Y2Network::SysconfigInterfaceFile] File to get interface configuration from
def initialize(file)
@file = file
end

# @return [Y2Network::ConnectionConfig::Ethernet]
def connection_config
Y2Network::ConnectionConfig::Ethernet.new.tap do |conn|
conn.interface = file.name
conn.bootproto = file.bootproto
conn.ip_address = file.ip_address
end
end
end
end
end
end
end
@@ -0,0 +1,63 @@
# 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/connection_config/wireless"

module Y2Network
module ConfigReader
module ConnectionConfig
module SysconfigHandlers
# This class is able to build a ConnectionConfig::Wireless object given a
# SysconfigInterfaceFile object.
class Wlan
# @return [Y2Network::SysconfigInterfaceFile]
attr_reader :file

def initialize(file)
@file = file
end

# Returns an ethernet connection configuration
#
# @return [ConnectionConfig::Ethernet]
def connection_config
Y2Network::ConnectionConfig::Wireless.new.tap do |conn|
conn.interface = file.name
conn.bootproto = file.bootproto
conn.ip_address = file.ip_address
conn.ap = file.wireless_ap
conn.ap_scanmode = file.wireless_ap_scanmode
conn.auth_mode = file.wireless_auth_mode
conn.default_key = file.wireless_default_key
conn.eap_auth = file.wireless_eap_auth
conn.eap_mode = file.wireless_eap_mode
conn.essid = file.wireless_essid
conn.key_length = file.wireless_key_length
conn.keys = file.wireless_keys
conn.mode = file.wireless_mode
conn.nwid = file.wireless_nwid
conn.wpa_password = file.wireless_wpa_password
conn.wpa_psk = file.wireless_wpa_psk
end
end
end
end
end
end
end
18 changes: 14 additions & 4 deletions src/lib/y2network/config_reader/sysconfig.rb
Expand Up @@ -24,6 +24,7 @@
require "y2network/routing_table"
require "y2network/sysconfig_routes_file"
require "y2network/config_reader/sysconfig_dns"
require "y2network/config_reader/sysconfig_interfaces"
require "y2network/interfaces_collection"

Yast.import "NetworkInterfaces"
Expand All @@ -37,13 +38,18 @@ def initialize(_opts = {})

# @return [Y2Network::Config] Network configuration
def config
interfaces = find_interfaces
routing_tables = find_routing_tables(interfaces)
routing_tables = find_routing_tables(interfaces_reader.interfaces)
routing = Routing.new(
tables: routing_tables, forward_ipv4: forward_ipv4?, forward_ipv6: forward_ipv6?
)

Config.new(interfaces: interfaces, routing: routing, dns: dns, source: :sysconfig)
Config.new(
interfaces: interfaces_reader.interfaces,
connections: interfaces_reader.connections,
routing: routing,
dns: dns,
source: :sysconfig
)
end

private
Expand All @@ -65,6 +71,10 @@ def find_interfaces
Y2Network::InterfacesCollection.new(interfaces)
end

def interfaces_reader
@interfaces_reader ||= Y2Network::ConfigReader::SysconfigInterfaces.new
end

# Reads routes
#
# Merges routes from /etc/sysconfig/network/routes and /etc/sysconfig/network/ifroute-*
Expand All @@ -74,7 +84,7 @@ def find_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|
iface_routes = interfaces.flat_map do |iface|
load_routes_from("/etc/sysconfig/network/ifroute-#{iface.name}")
end
all_routes = main_routes + iface_routes
Expand Down
101 changes: 101 additions & 0 deletions src/lib/y2network/config_reader/sysconfig_interfaces.rb
@@ -0,0 +1,101 @@
# 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 "yast"
require "y2network/interface"
require "y2network/virtual_interface"
require "y2network/physical_interface"
require "y2network/connection_config/ethernet"
require "y2network/config_reader/connection_config/sysconfig"
require "y2network/sysconfig_interface_file"

Yast.import "LanItems"

module Y2Network
module ConfigReader
# This class reads interfaces configuration from sysconfig files
#
# * Physical interfaces are read from the hardware.
# * Virtual interfaces + Connections are read from sysconfig.
#
# @see Y2Network::Interface
# @see Y2Network::Connection::Connection
class SysconfigInterfaces
# Returns the interfaces and connections configuration
#
# @todo Should we use different readers?
# @todo Are virtual interfaces coming from connections only?
def config
[interfaces, connections]
end

# List of interfaces
#
# @return [InterfacesCollection]
def interfaces
@interfaces ||= Y2Network::InterfacesCollection.new(physical_interfaces) # + virtual_interfaces
end

# List of virtual interfaces
#
# @return [Array<Interface>]
def virtual_interfaces
@virtual_interfaces ||= connections.map(&:interface).compact
end

# @return [Array<ConnectionConfig>]
def connections
configured_devices.each_with_object([]) do |name, connections|
interface = physical_interfaces.find { |i| i.name == name }
# TODO: it may happen that the interface does not exist yet (hotplug, usb, or whatever)
# How should we handle those cases?
connection = Y2Network::ConfigReader::ConnectionConfig::Sysconfig.new.read(interface)
connections << connection if connection
end
end

private

# Returns the physical interfaces
#
# Physical interfaces are read from the old LanItems module
#
# @return [Array<Interface>]
def physical_interfaces
@physical_interfaces ||= Yast::LanItems.Hardware.map do |card|
iface = Y2Network::PhysicalInterface.new(card["dev_name"])
iface.description = card["name"]
iface.type = card["type"].nil? ? :eth : card["type"].to_sym
iface
end
end

# @return [Regex] expression to filter out invalid ifcfg-* files
IGNORE_IFCFG_REGEX = /(\.bak|\.orig|\.rpmnew|\.rpmorig|-range|~|\.old|\.scpmbackup)$/

# List of devices which has a configuration file (ifcfg-*)
#
# @return [Array<String>] List of configured devices
def configured_devices
files = Yast::SCR.Dir(Yast::Path.new(".network.section"))
files.reject { |f| IGNORE_IFCFG_REGEX =~ f || f == "lo" }
end
end
end
end

0 comments on commit 6a95b71

Please sign in to comment.