Update RHEL cap for setting hostname
This commit is contained in:
parent
c2b9497923
commit
91ac0681cf
@ -1,9 +1,12 @@
|
||||
require 'vagrant/util/guest_hosts'
|
||||
|
||||
module VagrantPlugins
|
||||
module GuestRedHat
|
||||
module Cap
|
||||
class ChangeHostName
|
||||
|
||||
extend Vagrant::Util::GuestInspection::Linux
|
||||
extend Vagrant::Util::GuestHosts::Linux
|
||||
|
||||
def self.change_host_name(machine, name)
|
||||
comm = machine.communicate
|
||||
@ -15,11 +18,6 @@ module VagrantPlugins
|
||||
sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network
|
||||
# Update DNS
|
||||
sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{basename}\"/' /etc/sysconfig/network-scripts/ifcfg-*
|
||||
# Set the hostname - use hostnamectl if available
|
||||
echo '#{name}' > /etc/hostname
|
||||
grep -w '#{name}' /etc/hosts || {
|
||||
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
|
||||
}
|
||||
EOH
|
||||
|
||||
if hostnamectl?(comm)
|
||||
@ -40,6 +38,13 @@ module VagrantPlugins
|
||||
end
|
||||
comm.sudo(restart_command)
|
||||
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
|
||||
|
||||
@ -26,6 +26,9 @@ describe "VagrantPlugins::GuestRedHat::Cap::ChangeHostName" do
|
||||
let(:hostnamectl) { true }
|
||||
let(:networkd) { true }
|
||||
let(:network_manager) { false }
|
||||
let(:networks) { [
|
||||
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}]
|
||||
] }
|
||||
|
||||
before do
|
||||
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: hostname_changed ? 1 : 0)
|
||||
@ -33,12 +36,47 @@ describe "VagrantPlugins::GuestRedHat::Cap::ChangeHostName" do
|
||||
allow(cap).to receive(:hostnamectl?).and_return(hostnamectl)
|
||||
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)
|
||||
end
|
||||
|
||||
it "sets the hostname" do
|
||||
cap.change_host_name(machine, name)
|
||||
expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network/)
|
||||
expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network-scripts\/ifcfg/)
|
||||
context "minimal network config" do
|
||||
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(/\/etc\/sysconfig\/network/)
|
||||
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).to_not receive(:sudo).with(/\/etc\/sysconfig\/network/)
|
||||
end
|
||||
end
|
||||
|
||||
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(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
|
||||
|
||||
context "when hostnamectl is in use" do
|
||||
@ -64,7 +102,7 @@ describe "VagrantPlugins::GuestRedHat::Cap::ChangeHostName" do
|
||||
|
||||
it "does not change the hostname" do
|
||||
cap.change_host_name(machine, name)
|
||||
expect(comm.received_commands.size).to eq(1)
|
||||
expect(comm.received_commands.size).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user