Fixes #9763 #10300: Fall back on ifdown/ifup tools for network restart

This commit adds some additional logic that falls back to using the
ifdown/ifup tools to restart networking. On Ubuntu 14.04, the init
script was designed to always fail to restart newtorking, so it needs
to use the ifdown/up tools instead. This commit will use the networking
init script as a last resort to restart networking, assuming other
commands haven't broken networking already.

https://bugs.launchpad.net/ubuntu/+source/ifupdown/+bug/1301015
This commit is contained in:
Brian Cain 2018-10-23 10:17:39 -07:00
parent 6b34f3655a
commit 1761e65f26
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
2 changed files with 38 additions and 5 deletions

View File

@ -1,3 +1,5 @@
require "log4r"
module VagrantPlugins
module GuestDebian
module Cap
@ -6,6 +8,7 @@ module VagrantPlugins
extend Vagrant::Util::GuestInspection::Linux
def self.change_host_name(machine, name)
@logger = Log4r::Logger.new("vagrant::guest::debian::changehostname")
comm = machine.communicate
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
@ -44,16 +47,30 @@ module VagrantPlugins
EOH
end
restart_command = "/etc/init.d/networking restart"
init_restart_command = "/etc/init.d/networking restart"
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_networkd?(comm)
@logger.debug("Attempting to restart networking with systemd-networkd")
restart_command = "systemctl restart systemd-networkd.service"
elsif systemd_controlled?(comm, "NetworkManager.service")
@logger.debug("Attempting to restart networking with NetworkManager")
restart_command = "systemctl restart NetworkManager.service"
else
@logger.debug("Attempting to restart networking with ifup@.service")
restart_command = systemctl_restart_command
end
else
@logger.debug("Attempting to restart networking with ifdown/ifup net tools")
end
if !comm.test(restart_command, sudo: true)
@logger.debug("Attempting to restart networking with init networking service")
comm.sudo(init_restart_command)
end
comm.sudo(restart_command)
end
end
end

View File

@ -80,10 +80,17 @@ describe "VagrantPlugins::GuestDebian::Cap::ChangeHostName" do
context "when networkd and NetworkManager are not in use" do
let(:networkd) { false }
let(:network_manager) { false }
let(:systemd) { true }
it "restarts the network using service" do
it "restarts the network using systemctl" do
cap.change_host_name(machine, name)
expect(comm.received_commands[3]).to match(/networking restart/)
expect(comm.received_commands[3]).to match(/systemctl stop ifup/)
end
it "restarts networking with networking init script" do
comm.stub_command("systemctl stop ifup@eth0.service && systemctl start ifup@eth0.service", exit_code: 1)
cap.change_host_name(machine, name)
expect(comm.received_commands[4]).to match(/networking restart/)
end
end
@ -91,8 +98,17 @@ describe "VagrantPlugins::GuestDebian::Cap::ChangeHostName" do
let(:systemd) { false }
it "restarts the network using service" do
comm.stub_command("systemctl stop ifup@eth0.service && systemctl start ifup@eth0.service", exit_code: 1)
cap.change_host_name(machine, name)
expect(comm.received_commands[3]).to match(/networking restart/)
expect(comm.received_commands[4]).to match(/networking restart/)
end
it "restarts the network using ifdown/ifup" do
comm.stub_command("systemctl stop ifup@eth0.service && systemctl start ifup@eth0.service", exit_code: 1)
comm.stub_command("/etc/init.d/networking restart", exit_code: 1)
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