Skip to content

Commit

Permalink
Merge df2c83f into 7c017ff
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Aug 22, 2019
2 parents 7c017ff + df2c83f commit 08c60c7
Show file tree
Hide file tree
Showing 24 changed files with 908 additions and 60 deletions.
51 changes: 15 additions & 36 deletions src/lib/network/edit_nic_name.rb
Expand Up @@ -19,33 +19,17 @@ class EditNicName
# @return [String] current udev match criteria
attr_reader :old_key

# udev rule attribute for MAC address
MAC_UDEV_ATTR = "ATTR{address}".freeze

# udev rule attribute for BUS id
BUSID_UDEV_ATTR = "KERNELS".freeze

def initialize
# Constructor
#
# @param settings [Y2Network::InterfaceConfigBuilder] Interface configuration
def initialize(settings)
textdomain "network"

Yast.include self, "network/routines.rb"

current_item = LanItems.getCurrentItem
current_rule = LanItems.current_udev_rule

@old_name = LanItems.current_udev_name
unless current_rule.empty?
@old_key = :mac unless LanItems.GetItemUdev(MAC_UDEV_ATTR).empty?
@old_key = :bus_id unless LanItems.GetItemUdev(BUSID_UDEV_ATTR).empty?
end

if current_item["hwinfo"]
@mac = current_item["hwinfo"]["permanent_mac"]
@bus_id = current_item["hwinfo"]["busid"]
else
@mac = ""
@bus_id = ""
end
@settings = settings
interface = settings.interface
@old_name = interface.name
@old_key = interface.renaming_mechanism
@mac = interface.hardware.mac
@bus_id = interface.hardware.busid
end

# Opens dialog for editing NIC name and runs event loop.
Expand All @@ -61,22 +45,16 @@ def run
next if ret != :ok

new_name = UI.QueryWidget(:dev_name, :Value)
udev_type = UI.QueryWidget(:udev_type, :CurrentButton)

if CheckUdevNicName(new_name)
LanItems.rename(new_name)
@settings.rename_interface(new_name, udev_type)
else
UI.SetFocus(:dev_name)
ret = nil

next
end

udev_type = UI.QueryWidget(:udev_type, :CurrentButton)

# 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 udev_type && (old_key != udev_type)
LanItems.rename_current_device_in_routing(old_name) if new_name != old_name
end

close
Expand Down Expand Up @@ -146,11 +124,12 @@ def close
# @return [boolean] false if name is invalid
def CheckUdevNicName(name)
# check if the name is assigned to another device already
if LanItems.GetNetcardNames.include?(name) && name != LanItems.GetCurrentName
if @settings.name_exists?(name)
Popup.Error(_("Configuration name already exists."))
return false
end
if !ValidNicName(name)

if !@settings.valid_name?(name)
Popup.Error(_("Invalid configuration name."))
return false
end
Expand Down
11 changes: 11 additions & 0 deletions src/lib/y2network/config.rb
Expand Up @@ -129,6 +129,17 @@ def ==(other)
routing == other.routing && dns == other.dns
end

# Renames a given interface and the associated connections
#
# @param old_name [String] Old interface's name
# @param new_name [String] New interface's name
# @param mechanism [Symbol] Property to base the rename on (:mac or :bus_id)
def rename_interface(old_name, new_name, mechanism)
interface = interfaces.by_name(old_name)
interface.rename(new_name, mechanism)
connections.by_interface(old_name).each { |c| c.interface = new_name }
end

alias_method :eql?, :==
end
end
8 changes: 8 additions & 0 deletions src/lib/y2network/connection_configs_collection.rb
Expand Up @@ -53,6 +53,14 @@ def by_name(name)
connection_configs.find { |c| c.name == name }
end

# Returns connection configurations which are associated to the given interface
#
# @param interface_name [String] Interface name
# @return [Array<ConnectionConfig::Base>] Associated connection configs
def by_interface(interface_name)
connection_configs.select { |c| c.interface == interface_name }
end

# Adds or updates a connection configuration
#
# @note It uses the name to do the matching.
Expand Down
12 changes: 10 additions & 2 deletions src/lib/y2network/hwinfo.rb
Expand Up @@ -76,7 +76,8 @@ def initialize(name:)
{ name: "wl_auth_modes", default: "" },
{ name: "wl_enc_modes", default: nil },
{ name: "wl_channels", default: nil },
{ name: "wl_bitrates", default: nil }
{ name: "wl_bitrates", default: nil },
{ name: "dev_port", default: nil }
].each do |hwinfo_item|
define_method hwinfo_item[:name].downcase do
@hwinfo ? @hwinfo.fetch(hwinfo_item[:name], hwinfo_item[:default]) : hwinfo_item[:default]
Expand Down Expand Up @@ -117,7 +118,14 @@ def drivers
include Yast::I18n

def load_hwinfo(name)
Yast::LanItems.Hardware.find { |h| h["dev_name"] == name }
hw = Yast::LanItems.Hardware.find { |h| h["dev_name"] == name }
return nil if hw.nil?

raw_dev_port = Yast::SCR.Read(
Yast::Path.new(".target.string"), "/sys/class_net/#{name}/dev_port"
).to_s.strip
hw["dev_port"] = raw_dev_port unless raw_dev_port.empty?
hw
end
end
end
15 changes: 15 additions & 0 deletions src/lib/y2network/interface.rb
Expand Up @@ -17,6 +17,7 @@
# 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_type"
require "y2network/hwinfo"

Expand All @@ -35,6 +36,8 @@ module Y2Network
# @see Y2Network::VirtualInterface
# @see Y2Network::FakeInterface
class Interface
include Yast::Logger

# @return [String] Device name ('eth0', 'wlan0', etc.)
attr_accessor :name
# @return [String] Interface description
Expand All @@ -45,6 +48,8 @@ class Interface
attr_reader :configured
# @return [HwInfo]
attr_reader :hardware
# @return [Symbol] Mechanism to rename the interface (nil -no rename-, :bus_id or :mac)
attr_accessor :renaming_mechanism

# Shortcuts for accessing interfaces' ifcfg options
#
Expand Down Expand Up @@ -105,6 +110,16 @@ def drivers
hardware.drivers
end

# Renames the interface
#
# @param new_name [String] New interface's name
# @param mechanism [Symbol] Property to base the rename on (:mac or :bus_id)
def rename(new_name, mechanism)
log.info "Rename interface '#{name}' to '#{new_name}' using the '#{mechanism}'"
@name = new_name
@renaming_mechanism = mechanism
end

private

def system_config(name)
Expand Down
47 changes: 45 additions & 2 deletions src/lib/y2network/interface_config_builder.rb
Expand Up @@ -50,7 +50,7 @@ def self.for(type, config: nil)
require "y2network/interface_config_builders/#{type.file_name}"
InterfaceConfigBuilders.const_get(type.class_name).new(config: config)
rescue LoadError => e
log.info "Specialed builder for #{type} not found. Fallbacking to default. #{e.inspect}"
log.info "Specialized builder for #{type} not found. Falling back to default. #{e.inspect}"
new(type: type, config: config)
end

Expand Down Expand Up @@ -93,7 +93,8 @@ def save

@connection_config.name = name
@connection_config.interface = name
Yast::Lan.yast_config.connections.add_or_update(@connection_config)
yast_config.connections.add_or_update(@connection_config)
yast_config.rename_interface(@old_name, name, renaming_mechanism) if renamed_interface?

# create new instance as name can change
firewall_interface = Y2Firewall::Firewalld::Interface.new(name)
Expand All @@ -108,6 +109,41 @@ def save
nil
end

# Determines whether the interface has been renamed
#
# @return [Boolean] true if it was renamed; false otherwise
def renamed_interface?
return false unless interface
name != interface.name || @renaming_mechanism != interface.renaming_mechanism
end

# Renames the interface
#
# @param new_name [String] New interface's name
# @param renaming_mechanism [Symbol,nil] Mechanism to rename the interface
# (nil -no rename-, :mac or :bus_id)
def rename_interface(new_name, renaming_mechanism)
@old_name ||= name
self.name = new_name
@renaming_mechanism = renaming_mechanism
end

# Returns the current renaming mechanism
#
# @return [Symbol,nil] Mechanism to rename the interface (nil -no rename-, :mac or :bus_id)
def renaming_mechanism
@renaming_mechanism || interface.renaming_mechanism
end

# Returns the underlying interface
#
# If the interface has been renamed, take the old name into account.
#
# @return [Y2Network::Interface,nil]
def interface
@interface ||= yast_config.interfaces.by_name(@old_name || name)
end

# how many device names is proposed
NEW_DEVICES_COUNT = 10
# Proposes bunch of possible names for interface
Expand Down Expand Up @@ -602,5 +638,12 @@ def save_aliases_to_connection
)
end
end

# Helper method to access to the current configuration
#
# @return [Y2Network::Config]
def yast_config
Yast::Lan.yast_config
end
end
end
11 changes: 11 additions & 0 deletions src/lib/y2network/sysconfig/config_writer.rb
Expand Up @@ -22,6 +22,7 @@
require "y2network/sysconfig/routes_file"
require "y2network/sysconfig/dns_writer"
require "y2network/sysconfig/connection_config_writer"
require "y2network/sysconfig/interfaces_writer"

module Y2Network
module Sysconfig
Expand All @@ -46,6 +47,7 @@ def write(config, old_config = nil)
file.save

write_dns_settings(config, old_config)
write_interfaces(config.interfaces)
write_connections(config.connections)
end

Expand Down Expand Up @@ -168,6 +170,15 @@ def write_dns_settings(config, old_config)
writer.write(config.dns, old_dns)
end

# Updates the interfaces configuration
#
# @param interfaces [Y2Network::InterfacesCollection]
# @see Y2Network::Sysconfig::InterfacesWriter
def write_interfaces(interfaces)
writer = Y2Network::Sysconfig::InterfacesWriter.new
writer.write(interfaces)
end

# Writes connections configuration
#
# @todo Handle old connections (removing those that are needed, etc.)
Expand Down
31 changes: 30 additions & 1 deletion src/lib/y2network/sysconfig/interfaces_reader.rb
Expand Up @@ -26,6 +26,7 @@
require "y2network/sysconfig/connection_config_reader"
require "y2network/interfaces_collection"
require "y2network/connection_configs_collection"
require "y2network/udev_rule"

Yast.import "LanItems"
Yast.import "NetworkInterfaces"
Expand Down Expand Up @@ -74,12 +75,25 @@ def interfaces
# Physical interfaces are read from the old LanItems module
def find_physical_interfaces
return if @interfaces
physical_interfaces = Yast::LanItems.Hardware.map do |h|
physical_interfaces = hardware.map do |h|
build_physical_interface(h)
end
@interfaces = Y2Network::InterfacesCollection.new(physical_interfaces)
end

# Returns hardware information
#
# This method makes sure that the hardware information was read.
#
# @todo It still relies on Yast::LanItems.Hardware
#
# @return [Array<Hash>] Hardware information
def hardware
Yast::LanItems.Hardware unless Yast::LanItems.Hardware.empty?
Yast::LanItems.Read # try again if no hardware was found
Yast::LanItems.Hardware
end

# Finds the connections configurations
def find_connections
@connections ||=
Expand Down Expand Up @@ -110,6 +124,7 @@ def build_physical_interface(data)
Y2Network::PhysicalInterface.new(data["dev_name"]).tap do |iface|
iface.description = data["name"]
type = data["type"] || Yast::NetworkInterfaces.GetTypeFromSysfs(iface.name)
iface.renaming_mechanism = renaming_mechanism_for(iface.name)
iface.type = case type
when nil then InterfaceType::ETHERNET
when ::String then InterfaceType.from_short_name(type)
Expand Down Expand Up @@ -144,6 +159,20 @@ def add_interface(name, conn)
interface_class = conn.virtual? ? VirtualInterface : FakeInterface
@interfaces << interface_class.from_connection(name, conn)
end

# Detects the renaming mechanism used by the interface
#
# @param name [String] Interface's name
# @return [Symbol,nil] :mac (MAC address), :bus_id (BUS ID) or nil (no renaming)
def renaming_mechanism_for(name)
rule = UdevRule.find_for(name)
return nil unless rule
if rule.parts.any? { |p| p.key == "ATTR{address}" }
:mac
elsif rule.parts.any? { |p| p.key == "KERNELS" }
:bus_id
end
end
end
end
end

0 comments on commit 08c60c7

Please sign in to comment.