2013-04-04 18:49:26 +00:00
|
|
|
module VagrantPlugins
|
2013-05-07 09:38:46 +00:00
|
|
|
module GuestGentoo
|
2013-04-04 18:49:26 +00:00
|
|
|
module Cap
|
|
|
|
class ChangeHostName
|
|
|
|
def self.change_host_name(machine, name)
|
2016-06-23 01:31:17 +00:00
|
|
|
comm = machine.communicate
|
|
|
|
|
2016-06-23 02:29:46 +00:00
|
|
|
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
|
2016-06-23 01:31:17 +00:00
|
|
|
basename = name.split(".", 2)[0]
|
|
|
|
comm.sudo <<-EOH.gsub(/^ {14}/, "")
|
|
|
|
# Set the hostname
|
2017-03-24 11:55:44 +00:00
|
|
|
|
|
|
|
# Use hostnamectl on systemd
|
|
|
|
if [[ `systemctl` =~ -\.mount ]]; then
|
|
|
|
systemctl set-hostname '#{name}'
|
|
|
|
else
|
|
|
|
hostname '#{basename}'
|
|
|
|
echo "hostname=#{basename}" > /etc/conf.d/hostname
|
|
|
|
fi
|
2016-06-23 01:31:17 +00:00
|
|
|
|
|
|
|
# Prepend ourselves to /etc/hosts
|
|
|
|
grep -w '#{name}' /etc/hosts || {
|
2016-10-24 17:06:59 +00:00
|
|
|
echo -e '127.0.0.1\\t#{name}\\t#{basename}' | cat - /etc/hosts > /tmp/tmp-hosts &&
|
|
|
|
mv /tmp/tmp-hosts /etc/hosts
|
2016-06-23 01:31:17 +00:00
|
|
|
}
|
|
|
|
EOH
|
2013-04-04 18:49:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|