Skip to content

Commit

Permalink
Merge 4e0a75e into 8aea8b7
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Sep 16, 2019
2 parents 8aea8b7 + 4e0a75e commit 29027d2
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 644 deletions.
90 changes: 11 additions & 79 deletions src/include/network/lan/address.rb
Expand Up @@ -41,16 +41,11 @@ def initialize_network_lan_address(include_target)

textdomain "network"

Yast.import "Host"
Yast.import "Lan"
Yast.import "NetworkInterfaces"
Yast.import "ProductFeatures"
Yast.import "String"

Yast.include include_target, "network/lan/help.rb"
Yast.include include_target, "network/lan/hardware.rb"
Yast.include include_target, "network/complex.rb"
Yast.include include_target, "network/lan/bridge.rb"
Yast.include include_target, "network/lan/s390.rb"

@force_static_ip = ProductFeatures.GetBooleanFeature(
Expand All @@ -68,91 +63,28 @@ def AddressDialog(builder:)
log.info "ShowAndRun: #{ret}"

if ret != :back && ret != :abort
bootproto = builder.boot_protocol
ipaddr = builder.ip_address

# IP is mandatory for static configuration. Makes no sense to write static
# configuration without that.
return ret if bootproto == Y2Network::BootProtocol::STATIC && ipaddr.empty?

if bootproto == Y2Network::BootProtocol::STATIC
update_hostname(ipaddr, builder.hostname || "")
elsif LanItems.isCurrentDHCP && !LanItems.isCurrentHotplug
if LanItems.isCurrentDHCP && !LanItems.isCurrentHotplug
# fixed bug #73739 - if dhcp is used, dont set default gw statically
# but also: reset default gw only if DHCP* is used, this branch covers
# "No IP address" case, then default gw must stay (#460262)
# and also: don't delete default GW for usb/pcmcia devices (#307102)
if LanItems.isCurrentDHCP && !LanItems.isCurrentHotplug
yast_config = Y2Network::Config.find(:yast)
if yast_config && yast_config.routing && yast_config.routing.default_route
remove_gw = Popup.YesNo(
_(
"A static default route is defined.\n" \
"It is suggested to remove the static default route definition \n" \
"if one can be obtained also via DHCP.\n" \
"Do you want to remove the static default route?"
)
yast_config = Y2Network::Config.find(:yast)
if yast_config && yast_config.routing && yast_config.routing.default_route
remove_gw = Popup.YesNo(
_(
"A static default route is defined.\n" \
"It is suggested to remove the static default route definition \n" \
"if one can be obtained also via DHCP.\n" \
"Do you want to remove the static default route?"
)
yast_config.routing.remove_default_routes if remove_gw
end
)
yast_config.routing.remove_default_routes if remove_gw
end
end
end

log.info "AddressDialog res: #{ret.inspect}"
ret
end

private

# Performs hostname update
#
# This handles ip and hostname change when editing NIC properties.
# The method relies on old NIC's IP which is set globally at initialization
# of NIC edit dialog (@see LanItems#ipaddr)
#
# When hostname is empty, then old IP's record is cleared from /etc/hosts and
# new is not created.
# Otherwise the canonical name and all aliases in the record
# are replaced by new ones.
#
# @param ipaddr [String] ip address
# @param hostname [String] new hostname
def update_hostname(ipaddr, hostname)
ip_changed = LanItems.ipaddr != ipaddr
initial_hostname = initial_hostname(LanItems.ipaddr)
hostname_changed = initial_hostname != hostname

return if !(ip_changed || hostname_changed || hostname.empty?)

# store old names, remove the record
names = Host.names(LanItems.ipaddr).first
Host.remove_ip(LanItems.ipaddr)

if ip_changed && !hostname_changed && !names.nil?
log.info("Dropping record for #{LanItems.ipaddr} from /etc/hosts")

Host.add_name(ipaddr, names)
end
if !hostname.empty? && hostname_changed
log.info("Updating cannonical name for #{LanItems.ipaddr} in /etc/hosts")

Host.Update(initial_hostname, hostname, ipaddr)
end

nil
end

# Returns canonical hostname for the given ip
def initial_hostname(ipaddr)
host_list = Host.names(ipaddr)
if Ops.greater_than(Builtins.size(host_list), 1)
Builtins.y2milestone(
"More than one hostname for single IP detected, using the first one only"
)
end

String.FirstChunk(Ops.get(host_list, 0, ""), " \t")
end
end
end
72 changes: 0 additions & 72 deletions src/include/network/lan/bridge.rb

This file was deleted.

23 changes: 0 additions & 23 deletions src/lib/network/lan_items_summary.rb
Expand Up @@ -33,29 +33,6 @@ def initialize
Yast.import "Summary"
end

# Generates a summary in RichText format for the configured interfaces
#
# @example
# LanItemsSummary.new.default
# => "<ul><li><p>eth0<br>DHCP</p></li><li><p>eth1<br>NONE</p></li></ul>"
#
# @see Summary
# @return [String] summary in RichText
def default
items = []

LanItems.Items.each do |item, conf|
next if !Yast::LanItems.IsItemConfigured(item)

ifcfg = LanItems.GetDeviceMap(item) || {}
items << Summary.Device(conf["ifcfg"], ifcfg_protocol(ifcfg))
end

return Summary.NotConfigured if items.empty?

Summary.DevicesList(items)
end

# Generates a summary in RichText format for the configured interfaces
#
# @example
Expand Down
77 changes: 25 additions & 52 deletions src/lib/network/network_autoconfiguration.rb
Expand Up @@ -45,29 +45,31 @@ class NetworkAutoconfiguration
#
# returns [Boolean] true when at least one interface is active
def any_iface_active?
network_cards.any? { |c| configured?(c) && active_config?(c) }
config.interfaces.any? { |c| config.connections.by_name(c.name) && active_config?(c.name) }
end

def configure_dhcp
Yast.include self, "network/routines.rb"
Yast::Lan.Read(:cache)
Yast.include self, "network/routines.rb" # TODO: needed only for phy_connected

# find out network devices suitable for dhcp autoconfiguration.
# Such device has to:
# - be unconfigured
# - physically connected to a network
# (it speeds up the initialization phase of installer - bnc#872319)
dhcp_cards = network_cards.select do |c|
!configured?(c) && phy_connected?(c)
dhcp_cards = config.interfaces.select do |c|
next false if config.connections.by_name(c.name)
phy_connected?(c.name)
end
log.info "Candidates for enabling DHCP: #{dhcp_cards}"
log.info "Candidates for enabling DHCP: #{dhcp_cards.inspect}"

# TODO: time consuming, some progress would be nice
dhcp_cards.each { |d| setup_dhcp(d) }

activate_changes(dhcp_cards)
activate_changes(dhcp_cards.map(&:name))

# drop devices without dhcp lease
inactive_devices = dhcp_cards.select { |c| !active_config?(c) }
inactive_devices = dhcp_cards.select { |c| !active_config?(c.name) }
log.info "Inactive devices: #{inactive_devices}"

inactive_devices.each { |c| delete_config(c) }
Expand All @@ -82,10 +84,10 @@ def configure_dhcp
# try to find just one dhcp aware device for allowing default route
# if there is more than one dhcp devices enabled for setting default
# route (DHCLIENT_SET_DEFAULT_ROUTE = "yes"). bnc#868187
active_devices.find { |d| set_default_route_flag_if_wan_dev?(d) }
active_devices.find { |d| set_default_route_flag_if_wan_dev?(d.name) }
end

activate_changes(dhcp_cards)
activate_changes(dhcp_cards.map(&:name))
end

# Propose configuration for virtual devices
Expand Down Expand Up @@ -126,73 +128,41 @@ def configure_hosts

private

def network_cards
LanItems.Read
LanItems.GetNetcardNames
end

# Makes DHCP setup persistent
#
# instsys currently uses wicked as network services "manager" (including
# dhcp client). wicked is currently able to configure a card for dhcp leases
# only via loading config from file. All other ways are workarounds and
# needn't to work when wickedd* services are already running
# @param card [Y2Network::Interface]
def setup_dhcp(card)
index = LanItems.FindDeviceIndex(card)

raise "Failed to save configuration for device #{card}" if index == -1

LanItems.current = index
builder = Y2Network::InterfaceConfigBuilder.for(card.type)
builder.name = card.name

builder = Y2Network::InterfaceConfigBuilder.for(LanItems.GetCurrentType())
builder.name = LanItems.GetCurrentName()

LanItems.SetItem(builder: builder)

# tricky part if ifcfg is not set
# yes, this code smell and show bad API of LanItems
if !LanItems.IsCurrentConfigured
NetworkInterfaces.Add
current = LanItems.Items[LanItems.current]
current["ifcfg"] = card
end

builder.boot_protocol = "dhcp"
builder.startmode = "auto"
builder.boot_protocol = Y2Network::BootProtocol::DHCP
builder.startmode = Y2Network::Startmode.create("auto")

LanItems.Commit(builder)
end

def delete_config(devname)
LanItems.delete_dev(devname)
def delete_config(interface)
config.delete_interface(interface.name)
end

def write_configuration
NetworkInterfaces.Write("")
config.write
end

# Writes and activates changes in devices configurations
#
# @param devnames [Array] list of device names
# @return true when changes were successfully applied
def activate_changes(devnames)
return false if !write_configuration

# workaround for gh#yast/yast-core#74 (https://github.com/yast/yast-core/issues/74)
NetworkInterfaces.CleanCacheRead()
write_configuration

reload_config(devnames)
end

def configured?(devname)
# TODO: one day there should be LanItems.IsItemConfigured, but we currently
# miss index -> devname translation. As this LanItems internal structure
# will be subject of refactoring, we will use NetworkInterfaces directly.
# It currently doesn't hurt as it currently writes configuration for both
# wicked even sysconfig.
NetworkInterfaces.Check(devname)
end

# Checks if given device is active
#
# active device <=> a device which is reported as "up" by wicked
Expand Down Expand Up @@ -240,8 +210,7 @@ def set_default_route_flag_if_wan_dev?(devname)
# @param [String] devname name of device as seen by system (e.g. enp0s3)
# @param [String] value "yes" or "no", as in sysconfig
def set_default_route_flag(devname, value)
item_id = LanItems.FindDeviceIndex(devname)
LanItems.SetItemSysconfigOpt(item_id, "DHCLIENT_SET_DEFAULT_ROUTE", value)
# TODO: not implemented
end

# Decides if a proposal for virtualization host machine is required.
Expand All @@ -255,5 +224,9 @@ def virtual_proposal_required?

false
end

def config
Yast::Lan.yast_config
end
end
end
4 changes: 2 additions & 2 deletions src/lib/y2network/autoinst/interfaces_reader.rb
Expand Up @@ -86,8 +86,8 @@ def load_generic(config, interface_section)
# Assign first netmask, as prefixlen has precedence so it will overwrite it
ipaddr.netmask = interface_section.netmask if interface_section.netmask
ipaddr.prefix = interface_section.prefixlen.to_i if interface_section.prefixlen
broadcast = interface_section.broadcast && IPAddress.new(interface_section.broadcast)
remote = interface_section.remote_ipaddr && IPAddress.new(interface_section.remote_ipaddr)
broadcast = IPAddress.new(interface_section.broadcast) unless interface_section.broadcast.empty?
remote = IPAddress.new(interface_section.remote_ipaddr) unless interface_section.remote_ipaddr.empty?
config.ip = ConnectionConfig::IPConfig.new(ipaddr, broadcast: broadcast, remote_address: remote)
end

Expand Down

0 comments on commit 29027d2

Please sign in to comment.