Extract change hostnamet base class

This commit is contained in:
sophia 2020-07-20 11:54:57 -05:00
parent 91ac0681cf
commit 5afd7fdd73
14 changed files with 157 additions and 197 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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