added support for changing host name on up for Debian, Ubuntu and RedHat

This commit is contained in:
Michael Bearne 2011-01-14 17:51:19 +00:00
parent 9673b7eaee
commit f757184c06
8 changed files with 51 additions and 1 deletions

View File

@ -21,6 +21,7 @@ module Vagrant
use VM::NFS use VM::NFS
use VM::ClearSharedFolders use VM::ClearSharedFolders
use VM::ShareFolders use VM::ShareFolders
use VM::HostName
use VM::Network use VM::Network
use VM::Boot use VM::Boot
end) end)

View File

@ -15,6 +15,7 @@ module Vagrant
autoload :Export, 'vagrant/action/vm/export' autoload :Export, 'vagrant/action/vm/export'
autoload :ForwardPorts, 'vagrant/action/vm/forward_ports' autoload :ForwardPorts, 'vagrant/action/vm/forward_ports'
autoload :Halt, 'vagrant/action/vm/halt' autoload :Halt, 'vagrant/action/vm/halt'
autoload :HostName, 'vagrant/action/vm/host_name'
autoload :Import, 'vagrant/action/vm/import' autoload :Import, 'vagrant/action/vm/import'
autoload :MatchMACAddress, 'vagrant/action/vm/match_mac_address' autoload :MatchMACAddress, 'vagrant/action/vm/match_mac_address'
autoload :Network, 'vagrant/action/vm/network' autoload :Network, 'vagrant/action/vm/network'

View File

@ -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

View File

@ -14,6 +14,7 @@ module Vagrant
attr_accessor :box_ovf attr_accessor :box_ovf
attr_accessor :base_mac attr_accessor :base_mac
attr_accessor :boot_mode attr_accessor :boot_mode
attr_accessor :host_name
attr_reader :forwarded_ports attr_reader :forwarded_ports
attr_reader :shared_folders attr_reader :shared_folders
attr_reader :network_options attr_reader :network_options

View File

@ -78,6 +78,11 @@ module Vagrant
# #
# @param [Hash] net_options The options for the network. # @param [Hash] net_options The options for the network.
def enable_host_only_network(net_options); end def enable_host_only_network(net_options); end
def change_host_name(name)
raise BaseError, :_key => :unsupported_host_name
end
end end
end end
end end

View File

@ -21,6 +21,15 @@ module Vagrant
ssh.exec!("sudo /sbin/ifup eth#{net_options[:adapter]}") ssh.exec!("sudo /sbin/ifup eth#{net_options[:adapter]}")
end end
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 end
end end

View File

@ -23,6 +23,15 @@ module Vagrant
ssh.exec!("sudo /sbin/ifup eth#{net_options[:adapter]}") ssh.exec!("sudo /sbin/ifup eth#{net_options[:adapter]}")
end end
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 end
end end

View File

@ -320,6 +320,8 @@ en:
Skipping port forwarding '%{name}'. Skipping port forwarding '%{name}'.
halt: halt:
force: Forcing shutdown of VM... force: Forcing shutdown of VM...
host_name:
setting: "Setting host name..."
import: import:
importing: Importing base box '%{name}'... importing: Importing base box '%{name}'...
failure: |- failure: |-
@ -499,7 +501,8 @@ en:
Most of the time this is simply due to the fact that no one has contributed 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 back the SSH commands necessary to set this up. Please report a bug and this
will be fixed for your distro. will be fixed for your distro.
unsupported_host_name: |-
Setting host name is currently only supported on Debian, Ubuntu and RedHat
linux: linux:
attempting_halt: "Attempting graceful shutdown of linux..." attempting_halt: "Attempting graceful shutdown of linux..."
mount_fail: "Failed to mount shared folders. `vboxsf` was not available." mount_fail: "Failed to mount shared folders. `vboxsf` was not available."