Reduce commands. Allow nm reload/restart to bring interfaces up.

Reduce the total number of commands run to configure interfaces. If
a service reload/restart is required, only execute it once instead
of once per device. When nm is managing a device, the explicit up
is not required.
This commit is contained in:
Chris Roberts 2017-05-01 13:17:17 -07:00
parent 414184b76b
commit 1df852c88a
2 changed files with 16 additions and 13 deletions

View File

@ -14,7 +14,7 @@ module VagrantPlugins
network_scripts_dir = machine.guest.capability(:network_scripts_dir)
commands = []
commands = {:start => [], :middle => [], :end => []}
interfaces = machine.guest.capability(:network_interfaces)
# Check if NetworkManager is installed on the system
@ -64,21 +64,25 @@ module VagrantPlugins
final_path = "#{network_scripts_dir}/ifcfg-#{network[:device]}"
if nm_controlled
commands << "nmcli d disconnect '#{network[:device]}'"
if extra_opts[:nm_controlled] == "no"
commands[:start] << "nmcli d disconnect iface '#{network[:device]}'"
end
else
commands << "/sbin/ifdown '#{network[:device]}'"
end
commands << "mv -f '#{remote_path}' '#{final_path}'"
if nmcli_installed
commands << "nmcli c reload"
commands[:start] << "/sbin/ifdown '#{network[:device]}'"
end
commands[:middle] << "mv -f '#{remote_path}' '#{final_path}'"
if extra_opts[:nm_controlled] == "no"
commands << "/sbin/ifup '#{network[:device]}'"
else
commands << "nmcli c up ifname '#{network[:device]}'"
commands[:end] << "/sbin/ifup '#{network[:device]}'"
end
end
if nmcli_installed
commands[:middle] << "((nmcli c help 2>&1 | grep reload) && nmcli c reload) || " \
"(test -f /etc/init.d/NetworkManager && /etc/init.d/NetworkManager restart) || " \
"((systemctl | grep NetworkManager.service) && systemctl NetworkManager restart)"
end
commands = commands[:start] + commands[:middle] + commands[:end]
comm.sudo(commands.join("\n"))
comm.wait_for_ready(5)
end
end
end

View File

@ -104,7 +104,7 @@ describe "VagrantPlugins::GuestRedHat::Cap::ConfigureNetworks" do
expect(comm.received_commands[0]).to match(/nmcli.*disconnect.*eth1/)
expect(comm.received_commands[0]).to match(/nmcli c reload/)
expect(comm.received_commands[0]).to match(/ifup.*eth1/)
expect(comm.received_commands[0]).to match(/nmcli.*up.*eth2/)
expect(comm.received_commands[0]).to match(/nmcli.*reload/)
expect(comm.received_commands[0]).to_not match(/ifdown/)
end
end
@ -157,9 +157,8 @@ describe "VagrantPlugins::GuestRedHat::Cap::ConfigureNetworks" do
cap.configure_networks(machine, [network_1, network_2])
expect(comm.received_commands[0]).to_not match(/nmcli.*disconnect/)
expect(comm.received_commands[0]).to match(/ifdown/)
expect(comm.received_commands[0]).to match(/nmcli c reload/)
expect(comm.received_commands[0]).to match(/ifup.*eth1/)
expect(comm.received_commands[0]).to match(/nmcli.*up.*eth2/)
expect(comm.received_commands[0]).to match(/nmcli.*reload/)
end
end
end