2013-07-11 01:21:06 +00:00
|
|
|
module VagrantPlugins
|
2013-08-01 01:07:04 +00:00
|
|
|
module GuestDarwin
|
2013-07-11 01:21:06 +00:00
|
|
|
module Cap
|
|
|
|
class ChangeHostName
|
|
|
|
def self.change_host_name(machine, name)
|
2016-05-31 05:10:48 +00:00
|
|
|
comm = machine.communicate
|
|
|
|
|
2016-06-23 02:29:46 +00:00
|
|
|
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
|
2016-05-31 05:10:48 +00:00
|
|
|
basename = name.split(".", 2)[0]
|
|
|
|
|
2016-10-24 17:06:59 +00:00
|
|
|
# LocalHostName should not contain dots - it is used by Bonjour and
|
|
|
|
# visible through file sharing services.
|
|
|
|
comm.sudo <<-EOH.gsub(/^ */, '')
|
2016-06-25 19:22:58 +00:00
|
|
|
# Set hostname
|
2016-10-24 17:06:59 +00:00
|
|
|
scutil --set ComputerName '#{name}' &&
|
|
|
|
scutil --set HostName '#{name}' &&
|
|
|
|
scutil --set LocalHostName '#{basename}'
|
|
|
|
result=$?
|
|
|
|
if [ $result -ne 0 ]; then
|
|
|
|
exit $result
|
|
|
|
fi
|
2016-05-31 05:10:48 +00:00
|
|
|
|
|
|
|
hostname '#{name}'
|
|
|
|
|
|
|
|
# Remove comments and blank lines from /etc/hosts
|
|
|
|
sed -i'' -e 's/#.*$//' /etc/hosts
|
|
|
|
sed -i'' -e '/^$/d' /etc/hosts
|
|
|
|
|
|
|
|
# Prepend ourselves to /etc/hosts - sed on bsd is sad
|
|
|
|
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-05-31 05:10:48 +00:00
|
|
|
}
|
|
|
|
EOH
|
2013-07-11 01:21:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|