Few more VM actions converted to exceptions. Lots of errors gone.

This commit is contained in:
Mitchell Hashimoto 2010-08-30 20:07:50 -07:00
parent 72c3340336
commit aa00d15206
11 changed files with 147 additions and 106 deletions

View File

@ -11,7 +11,7 @@ module Vagrant
@env = env @env = env
threshold_check threshold_check
external_collision_check if !env.error? external_collision_check
end end
#-------------------------------------------------------------- #--------------------------------------------------------------
@ -23,7 +23,7 @@ module Vagrant
# 1024, which causes the forwarded ports to fail. # 1024, which causes the forwarded ports to fail.
def threshold_check def threshold_check
@env.env.config.vm.forwarded_ports.each do |name, options| @env.env.config.vm.forwarded_ports.each do |name, options|
return @env.error!(:vm_port_below_threshold) if options[:hostport] <= 1024 raise Errors::ForwardPortBelowThreshold.new if options[:hostport] <= 1024
end end
end end
@ -47,7 +47,9 @@ module Vagrant
if !options[:auto] if !options[:auto]
# Auto fixing is disabled for this port forward, so we # Auto fixing is disabled for this port forward, so we
# must throw an error so the user can fix it. # must throw an error so the user can fix it.
return @env.error!(:vm_port_collision, :name => name, :hostport => options[:hostport].to_s, :guestport => options[:guestport].to_s, :adapter => options[:adapter]) raise Errors::ForwardPortCollision.new(:name => name,
:host_port => options[:hostport].to_s,
:guest_port => options[:guestport].to_s)
end end
# Get the auto port range and get rid of the used ports and # Get the auto port range and get rid of the used ports and
@ -58,7 +60,10 @@ module Vagrant
range -= existing_ports range -= existing_ports
if range.empty? if range.empty?
return @env.error!(:vm_port_auto_empty, :vm_name => @env["vm"].name, :name => name, :options => options) raise Errors::ForwardPortAutolistEmpty.new(:vm_name => @env["vm"].name,
:name => name,
:host_port => options[:hostport].to_s,
:guest_port => options[:guestport].to_s)
end end
# Set the port up to be the first one and add that port to # Set the port up to be the first one and add that port to

View File

@ -15,7 +15,7 @@ module Vagrant
end end
# Flag as erroneous and return if import failed # Flag as erroneous and return if import failed
return env.error!(:virtualbox_import_failure) if !env['vm'].vm raise Errors::VMImportFailure.new if !env["vm"].vm
# Import completed successfully. Continue the chain # Import completed successfully. Continue the chain
@app.call(env) @app.call(env)

View File

@ -11,10 +11,7 @@ module Vagrant
@env = env @env = env
env["config"].vm.network_options.compact.each do |network_options| env["config"].vm.network_options.compact.each do |network_options|
if !verify_no_bridge_collision(network_options) raise Errors::NetworkCollision.new if !verify_no_bridge_collision(network_options)
env.error!(:network_collides)
return
end
end end
end end
@ -24,7 +21,7 @@ module Vagrant
@app.call(env) @app.call(env)
if !env.error? && enable_network? if enable_network?
catch_action_exception(env) do catch_action_exception(env) do
@env.ui.info "vagrant.actions.vm.network.enabling" @env.ui.info "vagrant.actions.vm.network.enabling"
@env["vm"].system.prepare_host_only_network @env["vm"].system.prepare_host_only_network
@ -90,7 +87,7 @@ module Vagrant
end end
end end
return @env.error!(:network_not_found, :name => net_options[:name]) if net_options[:name] raise Errors::NetworkNotFound.new(:name => net_options[:name]) if net_options[:name]
# One doesn't exist, create it. # One doesn't exist, create it.
@env.ui.info "vagrant.actions.vm.network.creating" @env.ui.info "vagrant.actions.vm.network.creating"

View File

@ -37,11 +37,11 @@ module Vagrant
export_folders export_folders
end end
return if env.error?
@app.call(env) @app.call(env)
mount_folders if !folders.empty? && !env.error? mount_folders if !folders.empty?
# TODO: Make recover method
clear_nfs_exports(env) if env.error? clear_nfs_exports(env) if env.error?
end end
@ -151,9 +151,9 @@ module Vagrant
# Verifies that the host is set and supports NFS. # Verifies that the host is set and supports NFS.
def verify_settings def verify_settings
return @env.error!(:nfs_host_required) if @env["host"].nil? raise Errors::NFSHostRequired.new if @env["host"].nil?
return @env.error!(:nfs_not_supported) if !@env["host"].nfs? raise Errors::NFSNotSupported.new if !@env["host"].nfs?
return @env.error!(:nfs_no_host_network) if @env["config"].vm.network_options.empty? raise Errors::NFSNoHostNetwork.new if @env["config"].vm.network_options.empty?
end end
end end
end end

View File

@ -79,6 +79,21 @@ module Vagrant
error_key(:cli_missing_env) error_key(:cli_missing_env)
end end
class ForwardPortAutolistEmpty < VagrantError
status_code(27)
error_key(:auto_empty, "vagrant.actions.vm.forward_ports")
end
class ForwardPortBelowThreshold < VagrantError
status_code(25)
error_key(:below_threshold_error, "vagrant.actions.vm.forward_ports")
end
class ForwardPortCollision < VagrantError
status_code(26)
error_key(:collision_error, "vagrant.actions.vm.forward_ports")
end
class MultiVMEnvironmentRequired < VagrantError class MultiVMEnvironmentRequired < VagrantError
status_code(5) status_code(5)
error_key(:multi_vm_required) error_key(:multi_vm_required)
@ -89,6 +104,31 @@ module Vagrant
error_key(:multi_vm_target_required) error_key(:multi_vm_target_required)
end end
class NetworkCollision < VagrantError
status_code(29)
error_key(:collides, "vagrant.actions.vm.network")
end
class NetworkNotFound < VagrantError
status_code(30)
error_key(:not_found, "vagrant.actions.vm.network")
end
class NFSHostRequired < VagrantError
status_code(31)
error_key(:host_required, "vagrant.actions.vm.nfs")
end
class NFSNotSupported < VagrantError
status_code(32)
error_key(:not_supported, "vagrant.actions.vm.nfs")
end
class NFSNoHostNetwork < VagrantError
status_code(33)
error_key(:no_host_network, "vagrant.actions.vm.nfs")
end
class NoEnvironmentError < VagrantError class NoEnvironmentError < VagrantError
status_code(3) status_code(3)
error_key(:no_env) error_key(:no_env)
@ -144,6 +184,11 @@ module Vagrant
error_key(:failed_to_boot, "vagrant.actions.vm.boot") error_key(:failed_to_boot, "vagrant.actions.vm.boot")
end end
class VMImportFailure < VagrantError
status_code(28)
error_key(:failure, "vagrant.actions.vm.import")
end
class VMNotCreatedError < VagrantError class VMNotCreatedError < VagrantError
status_code(6) status_code(6)
error_key(:vm_creation_required) error_key(:vm_creation_required)

View File

@ -149,6 +149,29 @@ en:
exporting: Exporting VM... exporting: Exporting VM...
power_off: "The Vagrant virtual environment you are trying to package must be powered off." power_off: "The Vagrant virtual environment you are trying to package must be powered off."
forward_ports: forward_ports:
auto_empty: |-
Vagrant found a port collision for the specified port and virtual machine.
While this port was marked to be auto-corrected, the ports in the
auto-correction range are all also used.
VM: %{vm_name}
Forwarded port: %{name} (%{guest_port} => %{host_port})
below_threshold_error: |-
The host port of all forwarded ports must be above 1024. VirtualBox
does not allow host ports to be below 1024. (Guest ports below 1024
are fine. For example: SSH on port 22 on the guest can be forwarded
to port 2222, but not 222).
collision_error: |-
Vagrant cannot forward the specified ports on this VM, since they
would collide with another VirtualBox virtual machine's forwarded
ports! The '%{name}' forwarded port (%{host_port}) is already in use on the host
machine.
To fix this, modify your current projects Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:
config.vm.forward_port("%{name}", %{guest_port}, 1234)
fixed_collision: Fixed port collision '%{name}'. Now on port %{new_port}. fixed_collision: Fixed port collision '%{name}'. Now on port %{new_port}.
forwarding: Forwarding ports... forwarding: Forwarding ports...
forwarding_entry: "-- %{name}: %{guest_port} => %{host_port} (adapter %{adapter})" forwarding_entry: "-- %{name}: %{guest_port} => %{host_port} (adapter %{adapter})"
@ -159,13 +182,37 @@ en:
force: Forcing shutdown of VM... force: Forcing shutdown of VM...
import: import:
importing: Importing base box '%{name}'... importing: Importing base box '%{name}'...
failure: |-
The VM import failed! Try running `VBoxManage import` on the box file
manually for more verbose error output.
match_mac: match_mac:
matching: Matching MAC address for NAT networking... matching: Matching MAC address for NAT networking...
network: network:
enabling: Enabling host only network... collides: |-
preparing: Preparing host only network... The specified host network collides with a non-hostonly network!
This will cause your specified IP to be inaccessible. Please change
the IP or name of your host only network to not match that of
a bridged or non-hostonly network.
creating: Creating new host only network for environment... creating: Creating new host only network for environment...
enabling: Enabling host only network...
not_found: |-
The specified host network could not be found: '%{name}.'
If the name specification is removed, Vagrant will create a new
host only network for you. Alternatively, please create the
specified network manually.
preparing: Preparing host only network...
nfs: nfs:
host_required: |-
A host class is required for NFS shared folders. By default, these
are auto-detected, but can be overriden with `config.vagrant.host`.
There is currently not host class loaded.
no_host_network: |-
NFS shared folders requires that host only networking is enabled.
Please enable host only networking via `config.vm.network`.
not_supported: |-
The host class is reporting that NFS is not supported by this host,
or `nfsd` may not be installed. Please verify that `nfsd` is installed
on your machine, and retry.
exporting: Exporting NFS shared folders... exporting: Exporting NFS shared folders...
mounting: Mounting NFS shared folders... mounting: Mounting NFS shared folders...
persist: persist:

View File

@ -68,27 +68,6 @@
The given box does not exist on the file system: The given box does not exist on the file system:
<%= source_url %> <%= source_url %>
:nfs_host_required: |-
A host class is required for NFS shared folders. By default, these
are auto-detected, but can be overriden with `config.vagrant.host`.
There is currently not host class loaded.
:nfs_not_supported: |-
The host class is reporting that NFS is not supported by this host,
or `nfsd` may not be installed. Please verify that `nfsd` is installed
on your machine, and retry.
:nfs_no_host_network: |-
NFS shared folders requires that host only networking is enabled.
Please enable host only networking via `config.vm.network`.
:network_not_found: |-
The specified host network could not be found: <%= name %>.
If the name specification is removed, Vagrant will create a new
host only network for you. Alternatively, please create the
specified network manually.
:network_collides: |-
The specified host network collides with a non-hostonly network!
This will cause your specified IP to be inaccessible. Please change
the IP or name of your host only network to not match that of
a bridged or non-hostonly network.
:package_requires_export: |- :package_requires_export: |-
Package must be used in conjunction with export. Package must be used in conjunction with export.
:provisioner_invalid_class: |- :provisioner_invalid_class: |-
@ -111,31 +90,6 @@
:system_unspecified: |- :system_unspecified: |-
A VM system type must be specified! This is done via the `config.vm.system` A VM system type must be specified! This is done via the `config.vm.system`
configuration value. Please read the documentation online for more information. configuration value. Please read the documentation online for more information.
:virtualbox_import_failure: |-
The VM import failed! Try running `VBoxManage import` on the box file
manually for more verbose error output.
:vm_port_auto_empty: |-
Vagrant found a port collision for the specified port and virtual machine.
While this port was marked to be auto-corrected, the ports in the
auto-correction range are all also used.
VM: <%= vm_name %>
Forwarded port: <%= name %> (<%= options[:guestport] %> => <%= options[:hostport] %>)
:vm_port_below_threshold: |-
The host port of all forwarded ports must be above 1024. VirtualBox
does not allow host ports to be below 1024. (Guest ports below 1024
are fine. For example: SSH on port 22 on the guest can be forwarded
to port 2222, but not 222).
:vm_port_collision: |-
Vagrant cannot forward the specified ports on this VM, since they
would collide with another VirtualBox virtual machine's forwarded
ports! The "<%= name %>" forwarded port (<%= hostport %>) is already in use on the host
machine.
To fix this, modify your current projects Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:
config.vm.forward_port("<%= name %>", <%= guestport %>, 1234)
:vm_mount_fail: |- :vm_mount_fail: |-
Failed to mount shared folders. vboxsf was not available. Failed to mount shared folders. vboxsf was not available.
#--------------------------------------------------------------------- #---------------------------------------------------------------------

View File

@ -26,9 +26,10 @@ class ForwardPortsVMActionTest < Test::Unit::TestCase
should "error if has a port below threshold" do should "error if has a port below threshold" do
@env.env.config.vm.forwarded_ports.clear @env.env.config.vm.forwarded_ports.clear
@env.env.config.vm.forward_port("foo", 22, 222) @env.env.config.vm.forward_port("foo", 22, 222)
assert_raises(Vagrant::Errors::ForwardPortBelowThreshold) {
@klass.new(@app, @env) @klass.new(@app, @env)
assert @env.error? }
assert_equal :vm_port_below_threshold, @env.error.first
end end
should "not error if ports are fine" do should "not error if ports are fine" do
@ -84,9 +85,10 @@ class ForwardPortsVMActionTest < Test::Unit::TestCase
should "error if auto forwarding is disabled" do should "error if auto forwarding is disabled" do
@options[:auto] = false @options[:auto] = false
assert_raises(Vagrant::Errors::ForwardPortCollision) {
@instance.handle_collision(@name, @options, @used_ports) @instance.handle_collision(@name, @options, @used_ports)
assert @env.error? }
assert_equal :vm_port_collision, @env.error.first
end end
should "set the host port to the first available port" do should "set the host port to the first available port" do
@ -111,9 +113,10 @@ class ForwardPortsVMActionTest < Test::Unit::TestCase
should "raise an exception if there are no auto ports available" do should "raise an exception if there are no auto ports available" do
@env.env.config.vm.auto_port_range = (1..3) @env.env.config.vm.auto_port_range = (1..3)
assert_raises(Vagrant::Errors::ForwardPortAutolistEmpty) {
@instance.handle_collision(@name, @options, @used_ports) @instance.handle_collision(@name, @options, @used_ports)
assert @env.error? }
assert_equal :vm_port_auto_empty, @env.error.first
end end
end end

View File

@ -33,9 +33,9 @@ class ImportVMActionTest < Test::Unit::TestCase
should "mark environment erroneous and not continue chain on failure" do should "mark environment erroneous and not continue chain on failure" do
@app.expects(:call).never @app.expects(:call).never
assert_raises(Vagrant::Errors::VMImportFailure) {
@instance.call(@env) @instance.call(@env)
}
assert @env.error?
end end
should "run the destroy action on recover" do should "run the destroy action on recover" do

View File

@ -18,7 +18,7 @@ class NetworkVMActionTest < Test::Unit::TestCase
context "initializing" do context "initializing" do
should "verify no bridge collisions for each network enabled" do should "verify no bridge collisions for each network enabled" do
@env.env.config.vm.network("foo") @env.env.config.vm.network("foo")
@klass.any_instance.expects(:verify_no_bridge_collision).once.with() do |options| @klass.any_instance.expects(:verify_no_bridge_collision).returns(true).once.with() do |options|
assert_equal "foo", options[:ip] assert_equal "foo", options[:ip]
true true
end end
@ -139,8 +139,9 @@ class NetworkVMActionTest < Test::Unit::TestCase
mock_interface(:name => @options[:name], mock_interface(:name => @options[:name],
:interface_type => :bridged) :interface_type => :bridged)
assert_raises(Vagrant::Errors::NetworkNotFound) {
@instance.network_name(@options) @instance.network_name(@options)
assert @env.error? }
end end
should "return the network which matches the name if given" do should "return the network which matches the name if given" do
@ -154,9 +155,9 @@ class NetworkVMActionTest < Test::Unit::TestCase
@options[:name] = "foo" @options[:name] = "foo"
@interfaces.expects(:create).never @interfaces.expects(:create).never
assert_raises(Vagrant::Errors::NetworkNotFound) {
@instance.network_name(@options) @instance.network_name(@options)
assert @env.error? }
assert_equal :network_not_found, @env.error.first
end end
should "create a network for the IP and netmask" do should "create a network for the IP and netmask" do

View File

@ -66,18 +66,6 @@ class NFSVMActionTest < Test::Unit::TestCase
@instance.call(@env) @instance.call(@env)
end end
should "halt chain if environment error occured" do
@env.error!(:foo)
seq = sequence('seq')
@instance.expects(:extract_folders).in_sequence(seq)
@instance.expects(:prepare_folders).in_sequence(seq)
@instance.expects(:clear_nfs_exports).in_sequence(seq)
@instance.expects(:export_folders).in_sequence(seq)
@app.expects(:call).never
@instance.call(@env)
end
should "not mount folders if an error occured" do should "not mount folders if an error occured" do
seq = sequence("seq") seq = sequence("seq")
@app.expects(:call).in_sequence(seq).with() do @app.expects(:call).in_sequence(seq).with() do
@ -240,29 +228,30 @@ class NFSVMActionTest < Test::Unit::TestCase
should "error environment if host is nil" do should "error environment if host is nil" do
@env.env.stubs(:host).returns(nil) @env.env.stubs(:host).returns(nil)
assert_raises(Vagrant::Errors::NFSHostRequired) {
@instance.verify_settings @instance.verify_settings
assert @env.error? }
assert_equal :nfs_host_required, @env.error.first
end end
should "error environment if host does not support NFS" do should "error environment if host does not support NFS" do
@env.env.host.stubs(:nfs?).returns(false) @env.env.host.stubs(:nfs?).returns(false)
assert_raises(Vagrant::Errors::NFSNotSupported) {
@instance.verify_settings @instance.verify_settings
assert @env.error? }
assert_equal :nfs_not_supported, @env.error.first
end end
should "error environment if host only networking is not enabled" do should "error environment if host only networking is not enabled" do
@env.env.config.vm.network_options.clear @env.env.config.vm.network_options.clear
assert_raises(Vagrant::Errors::NFSNoHostNetwork) {
@instance.verify_settings @instance.verify_settings
assert @env.error? }
assert_equal :nfs_no_host_network, @env.error.first
end end
should "be fine if everything passes" do should "be fine if everything passes" do
@env.env.host.stubs(:nfs?).returns(true) @env.env.host.stubs(:nfs?).returns(true)
assert_nothing_raised {
@instance.verify_settings @instance.verify_settings
assert !@env.error? }
end end
end end
end end