diff --git a/plugins/guests/redhat/cap/change_host_name.rb b/plugins/guests/redhat/cap/change_host_name.rb index 5ceb63665..9066b1d04 100644 --- a/plugins/guests/redhat/cap/change_host_name.rb +++ b/plugins/guests/redhat/cap/change_host_name.rb @@ -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 diff --git a/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb b/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb index 8d0c9ebd4..ccaf71b30 100644 --- a/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb @@ -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