2013-04-04 06:18:12 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestDebian
|
|
|
|
module Cap
|
|
|
|
class ChangeHostName
|
|
|
|
def self.change_host_name(machine, name)
|
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}/, '')
|
2016-06-25 19:51:58 +00:00
|
|
|
# Ensure exit on command error
|
2016-06-25 01:18:04 +00:00
|
|
|
set -e
|
|
|
|
|
2016-06-05 01:31:41 +00:00
|
|
|
# Set the hostname
|
2016-06-23 01:32:33 +00:00
|
|
|
echo '#{basename}' > /etc/hostname
|
2016-06-05 01:31:41 +00:00
|
|
|
hostname -F /etc/hostname
|
|
|
|
|
2016-06-25 19:51:58 +00:00
|
|
|
if command -v hostnamectl; then
|
|
|
|
hostnamectl set-hostname '#{basename}'
|
|
|
|
fi
|
|
|
|
|
2016-06-05 01:31:41 +00:00
|
|
|
# Remove comments and blank lines from /etc/hosts
|
|
|
|
sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts
|
|
|
|
|
|
|
|
# Prepend ourselves to /etc/hosts
|
|
|
|
grep -w '#{name}' /etc/hosts || {
|
|
|
|
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
|
|
|
|
}
|
|
|
|
|
|
|
|
# Update mailname
|
|
|
|
echo '#{name}' > /etc/mailname
|
|
|
|
|
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
|
|
|
|
EOH
|
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
|