Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
974 additions
and
26 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
68 changes: 68 additions & 0 deletions
68
src/lib/y2network/config_reader/connection_config/sysconfig.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,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 |
51 changes: 51 additions & 0 deletions
51
src/lib/y2network/config_reader/connection_config/sysconfig_handlers/eth.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 "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 |
63 changes: 63 additions & 0 deletions
63
src/lib/y2network/config_reader/connection_config/sysconfig_handlers/wlan.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,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 |
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
101 changes: 101 additions & 0 deletions
101
src/lib/y2network/config_reader/sysconfig_interfaces.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,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 |
Oops, something went wrong.