Merge pull request #11567 from soapy1/suse-hosts-mod

Set hostname according to hostname flag and ip for suse
This commit is contained in:
Sophia Castellarin 2020-06-15 14:29:43 -05:00 committed by GitHub
commit d9bbdab428
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 113 additions and 18 deletions

View File

@ -0,0 +1,47 @@
module Vagrant
module Util
# Helper methods for modfiying guests /etc/hosts file
module GuestHosts
# Linux specific inspection helpers
module Linux
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
#
# @param [Communicator]
# @param [String] full hostanme
# @param [int] (option) defines the upper bound for searching for an available loopback address
def add_hostname_to_loopback_interface(comm, name, loop_bound=DEAFAULT_LOOPBACK_CHECK_LIMIT)
basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, '')
grep -w '#{name}' /etc/hosts || {
for i in {1..#{loop_bound}}; do
grep -w "127.0.${i}.1" /etc/hosts || {
echo "127.0.${i}.1 #{name} #{basename}" >> /etc/hosts
break
}
done
}
EOH
end
# 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 '#{ip}'\\t#{name}\\t#{basename}' /etc/hosts
EOH
end
end
end
end
end

View File

@ -1,20 +1,25 @@
require 'vagrant/util/guest_hosts'
module VagrantPlugins
module GuestSUSE
module Cap
class ChangeHostName
extend Vagrant::Util::GuestHosts::Linux
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, network_with_hostname[:ip])
else
add_hostname_to_loopback_interface(comm, name)
end
end
end
end

View File

@ -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_interface)
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_interface).once
cap.change_host_name(machine, name)
end
end
end
end