From 0fbe9b0acaae905b36f0ee54b659bb6252f0372c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 3 Apr 2013 23:53:17 -0700 Subject: [PATCH] Ubuntu change_host_name cap --- plugins/guests/ubuntu/cap/change_host_name.rb | 23 +++++++++++++++++++ plugins/guests/ubuntu/guest.rb | 16 ------------- plugins/guests/ubuntu/plugin.rb | 5 ++++ 3 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 plugins/guests/ubuntu/cap/change_host_name.rb diff --git a/plugins/guests/ubuntu/cap/change_host_name.rb b/plugins/guests/ubuntu/cap/change_host_name.rb new file mode 100644 index 000000000..db052e40f --- /dev/null +++ b/plugins/guests/ubuntu/cap/change_host_name.rb @@ -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 diff --git a/plugins/guests/ubuntu/guest.rb b/plugins/guests/ubuntu/guest.rb index 707f2e8fa..35e3d30c9 100644 --- a/plugins/guests/ubuntu/guest.rb +++ b/plugins/guests/ubuntu/guest.rb @@ -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 diff --git a/plugins/guests/ubuntu/plugin.rb b/plugins/guests/ubuntu/plugin.rb index f8e5c0708..38e054972 100644 --- a/plugins/guests/ubuntu/plugin.rb +++ b/plugins/guests/ubuntu/plugin.rb @@ -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