Make requiring a single VM target in a command more DRY
This commit is contained in:
parent
0887a18079
commit
ad7f4c79ad
|
@ -59,7 +59,9 @@ module Vagrant
|
||||||
# specific VM name is specified.
|
# specific VM name is specified.
|
||||||
#
|
#
|
||||||
# @param [String] name The name of the VM. Nil if every VM.
|
# @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
|
# Using VMs requires a Vagrant environment to be properly setup
|
||||||
raise Errors::NoEnvironmentError if !@env.root_path
|
raise Errors::NoEnvironmentError if !@env.root_path
|
||||||
|
|
||||||
|
@ -73,6 +75,9 @@ module Vagrant
|
||||||
vms = @env.vms_ordered
|
vms = @env.vms_ordered
|
||||||
end
|
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!
|
# Go through each VM and yield it!
|
||||||
vms.each do |old_vm|
|
vms.each do |old_vm|
|
||||||
# We get a new VM from the environment here to avoid potentially
|
# We get a new VM from the environment here to avoid potentially
|
||||||
|
|
|
@ -51,12 +51,7 @@ module Vagrant
|
||||||
end
|
end
|
||||||
|
|
||||||
def package_target(name, options)
|
def package_target(name, options)
|
||||||
if @env.multivm? && name.nil?
|
with_target_vms(name, true) do |vm|
|
||||||
# In a multi-VM environment, a name is required.
|
|
||||||
raise Errors::MultiVMTargetRequired, :command => "package"
|
|
||||||
end
|
|
||||||
|
|
||||||
with_target_vms(name) do |vm|
|
|
||||||
raise Errors::VMNotCreatedError if !vm.created?
|
raise Errors::VMNotCreatedError if !vm.created?
|
||||||
@logger.debug("Packaging VM: #{vm.name}")
|
@logger.debug("Packaging VM: #{vm.name}")
|
||||||
package_vm(vm, options)
|
package_vm(vm, options)
|
||||||
|
|
|
@ -19,11 +19,8 @@ module Vagrant
|
||||||
argv = parse_options(opts)
|
argv = parse_options(opts)
|
||||||
return if !argv
|
return if !argv
|
||||||
|
|
||||||
# SSH always requires a target VM
|
|
||||||
raise Errors::MultiVMTargetRequired, :command => "ssh" if @env.multivm? && !argv[0]
|
|
||||||
|
|
||||||
# Execute the actual SSH
|
# 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
|
# Basic checks that are required for proper SSH
|
||||||
raise Errors::VMNotCreatedError if !vm.created?
|
raise Errors::VMNotCreatedError if !vm.created?
|
||||||
raise Errors::VMInaccessible if !vm.state == :inaccessible
|
raise Errors::VMInaccessible if !vm.state == :inaccessible
|
||||||
|
|
|
@ -19,10 +19,7 @@ module Vagrant
|
||||||
argv = parse_options(opts)
|
argv = parse_options(opts)
|
||||||
return if !argv
|
return if !argv
|
||||||
|
|
||||||
# SSH-config always requires a target VM
|
with_target_vms(argv[0], true) do |vm|
|
||||||
raise Errors::MultiVMTargetRequired, :command => "ssh_config" if @env.multivm? && !argv[0]
|
|
||||||
|
|
||||||
with_target_vms(argv[0]) do |vm|
|
|
||||||
raise Errors::VMNotCreatedError if !vm.created?
|
raise Errors::VMNotCreatedError if !vm.created?
|
||||||
raise Errors::VMInaccessible if !vm.vm.accessible?
|
raise Errors::VMInaccessible if !vm.vm.accessible?
|
||||||
|
|
||||||
|
|
|
@ -67,7 +67,8 @@ en:
|
||||||
You specified: %{home_path}
|
You specified: %{home_path}
|
||||||
interrupted: "Vagrant exited after cleanup due to external interrupt."
|
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_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: |-
|
no_env: |-
|
||||||
A Vagrant environment is required to run this command. Run `vagrant init`
|
A Vagrant environment is required to run this command. Run `vagrant init`
|
||||||
to set one up.
|
to set one up.
|
||||||
|
|
Loading…
Reference in New Issue