2013-04-04 06:18:12 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestDebian
|
|
|
|
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]
|
|
|
|
|
2013-04-04 06:18:12 +00:00
|
|
|
comm.sudo("sed -i 's/.*$/#{name.split('.')[0]}/' /etc/hostname")
|
2013-09-22 07:33:24 +00:00
|
|
|
|
|
|
|
# hosts should resemble:
|
2013-10-15 18:57:40 +00:00
|
|
|
# 127.0.0.1 localhost host.fqdn.com host
|
2013-09-22 07:33:24 +00:00
|
|
|
# 127.0.1.1 host.fqdn.com host
|
|
|
|
# First to set fqdn
|
2013-10-15 18:57:40 +00:00
|
|
|
comm.sudo("sed -ri 's@^(([0-9]{1,3}\.){3}[0-9]{1,3})\\s+(localhost)\\b.*$@\\1\\t#{name} #{name.split('.')[0]} \\3@g' /etc/hosts")
|
|
|
|
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")
|
2013-09-22 07:33:24 +00:00
|
|
|
|
2013-04-04 06:18:12 +00:00
|
|
|
comm.sudo("hostname -F /etc/hostname")
|
|
|
|
comm.sudo("hostname --fqdn > /etc/mailname")
|
2013-08-05 16:02:12 +00:00
|
|
|
comm.sudo("ifdown -a; ifup -a; ifup eth0")
|
2013-04-04 06:18:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|