Allow multiple VMs to be specified for relevant commands

This commit is contained in:
withnale 2012-03-11 22:32:17 +00:00 committed by Mitchell Hashimoto
parent 3338b6c134
commit ff36845259
12 changed files with 32 additions and 28 deletions

View File

@ -64,7 +64,7 @@ module Vagrant
# @param [String] name The name of the VM. Nil if every VM.
# @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, options=nil)
def with_target_vms(names=nil, options=nil)
# Using VMs requires a Vagrant environment to be properly setup
raise Errors::NoEnvironmentError if !@env.root_path
@ -73,28 +73,30 @@ module Vagrant
# First determine the proper array of VMs.
vms = []
if name
raise Errors::MultiVMEnvironmentRequired if !@env.multivm?
if names.length > 0
names.each do |name|
raise Errors::MultiVMEnvironmentRequired if !@env.multivm?
if name =~ /^\/(.+?)\/$/
# This is a regular expression name, so we convert to a regular
# expression and allow that sort of matching.
regex = Regexp.new($1.to_s)
if name =~ /^\/(.+?)\/$/
# This is a regular expression name, so we convert to a regular
# expression and allow that sort of matching.
regex = Regexp.new($1.to_s)
@env.vms.each do |name, vm|
vms << vm if name =~ regex
@env.vms.each do |name, vm|
vms << vm if name =~ regex
end
raise Errors::VMNoMatchError if vms.empty?
else
# String name, just look for a specific VM
vms << @env.vms[name.to_sym]
raise Errors::VMNotFoundError, :name => name if !vms[0]
end
raise Errors::VMNoMatchError if vms.empty?
else
# String name, just look for a specific VM
vms << @env.vms[name.to_sym]
raise Errors::VMNotFoundError, :name => name if !vms[0]
end
else
vms = @env.vms_ordered
end
# Make sure we're only working with one VM if single target
raise Errors::MultiVMTargetRequired if options[:single_target] && vms.length != 1

View File

@ -20,7 +20,7 @@ module Vagrant
return if !argv
@logger.debug("'Destroy' each target VM...")
with_target_vms(argv[0], :reverse => true) do |vm|
with_target_vms(argv, :reverse => true) do |vm|
if vm.created?
# Boolean whether we should actually go through with the destroy
# or not. This is true only if the "--force" flag is set or if the

View File

@ -21,7 +21,7 @@ module Vagrant
return if !argv
@logger.debug("Halt command: #{argv.inspect} #{options.inspect}")
with_target_vms(argv[0]) do |vm|
with_target_vms(argv) do |vm|
if vm.created?
@logger.info("Halting #{vm.name}")
vm.halt(:force => options[:force])

View File

@ -54,7 +54,7 @@ module Vagrant
end
def package_target(name, options)
with_target_vms(name, :single_target => true) do |vm|
with_target_vms([ name ], :single_target => true) do |vm|
raise Errors::VMNotCreatedError if !vm.created?
@logger.debug("Packaging VM: #{vm.name}")
package_vm(vm, options)

View File

@ -16,7 +16,8 @@ module Vagrant
# Go over each VM and provision!
@logger.debug("'provision' each target VM...")
with_target_vms(argv[0]) do |vm|
with_target_vms(argv) do |vm|
if vm.created?
if vm.state == :running
@logger.info("Provisioning: #{vm.name}")

View File

@ -9,7 +9,8 @@ module Vagrant
def execute
options = {}
opts = OptionParser.new do |opts|
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant reload [vm-name]"
opts.separator ""
build_start_options(opts, options)
@ -20,7 +21,7 @@ module Vagrant
return if !argv
@logger.debug("'reload' each target VM...")
with_target_vms(argv[0]) do |vm|
with_target_vms(argv) do |vm|
if vm.created?
@logger.info("Reloading: #{vm.name}")
vm.reload(options)

View File

@ -15,7 +15,7 @@ module Vagrant
return if !argv
@logger.debug("'resume' each target VM...")
with_target_vms(argv[0]) do |vm|
with_target_vms(argv) do |vm|
if vm.created?
@logger.info("Resume: #{vm.name}")
vm.resume

View File

@ -36,7 +36,7 @@ module Vagrant
argv = [] if argv == ssh_args
# Execute the actual SSH
with_target_vms(argv[0], :single_target => true) do |vm|
with_target_vms(argv, :single_target => true) do |vm|
# Basic checks that are required for proper SSH
raise Errors::VMNotCreatedError if !vm.created?
raise Errors::VMInaccessible if !vm.state == :inaccessible

View File

@ -23,7 +23,7 @@ module Vagrant
argv = parse_options(opts)
return if !argv
with_target_vms(argv[0], :single_target => true) do |vm|
with_target_vms(argv, :single_target => true) do |vm|
raise Errors::VMNotCreatedError if !vm.created?
raise Errors::VMInaccessible if !vm.state == :inaccessible

View File

@ -16,7 +16,7 @@ module Vagrant
state = nil
results = []
with_target_vms(argv[0]) do |vm|
with_target_vms(argv) do |vm|
state = vm.state.to_s if !state
results << "#{vm.name.to_s.ljust(25)}#{vm.state.to_s.gsub("_", " ")}"
end

View File

@ -15,7 +15,7 @@ module Vagrant
return if !argv
@logger.debug("'suspend' each target VM...")
with_target_vms(argv[0]) do |vm|
with_target_vms(argv) do |vm|
if vm.created?
@logger.info("Suspending: #{vm.name}")
vm.suspend

View File

@ -21,7 +21,7 @@ module Vagrant
# Go over each VM and bring it up
@logger.debug("'Up' each target VM...")
with_target_vms(argv[0]) do |vm|
with_target_vms(argv) do |vm|
if vm.created?
@logger.info("Booting: #{vm.name}")
vm.ui.info I18n.t("vagrant.commands.up.vm_created")