SetHostname uses new cap system

This commit is contained in:
Mitchell Hashimoto 2013-04-03 23:18:12 -07:00
parent 511522e9ae
commit 61d2f9f96f
5 changed files with 28 additions and 24 deletions

View File

@ -18,7 +18,7 @@ module Vagrant
hostname = env[:machine].config.vm.hostname
if !hostname.nil?
env[:ui].info I18n.t("vagrant.actions.vm.hostname.setting")
env[:machine].guest.change_host_name(hostname)
env[:machine].guest.capability(:change_host_name, hostname)
end
end
end

View File

@ -0,0 +1,18 @@
module VagrantPlugins
module GuestDebian
module Cap
class ChangeHostName
def self.change_host_name(machine, name)
machine.communicate.tap do |comm|
if !comm.test("hostname --fqdn | grep '^#{name}$' || hostname --short | grep '^#{name}$'")
comm.sudo("sed -r -i 's/^(127[.]0[.]1[.]1[[:space:]]+).*$/\\1#{name} #{name.split('.')[0]}/' /etc/hosts")
comm.sudo("sed -i 's/.*$/#{name.split('.')[0]}/' /etc/hostname")
comm.sudo("hostname -F /etc/hostname")
comm.sudo("hostname --fqdn > /etc/mailname")
end
end
end
end
end
end
end

View File

@ -1,3 +1,6 @@
require 'set'
require 'tempfile'
require "vagrant/util/template_renderer"
module VagrantPlugins

View File

@ -1,31 +1,9 @@
require 'set'
require 'tempfile'
require "vagrant"
require 'vagrant/util/template_renderer'
require Vagrant.source_root.join("plugins/guests/linux/guest")
module VagrantPlugins
module GuestDebian
class Guest < VagrantPlugins::GuestLinux::Guest
# Make the TemplateRenderer top-level
include Vagrant::Util
class Guest < Vagrant.plugin("2", :guest)
def detect?(machine)
machine.communicate.test("cat /proc/version | grep 'Debian'")
end
def change_host_name(name)
vm.communicate.tap do |comm|
if !comm.test("hostname --fqdn | grep '^#{name}$' || hostname --short | grep '^#{name}$'")
comm.sudo("sed -r -i 's/^(127[.]0[.]1[.]1[[:space:]]+).*$/\\1#{name} #{name.split('.')[0]}/' /etc/hosts")
comm.sudo("sed -i 's/.*$/#{name.split('.')[0]}/' /etc/hostname")
comm.sudo("hostname -F /etc/hostname")
comm.sudo("hostname --fqdn > /etc/mailname")
end
end
end
end
end
end

View File

@ -15,6 +15,11 @@ module VagrantPlugins
require_relative "cap/configure_networks"
Cap::ConfigureNetworks
end
guest_capability("debian", "change_host_name") do
require_relative "cap/change_host_name"
Cap::ChangeHostName
end
end
end
end