`vagrant status` updated to work with multi-VM. Global status not yet functional.

This commit is contained in:
Mitchell Hashimoto 2010-05-16 17:40:40 -07:00
parent 0314e6ef6c
commit 709c50e7b6
9 changed files with 126 additions and 22 deletions

View File

@ -12,7 +12,7 @@ module Vagrant
attr_reader :env
class <<self
class << self
# Contains the list of registered subcommands. The registered commands are
# stored in a hash table and are therefore unordered.
#
@ -126,7 +126,6 @@ module Vagrant
# to parse command line options.
def parse_options(args)
option_parser.parse!(args)
options
rescue OptionParser::InvalidOption
show_help
end
@ -164,4 +163,4 @@ module Vagrant
end
end
end
end
end

View File

@ -8,43 +8,84 @@ module Vagrant
description "Shows the status of the Vagrant environment."
def execute(args=[])
parse_options(args)
args = parse_options(args)
if args.length > 1
# There should never be more than 1 arg
show_help
return
end
if !options[:global]
show_local_status
show_local_status(*args)
else
show_global_status
end
end
# Shows the status of the CURRENT environment (the current working
# directory). This prints out a human friendly sentence or paragraph
# describing the state of the Vagrant environment represented by the
# current working directory.
def show_local_status
# directory). If a specific VM was given, it will print out
# detailed information regarding that VM. If no single VM was
# specified and it is a multi-VM environment, it will simply
# show a listing of all the VMs and their short one word
# statuses.
def show_local_status(vm=nil)
if !env.root_path
wrap_output { puts Translator.t(:status_no_environment) }
return
end
if vm.nil?
if env.multivm?
# No specific VM was specified in a multi-vm environment,
# so show short info for each VM
show_list
else
# Set the VM to just be the root VM
vm = env.vms.values.first
end
else
# Try to get the vm based on the name. If the specified VM
# doesn't exist, then error saying so
vm = env.vms[vm.to_sym]
end
show_single(vm)
end
# Lists the available VMs and brief statuses about each.
def show_list
wrap_output do
puts Translator.t(:status_listing)
env.vms.each do |name, vm|
puts "#{name.ljust(20)}#{vm.state}"
end
end
end
# Shows a paragraph of information based on the current state of
# a single, specified VM.
def show_single(vm)
string_key = nil
if !env.root_path
string_key = :status_no_environment
elsif !env.vm
if !vm.created?
string_key = :status_not_created
else
additional_key = nil
if env.vm.vm.running?
if vm.vm.running?
additional_key = :status_created_running
elsif env.vm.vm.saved?
elsif vm.vm.saved?
additional_key = :status_created_saved
elsif env.vm.vm.powered_off?
elsif vm.vm.powered_off?
additional_key = :status_created_powered_off
end
string_key = [:status_created, {
:vm_state => env.vm.vm.state,
:vm_state => vm.vm.state,
:additional_message => additional_key ? Translator.t(additional_key) : ""
}]
end
string_key = [string_key, {}] unless string_key.is_a?(Array)
wrap_output { puts Translator.t(*string_key) }
end
@ -70,6 +111,7 @@ module Vagrant
# Defaults
options[:global] = false
options[:vm] = nil
opts.on("-g", "--global", "Show global status of Vagrant (running VMs managed by Vagrant)") do |v|
options[:global] = true
@ -77,4 +119,4 @@ module Vagrant
end
end
end
end
end

View File

@ -100,6 +100,17 @@ module Vagrant
@vms ||= {}
end
# Returns a boolean whether this environment represents a multi-VM
# environment or not. This will work even when called on child
# environments.
def multivm?
if parent
parent.multivm?
else
vms.length > 1
end
end
#---------------------------------------------------------------
# Load Methods
#---------------------------------------------------------------

View File

@ -4,6 +4,7 @@ module Vagrant
attr_reader :env
attr_reader :system
attr_reader :name
attr_accessor :vm
class << self
@ -25,6 +26,7 @@ module Vagrant
opts = defaults.merge(opts || {})
@vm = opts[:vm]
@name = opts[:vm_name]
if !opts[:env].nil?
# We have an environment, so we create a new child environment
@ -37,7 +39,7 @@ module Vagrant
:vm => self
}).load!
# Load the associated system.
# Load the associated system.
load_system!
end
end

View File

@ -6,6 +6,10 @@
#---------------------------------------------------------------------
# CATEGORY: Status Messages
#---------------------------------------------------------------------
:status_listing: |-
This environment represents multiple VMs. The VMs will be listed
below with a short status. For more detailed information about a
VM, run `vagrant status NAME`.
:status_no_environment: |-
No vagrant environment detected. Run `vagrant init` to setup a Vagrantfile
in the current directory to get started with Vagrant.

View File

@ -112,8 +112,9 @@ class CommandsBaseTest < Test::Unit::TestCase
end
should "parse the options with the args" do
@option_parser.expects(:parse!).with(@args).once
assert_equal @options, @instance.parse_options(@args)
result = mock("result")
@option_parser.expects(:parse!).with(@args).once.returns(result)
assert_equal result, @instance.parse_options(@args)
end
end
end

View File

@ -26,5 +26,15 @@ class CommandsStatusTest < Test::Unit::TestCase
@instance.expects(:show_global_status).once
@instance.execute(["--global"])
end
should "show help if too many args are given" do
@instance.expects(:show_help).once
@instance.execute(["1","2","3"])
end
should "pass the VM name to local status if given" do
@instance.expects(:show_local_status).with("foo").once
@instance.execute(["foo"])
end
end
end

View File

@ -124,6 +124,41 @@ class EnvironmentTest < Test::Unit::TestCase
end
end
context "multivm? helper" do
setup do
@env = mock_environment
end
context "with a parent" do
setup do
@parent = mock('parent')
@env.stubs(:parent).returns(@parent)
end
should "return the value of multivm? from the parent" do
result = mock("result")
@parent.stubs(:multivm?).returns(result)
assert_equal result, @env.multivm?
end
end
context "without a parent" do
setup do
@env.stubs(:parent).returns(nil)
end
should "return true if VM length greater than 1" do
@env.stubs(:vms).returns([1,2,3])
assert @env.multivm?
end
should "return false if VM length is 1" do
@env.stubs(:vms).returns([1])
assert !@env.multivm?
end
end
end
context "loading" do
setup do
@env = mock_environment

View File

@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
s.authors = ["Mitchell Hashimoto", "John Bender"]
s.date = %q{2010-05-15}
s.date = %q{2010-05-16}
s.default_executable = %q{vagrant}
s.description = %q{Vagrant is a tool for building and distributing virtualized development environments.}
s.email = ["mitchell.hashimoto@gmail.com", "john.m.bender@gmail.com"]