Skip to content

Commit

Permalink
Merge pull request #830 from yast/builder_types
Browse files Browse the repository at this point in the history
add ipoib to builder and own types
  • Loading branch information
jreidinger committed Jun 14, 2019
2 parents 5f7f0e9 + 95afc0c commit 6c6bf8e
Show file tree
Hide file tree
Showing 24 changed files with 456 additions and 230 deletions.
3 changes: 1 addition & 2 deletions src/include/network/lan/complex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,8 @@ def handleOverview(_key, event)
Y2Network::Sequences::Interface.new.add
return :redraw
when :edit
builder = Y2Network::InterfaceConfigBuilder.new
builder = Y2Network::InterfaceConfigBuilder.for(LanItems.GetCurrentType())
builder.name = LanItems.GetCurrentName()
builder.type = LanItems.GetCurrentType()
if LanItems.IsCurrentConfigured
builder.load_sysconfig(LanItems.GetCurrentMap())
builder.load_s390_config(LanItems.s390_ReadQethConfig(builder.name))
Expand Down
4 changes: 2 additions & 2 deletions src/lib/network/network_autoconfiguration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "yast"
require "network/wicked"
require "y2network/interface_config_builder"

require "shellwords"

Expand Down Expand Up @@ -134,9 +135,8 @@ def setup_dhcp(card)
current["ifcfg"] = card
end

builder = Y2Network::InterfaceConfigBuilder.new
builder = Y2Network::InterfaceConfigBuilder.for(LanItems.GetCurrentType())
builder.name = LanItems.GetCurrentName()
builder.type = LanItems.GetCurrentType()
builder["BOOTPROTO"] = "dhcp"
builder["STARTMODE"] = "auto"

Expand Down
2 changes: 1 addition & 1 deletion src/lib/y2network/clients/routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Routing < Yast::Client
# Constructor
def initialize
textdomain "network"
Yast.include self, "network/services/routing.rb"
end

def main
Expand All @@ -76,7 +77,6 @@ def log_and_return(&block)
# Main Routing GUI
def RoutingGUI
read
Yast.include self, "network/services/routing.rb"

Wizard.CreateDialog
Wizard.SetDesktopTitleAndIcon("routing")
Expand Down
3 changes: 1 addition & 2 deletions src/lib/y2network/dialogs/add_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ def run
end

# TODO: use factory to get proper builder
builder = InterfaceConfigBuilder.new
builder.type = @type_widget.result
builder = InterfaceConfigBuilder.for(@type_widget.result)
proposed_name = Yast::LanItems.new_type_devices(@type_widget.result, 1).first
builder.name = proposed_name
Yast::NetworkInterfaces.Name = proposed_name
Expand Down
57 changes: 56 additions & 1 deletion src/lib/y2network/interface_config_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ module Y2Network
# Collects data from the UI until we have enough of it to create a {Y2Network::Interface}.
# {Yast::LanItemsClass#Commit Yast::LanItems.Commit(builder)} uses it.
class InterfaceConfigBuilder
include Yast::Logger

# Load fresh instance of interface config builder for given type.
# It can be specialized type or generic, depending if specialized is needed.
# @param type [String] type of device
# TODO: it would be nice to have type of device as Enum and not pure string
def self.for(type)
require "y2network/interface_config_builders/#{type}"
InterfaceConfigBuilders.const_get(type.to_s.capitalize).new
rescue LoadError => e
log.info "Specialed builder for #{type} not found. Fallbacking to default. #{e.inspect}"
new(type: type)
end

# @return [String] Device name (eth0, wlan0, etc.)
attr_accessor :name
# @return [String] type of @see Y2Network::Interface which is intended to be build (e.g. "eth")
Expand All @@ -37,7 +51,8 @@ class InterfaceConfigBuilder
# Constructor
#
# Load with reasonable defaults
def initialize
def initialize(type: nil)
@type = type
@config = init_device_config({})
@s390_config = init_device_s390_config({})
end
Expand Down Expand Up @@ -68,6 +83,8 @@ def save
firewall_interface.zone = firewall_zone if !firewall_interface.zone || firewall_zone != firewall_interface.zone.name
end

save_aliases

nil
end

Expand Down Expand Up @@ -126,6 +143,26 @@ def driver_options=(value)
@driver_options = value
end

def aliases
@aliases ||= Yast::LanItems.aliases.each_value.map do |data|
{
label: data["LABEL"] || "",
ip: data["IPADDR"] || "",
mask: data["NETMASK"] || "",
prefixlen: data["PREFIXLEN"] || ""
}
end
end

def aliases=(value)
@aliases = value
end

def udev_name
# cannot cache as EditNicName dialog can change it
Yast::LanItems.current_udev_name
end

# Provides stored configuration in sysconfig format
#
# @return [Hash<String, String>] where key is sysconfig option and value is the option's value
Expand Down Expand Up @@ -250,5 +287,23 @@ def hotplug_interface?
def hwinfo
@hwinfo ||= Hwinfo.new(name: name)
end

def save_aliases
lan_items_format = aliases.each_with_index.each_with_object({}) do |(a, i), res|
res[i] = {
"IPADDR" => a[:ip],
"LABEL" => a[:label],
"PREFIXLEN" => a[:prefixlen],
"NETMASK" => a[:mask]

}
end
log.info "setting new aliases #{lan_items_format.inspect}"
aliases_to_delete = Yast::LanItems.aliases.dup # #48191
Yast::LanItems.aliases = lan_items_format
aliases_to_delete.each_pair do |a, v|
Yast::NetworkInterfaces.DeleteAlias(Yast::NetworkInterfaces.Name, a) if v
end
end
end
end
26 changes: 26 additions & 0 deletions src/lib/y2network/interface_config_builders/ib.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "yast"
require "y2network/interface_config_builder"

Yast.import "LanItems"

module Y2Network
module InterfaceConfigBuilders
class Ib < InterfaceConfigBuilder
def initialize
super(type: "ib")
end

attr_writer :ipoib_mode

def ipoib_mode
@ipoib_mode ||= Yast::LanItems.ipoib_mode || "default"
end

def save
super

Yast::LanItems.ipoib_mode = ipoib_mode == "default" ? nil : ipoib_mode
end
end
end
end
47 changes: 47 additions & 0 deletions src/lib/y2network/interface_config_builders/vlan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "yast"
require "y2network/interface_config_builder"

Yast.import "LanItems"
Yast.import "NetworkInterfaces"

module Y2Network
module InterfaceConfigBuilders
class Vlan < InterfaceConfigBuilder
def initialize
super(type: "vlan")
end

def etherdevice
@config["ETHERDEVICE"]
end

def etherdevice=(value)
@config["ETHERDEVICE"] = value
end

# @return [Hash<String, String>] returns ordered list of devices that can be used for vlan
# Keys are ids for #etherdevice and value are label
def possible_vlans
res = {}
# unconfigured devices
Yast::LanItems.Items.each_value do |lan_item|
next unless (lan_item["ifcfg"] || "").empty?
dev_name = lan_item.fetch("hwinfo", {}).fetch("dev_name", "")
res[dev_name] = dev_name
end
# configured devices
configurations = Yast::NetworkInterfaces.FilterDevices("netcard")
# TODO: API looks horrible
Yast::NetworkInterfaces.CardRegex["netcard"].split("|").each do |devtype|
(configurations[devtype] || {}).each_key do |devname|
next if Yast::NetworkInterfaces.GetType(devname) == type

res[devname] = "#{devname} - #{Yast::Ops.get_string(configurations, [devtype, devname, "NAME"], "")}"
end
end

res
end
end
end
end
Loading

0 comments on commit 6c6bf8e

Please sign in to comment.