Skip to content

Commit

Permalink
Create a backup of the modified interface and restore if canceled
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Feb 22, 2019
1 parent d866613 commit 3f698ba
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 50 deletions.
65 changes: 44 additions & 21 deletions src/include/network/lan/address.rb
Expand Up @@ -1336,6 +1336,14 @@ def AddressDialog
Builtins.y2milestone("ShowAndRun: %1", ret)

if ret != :back && ret != :abort
# When virtual interfaces are added the list of routing devices needs
# to be updated to offer them
LanItems.update_routing_devices! if LanItems.update_routing_devices?
if !@settings.fetch("IFCFG", "").empty? && (@settings["IFCFG"] != LanItems.device)
LanItems.update_routes!(@settings["IFCFG"]) if update_routes?(@settings["IFCFG"])
end

LanItems.reset_backup!
# general tab
LanItems.startmode = Ops.get_string(@settings, "STARTMODE", "")
LanItems.mtu = Ops.get_string(@settings, "MTU", "")
Expand Down Expand Up @@ -1371,27 +1379,23 @@ def AddressDialog
end
end

# When virtual interfaces are added the list of routing devices needs
# to be updated to offer them
LanItems.update_routing_devices! if LanItems.update_routing_devices?
end

if LanItems.type == "vlan"
LanItems.vlan_etherdevice = Ops.get_string(@settings, "ETHERDEVICE", "")
LanItems.vlan_id = Builtins.tostring(
Ops.get_integer(@settings, "VLAN_ID", 0)
)
elsif Builtins.contains(["tun", "tap"], LanItems.type)
LanItems.tunnel_set_owner = Ops.get_string(
@settings,
"TUNNEL_SET_OWNER",
""
)
LanItems.tunnel_set_group = Ops.get_string(
@settings,
"TUNNEL_SET_GROUP",
""
)
if LanItems.type == "vlan"
LanItems.vlan_etherdevice = Ops.get_string(@settings, "ETHERDEVICE", "")
LanItems.vlan_id = Builtins.tostring(
Ops.get_integer(@settings, "VLAN_ID", 0)
)
elsif Builtins.contains(["tun", "tap"], LanItems.type)
LanItems.tunnel_set_owner = Ops.get_string(
@settings,
"TUNNEL_SET_OWNER",
""
)
LanItems.tunnel_set_group = Ops.get_string(
@settings,
"TUNNEL_SET_GROUP",
""
)
end
end

# proceed with WLAN settings if appropriate, #42420
Expand All @@ -1402,6 +1406,25 @@ def AddressDialog

private

# When an interface name has changed, it returns whether the user wants to
# update the interface name in the related routes or not.
#
# return [Boolean] whether the routes have to be updated or not
def update_routes?(previous_name)
return false unless Routing.device_routes?(previous_name)

Popup.YesNoHeadline(
Label.WarningMsg,
# TRANSLATORS: Ask for fixing a possible conflict after renaming
# an interface, %s are the previous and current interface names
format(_("The interface %s has been renamed to %s. There are \n" \
"some routes that still use the previous name.\n\n" \
"Would you like to update them now?\n"),
"'#{previous_name}'",
"'#{LanItems.current_name}'")
)
end

# Initializes the Address Dialog @settings with the corresponding LanItems values
def initialize_address_settings
@settings = {
Expand Down
2 changes: 2 additions & 0 deletions src/include/network/lan/complex.rb
Expand Up @@ -374,6 +374,7 @@ def handleOverview(_key, event)
if Ops.get_string(event, "EventReason", "") == "Activated"
case Ops.get_symbol(event, "ID")
when :add
LanItems.create_backup!
LanItems.AddNew
Lan.Add

Expand All @@ -388,6 +389,7 @@ def handleOverview(_key, event)

return :add
when :edit
LanItems.create_backup!
if LanItems.IsCurrentConfigured
LanItems.SetItem

Expand Down
24 changes: 0 additions & 24 deletions src/lib/network/edit_nic_name.rb
Expand Up @@ -74,11 +74,6 @@ def run
# 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 new_name != old_name
LanItems.update_routing_devices!
LanItems.update_routes!(old_name) if update_routes?(old_name)
end
end

close
Expand Down Expand Up @@ -162,24 +157,5 @@ def CheckUdevNicName(name)

true
end

# When an interface name has changed, it returns whether the user wants to
# update the interface name in the related routes or not.
#
# return [Boolean] whether the routes have to be updated or not
def update_routes?(previous_name)
return false unless Routing.device_routes?(previous_name)

Popup.YesNoHeadline(
Label.WarningMsg,
# TRANSLATORS: Ask for fixing a possible conflict after renaming
# an interface, %s are the previous and current interface names
format(_("The interface %s has been renamed to %s. There are \n" \
"some routes that still use the previous name.\n\n" \
"Would you like to update them now?\n"),
"'#{previous_name}'",
"'#{LanItems.current_name}'")
)
end
end
end
27 changes: 22 additions & 5 deletions src/modules/LanItems.rb
Expand Up @@ -50,6 +50,7 @@ class LanItemsClass < Module
attr_reader :ipoib_modes
attr_accessor :ipoib_mode
attr_accessor :firewall_zone
attr_accessor :current_backup

include Logger
include Wicked
Expand Down Expand Up @@ -1993,6 +1994,7 @@ def setup_bonding(devmap, slaves, options)
def Commit
if @operation != :add && @operation != :edit
log.error("Unknown operation: #{@operation}")
reset_backup!
raise ArgumentError, "Unknown operation: #{@operation}"
end

Expand Down Expand Up @@ -2184,22 +2186,21 @@ def Commit
end

@operation = nil
reset_backup!
true
end

# Remove a half-configured item.
# @return [true] so that this can be used for the :abort callback
def Rollback
log.info "rollback item #{@current}"
log.info "rollback item #{@current} and operation #{LanItems.operation}"
# Do not delete elements that are :edited but does not contain hwinfo
# yet (Add a virtual device and then edit it canceling the process during the
# edition)
if LanItems.operation == :add && getCurrentItem.fetch("hwinfo", {}).empty?
LanItems.Items.delete(@current)
elsif IsCurrentConfigured()
if !getNetworkInterfaces.include?(getCurrentItem["ifcfg"])
LanItems.Items[@current].delete("ifcfg")
end
else
LanItems.restore_backup!
end

true
Expand Down Expand Up @@ -2655,6 +2656,7 @@ def enslaved?(ifcfg_name)
# Return the current name of the {LanItem} given
#
# @param item_id [Integer] a key for {#Items}
# @return [String] device name
def current_name_for(item_id)
renamed?(item_id) ? renamed_to(item_id) : GetDeviceName(item_id)
end
Expand Down Expand Up @@ -2687,6 +2689,21 @@ def update_routes!(previous_name)
Routing.device_routes(previous_name).each { |r| r["device"] = current_name }
end

def restore_backup!
return unless current_backup

Items()[@current] = Marshal.load(current_backup)
reset_backup!
end

def create_backup!
@current_backup = Marshal.dump(getCurrentItem)
end

def reset_backup!
@current_backup = nil
end

private

# Checks if given lladdr can be written into ifcfg
Expand Down

0 comments on commit 3f698ba

Please sign in to comment.