2013-04-04 06:53:17 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestUbuntu
|
|
|
|
module Cap
|
|
|
|
class ChangeHostName
|
|
|
|
def self.change_host_name(machine, name)
|
|
|
|
machine.communicate.tap do |comm|
|
2013-09-22 07:33:24 +00:00
|
|
|
|
|
|
|
# Get the current hostname
|
|
|
|
# if existing fqdn setup improperly, this returns just hostname
|
|
|
|
old = ''
|
|
|
|
comm.sudo "hostname -f" do |type, data|
|
|
|
|
if type == :stdout
|
|
|
|
old = data.chomp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# this works even if they're not both fqdn
|
|
|
|
if old.split('.')[0] != name.split('.')[0]
|
|
|
|
|
|
|
|
comm.sudo("sed -i 's/.*$/#{name.split('.')[0]}/' /etc/hostname")
|
|
|
|
|
|
|
|
# hosts should resemble:
|
2013-11-23 02:18:59 +00:00
|
|
|
# 127.0.0.1 localhost
|
2013-09-22 07:33:24 +00:00
|
|
|
# 127.0.1.1 host.fqdn.com host
|
2013-11-22 03:55:17 +00:00
|
|
|
if name.split('.').length > 1
|
|
|
|
# if there's an FQDN, put it in the right format
|
|
|
|
comm.sudo("sed -ri 's@^(([0-9]{1,3}\.){3}[0-9]{1,3})\\s+(#{old.split('.')[0]})\\b.*$@\\1\\t#{name} #{name.split('.')[0]}@g' /etc/hosts")
|
|
|
|
else
|
|
|
|
# if there's not an FQDN, don't print the hostname twice
|
|
|
|
comm.sudo("sed -ri 's@^(([0-9]{1,3}\.){3}[0-9]{1,3})\\s+(#{old.split('.')[0]})\\b.*$@\\1\\t#{name}@g' /etc/hosts")
|
|
|
|
end
|
2013-09-22 07:33:24 +00:00
|
|
|
|
2013-04-04 06:53:17 +00:00
|
|
|
if comm.test("[ `lsb_release -c -s` = hardy ]")
|
|
|
|
# hostname.sh returns 1, so I grep for the right name in /etc/hostname just to have a 0 exitcode
|
|
|
|
comm.sudo("/etc/init.d/hostname.sh start; grep '#{name}' /etc/hostname")
|
|
|
|
else
|
|
|
|
comm.sudo("service hostname start")
|
|
|
|
end
|
|
|
|
comm.sudo("hostname --fqdn > /etc/mailname")
|
2013-07-17 18:39:32 +00:00
|
|
|
comm.sudo("ifdown -a; ifup -a; ifup -a --allow=hotplug")
|
2013-04-04 06:53:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|