Make atomic-host guest respect 'hostname' network config option
This commit is contained in:
parent
71ea0e6f2a
commit
326f0a4858
@ -1,7 +1,11 @@
|
||||
require 'vagrant/util/guest_hosts'
|
||||
|
||||
module VagrantPlugins
|
||||
module GuestAtomic
|
||||
module Cap
|
||||
class ChangeHostName
|
||||
extend Vagrant::Util::GuestHosts::Linux
|
||||
|
||||
def self.change_host_name(machine, name)
|
||||
comm = machine.communicate
|
||||
|
||||
@ -10,13 +14,14 @@ module VagrantPlugins
|
||||
comm.sudo <<-EOH.gsub(/^ {14}/, "")
|
||||
# Set hostname
|
||||
hostnamectl set-hostname '#{basename}'
|
||||
|
||||
# Prepend ourselves to /etc/hosts
|
||||
test $? -eq 0 && (grep -w '#{name}' /etc/hosts || {
|
||||
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /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
|
||||
|
||||
@ -1,14 +1,17 @@
|
||||
require_relative "../../../../base"
|
||||
|
||||
describe "VagrantPlugins::GuestAtomic::Cap::ChangeHostName" do
|
||||
let(:caps) do
|
||||
let(:described_class) do
|
||||
VagrantPlugins::GuestAtomic::Plugin
|
||||
.components
|
||||
.guest_capabilities[:atomic]
|
||||
.get(:change_host_name)
|
||||
end
|
||||
|
||||
let(:machine) { double("machine") }
|
||||
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||
let(:name) { "banana-rama.example.com" }
|
||||
let(:basename) { "banana-rama" }
|
||||
|
||||
before do
|
||||
allow(machine).to receive(:communicate).and_return(comm)
|
||||
@ -19,21 +22,54 @@ describe "VagrantPlugins::GuestAtomic::Cap::ChangeHostName" do
|
||||
end
|
||||
|
||||
describe ".change_host_name" do
|
||||
let(:cap) { caps.get(:change_host_name) }
|
||||
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"}]
|
||||
] }
|
||||
|
||||
let(:name) { "banana-rama.example.com" }
|
||||
before do
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
end
|
||||
|
||||
it "sets the hostname" do
|
||||
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
|
||||
it "sets the hostname" do
|
||||
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
|
||||
|
||||
cap.change_host_name(machine, name)
|
||||
expect(comm.received_commands[1]).to match(/hostnamectl set-hostname 'banana-rama'/)
|
||||
described_class.change_host_name(machine, name)
|
||||
expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{basename}'/)
|
||||
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(/hostnamectl set-hostname/)
|
||||
end
|
||||
end
|
||||
|
||||
it "does not change the hostname if already set" do
|
||||
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0)
|
||||
cap.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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user