2018-10-23 17:17:39 +00:00
|
|
|
require "log4r"
|
2018-10-24 18:29:59 +00:00
|
|
|
require_relative "../../linux/cap/network_interfaces"
|
2018-10-23 17:17:39 +00:00
|
|
|
|
2013-04-04 06:18:12 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestDebian
|
|
|
|
module Cap
|
|
|
|
class ChangeHostName
|
2018-10-16 18:54:58 +00:00
|
|
|
|
|
|
|
extend Vagrant::Util::GuestInspection::Linux
|
|
|
|
|
2013-04-04 06:18:12 +00:00
|
|
|
def self.change_host_name(machine, name)
|
2018-10-23 17:17:39 +00:00
|
|
|
@logger = Log4r::Logger.new("vagrant::guest::debian::changehostname")
|
2016-06-05 01:31:41 +00:00
|
|
|
comm = machine.communicate
|
|
|
|
|
2016-06-23 02:29:46 +00:00
|
|
|
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
|
2016-06-05 01:31:41 +00:00
|
|
|
basename = name.split(".", 2)[0]
|
|
|
|
comm.sudo <<-EOH.gsub(/^ {14}/, '')
|
|
|
|
# Set the hostname
|
2016-06-23 01:32:33 +00:00
|
|
|
echo '#{basename}' > /etc/hostname
|
2016-06-25 19:51:58 +00:00
|
|
|
|
2016-06-05 01:31:41 +00:00
|
|
|
# Prepend ourselves to /etc/hosts
|
|
|
|
grep -w '#{name}' /etc/hosts || {
|
2018-01-26 11:02:17 +00:00
|
|
|
if grep -w '^127\\.0\\.1\\.1' /etc/hosts ; then
|
|
|
|
sed -i'' 's/^127\\.0\\.1\\.1\\s.*$/127.0.1.1\\t#{name}\\t#{basename}/' /etc/hosts
|
|
|
|
else
|
|
|
|
sed -i'' '1i 127.0.1.1\\t#{name}\\t#{basename}' /etc/hosts
|
|
|
|
fi
|
2016-06-05 01:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Update mailname
|
|
|
|
echo '#{name}' > /etc/mailname
|
|
|
|
|
2018-10-16 18:54:58 +00:00
|
|
|
EOH
|
|
|
|
|
|
|
|
if hostnamectl?(comm)
|
|
|
|
comm.sudo("hostnamectl set-hostname '#{basename}'")
|
|
|
|
else
|
|
|
|
comm.sudo <<-EOH.gsub(/^ {14}/, '')
|
|
|
|
hostname -F /etc/hostname
|
2016-06-25 19:51:58 +00:00
|
|
|
# Restart hostname services
|
|
|
|
if test -f /etc/init.d/hostname; then
|
|
|
|
/etc/init.d/hostname start || true
|
2016-06-05 01:31:41 +00:00
|
|
|
fi
|
|
|
|
|
2016-06-25 19:51:58 +00:00
|
|
|
if test -f /etc/init.d/hostname.sh; then
|
|
|
|
/etc/init.d/hostname.sh start || true
|
2016-06-05 01:31:41 +00:00
|
|
|
fi
|
2018-10-16 18:54:58 +00:00
|
|
|
EOH
|
|
|
|
end
|
|
|
|
|
2018-10-24 18:29:59 +00:00
|
|
|
restart_command = nil
|
2018-10-16 18:54:58 +00:00
|
|
|
if systemd?(comm)
|
|
|
|
if systemd_networkd?(comm)
|
2018-10-23 17:17:39 +00:00
|
|
|
@logger.debug("Attempting to restart networking with systemd-networkd")
|
2018-10-16 18:54:58 +00:00
|
|
|
restart_command = "systemctl restart systemd-networkd.service"
|
|
|
|
elsif systemd_controlled?(comm, "NetworkManager.service")
|
2018-10-23 17:17:39 +00:00
|
|
|
@logger.debug("Attempting to restart networking with NetworkManager")
|
2018-10-16 18:54:58 +00:00
|
|
|
restart_command = "systemctl restart NetworkManager.service"
|
|
|
|
end
|
2018-10-24 18:29:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if restart_command
|
|
|
|
comm.sudo(restart_command)
|
2018-10-23 17:17:39 +00:00
|
|
|
else
|
2018-10-24 18:29:59 +00:00
|
|
|
restart_each_interface(machine, @logger)
|
2018-10-23 17:17:39 +00:00
|
|
|
end
|
2018-10-24 18:29:59 +00:00
|
|
|
end
|
|
|
|
end
|
2018-10-23 17:17:39 +00:00
|
|
|
|
2018-10-24 18:29:59 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
# Due to how most Debian systems and older Ubuntu systems handle restarting
|
|
|
|
# networking, we cannot simply run the networking init script or use the ifup/down
|
|
|
|
# tools to restart all interfaces to renew the machines DHCP lease when setting
|
|
|
|
# its hostname. This method is a workaround for those older systems that
|
|
|
|
# cannoy reliably restart networking. It restarts each individual interface
|
|
|
|
# on its own instead.
|
|
|
|
#
|
|
|
|
# @param [Vagrant::Machine] machine
|
|
|
|
# @param [Log4r::Logger] logger
|
|
|
|
def self.restart_each_interface(machine, logger)
|
|
|
|
comm = machine.communicate
|
|
|
|
interfaces = VagrantPlugins::GuestLinux::Cap::NetworkInterfaces.network_interfaces(machine)
|
|
|
|
nettools = true
|
|
|
|
if systemd?(comm)
|
|
|
|
@logger.debug("Attempting to restart networking with systemctl")
|
|
|
|
nettools = false
|
|
|
|
else
|
|
|
|
@logger.debug("Attempting to restart networking with ifup/down nettools")
|
|
|
|
end
|
|
|
|
|
|
|
|
interfaces.each do |iface|
|
|
|
|
logger.debug("Restarting interface #{iface} on guest #{machine.name}")
|
|
|
|
if nettools
|
2018-10-24 22:27:33 +00:00
|
|
|
restart_command = "ifdown #{iface};ifup #{iface}"
|
2018-10-24 18:29:59 +00:00
|
|
|
else
|
2018-10-24 22:27:33 +00:00
|
|
|
restart_command = "systemctl stop ifup@#{iface}.service;systemctl start ifup@#{iface}.service"
|
2018-10-16 18:54:58 +00:00
|
|
|
end
|
2018-10-24 18:29:59 +00:00
|
|
|
comm.sudo(restart_command)
|
2013-04-04 06:18:12 +00:00
|
|
|
end
|
2014-05-05 01:32:33 +00:00
|
|
|
end
|
2013-04-04 06:18:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|