diff --git a/lib/vagrant/action/builtin.rb b/lib/vagrant/action/builtin.rb index 132701787..47d87e661 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -21,6 +21,7 @@ module Vagrant use VM::NFS use VM::ClearSharedFolders use VM::ShareFolders + use VM::HostName use VM::Network use VM::Boot end) diff --git a/lib/vagrant/action/vm.rb b/lib/vagrant/action/vm.rb index 789da8f02..3aaee8cc9 100644 --- a/lib/vagrant/action/vm.rb +++ b/lib/vagrant/action/vm.rb @@ -15,6 +15,7 @@ module Vagrant autoload :Export, 'vagrant/action/vm/export' autoload :ForwardPorts, 'vagrant/action/vm/forward_ports' autoload :Halt, 'vagrant/action/vm/halt' + autoload :HostName, 'vagrant/action/vm/host_name' autoload :Import, 'vagrant/action/vm/import' autoload :MatchMACAddress, 'vagrant/action/vm/match_mac_address' autoload :Network, 'vagrant/action/vm/network' diff --git a/lib/vagrant/action/vm/host_name.rb b/lib/vagrant/action/vm/host_name.rb new file mode 100644 index 000000000..ca557a028 --- /dev/null +++ b/lib/vagrant/action/vm/host_name.rb @@ -0,0 +1,21 @@ +module Vagrant + class Action + module VM + class HostName + def initialize(app, env, options=nil) + @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") + env["vm"].system.change_host_name(host_name) + end + end + end + end + end +end \ No newline at end of file diff --git a/lib/vagrant/config/vm.rb b/lib/vagrant/config/vm.rb index 0d2036da2..4631144fb 100644 --- a/lib/vagrant/config/vm.rb +++ b/lib/vagrant/config/vm.rb @@ -14,6 +14,7 @@ module Vagrant attr_accessor :box_ovf attr_accessor :base_mac attr_accessor :boot_mode + attr_accessor :host_name attr_reader :forwarded_ports attr_reader :shared_folders attr_reader :network_options diff --git a/lib/vagrant/systems/base.rb b/lib/vagrant/systems/base.rb index 1ab221f53..6c8393254 100644 --- a/lib/vagrant/systems/base.rb +++ b/lib/vagrant/systems/base.rb @@ -78,6 +78,11 @@ module Vagrant # # @param [Hash] net_options The options for the network. def enable_host_only_network(net_options); end + + 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 598b79c01..b3ae81555 100644 --- a/lib/vagrant/systems/debian.rb +++ b/lib/vagrant/systems/debian.rb @@ -21,6 +21,15 @@ module Vagrant ssh.exec!("sudo /sbin/ifup eth#{net_options[:adapter]}") end end + + 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 + end + end + end end end diff --git a/lib/vagrant/systems/redhat.rb b/lib/vagrant/systems/redhat.rb index 977acce79..a86908561 100644 --- a/lib/vagrant/systems/redhat.rb +++ b/lib/vagrant/systems/redhat.rb @@ -23,6 +23,15 @@ module Vagrant ssh.exec!("sudo /sbin/ifup eth#{net_options[:adapter]}") end end + + 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/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network") unless host_name_already_set + ssh.exec!("sudo hostname #{name}") unless host_name_already_set + end + end + end end end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 548c5a10b..246a71dc2 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -320,6 +320,8 @@ en: Skipping port forwarding '%{name}'. halt: force: Forcing shutdown of VM... + host_name: + setting: "Setting host name..." import: importing: Importing base box '%{name}'... failure: |- @@ -504,7 +506,8 @@ en: Most of the time this is simply due to the fact that no one has contributed 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 linux: attempting_halt: "Attempting graceful shutdown of linux..." mount_fail: "Failed to mount shared folders. `vboxsf` was not available."