Merge pull request #10301 from briancain/debian-fixup-hostname-change

Fixes #9763, #10300: Split out how hostname is set with Debian hosts
This commit is contained in:
Brian Cain 2018-10-18 08:57:29 -07:00 committed by GitHub
commit e54c9b22ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 93 additions and 13 deletions

View File

@ -2,6 +2,9 @@ module VagrantPlugins
module GuestDebian
module Cap
class ChangeHostName
extend Vagrant::Util::GuestInspection::Linux
def self.change_host_name(machine, name)
comm = machine.communicate
@ -10,11 +13,6 @@ module VagrantPlugins
comm.sudo <<-EOH.gsub(/^ {14}/, '')
# Set the hostname
echo '#{basename}' > /etc/hostname
hostname -F /etc/hostname
if command -v hostnamectl; then
hostnamectl set-hostname '#{basename}'
fi
# Prepend ourselves to /etc/hosts
grep -w '#{name}' /etc/hosts || {
@ -28,6 +26,13 @@ module VagrantPlugins
# Update mailname
echo '#{name}' > /etc/mailname
EOH
if hostnamectl?(comm)
comm.sudo("hostnamectl set-hostname '#{basename}'")
else
comm.sudo <<-EOH.gsub(/^ {14}/, '')
hostname -F /etc/hostname
# Restart hostname services
if test -f /etc/init.d/hostname; then
/etc/init.d/hostname start || true
@ -36,11 +41,19 @@ module VagrantPlugins
if test -f /etc/init.d/hostname.sh; then
/etc/init.d/hostname.sh start || true
fi
if test -x /sbin/dhclient ; then
/sbin/dhclient -r
/sbin/dhclient -nw
fi
EOH
EOH
end
restart_command = "/etc/init.d/networking restart"
if systemd?(comm)
if systemd_networkd?(comm)
restart_command = "systemctl restart systemd-networkd.service"
elsif systemd_controlled?(comm, "NetworkManager.service")
restart_command = "systemctl restart NetworkManager.service"
end
end
comm.sudo(restart_command)
end
end
end

View File

@ -20,14 +20,81 @@ describe "VagrantPlugins::GuestDebian::Cap::ChangeHostName" do
describe ".change_host_name" do
let(:cap) { caps.get(:change_host_name) }
let(:name) { 'banana-rama.example.com' }
let(:systemd) { true }
let(:hostnamectl) { true }
let(:networkd) { true }
let(:network_manager) { false }
before do
allow(cap).to receive(:systemd?).and_return(systemd)
allow(cap).to receive(:hostnamectl?).and_return(hostnamectl)
allow(cap).to receive(:systemd_networkd?).and_return(networkd)
allow(cap).to receive(:systemd_controlled?).with(anything, /NetworkManager/).and_return(network_manager)
end
it "sets the hostname if not set" do
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
cap.change_host_name(machine, name)
expect(comm.received_commands[1]).to match(/hostname -F \/etc\/hostname/)
expect(comm.received_commands[1]).to match(/hostname.sh start/)
expect(comm.received_commands[1]).to match(/echo 'banana-rama' > \/etc\/hostname/)
end
context "when hostnamectl is in use" do
let(:hostnamectl) { true }
it "sets hostname with hostnamectl" do
cap.change_host_name(machine, name)
expect(comm.received_commands[2]).to match(/hostnamectl/)
end
end
context "when hostnamectl is not in use" do
let(:hostnamectl) { false }
it "sets hostname with hostname command" do
cap.change_host_name(machine, name)
expect(comm.received_commands[2]).to match(/hostname -F/)
end
end
context "restarts the network" do
context "when networkd is in use" do
let(:networkd) { true }
it "restarts networkd with systemctl" do
cap.change_host_name(machine, name)
expect(comm.received_commands[3]).to match(/systemctl restart systemd-networkd/)
end
end
context "when NetworkManager is in use" do
let(:networkd) { false }
let(:network_manager) { true }
it "restarts NetworkManager with systemctl" do
cap.change_host_name(machine, name)
expect(comm.received_commands[3]).to match(/systemctl restart NetworkManager/)
end
end
context "when networkd and NetworkManager are not in use" do
let(:networkd) { false }
let(:network_manager) { false }
it "restarts the network using service" do
cap.change_host_name(machine, name)
expect(comm.received_commands[3]).to match(/networking restart/)
end
end
context "when systemd is not in use" do
let(:systemd) { false }
it "restarts the network using service" do
cap.change_host_name(machine, name)
expect(comm.received_commands[3]).to match(/networking restart/)
end
end
end
it "does not set the hostname if unset" do