Set hostname in /etc/hosts as first step to changing hostname

This commit is contained in:
sophia 2020-09-08 09:55:21 -05:00
parent ffa45c4e96
commit 638e3d35b9
2 changed files with 12 additions and 8 deletions

View File

@ -1,5 +1,6 @@
require "log4r"
require 'vagrant/util/guest_hosts'
require 'vagrant/util/guest_inspection'
require_relative "../../linux/cap/network_interfaces"
module VagrantPlugins
@ -15,6 +16,13 @@ module VagrantPlugins
comm = machine.communicate
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
network_with_hostname = machine.config.vm.networks.map {|_, c| c if c[:hostname] }.compact[0]
if network_with_hostname
replace_host(comm, name, network_with_hostname[:ip])
else
add_hostname_to_loopback_interface(comm, name)
end
basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, '')
# Set the hostname
@ -58,13 +66,6 @@ module VagrantPlugins
restart_each_interface(machine, @logger)
end
end
network_with_hostname = machine.config.vm.networks.map {|_, c| c if c[:hostname] }.compact[0]
if network_with_hostname
replace_host(comm, name, network_with_hostname[:ip])
else
add_hostname_to_loopback_interface(comm, name)
end
end
protected

View File

@ -36,6 +36,8 @@ describe "VagrantPlugins::GuestDebian::Cap::ChangeHostName" do
allow(cap).to receive(:systemd_networkd?).and_return(networkd)
allow(cap).to receive(:systemd_controlled?).with(anything, /NetworkManager/).and_return(network_manager)
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
allow(cap).to receive(:add_hostname_to_loopback_interface)
allow(cap).to receive(:replace_host)
end
context "minimal network config" do
@ -153,8 +155,9 @@ describe "VagrantPlugins::GuestDebian::Cap::ChangeHostName" do
it "does not set the hostname if unset" do
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0)
expect(cap).to_not receive(:add_hostname_to_loopback_interface)
expect(cap).to_not receive(:replace_host)
cap.change_host_name(machine, name)
expect(comm.received_commands.size).to eq(2)
end
end