diff --git a/CHANGELOG.md b/CHANGELOG.md index 39c15988a..7f644fec8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - New `shell` provisioner which simply uploads and executes a script as root on the VM. - Gentoo host only networking no longer fails if alrady setup. [GH-286] + - Set the host name of your guest OS with `config.vm.host_name` [GH-273] ## 0.7.0 (January 19, 2011) diff --git a/lib/vagrant/action/vm/host_name.rb b/lib/vagrant/action/vm/host_name.rb index ca557a028..94cfb1e6b 100644 --- a/lib/vagrant/action/vm/host_name.rb +++ b/lib/vagrant/action/vm/host_name.rb @@ -2,13 +2,13 @@ module Vagrant class Action module VM class HostName - def initialize(app, env, options=nil) + def initialize(app, env) @app = app - env.merge!(options || {}) end def call(env) @app.call(env) + host_name = env["config"].vm.host_name if !host_name.nil? env.ui.info I18n.t("vagrant.actions.vm.host_name.setting") @@ -18,4 +18,4 @@ module Vagrant end end end -end \ No newline at end of file +end diff --git a/lib/vagrant/systems/base.rb b/lib/vagrant/systems/base.rb index 6c8393254..34b38500d 100644 --- a/lib/vagrant/systems/base.rb +++ b/lib/vagrant/systems/base.rb @@ -82,7 +82,6 @@ module Vagrant def change_host_name(name) raise BaseError, :_key => :unsupported_host_name end - end end end diff --git a/lib/vagrant/systems/debian.rb b/lib/vagrant/systems/debian.rb index b3ae81555..674ec1ff8 100644 --- a/lib/vagrant/systems/debian.rb +++ b/lib/vagrant/systems/debian.rb @@ -24,12 +24,12 @@ module Vagrant def change_host_name(name) vm.ssh.execute do |ssh| - host_name_already_set = ssh.test?("sudo hostname | grep '#{name}'") - ssh.exec!("sudo sed -i 's/.*$/#{name}/' /etc/hostname") unless host_name_already_set - ssh.exec!("sudo service hostname start") unless host_name_already_set + if !ssh.test?("sudo hostname | grep '#{name}'") + ssh.exec!("sudo sed -i 's/.*$/#{name}/' /etc/hostname") + ssh.exec!("sudo service hostname start") + end end end - end end end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 246a71dc2..935a3375e 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -507,7 +507,9 @@ en: back the SSH commands necessary to set this up. Please report a bug and this will be fixed for your distro. unsupported_host_name: |- - Setting host name is currently only supported on Debian, Ubuntu and RedHat + Setting host name is currently only supported on Debian, Ubuntu and RedHat. + If you'd like your guest OS to be supported, please open a ticket on the + project. linux: attempting_halt: "Attempting graceful shutdown of linux..." mount_fail: "Failed to mount shared folders. `vboxsf` was not available." diff --git a/test/vagrant/action/vm/host_name_test.rb b/test/vagrant/action/vm/host_name_test.rb new file mode 100644 index 000000000..bb6a74e58 --- /dev/null +++ b/test/vagrant/action/vm/host_name_test.rb @@ -0,0 +1,36 @@ +require "test_helper" + +class HostNameVMActionTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Action::VM::HostName + @app, @env = action_env + @instance = @klass.new(@app, @env) + + @vm = mock("vm") + @env["vm"] = @vm + + @internal_vm = mock("internal") + @vm.stubs(:vm).returns(@internal_vm) + end + + should "not run anything if no host name is set" do + @env["config"].vm.host_name = nil + @env["vm"].expects(:system).never + @app.expects(:call).with(@env).once + + @instance.call(@env) + end + + should "change host name if set" do + @env["config"].vm.host_name = "foo" + + system = mock("system") + @vm.stubs(:system).returns(system) + + seq = sequence("host_seq") + @app.expects(:call).with(@env).in_sequence(seq) + system.expects(:change_host_name).with(@env["config"].vm.host_name).in_sequence(seq) + + @instance.call(@env) + end +end