Restart each interface if systemd-networkd or networkmanager is not used

This commit is a workaround due to how older debian and ubuntu systems
fail to properly restart networking. Instead of relying on the init
scripts or ifup/down tools to restart each interface, this commit
instead restarts each interface individually
This commit is contained in:
Brian Cain 2018-10-24 11:29:59 -07:00
parent 1761e65f26
commit e8c6916ebc
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
2 changed files with 81 additions and 23 deletions

View File

@ -1,4 +1,5 @@
require "log4r" require "log4r"
require_relative "../../linux/cap/network_interfaces"
module VagrantPlugins module VagrantPlugins
module GuestDebian module GuestDebian
@ -47,11 +48,7 @@ module VagrantPlugins
EOH EOH
end end
init_restart_command = "/etc/init.d/networking restart" restart_command = nil
ifdownup_restart_command = "ifdown --exclude=lo -a && ifup --exclude=lo -a"
systemctl_restart_command = "systemctl stop ifup@eth0.service && systemctl start ifup@eth0.service"
restart_command = ifdownup_restart_command
if systemd?(comm) if systemd?(comm)
if systemd_networkd?(comm) if systemd_networkd?(comm)
@logger.debug("Attempting to restart networking with systemd-networkd") @logger.debug("Attempting to restart networking with systemd-networkd")
@ -59,20 +56,49 @@ module VagrantPlugins
elsif systemd_controlled?(comm, "NetworkManager.service") elsif systemd_controlled?(comm, "NetworkManager.service")
@logger.debug("Attempting to restart networking with NetworkManager") @logger.debug("Attempting to restart networking with NetworkManager")
restart_command = "systemctl restart NetworkManager.service" restart_command = "systemctl restart NetworkManager.service"
else
@logger.debug("Attempting to restart networking with ifup@.service")
restart_command = systemctl_restart_command
end end
else
@logger.debug("Attempting to restart networking with ifdown/ifup net tools")
end end
if !comm.test(restart_command, sudo: true) if restart_command
@logger.debug("Attempting to restart networking with init networking service") comm.sudo(restart_command)
comm.sudo(init_restart_command) else
restart_each_interface(machine, @logger)
end end
end end
end end
protected
# Due to how most Debian systems and older Ubuntu systems handle restarting
# networking, we cannot simply run the networking init script or use the ifup/down
# tools to restart all interfaces to renew the machines DHCP lease when setting
# its hostname. This method is a workaround for those older systems that
# cannoy reliably restart networking. It restarts each individual interface
# on its own instead.
#
# @param [Vagrant::Machine] machine
# @param [Log4r::Logger] logger
def self.restart_each_interface(machine, logger)
comm = machine.communicate
interfaces = VagrantPlugins::GuestLinux::Cap::NetworkInterfaces.network_interfaces(machine)
nettools = true
if systemd?(comm)
@logger.debug("Attempting to restart networking with systemctl")
nettools = false
else
@logger.debug("Attempting to restart networking with ifup/down nettools")
end
interfaces.each do |iface|
logger.debug("Restarting interface #{iface} on guest #{machine.name}")
if nettools
restart_command = "ifdown #{iface} && ifup #{iface}"
else
restart_command = "systemctl stop ifup@#{iface}.service && systemctl start ifup@#{iface}.service"
end
comm.sudo(restart_command)
end
end
end end
end end
end end

View File

@ -7,7 +7,8 @@ describe "VagrantPlugins::GuestDebian::Cap::ChangeHostName" do
.guest_capabilities[:debian] .guest_capabilities[:debian]
end end
let(:machine) { double("machine") } let(:machine) { double("machine", name: "guestname") }
let(:logger) { double("logger", debug: true) }
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
before do before do
@ -83,14 +84,15 @@ describe "VagrantPlugins::GuestDebian::Cap::ChangeHostName" do
let(:systemd) { true } let(:systemd) { true }
it "restarts the network using systemctl" do it "restarts the network using systemctl" do
expect(cap).to receive(:restart_each_interface).
with(machine, anything)
cap.change_host_name(machine, name) cap.change_host_name(machine, name)
expect(comm.received_commands[3]).to match(/systemctl stop ifup/)
end end
it "restarts networking with networking init script" do it "restarts networking with networking init script" do
comm.stub_command("systemctl stop ifup@eth0.service && systemctl start ifup@eth0.service", exit_code: 1) expect(cap).to receive(:restart_each_interface).
with(machine, anything)
cap.change_host_name(machine, name) cap.change_host_name(machine, name)
expect(comm.received_commands[4]).to match(/networking restart/)
end end
end end
@ -98,17 +100,15 @@ describe "VagrantPlugins::GuestDebian::Cap::ChangeHostName" do
let(:systemd) { false } let(:systemd) { false }
it "restarts the network using service" do it "restarts the network using service" do
comm.stub_command("systemctl stop ifup@eth0.service && systemctl start ifup@eth0.service", exit_code: 1) expect(cap).to receive(:restart_each_interface).
with(machine, anything)
cap.change_host_name(machine, name) cap.change_host_name(machine, name)
expect(comm.received_commands[4]).to match(/networking restart/)
end end
it "restarts the network using ifdown/ifup" do it "restarts the network using ifdown/ifup" do
comm.stub_command("systemctl stop ifup@eth0.service && systemctl start ifup@eth0.service", exit_code: 1) expect(cap).to receive(:restart_each_interface).
comm.stub_command("/etc/init.d/networking restart", exit_code: 1) with(machine, anything)
cap.change_host_name(machine, name) cap.change_host_name(machine, name)
expect(comm.received_commands[4]).to match(/ifdown/)
expect(comm.received_commands[4]).to match(/ifup/)
end end
end end
end end
@ -119,4 +119,36 @@ describe "VagrantPlugins::GuestDebian::Cap::ChangeHostName" do
expect(comm.received_commands.size).to eq(1) expect(comm.received_commands.size).to eq(1)
end end
end end
describe ".restart_each_interface" do
let(:cap) { caps.get(:change_host_name) }
let(:systemd) { true }
let(:interfaces) { ["eth0", "eth1", "eth2"] }
before do
allow(cap).to receive(:systemd?).and_return(systemd)
allow(VagrantPlugins::GuestLinux::Cap::NetworkInterfaces).to receive(:network_interfaces).
and_return(interfaces)
end
context "with nettools" do
let(:systemd) { false }
it "restarts every interface" do
cap.send(:restart_each_interface, machine, logger)
expect(comm.received_commands[0]).to match(/ifdown eth0 && ifup eth0/)
expect(comm.received_commands[1]).to match(/ifdown eth1 && ifup eth1/)
expect(comm.received_commands[2]).to match(/ifdown eth2 && ifup eth2/)
end
end
context "with systemctl" do
it "restarts every interface" do
cap.send(:restart_each_interface, machine, logger)
expect(comm.received_commands[0]).to match(/systemctl stop ifup@eth0.service && systemctl start ifup@eth0.service/)
expect(comm.received_commands[1]).to match(/systemctl stop ifup@eth1.service && systemctl start ifup@eth1.service/)
expect(comm.received_commands[2]).to match(/systemctl stop ifup@eth2.service && systemctl start ifup@eth2.service/)
end
end
end
end end