vagrant/plugins/guests/debian/cap/change_host_name.rb

39 lines
1.2 KiB
Ruby
Raw Normal View History

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|
# 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")
# hosts should resemble:
# 127.0.1.1 host.fqdn.com host
# First to set fqdn
comm.sudo("sed -i 's@#{old}@#{name}@' /etc/hosts")
# Second to set hostname
comm.sudo("sed -i 's@#{old.split('.')[0]}@#{name.split('.')[0]}@' /etc/hosts")
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