Lots more VBoxManage

This commit is contained in:
Mitchell Hashimoto 2011-12-21 13:37:59 -08:00
parent 7527f6b7b6
commit 48ea8173c6
10 changed files with 90 additions and 77 deletions

View File

@ -17,7 +17,7 @@ Vagrant::Config.run do |config|
config.vm.box_url = nil config.vm.box_url = nil
config.vm.base_mac = nil config.vm.base_mac = nil
config.vm.forward_port("ssh", 22, 2222, :auto => true) config.vm.forward_port("ssh", 22, 2222, :auto => true)
config.vm.boot_mode = "vrdp" config.vm.boot_mode = "headless"
config.vm.guest = :linux config.vm.guest = :linux
# Share the root folder. This can then be overridden by # Share the root folder. This can then be overridden by

View File

@ -29,8 +29,9 @@ module Vagrant
use VM::ShareFolders use VM::ShareFolders
use VM::HostName use VM::HostName
use VM::Network use VM::Network
use VM::Customize # TODO: Bring back some sort of "customize" feature.
use VM::Modify # use VM::Customize
# use VM::Modify
use VM::Boot use VM::Boot
end end
end end

View File

@ -19,7 +19,7 @@ module Vagrant
def boot def boot
@env[:ui].info I18n.t("vagrant.actions.vm.boot.booting") @env[:ui].info I18n.t("vagrant.actions.vm.boot.booting")
@env[:vm].vm.start(@env[:vm].config.vm.boot_mode) @env[:vm].driver.start(@env[:vm].config.vm.boot_mode)
end end
def wait_for_boot def wait_for_boot

View File

@ -8,17 +8,8 @@ module Vagrant
end end
def call(env) def call(env)
proc = lambda do |vm| env[:vm].driver.clear_shared_folders
if vm.shared_folders.length > 0
env[:ui].info I18n.t("vagrant.actions.vm.clear_shared_folders.deleting")
vm.shared_folders.dup.each do |shared_folder|
shared_folder.destroy
end
end
end
env["vm.modify"].call(proc)
@app.call(env) @app.call(env)
end end
end end

View File

@ -118,20 +118,6 @@ module Vagrant
@env[:vm].driver.forward_ports(ports) @env[:vm].driver.forward_ports(ports)
end end
#--------------------------------------------------------------
# General Helpers
#--------------------------------------------------------------
# Forwards a port.
def forward_port(vm, name, options)
port = VirtualBox::NATForwardedPort.new
port.name = name
port.guestport = options[:guestport]
port.hostport = options[:hostport]
port.protocol = options[:protocol] || :tcp
vm.network_adapters[options[:adapter]].nat_driver.forwarded_ports << port
end
end end
end end
end end

View File

@ -33,18 +33,17 @@ module Vagrant
end end
def create_metadata def create_metadata
proc = lambda do |vm| @env[:ui].info I18n.t("vagrant.actions.vm.share_folders.creating")
@env[:ui].info I18n.t("vagrant.actions.vm.share_folders.creating")
shared_folders.each do |name, data| folders = []
folder = VirtualBox::SharedFolder.new shared_folders.each do |name, data|
folder.name = name folders << {
folder.host_path = File.expand_path(data[:hostpath], @env[:root_path]) :name => name,
vm.shared_folders << folder :hostpath => File.expand_path(data[:hostpath], @env[:root_path])
end }
end end
@env["vm.modify"].call(proc) @env[:vm].driver.share_folders(folders)
end end
def mount_shared_folders def mount_shared_folders

View File

@ -95,7 +95,7 @@ module Vagrant
def validate(env, errors) def validate(env, errors)
errors.add(I18n.t("vagrant.config.vm.box_missing")) if !box errors.add(I18n.t("vagrant.config.vm.box_missing")) if !box
errors.add(I18n.t("vagrant.config.vm.box_not_found", :name => box)) if box && !box_url && !env.boxes.find(box) errors.add(I18n.t("vagrant.config.vm.box_not_found", :name => box)) if box && !box_url && !env.boxes.find(box)
errors.add(I18n.t("vagrant.config.vm.boot_mode_invalid")) if ![:vrdp, :gui].include?(boot_mode.to_sym) errors.add(I18n.t("vagrant.config.vm.boot_mode_invalid")) if ![:headless, :gui].include?(boot_mode.to_sym)
errors.add(I18n.t("vagrant.config.vm.base_mac_invalid")) if env.boxes.find(box) && !base_mac errors.add(I18n.t("vagrant.config.vm.base_mac_invalid")) if env.boxes.find(box) && !base_mac
shared_folders.each do |name, options| shared_folders.each do |name, options|

View File

@ -35,6 +35,16 @@ module Vagrant
execute("modifyvm", @uuid, *args) if !args.empty? execute("modifyvm", @uuid, *args) if !args.empty?
end end
# This clears all the shared folders that have been set
# on the virtual machine.
def clear_shared_folders
execute("showvminfo", @uuid, "--machinereadable").split("\n").each do |line|
if line =~ /^SharedFolderNameMachineMapping\d+="(.+?)"$/
execute("sharedfolder", "remove", @uuid, "--name", $1.to_s)
end
end
end
# This deletes the VM with the given name. # This deletes the VM with the given name.
def delete def delete
execute("unregistervm", @uuid, "--delete") execute("unregistervm", @uuid, "--delete")
@ -49,8 +59,8 @@ module Vagrant
# #
# { # {
# :name => "foo", # :name => "foo",
# :host_port => 8500, # :hostport => 8500,
# :guest_port => 80, # :guestport => 80,
# :adapter => 1, # :adapter => 1,
# :protocol => "tcp" # :protocol => "tcp"
# } # }
@ -66,9 +76,9 @@ module Vagrant
pf_builder = [options[:name], pf_builder = [options[:name],
options[:protocol] || "tcp", options[:protocol] || "tcp",
"", "",
options[:host_port], options[:hostport],
"", "",
options[:guest_port]] options[:guestport]]
args.concat(["--natpf#{options[:adapter] || 1}", args.concat(["--natpf#{options[:adapter] || 1}",
pf_builder.join(",")]) pf_builder.join(",")])
@ -89,6 +99,35 @@ module Vagrant
nil nil
end end
# This returns a list of the forwarded ports in the form
# of `[nic, name, hostport, guestport]`.
#
# @return [Array<Array>]
def read_forwarded_ports(uuid=nil, active_only=false)
uuid ||= @uuid
results = []
current_nic = nil
execute("showvminfo", uuid, "--machinereadable").split("\n").each do |line|
# This is how we find the nic that a FP is attached to,
# since this comes first.
current_nic = $1.to_i if line =~ /^nic(\d+)=".+?"$/
# If we care about active VMs only, then we check the state
# to verify the VM is running.
if active_only && line =~ /^VMState="(.+?)"$/ && $1.to_s != "running"
return []
end
# Parse out the forwarded port information
if line =~ /^Forwarding.+?="(.+?),.+?,.*?,(.+?),.*?,(.+?)"$/
results << [current_nic, $1.to_s, $2.to_i, $3.to_i]
end
end
results
end
# This reads the guest additions version for a VM. # This reads the guest additions version for a VM.
def read_guest_additions_version def read_guest_additions_version
output = execute("guestproperty", "get", @uuid, "/VirtualBox/GuestAdd/Version") output = execute("guestproperty", "get", @uuid, "/VirtualBox/GuestAdd/Version")
@ -129,35 +168,33 @@ module Vagrant
execute("modifyvm", @uuid, "--macaddress1", mac) execute("modifyvm", @uuid, "--macaddress1", mac)
end end
protected # Sets up the shared folder metadata for a virtual machine.
# This returns a list of the forwarded ports in the form
# of `[nic, name, hostport, guestport]`.
# #
# @return [Array<Array>] # The structure of a folder definition should be the following:
def read_forwarded_ports(uuid, active_only=false) #
results = [] # {
current_nic = nil # :name => "foo",
execute("showvminfo", uuid, "--machinereadable").split("\n").each do |line| # :hostpath => "/foo/bar"
# This is how we find the nic that a FP is attached to, # }
# since this comes first. #
current_nic = $1.to_i if line =~ /^nic(\d+)=".+?"$/ # @param [Array<Hash>] folders An array of folder definitions to
# setup.
# If we care about active VMs only, then we check the state def share_folders(folders)
# to verify the VM is running. folders.each do |folder|
if active_only && line =~ /^VMState="(.+?)"$/ && $1.to_s != "running" execute("sharedfolder", "add", @uuid, "--name",
return [] folder[:name], "--hostpath", folder[:hostpath])
end
# Parse out the forwarded port information
if line =~ /^Forwarding.+?="(.+?),.+?,.*?,(.+?),.*?,(.+?)"$/
results << [current_nic, $1.to_s, $2.to_i, $3.to_i]
end
end end
results
end end
# Starts the virtual machine in the given mode.
#
# @param [String] mode Mode to boot the VM: either "headless" or "gui"
def start(mode)
execute("startvm", @uuid, "--type", mode.to_s)
end
protected
# This returns the version of VirtualBox that is running. # This returns the version of VirtualBox that is running.
# #
# @return [String] # @return [String]

View File

@ -180,15 +180,14 @@ module Vagrant
# Check if we have an SSH forwarded port # Check if we have an SSH forwarded port
pnum_by_name = nil pnum_by_name = nil
pnum_by_destination = nil pnum_by_destination = nil
@vm.vm.network_adapters.each do |na| @vm.driver.read_forwarded_ports.each do |_, name, hostport, guestport|
# Look for the port number by name... # Record the forwarded port if we find it by name
pnum_by_name = na.nat_driver.forwarded_ports.detect do |fp| if name == @vm.config.ssh.forwarded_port_key
fp.name == @vm.config.ssh.forwarded_port_key pnum_by_name = hostport
end end
# Look for the port number by destination... if guestport == @vm.config.ssh.forwarded_port_destination
pnum_by_destination = na.nat_driver.forwarded_ports.detect do |fp| pnum_by_destination = hostport
fp.guestport == @vm.config.ssh.forwarded_port_destination
end end
# pnum_by_name is what we're looking for here, so break early # pnum_by_name is what we're looking for here, so break early
@ -196,8 +195,8 @@ module Vagrant
break if pnum_by_name break if pnum_by_name
end end
return pnum_by_name.hostport if pnum_by_name return pnum_by_name if pnum_by_name
return pnum_by_destination.hostport if pnum_by_destination return pnum_by_destination if pnum_by_destination
# This should NEVER happen. # This should NEVER happen.
raise Errors::SSHPortNotDetected raise Errors::SSHPortNotDetected

View File

@ -178,7 +178,7 @@ en:
private_key_missing: "`private_key_path` file must exist: %{path}" private_key_missing: "`private_key_path` file must exist: %{path}"
vm: vm:
base_mac_invalid: "Base MAC address for eth0/NAT must be set. Contact box maintainer for more information." base_mac_invalid: "Base MAC address for eth0/NAT must be set. Contact box maintainer for more information."
boot_mode_invalid: "Boot mode must be one of: vrdp or gui" boot_mode_invalid: "Boot mode must be one of: headless or gui"
box_missing: "A box must be specified." box_missing: "A box must be specified."
box_not_found: "The box '%{name}' could not be found." box_not_found: "The box '%{name}' could not be found."
network_ip_invalid: "The host only network IP '%{ip}' is invalid." network_ip_invalid: "The host only network IP '%{ip}' is invalid."