diff --git a/plugins/guests/alt/cap/change_host_name.rb b/plugins/guests/alt/cap/change_host_name.rb index 950884370..4a815321b 100644 --- a/plugins/guests/alt/cap/change_host_name.rb +++ b/plugins/guests/alt/cap/change_host_name.rb @@ -1,66 +1,54 @@ -require 'vagrant/util/guest_hosts' +require_relative '../../linux/cap/change_host_name' module VagrantPlugins module GuestALT module Cap class ChangeHostName - extend Vagrant::Util::GuestHosts::Linux + extend VagrantPlugins::GuestLinux::Cap::ChangeHostName + + def self.change_name_command(name) + basename = name.split(".", 2)[0] + return <<-EOH.gsub(/^ {14}/, '') + # Save current hostname saved in /etc/hosts + CURRENT_HOSTNAME_FULL="$(hostname -f)" + CURRENT_HOSTNAME_SHORT="$(hostname -s)" - def self.change_host_name(machine, name) - comm = machine.communicate + # New hostname to be saved in /etc/hosts + NEW_HOSTNAME_FULL='#{name}' + NEW_HOSTNAME_SHORT="${NEW_HOSTNAME_FULL%%.*}" - 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]) + # Set the hostname - use hostnamectl if available + if command -v hostnamectl; then + hostnamectl set-hostname --static '#{name}' + hostnamectl set-hostname --transient '#{name}' else - add_hostname_to_loopback_interface(comm, name) - end + hostname '#{name}' + fi - if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) - basename = name.split('.', 2)[0] - comm.sudo <<-EOH.gsub(/^ {14}/, '') - # Save current hostname saved in /etc/hosts - CURRENT_HOSTNAME_FULL="$(hostname -f)" - CURRENT_HOSTNAME_SHORT="$(hostname -s)" + # Persist hostname change across reboots + if [ -f /etc/sysconfig/network ]; then + sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network + elif [ -f /etc/hostname ]; then + sed -i 's/.*/#{name}/' /etc/hostname + else + echo 'Unrecognized system. Hostname change may not persist across reboots.' + exit 0 + fi - # New hostname to be saved in /etc/hosts - NEW_HOSTNAME_FULL='#{name}' - NEW_HOSTNAME_SHORT="${NEW_HOSTNAME_FULL%%.*}" - - # Set the hostname - use hostnamectl if available - if command -v hostnamectl; then - hostnamectl set-hostname --static '#{name}' - hostnamectl set-hostname --transient '#{name}' - else - hostname '#{name}' - fi - - # Persist hostname change across reboots - if [ -f /etc/sysconfig/network ]; then - sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network - elif [ -f /etc/hostname ]; then - sed -i 's/.*/#{name}/' /etc/hostname - else - echo 'Unrecognized system. Hostname change may not persist across reboots.' - exit 0 - fi - - # Restart the network if we find a recognized SYS V init script - if command -v service; then - if [ -f /etc/init.d/network ]; then - service network restart - elif [ -f /etc/init.d/networking ]; then - service networking restart - elif [ -f /etc/init.d/NetworkManager ]; then - service NetworkManager restart - else - echo 'Unrecognized system. Networking was not restarted following hostname change.' - exit 0 - fi - fi - - EOH - end + # Restart the network if we find a recognized SYS V init script + if command -v service; then + if [ -f /etc/init.d/network ]; then + service network restart + elif [ -f /etc/init.d/networking ]; then + service networking restart + elif [ -f /etc/init.d/NetworkManager ]; then + service NetworkManager restart + else + echo 'Unrecognized system. Networking was not restarted following hostname change.' + exit 0 + fi + fi + EOH end end end diff --git a/plugins/guests/arch/cap/change_host_name.rb b/plugins/guests/arch/cap/change_host_name.rb index ed106b2a5..0c355d584 100644 --- a/plugins/guests/arch/cap/change_host_name.rb +++ b/plugins/guests/arch/cap/change_host_name.rb @@ -1,27 +1,17 @@ -require 'vagrant/util/guest_hosts' +require_relative '../../linux/cap/change_host_name' module VagrantPlugins module GuestArch module Cap class ChangeHostName - extend Vagrant::Util::GuestHosts::Linux + extend VagrantPlugins::GuestLinux::Cap::ChangeHostName - def self.change_host_name(machine, name) - comm = machine.communicate - - if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) - basename = name.split(".", 2)[0] - comm.sudo <<-EOH.gsub(/^ {14}/, "") - # Set hostname - hostnamectl set-hostname '#{basename}' - 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 + def self.change_name_command(name) + basename = name.split(".", 2)[0] + return <<-EOH.gsub(/^ {14}/, "") + # Set hostname + hostnamectl set-hostname '#{basename}' + EOH end end end diff --git a/plugins/guests/atomic/cap/change_host_name.rb b/plugins/guests/atomic/cap/change_host_name.rb index 15ca60188..64407a052 100644 --- a/plugins/guests/atomic/cap/change_host_name.rb +++ b/plugins/guests/atomic/cap/change_host_name.rb @@ -1,27 +1,17 @@ -require 'vagrant/util/guest_hosts' +require_relative '../../linux/cap/change_host_name' module VagrantPlugins module GuestAtomic module Cap class ChangeHostName - extend Vagrant::Util::GuestHosts::Linux + extend VagrantPlugins::GuestLinux::Cap::ChangeHostName - def self.change_host_name(machine, name) - comm = machine.communicate - - if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) - basename = name.split(".", 2)[0] - comm.sudo <<-EOH.gsub(/^ {14}/, "") - # Set hostname - hostnamectl set-hostname '#{basename}' - 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 + def self.change_name_command(name) + basename = name.split(".", 2)[0] + return <<-EOH.gsub(/^ {14}/, "") + # Set hostname + hostnamectl set-hostname '#{basename}' + EOH end end end diff --git a/plugins/guests/gentoo/cap/change_host_name.rb b/plugins/guests/gentoo/cap/change_host_name.rb index 0be1627c0..75202e29a 100644 --- a/plugins/guests/gentoo/cap/change_host_name.rb +++ b/plugins/guests/gentoo/cap/change_host_name.rb @@ -1,35 +1,22 @@ -require 'vagrant/util/guest_hosts' +require_relative '../../linux/cap/change_host_name' module VagrantPlugins module GuestGentoo module Cap class ChangeHostName - extend Vagrant::Util::GuestHosts::Linux + extend VagrantPlugins::GuestLinux::Cap::ChangeHostName - def self.change_host_name(machine, name) - comm = machine.communicate - - if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) - basename = name.split(".", 2)[0] - comm.sudo <<-EOH.gsub(/^ {14}/, "") - # Set the hostname - - # Use hostnamectl on systemd - if [[ `systemctl` =~ -\.mount ]]; then - systemctl set-hostname '#{name}' - else - hostname '#{basename}' - echo "hostname=#{basename}" > /etc/conf.d/hostname - fi - 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 + def self.change_name_command(name) + basename = name.split(".", 2)[0] + return <<-EOH.gsub(/^ {14}/, '') + # Use hostnamectl on systemd + if [[ `systemctl` =~ -\.mount ]]; then + systemctl set-hostname '#{name}' + else + hostname '#{basename}' + echo "hostname=#{basename}" > /etc/conf.d/hostname + fi + EOH end end end diff --git a/plugins/guests/linux/cap/change_host_name.rb b/plugins/guests/linux/cap/change_host_name.rb new file mode 100644 index 000000000..a8a154e43 --- /dev/null +++ b/plugins/guests/linux/cap/change_host_name.rb @@ -0,0 +1,38 @@ +require 'vagrant/util/guest_hosts' + +module VagrantPlugins + module GuestLinux + module Cap + module ChangeHostName + include Vagrant::Util::GuestHosts::Linux + + def change_name_command(name) + return <<-EOH.gsub(/^ {14}/, '') + # Set the hostname + echo '#{name}' > /etc/hostname + hostname '#{name}' + EOH + end + + def change_host_name?(comm, name) + !comm.test("hostname -f | grep '^#{name}$'", sudo: false) + end + + def change_host_name(machine, name) + comm = machine.communicate + + 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 + + if change_host_name?(comm, name) + comm.sudo(change_name_command(name)) + end + end + end + end + end +end diff --git a/plugins/guests/photon/cap/change_host_name.rb b/plugins/guests/photon/cap/change_host_name.rb index a2cf2adce..effaafb01 100644 --- a/plugins/guests/photon/cap/change_host_name.rb +++ b/plugins/guests/photon/cap/change_host_name.rb @@ -1,23 +1,17 @@ +require 'vagrant/util/guest_hosts' + module VagrantPlugins module GuestPhoton module Cap class ChangeHostName - def self.change_host_name(machine, name) - comm = machine.communicate - - if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) - basename = name.split(".", 2)[0] - comm.sudo <<-EOH.gsub(/^ {14}/, '') - # Set the hostname - echo '#{name}' > /etc/hostname - hostname '#{name}' - - # Prepend ourselves to /etc/hosts - grep -w '#{name}' /etc/hosts || { - sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts - } - EOH - end + extend Vagrant::Util::GuestHosts::Linux + + def self.change_name_command(name) + return <<-EOH.gsub(/^ {14}/, "") + # Set the hostname + echo '#{name}' > /etc/hostname + hostname '#{name}' + EOH end end end diff --git a/plugins/guests/pld/cap/change_host_name.rb b/plugins/guests/pld/cap/change_host_name.rb index 2aa74ca1c..33f50ca4f 100644 --- a/plugins/guests/pld/cap/change_host_name.rb +++ b/plugins/guests/pld/cap/change_host_name.rb @@ -1,33 +1,21 @@ -require 'vagrant/util/guest_hosts' +require_relative '../../linux/cap/change_host_name' module VagrantPlugins module GuestPld module Cap class ChangeHostName - extend Vagrant::Util::GuestHosts::Linux + extend VagrantPlugins::GuestLinux::Cap::ChangeHostName - def self.change_host_name(machine, name) - comm = machine.communicate + def self.change_name_command(name) + return <<-EOH.gsub(/^ {14}/, "") + hostname '#{name}' + sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network - 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 - - if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) - basename = name.split(".", 2)[0] - comm.sudo <<-EOH.gsub(/^ {14}/, '') - hostname '#{name}' - sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network + sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{name}\"/' /etc/sysconfig/interfaces/ifcfg-* - sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{name}\"/' /etc/sysconfig/interfaces/ifcfg-* - - # Restart networking - service network restart - EOH - end + # Restart networking + service network restart + EOH end end end diff --git a/plugins/guests/slackware/cap/change_host_name.rb b/plugins/guests/slackware/cap/change_host_name.rb index af7d3ef1a..ecd299b43 100644 --- a/plugins/guests/slackware/cap/change_host_name.rb +++ b/plugins/guests/slackware/cap/change_host_name.rb @@ -1,30 +1,19 @@ -require 'vagrant/util/guest_hosts' +require_relative '../../linux/cap/change_host_name' module VagrantPlugins module GuestSlackware module Cap class ChangeHostName - extend Vagrant::Util::GuestHosts::Linux + extend VagrantPlugins::GuestLinux::Cap::ChangeHostName - def self.change_host_name(machine, name) - comm = machine.communicate - - if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) - basename = name.split(".", 2)[0] - comm.sudo <<-EOH.gsub(/^ {14}/, '') - # Set the hostname - chmod o+w /etc/hostname - echo '#{name}' > /etc/hostname - chmod o-w /etc/hostname - hostname -F /etc/hostname - 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 + def self.change_name_command(name) + return <<-EOH.gsub(/^ {14}/, "") + # Set the hostname + chmod o+w /etc/hostname + echo '#{name}' > /etc/hostname + chmod o-w /etc/hostname + hostname -F /etc/hostname + EOH end end end diff --git a/plugins/guests/suse/cap/change_host_name.rb b/plugins/guests/suse/cap/change_host_name.rb index ab2369771..bd98e97d5 100644 --- a/plugins/guests/suse/cap/change_host_name.rb +++ b/plugins/guests/suse/cap/change_host_name.rb @@ -1,26 +1,22 @@ -require 'vagrant/util/guest_hosts' +require_relative '../../linux/cap/change_host_name' module VagrantPlugins module GuestSUSE module Cap class ChangeHostName - extend Vagrant::Util::GuestHosts::Linux + extend VagrantPlugins::GuestLinux::Cap::ChangeHostName - def self.change_host_name(machine, name) - comm = machine.communicate + def self.change_host_name?(comm, name) basename = name.split(".", 2)[0] - if !comm.test('test "$(hostnamectl --static status)" = "#{basename}"', sudo: false) - comm.sudo <<-EOH.gsub(/^ {14}/, '') - 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 + !comm.test('test "$(hostnamectl --static status)" = "#{basename}"', sudo: false) + end + + def self.change_name_command(name) + basename = name.split(".", 2)[0] + return <<-EOH.gsub(/^ {14}/, "") + hostnamectl set-hostname '#{basename}' + echo #{name} > /etc/HOSTNAME + EOH end end end diff --git a/test/unit/plugins/guests/arch/cap/change_host_name_test.rb b/test/unit/plugins/guests/arch/cap/change_host_name_test.rb index b644c8b2b..173bc7f63 100644 --- a/test/unit/plugins/guests/arch/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/arch/cap/change_host_name_test.rb @@ -35,7 +35,7 @@ describe "VagrantPlugins::GuestArch::Cap::ChangeHostName" do comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) described_class.change_host_name(machine, name) - expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{basename}'/) + expect(comm.received_commands[2]).to match(/hostnamectl set-hostname '#{basename}'/) end it "does not change the hostname if already set" do diff --git a/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb b/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb index ba15a08e6..ba80977a8 100644 --- a/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb @@ -35,7 +35,7 @@ describe "VagrantPlugins::GuestAtomic::Cap::ChangeHostName" do comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) described_class.change_host_name(machine, name) - expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{basename}'/) + expect(comm.received_commands[2]).to match(/hostnamectl set-hostname '#{basename}'/) end it "does not change the hostname if already set" do diff --git a/test/unit/plugins/guests/gentoo/cap/change_host_name_test.rb b/test/unit/plugins/guests/gentoo/cap/change_host_name_test.rb index 39c4234d4..3120fca2f 100644 --- a/test/unit/plugins/guests/gentoo/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/gentoo/cap/change_host_name_test.rb @@ -35,7 +35,7 @@ describe "VagrantPlugins::GuestGentoo::Cap::ChangeHostName" do comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) described_class.change_host_name(machine, name) - expect(comm.received_commands[1]).to match(/systemctl set-hostname '#{name}'/) + expect(comm.received_commands[2]).to match(/systemctl set-hostname '#{name}'/) end it "does not change the hostname if already set" do diff --git a/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb b/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb index 75f116b77..906dfdf02 100644 --- a/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb @@ -35,7 +35,7 @@ describe "VagrantPlugins::GuestSlackware::Cap::ChangeHostName" do comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) described_class.change_host_name(machine, name) - expect(comm.received_commands[1]).to match(/hostname -F \/etc\/hostname/) + expect(comm.received_commands[2]).to match(/hostname -F \/etc\/hostname/) end it "does not change the hostname if already set" do diff --git a/test/unit/plugins/guests/suse/cap/change_host_name_test.rb b/test/unit/plugins/guests/suse/cap/change_host_name_test.rb index 9f601cfc4..16c6041ea 100644 --- a/test/unit/plugins/guests/suse/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/suse/cap/change_host_name_test.rb @@ -35,15 +35,15 @@ describe "VagrantPlugins::GuestSUSE::Cap::ChangeHostName" 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(/echo #{name} > \/etc\/HOSTNAME/) - expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{basename}'/) + expect(comm.received_commands[2]).to match(/echo #{name} > \/etc\/HOSTNAME/) + expect(comm.received_commands[2]).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) + expect(comm.received_commands.size).to eq(2) end end