Ubuntu change_host_name cap

This commit is contained in:
Mitchell Hashimoto 2013-04-03 23:53:17 -07:00
parent 22f3ea6606
commit 0fbe9b0aca
3 changed files with 28 additions and 16 deletions

View File

@ -0,0 +1,23 @@
module VagrantPlugins
module GuestUbuntu
module Cap
class ChangeHostName
def self.change_host_name(machine, name)
machine.communicate.tap do |comm|
if !comm.test("sudo hostname | grep '^#{name}$'")
comm.sudo("sed -i 's/.*$/#{name}/' /etc/hostname")
comm.sudo("sed -i 's@^\\(127[.]0[.]1[.]1[[:space:]]\\+\\)@\\1#{name} #{name.split('.')[0]} @' /etc/hosts")
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")
end
end
end
end
end
end
end

View File

@ -27,22 +27,6 @@ module VagrantPlugins
vm.communicate.sudo("[ -x /sbin/initctl ] && /sbin/initctl emit vagrant-mounted MOUNTPOINT=#{real_guestpath}")
end
end
def change_host_name(name)
vm.communicate.tap do |comm|
if !comm.test("sudo hostname | grep '^#{name}$'")
comm.sudo("sed -i 's/.*$/#{name}/' /etc/hostname")
comm.sudo("sed -i 's@^\\(127[.]0[.]1[.]1[[:space:]]\\+\\)@\\1#{name} #{name.split('.')[0]} @' /etc/hosts")
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")
end
end
end
end
end
end

View File

@ -10,6 +10,11 @@ module VagrantPlugins
require File.expand_path("../guest", __FILE__)
Guest
end
guest_capability("ubuntu", "change_host_name") do
require_relative "cap/change_host_name"
Cap::ChangeHostName
end
end
end
end