From 1761e65f2682b2aa0e459565c35ef5cbca672442 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 23 Oct 2018 10:17:39 -0700 Subject: [PATCH] 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 --- plugins/guests/debian/cap/change_host_name.rb | 21 ++++++++++++++++-- .../debian/cap/change_host_name_test.rb | 22 ++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/plugins/guests/debian/cap/change_host_name.rb b/plugins/guests/debian/cap/change_host_name.rb index d8a127100..2323734bd 100644 --- a/plugins/guests/debian/cap/change_host_name.rb +++ b/plugins/guests/debian/cap/change_host_name.rb @@ -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 diff --git a/test/unit/plugins/guests/debian/cap/change_host_name_test.rb b/test/unit/plugins/guests/debian/cap/change_host_name_test.rb index 73a70564c..b5a319185 100644 --- a/test/unit/plugins/guests/debian/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/debian/cap/change_host_name_test.rb @@ -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