Set hostname according to hostname flag and ip for suse
This commit is contained in:
parent
ed41cbd535
commit
f5d5baec40
@ -4,19 +4,38 @@ module VagrantPlugins
|
||||
class ChangeHostName
|
||||
def self.change_host_name(machine, name)
|
||||
comm = machine.communicate
|
||||
|
||||
basename = name.split(".", 2)[0]
|
||||
if !comm.test('test "$(hostnamectl --static status)" = "#{basename}"', sudo: false)
|
||||
comm.sudo <<-EOH.gsub(/^ {14}/, '')
|
||||
hostnamectl set-hostname '#{basename}'
|
||||
|
||||
# Prepend ourselves to /etc/hosts
|
||||
grep -w '#{name}' /etc/hosts || {
|
||||
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
|
||||
}
|
||||
hostnamectl set-hostname '#{basename}'
|
||||
echo #{name} > /etc/HOSTNAME
|
||||
EOH
|
||||
network_with_hostname = machine.config.vm.networks.map {|t, c| c if c[:hostname] }.compact[0]
|
||||
if network_with_hostname
|
||||
replace_host(comm, name, basename, network_with_hostname[:ip])
|
||||
else
|
||||
add_hostname_to_loopback(comm, name, basename)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.add_hostname_to_loopback(comm, name, basename)
|
||||
# Add hostname to /etc/hosts if not already there
|
||||
comm.sudo <<-EOH.gsub(/^ {14}/, '')
|
||||
grep -w '#{name}' /etc/hosts || {
|
||||
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
|
||||
}
|
||||
EOH
|
||||
end
|
||||
|
||||
def self.replace_host(comm, name, basename, ip)
|
||||
# Remove any line in /etc/hosts that contains hostname,
|
||||
# then add hostname with associated ip
|
||||
comm.sudo <<-EOH.gsub(/^ {14}/, '')
|
||||
sed -i '/#{name}/d' /etc/hosts
|
||||
sed -i'' '1i '#{ip}'\\t#{name}\\t#{basename}' /etc/hosts
|
||||
EOH
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -9,6 +9,9 @@ describe "VagrantPlugins::GuestSUSE::Cap::ChangeHostName" do
|
||||
|
||||
let(:machine) { double("machine") }
|
||||
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||
let(:cap) { caps.get(:change_host_name) }
|
||||
let(:name) { "banana-rama.example.com" }
|
||||
let(:basename) { "banana-rama" }
|
||||
|
||||
before do
|
||||
allow(machine).to receive(:communicate).and_return(comm)
|
||||
@ -19,23 +22,63 @@ describe "VagrantPlugins::GuestSUSE::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" }
|
||||
let(:basename) { "banana-rama" }
|
||||
before do
|
||||
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
|
||||
end
|
||||
|
||||
it "sets the hostname" do
|
||||
comm.stub_command('test "$(hostnamectl --static status)" = "#{basename}"', exit_code: 1)
|
||||
it "sets the hostname" do
|
||||
comm.stub_command('test "$(hostnamectl --static status)" = "#{basename}"', exit_code: 1)
|
||||
|
||||
cap.change_host_name(machine, name)
|
||||
expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{basename}'/)
|
||||
cap.change_host_name(machine, name)
|
||||
expect(comm.received_commands[1]).to match(/echo #{name} > \/etc\/HOSTNAME/)
|
||||
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('test "$(hostnamectl --static status)" = "#{basename}"', exit_code: 0)
|
||||
|
||||
cap.change_host_name(machine, name)
|
||||
expect(comm.received_commands.size).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
it "does not change the hostname if already set" do
|
||||
comm.stub_command('test "$(hostnamectl --static status)" = "#{basename}"', exit_code: 0)
|
||||
context "multiple networks configured with hostname" do
|
||||
before do
|
||||
allow(comm).to receive(:test).with('test "$(hostnamectl --static status)" = "#{basename}"', sudo: false).and_return(false)
|
||||
end
|
||||
|
||||
cap.change_host_name(machine, name)
|
||||
expect(comm.received_commands.size).to eq(1)
|
||||
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(cap).to receive(:replace_host)
|
||||
expect(cap).to_not receive(:add_hostname_to_loopback)
|
||||
cap.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(cap).to_not receive(:replace_host)
|
||||
expect(cap).to receive(:add_hostname_to_loopback).once
|
||||
cap.change_host_name(machine, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user