From 61d2f9f96fc0f0ef5869c732674f25c4ccc85c8c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 3 Apr 2013 23:18:12 -0700 Subject: [PATCH] SetHostname uses new cap system --- lib/vagrant/action/builtin/set_hostname.rb | 2 +- plugins/guests/debian/cap/change_host_name.rb | 18 ++++++++++++++ .../guests/debian/cap/configure_networks.rb | 3 +++ plugins/guests/debian/guest.rb | 24 +------------------ plugins/guests/debian/plugin.rb | 5 ++++ 5 files changed, 28 insertions(+), 24 deletions(-) create mode 100644 plugins/guests/debian/cap/change_host_name.rb diff --git a/lib/vagrant/action/builtin/set_hostname.rb b/lib/vagrant/action/builtin/set_hostname.rb index 39f06bc4c..45458db0b 100644 --- a/lib/vagrant/action/builtin/set_hostname.rb +++ b/lib/vagrant/action/builtin/set_hostname.rb @@ -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 diff --git a/plugins/guests/debian/cap/change_host_name.rb b/plugins/guests/debian/cap/change_host_name.rb new file mode 100644 index 000000000..6ce256d7e --- /dev/null +++ b/plugins/guests/debian/cap/change_host_name.rb @@ -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 diff --git a/plugins/guests/debian/cap/configure_networks.rb b/plugins/guests/debian/cap/configure_networks.rb index dc949df5e..16e50158e 100644 --- a/plugins/guests/debian/cap/configure_networks.rb +++ b/plugins/guests/debian/cap/configure_networks.rb @@ -1,3 +1,6 @@ +require 'set' +require 'tempfile' + require "vagrant/util/template_renderer" module VagrantPlugins diff --git a/plugins/guests/debian/guest.rb b/plugins/guests/debian/guest.rb index 4dfb38a38..ec6b6a9a1 100644 --- a/plugins/guests/debian/guest.rb +++ b/plugins/guests/debian/guest.rb @@ -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 diff --git a/plugins/guests/debian/plugin.rb b/plugins/guests/debian/plugin.rb index f0c9ccceb..45fc1544a 100644 --- a/plugins/guests/debian/plugin.rb +++ b/plugins/guests/debian/plugin.rb @@ -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