diff --git a/lib/vagrant/command/base.rb b/lib/vagrant/command/base.rb index 332a21da8..b84fc51d1 100644 --- a/lib/vagrant/command/base.rb +++ b/lib/vagrant/command/base.rb @@ -59,7 +59,9 @@ module Vagrant # specific VM name is specified. # # @param [String] name The name of the VM. Nil if every VM. - def with_target_vms(name=nil) + # @param [Boolean] single_target If true, then an exception will be + # raised if more than one target is found. + def with_target_vms(name=nil, single_target=false) # Using VMs requires a Vagrant environment to be properly setup raise Errors::NoEnvironmentError if !@env.root_path @@ -73,6 +75,9 @@ module Vagrant vms = @env.vms_ordered end + # Make sure we're only working with one VM if single target + raise Errors::MultiVMTargetRequired if single_target && vms.length != 1 + # Go through each VM and yield it! vms.each do |old_vm| # We get a new VM from the environment here to avoid potentially diff --git a/lib/vagrant/command/package.rb b/lib/vagrant/command/package.rb index b07351d27..349c576b0 100644 --- a/lib/vagrant/command/package.rb +++ b/lib/vagrant/command/package.rb @@ -51,12 +51,7 @@ module Vagrant end def package_target(name, options) - if @env.multivm? && name.nil? - # In a multi-VM environment, a name is required. - raise Errors::MultiVMTargetRequired, :command => "package" - end - - with_target_vms(name) do |vm| + with_target_vms(name, true) do |vm| raise Errors::VMNotCreatedError if !vm.created? @logger.debug("Packaging VM: #{vm.name}") package_vm(vm, options) diff --git a/lib/vagrant/command/ssh.rb b/lib/vagrant/command/ssh.rb index 7be4f020f..27831dd0d 100644 --- a/lib/vagrant/command/ssh.rb +++ b/lib/vagrant/command/ssh.rb @@ -19,11 +19,8 @@ module Vagrant argv = parse_options(opts) return if !argv - # SSH always requires a target VM - raise Errors::MultiVMTargetRequired, :command => "ssh" if @env.multivm? && !argv[0] - # Execute the actual SSH - with_target_vms(argv[0]) do |vm| + with_target_vms(argv[0], true) do |vm| # Basic checks that are required for proper SSH raise Errors::VMNotCreatedError if !vm.created? raise Errors::VMInaccessible if !vm.state == :inaccessible diff --git a/lib/vagrant/command/ssh_config.rb b/lib/vagrant/command/ssh_config.rb index 9d2e0c10e..732ef1467 100644 --- a/lib/vagrant/command/ssh_config.rb +++ b/lib/vagrant/command/ssh_config.rb @@ -19,10 +19,7 @@ module Vagrant argv = parse_options(opts) return if !argv - # SSH-config always requires a target VM - raise Errors::MultiVMTargetRequired, :command => "ssh_config" if @env.multivm? && !argv[0] - - with_target_vms(argv[0]) do |vm| + with_target_vms(argv[0], true) do |vm| raise Errors::VMNotCreatedError if !vm.created? raise Errors::VMInaccessible if !vm.vm.accessible? diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 10485f336..40d6f5d96 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -67,7 +67,8 @@ en: You specified: %{home_path} interrupted: "Vagrant exited after cleanup due to external interrupt." multi_vm_required: "A multi-vm environment is required for name specification to this command." - multi_vm_target_required: "`vagrant %{command}` requires a specific VM name to target in a multi-VM environment." + multi_vm_target_required: |- + This command requires a specific VM name to target in a multi-VM environment. no_env: |- A Vagrant environment is required to run this command. Run `vagrant init` to set one up.