Make darwin guest respect 'hostname' network config option
This commit is contained in:
parent
964cafc606
commit
db6d1b4aa6
@ -2,11 +2,10 @@ module Vagrant
|
||||
module Util
|
||||
# Helper methods for modfiying guests /etc/hosts file
|
||||
module GuestHosts
|
||||
# Linux specific inspection helpers
|
||||
module Linux
|
||||
|
||||
module Unix
|
||||
DEAFAULT_LOOPBACK_CHECK_LIMIT = 5.freeze
|
||||
|
||||
|
||||
# Add hostname to a loopback address on /etc/hosts if not already there
|
||||
# Will insert name at the first free address of the form 127.0.X.1, up to
|
||||
# the loop_bound
|
||||
@ -27,7 +26,11 @@ module Vagrant
|
||||
}
|
||||
EOH
|
||||
end
|
||||
end
|
||||
|
||||
# Linux specific inspection helpers
|
||||
module Linux
|
||||
include Unix
|
||||
# Remove any line in /etc/hosts that contains hostname,
|
||||
# then add hostname with associated ip
|
||||
#
|
||||
@ -42,6 +45,24 @@ module Vagrant
|
||||
EOH
|
||||
end
|
||||
end
|
||||
|
||||
# Darwin specific inspection helpers
|
||||
module Darwin
|
||||
include Unix
|
||||
# Remove any line in /etc/hosts that contains hostname,
|
||||
# then add hostname with associated ip
|
||||
#
|
||||
# @param [Communicator]
|
||||
# @param [String] full hostanme
|
||||
# @param [String] target ip
|
||||
def replace_host(comm, name, ip)
|
||||
basename = name.split(".", 2)[0]
|
||||
comm.sudo <<-EOH.gsub(/^ {14}/, '')
|
||||
sed -i '' '/#{name}/d' /etc/hosts
|
||||
sed -i '' '1i\\\n#{ip}\t#{name}\t#{basename}\n' /etc/hosts
|
||||
EOH
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,7 +1,11 @@
|
||||
require 'vagrant/util/guest_hosts'
|
||||
|
||||
module VagrantPlugins
|
||||
module GuestDarwin
|
||||
module Cap
|
||||
class ChangeHostName
|
||||
extend Vagrant::Util::GuestHosts::Darwin
|
||||
|
||||
def self.change_host_name(machine, name)
|
||||
comm = machine.communicate
|
||||
|
||||
@ -21,14 +25,14 @@ module VagrantPlugins
|
||||
fi
|
||||
|
||||
hostname '#{name}'
|
||||
|
||||
# Prepend ourselves to /etc/hosts - sed on bsd is sad
|
||||
grep -w '#{name}' /etc/hosts || {
|
||||
echo -e '127.0.0.1\\t#{name}\\t#{basename}' | cat - /etc/hosts > /tmp/tmp-hosts &&
|
||||
mv /tmp/tmp-hosts /etc/hosts
|
||||
}
|
||||
EOH
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
@ -10,9 +10,13 @@ describe "VagrantPlugins::GuestDarwin::Cap::ChangeHostName" do
|
||||
|
||||
let(:machine) { double("machine") }
|
||||
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||
let(:name) { "banana-rama.example.com" }
|
||||
let(:basename) { "banana-rama" }
|
||||
let(:networks) {}
|
||||
|
||||
before do
|
||||
allow(machine).to receive(:communicate).and_return(comm)
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
end
|
||||
|
||||
after do
|
||||
@ -20,21 +24,53 @@ describe "VagrantPlugins::GuestDarwin::Cap::ChangeHostName" do
|
||||
end
|
||||
|
||||
describe ".change_host_name" do
|
||||
let(:name) { "banana-rama.example.com" }
|
||||
context "minimal network config" do
|
||||
let(:networks) { [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}]
|
||||
] }
|
||||
|
||||
it "sets the hostname" do
|
||||
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
|
||||
described_class.change_host_name(machine, name)
|
||||
expect(comm.received_commands[1]).to match(/scutil --set ComputerName 'banana-rama.example.com'/)
|
||||
expect(comm.received_commands[1]).to match(/scutil --set HostName 'banana-rama.example.com'/)
|
||||
expect(comm.received_commands[1]).to match(/scutil --set LocalHostName 'banana-rama'/)
|
||||
expect(comm.received_commands[1]).to match(/hostname 'banana-rama.example.com'/)
|
||||
it "sets the hostname" do
|
||||
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
|
||||
described_class.change_host_name(machine, name)
|
||||
expect(comm.received_commands[1]).to match(/scutil --set ComputerName 'banana-rama.example.com'/)
|
||||
expect(comm.received_commands[1]).to match(/scutil --set HostName 'banana-rama.example.com'/)
|
||||
expect(comm.received_commands[1]).to match(/scutil --set LocalHostName 'banana-rama'/)
|
||||
expect(comm.received_commands[1]).to match(/hostname 'banana-rama.example.com'/)
|
||||
expect(described_class).to_not receive(:add_hostname_to_loopback_interface)
|
||||
end
|
||||
|
||||
it "does not change the hostname if already set" do
|
||||
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0)
|
||||
described_class.change_host_name(machine, name)
|
||||
expect(comm).to_not receive(:sudo).with(/scutil --set ComputerName 'banana-rama.example.com'/)
|
||||
expect(described_class).to_not receive(:add_hostname_to_loopback_interface)
|
||||
end
|
||||
end
|
||||
|
||||
it "does not change the hostname if already set" do
|
||||
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0)
|
||||
described_class.change_host_name(machine, name)
|
||||
expect(comm.received_commands.size).to eq(1)
|
||||
context "multiple networks configured with hostname" do
|
||||
it "adds a new entry only for the hostname" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :hostname=>true, :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to receive(:replace_host)
|
||||
expect(described_class).to_not receive(:add_hostname_to_loopback_interface)
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
|
||||
it "appends an entry to the loopback interface" do
|
||||
networks = [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}],
|
||||
[:public_network, {:ip=>"192.168.0.1", :protocol=>"tcp", :id=>"93a4ad88-0774-4127-a161-ceb715ff372f"}],
|
||||
[:public_network, {:ip=>"192.168.0.2", :protocol=>"tcp", :id=>"5aebe848-7d85-4425-8911-c2003d924120"}]
|
||||
]
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
expect(described_class).to_not receive(:replace_host)
|
||||
expect(described_class).to receive(:add_hostname_to_loopback_interface).once
|
||||
described_class.change_host_name(machine, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
43
test/unit/vagrant/util/guest_hosts_test.rb
Normal file
43
test/unit/vagrant/util/guest_hosts_test.rb
Normal file
@ -0,0 +1,43 @@
|
||||
require File.expand_path("../../../base", __FILE__)
|
||||
require 'vagrant/util/guest_hosts'
|
||||
|
||||
describe "Vagrant::Util::GuestHosts" do
|
||||
include_context "unit"
|
||||
|
||||
let(:machine) { double("machine") }
|
||||
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||
|
||||
before do
|
||||
allow(machine).to receive(:communicate).and_return(comm)
|
||||
end
|
||||
|
||||
describe "Linux" do
|
||||
subject{ Class.new { extend Vagrant::Util::GuestHosts::Linux } }
|
||||
|
||||
it "can add replace hostname" do
|
||||
subject.replace_host(comm, "test.end", "192.186.4.2")
|
||||
expect(comm.received_commands[0]).to match(/sed -i '\/test.end\/d' \/etc\/hosts/)
|
||||
end
|
||||
|
||||
it "can add hostname to loopback interface" do
|
||||
subject.add_hostname_to_loopback_interface(comm, "test.end", 40)
|
||||
expect(comm.received_commands[0]).to match(/for i in {1..40}; do/)
|
||||
expect(comm.received_commands[0]).to match(/echo \"127.0.\${i}.1 test.end test\" >> \/etc\/hosts/)
|
||||
end
|
||||
end
|
||||
|
||||
describe "Darwin" do
|
||||
subject{ Class.new { extend Vagrant::Util::GuestHosts::Darwin } }
|
||||
|
||||
it "can add replace hostname" do
|
||||
subject.replace_host(comm, "test.end", "192.186.4.2")
|
||||
expect(comm.received_commands[0]).to match(/sed -i '' '\/test.end\/d' \/etc\/hosts/)
|
||||
end
|
||||
|
||||
it "can add hostname to loopback interface" do
|
||||
subject.add_hostname_to_loopback_interface(comm, "test.end", 40)
|
||||
expect(comm.received_commands[0]).to match(/for i in {1..40}; do/)
|
||||
expect(comm.received_commands[0]).to match(/echo \"127.0.\${i}.1 test.end test\" >> \/etc\/hosts/)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user