vagrant/plugins/guests/redhat/cap/change_host_name.rb

48 lines
1.6 KiB
Ruby
Raw Normal View History

2013-04-04 18:49:26 +00:00
module VagrantPlugins
2013-04-04 19:09:40 +00:00
module GuestRedHat
2013-04-04 18:49:26 +00:00
module Cap
class ChangeHostName
2018-09-28 14:59:39 +00:00
extend Vagrant::Util::GuestInspection::Linux
2013-04-04 18:49:26 +00:00
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}/, '')
# Update sysconfig
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)
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"
2018-10-01 15:43:49 +00:00
if systemd?(comm)
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)
2013-04-04 18:49:26 +00:00
end
end
end
end
end
end