Update redhat change host name capability to support systemd

Update capability to use guest inspection module for determining
correct actions to execute. When systemd is in use restart the
correct active service, either NetworkManager or networkd. Default
to using the original service restart when systemd service is not
found.
This commit is contained in:
Chris Roberts 2018-09-20 16:44:08 -07:00
parent fb5fc0e657
commit ff021fcab4
1 changed files with 21 additions and 20 deletions

View File

@ -2,6 +2,9 @@ module VagrantPlugins
module GuestRedHat module GuestRedHat
module Cap module Cap
class ChangeHostName class ChangeHostName
extend Vagrant::Util::GuestInspection
def self.change_host_name(machine, name) def self.change_host_name(machine, name)
comm = machine.communicate comm = machine.communicate
@ -10,34 +13,32 @@ module VagrantPlugins
comm.sudo <<-EOH.gsub(/^ {14}/, '') comm.sudo <<-EOH.gsub(/^ {14}/, '')
# Update sysconfig # Update sysconfig
sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network
# Update DNS # Update DNS
sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{basename}\"/' /etc/sysconfig/network-scripts/ifcfg-* sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{basename}\"/' /etc/sysconfig/network-scripts/ifcfg-*
# Set the hostname - use hostnamectl if available # Set the hostname - use hostnamectl if available
echo '#{name}' > /etc/hostname echo '#{name}' > /etc/hostname
if command -v hostnamectl; then
hostnamectl set-hostname --static '#{name}'
hostnamectl set-hostname --transient '#{name}'
else
hostname -F /etc/hostname
fi
# Prepend ourselves to /etc/hosts
grep -w '#{name}' /etc/hosts || { grep -w '#{name}' /etc/hosts || {
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
} }
# Restart network
if (test -f /usr/bin/systemctl && systemctl -q is-active NetworkManager.service); then
systemctl restart NetworkManager.service
elif test -f /etc/init.d/network; then
service network restart
else
printf "Could not restart the network to set the new hostname!\n"
exit 1
fi
EOH EOH
if hostnamectl?(comm)
comm.sudo("hostnamectl set-hostname --static '#{name}' ; " \
"hostnamectl set-hostname --transient '#{name}'")
else
comm.sudo("hostname -F /etc/hostname")
end
restart_command = "service network restart"
if systemd?
if systemd_networkd?(comm)
restart_command = "systemctl restart systemd-networkd.service"
elsif systemd_controlled?(comm, "NetworkManager.service")
restart_command = "systemctl restart NetworkManager.service"
end
end
comm.sudo(restart_command)
end end
end end
end end