Skip to content

Commit

Permalink
Merge d2a8c51 into b06d273
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Aug 29, 2019
2 parents b06d273 + d2a8c51 commit 0994d13
Show file tree
Hide file tree
Showing 25 changed files with 808 additions and 435 deletions.
176 changes: 0 additions & 176 deletions src/lib/network/edit_nic_name.rb

This file was deleted.

68 changes: 68 additions & 0 deletions src/lib/y2network/dialogs/rename_interface.rb
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 "cwm/popup"
require "y2network/widgets/interface_name"
require "y2network/widgets/renaming_mechanism"
require "y2network/virtual_interface"

module Y2Network
module Dialogs
# This dialog allows the user to rename a network interface
#
# It allows the user to enter a new name and to select the attribute to
# base the rename on. Supported attributes are MAC address and Bus ID.
class RenameInterface < CWM::Popup
# Constructor
#
# @param builder [Y2Network::InterfaceConfigBuilder] Interface configuration builder object
def initialize(builder)
textdomain "network"

@builder = builder
@old_name = builder.interface.name
end

# @see CWM::CustomWidget
def contents
VBox(
Left(name_widget),
VSpacing(0.5),
rename_hwinfo_widget
)
end

private

# Interface name widget
#
# @return [Y2Network::Widgets::InterfaceName]
def name_widget
@name_widget ||= Y2Network::Widgets::InterfaceName.new(@builder)
end

# Widget to select the hardware information to base the rename on
#
# @return [Y2Network::Widgets::RenamingMechanism]
def rename_hwinfo_widget
@rename_hwinfo_widget ||= Y2Network::Widgets::RenamingMechanism.new(@builder)
end
end
end
end
98 changes: 81 additions & 17 deletions src/lib/y2network/hwinfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,69 @@ def initialize
# FIXME: decide whether it should read hwinfo (on demand or at once) for a network
# device and store only necessary info or just parse provided hash
class Hwinfo
# TODO: this method should be private
attr_reader :hwinfo

def initialize(name:)
class << self
# Creates a new instance containing hardware information for a given interface
#
# It retrieves the information from two sources:
#
# * hardware (through {Yast::LanItems} for the time being),
# * from existing udev rules.
#
# @todo Probably, this logic should be moved to a separate class.
#
# @param name [String] Interface's name
# @return [Hwinfo]
def for(name)
hwinfo_from_hardware(name) || hwinfo_from_udev(name) || Hwinfo.new
end

private

# Returns hardware information for the given device
#
# It relies on the {Yast::LanItems} module.
#
# @param name [String] Interface's name
# @return [Hwinfo,nil] Hardware info or nil if not found
def hwinfo_from_hardware(name)
netcards = HardwareWrapper.new.ReadHardware("netcard")
hw = netcards.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?
new(hw)
end

# Returns hardware information for the given device
#
# It relies on udev rules.
#
# @param name [String] Interface's name
# @return [Hwinfo,nil] Hardware info or nil if not found
def hwinfo_from_udev(name)
udev_rule = UdevRule.find_for(name)
return nil if udev_rule.nil?
info = {
udev: udev_rule.bus_id,
mac: udev_rule.mac,
dev_port: udev_rule.dev_port
}.compact
new(info)
end
end

# Constructor
#
# @param hwinfo [Hash<String,Object>] Hardware information
def initialize(hwinfo = {})
# FIXME: store only what's needed.
@hwinfo = load_hwinfo(name)
@hwinfo = Hash[hwinfo.map { |k, v| [k.to_s, v] }]
end

# Shortcuts for accessing hwinfo items. Each hwinfo item has own method for reading
Expand Down Expand Up @@ -72,8 +130,8 @@ def initialize(name:)
# @return [String,nil]
[
{ name: "dev_name", default: "" },
{ name: "mac", default: "" },
{ name: "busid", default: "" },
{ name: "mac", default: nil },
{ name: "busid", default: nil },
{ name: "link", default: false },
{ name: "driver", default: "" },
{ name: "module", default: nil },
Expand All @@ -94,7 +152,7 @@ def initialize(name:)
alias_method :name, :dev_name

def exists?
!@hwinfo.nil?
!@hwinfo.empty?
end

# Device type description
Expand All @@ -103,6 +161,14 @@ def description
@hwinfo ? @hwinfo.fetch("name", "") : ""
end

# Merges data from another Hwinfo object
#
# @param other [Hwinfo] Object to merge data from
def merge!(other)
@hwinfo.merge!(other.hwinfo)
self
end

# Returns the list of kernel modules
#
# The list of modules is internally represented as:
Expand All @@ -118,18 +184,16 @@ def drivers
modules.map { |m| Driver.new(*m) }
end

private

def load_hwinfo(name)
netcards = HardwareWrapper.new.ReadHardware("netcard")
hw = netcards.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
# Determines whether two objects are equivalent
#
# Ignores any element having a nil value.
#
# @param other [Hwinfo] Object to compare with
# @return [Boolean]
def ==(other)
hwinfo.compact == other.hwinfo.compact
end

alias_method :eql?, :==
end
end
Loading

0 comments on commit 0994d13

Please sign in to comment.