Environment loads blank VMs for non-created VMs.

This commit is contained in:
Mitchell Hashimoto 2010-05-15 01:34:31 -07:00
parent 689a416809
commit 33bfe75cbd
7 changed files with 136 additions and 73 deletions

View File

@ -14,4 +14,5 @@ group :test do
gem "contest", ">= 0.1.2"
gem "mocha"
gem "ruby-debug", ">= 0.10.3" if RUBY_VERSION < '1.9'
gem "jeweler", "~> 1.4.0"
end

View File

@ -1 +1 @@
0.3.3.dev
0.3.4.dev

View File

@ -5,7 +5,7 @@ module Vagrant
class Command
attr_reader :env
class <<self
class << self
# Executes a given subcommand within the current environment (from the
# current working directory).
def execute(*args)
@ -24,4 +24,4 @@ module Vagrant
Commands::Base.dispatch(env, *args)
end
end
end
end

View File

@ -9,12 +9,18 @@ module Vagrant
description "Creates the vagrant environment"
def execute(args=[])
if env.vm
logger.info "VM already created. Starting VM if its not already running..."
env.vm.start
else
env.require_box
env.create_vm.execute!(Actions::VM::Up)
# First verify that all VMs have valid boxes
env.vms.each { |name, vm| vm.env.require_box unless vm.created? }
# Next, handle each VM
env.vms.each do |name, vm|
if vm.created?
logger.info "VM '#{name}' already created. Booting if its not already running..."
# vm.start
else
logger.info "Creating VM '#{name}'"
# env.create_vm.execute!(Actions::VM::Up)
end
end
end
@ -23,4 +29,4 @@ module Vagrant
end
end
end
end
end

View File

@ -5,6 +5,7 @@ module Vagrant
class Environment
ROOTFILE_NAME = "Vagrantfile"
HOME_SUBDIRS = ["tmp", "boxes"]
DEFAULT_VM = :default
include Util
@ -76,7 +77,7 @@ module Vagrant
# The path to the `dotfile`, which contains the persisted UUID of
# the VM if it exists.
def dotfile_path
File.join(root_path, config.vagrant.dotfile_name)
root_path ? File.join(root_path, config.vagrant.dotfile_name) : nil
end
# The path to the home directory, which is usually in `~/.vagrant/~
@ -204,19 +205,18 @@ module Vagrant
# nothing.
return if vm_name
# Or, return if there is no root path set or the dotfile doesn't
# exist, since we can't do anything in that case anyways.
return if !root_path || !File.file?(dotfile_path)
# First load the defaults (blank, noncreated VMs)
load_blank_vms!
# Empty out previously loaded vms
vms.clear
# If we have no dotfile, then return
return if !dotfile_path || !File.file?(dotfile_path)
# Open and parse the dotfile
File.open(dotfile_path) do |f|
data = { :__vagrant => f.read }
data = { DEFAULT_VM => f.read }
begin
data = JSON.parse(data[:__vagrant])
data = JSON.parse(data[DEFAULT_VM])
rescue JSON::ParserError
# Most likely an older (<= 0.3.x) dotfile. Try to load it
# as the :__vagrant VM.
@ -231,6 +231,20 @@ module Vagrant
# Just rescue it.
end
# Loads blank VMs into the `vms` attribute.
def load_blank_vms!
# Clear existing vms
vms.clear
# Load up the blank VMs
defined_vms = config.vm.defined_vms.keys
defined_vms = [DEFAULT_VM] if defined_vms.empty?
defined_vms.each do |name|
vms[name] = Vagrant::VM.new(:vm_name => name, :env => self)
end
end
# Loads the activelist for this environment
def load_active_list!
@active_list = ActiveList.new(self)

View File

@ -372,12 +372,20 @@ class EnvironmentTest < Test::Unit::TestCase
File.stubs(:file?).returns(true)
end
should "blank the VMs" do
load_seq = sequence("load_seq")
@env.stubs(:root_path).returns("foo")
@env.expects(:load_blank_vms!).in_sequence(load_seq)
File.expects(:open).in_sequence(load_seq)
@env.load_vm!
end
should "load the UUID if the JSON parsing fails" do
vm = mock("vm")
filemock = mock("filemock")
filemock.expects(:read).returns("foo")
Vagrant::VM.expects(:find).with("foo", @env, :__vagrant).returns(vm)
Vagrant::VM.expects(:find).with("foo", @env, Vagrant::Environment::DEFAULT_VM).returns(vm)
File.expects(:open).with(@env.dotfile_path).once.yields(filemock)
File.expects(:file?).with(@env.dotfile_path).once.returns(true)
@env.load_vm!
@ -413,10 +421,13 @@ class EnvironmentTest < Test::Unit::TestCase
@env.load_vm!
end
should "do nothing if the root path is nil" do
should "do nothing if the dotfile is nil" do
@env.stubs(:dotfile_path).returns(nil)
File.expects(:open).never
@env.stubs(:root_path).returns(nil)
@env.load_vm!
assert_nothing_raised {
@env.load_vm!
}
end
should "do nothing if dotfile is not a file" do
@ -432,6 +443,37 @@ class EnvironmentTest < Test::Unit::TestCase
end
end
context "loading blank VMs" do
setup do
@env = mock_environment
end
should "blank the VMs" do
@env = mock_environment do |config|
config.vm.define :foo do |config|
end
config.vm.define :bar do |config|
end
end
@env.load_blank_vms!
assert_equal 2, @env.vms.length
assert_equal [:foo, :bar], @env.vms.keys
assert(@env.vms.all? { |name, vm| !vm.created? })
end
should "load the default VM blank if no multi-VMs are specified" do
assert @env.config.vm.defined_vms.empty? # sanity
@env.load_blank_vms!
assert_equal 1, @env.vms.length
assert !@env.vms.values.first.created?
end
end
context "loading the active list" do
setup do
@env = mock_environment

View File

@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{vagrant}
s.version = "0.3.3.dev"
s.version = "0.3.4.dev"
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-01}
s.date = %q{2010-05-15}
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"]
@ -175,67 +175,67 @@ Gem::Specification.new do |s|
s.summary = %q{Vagrant is a tool for building and distributing virtualized development environments.}
s.test_files = [
"test/test_helper.rb",
"test/vagrant/actions/base_test.rb",
"test/vagrant/actions/box/add_test.rb",
"test/vagrant/actions/box/destroy_test.rb",
"test/vagrant/actions/box/download_test.rb",
"test/vagrant/actions/box/unpackage_test.rb",
"test/vagrant/actions/box/verify_test.rb",
"test/vagrant/actions/collection_test.rb",
"test/vagrant/actions/runner_test.rb",
"test/vagrant/actions/vm/boot_test.rb",
"test/vagrant/actions/vm/customize_test.rb",
"test/vagrant/actions/vm/destroy_test.rb",
"test/vagrant/actions/vm/down_test.rb",
"test/vagrant/actions/vm/export_test.rb",
"test/vagrant/actions/vm/forward_ports_test.rb",
"test/vagrant/actions/vm/halt_test.rb",
"test/vagrant/actions/vm/import_test.rb",
"test/vagrant/actions/vm/move_hard_drive_test.rb",
"test/vagrant/actions/vm/package_test.rb",
"test/vagrant/actions/vm/provision_test.rb",
"test/vagrant/actions/vm/reload_test.rb",
"test/vagrant/actions/vm/resume_test.rb",
"test/vagrant/actions/vm/shared_folders_test.rb",
"test/vagrant/actions/vm/start_test.rb",
"test/vagrant/actions/vm/suspend_test.rb",
"test/vagrant/actions/vm/up_test.rb",
"test/vagrant/active_list_test.rb",
"test/vagrant/vm_test.rb",
"test/vagrant/command_test.rb",
"test/vagrant/environment_test.rb",
"test/vagrant/util_test.rb",
"test/vagrant/box_test.rb",
"test/vagrant/busy_test.rb",
"test/vagrant/command_test.rb",
"test/vagrant/provisioners/base_test.rb",
"test/vagrant/provisioners/chef_test.rb",
"test/vagrant/provisioners/chef_server_test.rb",
"test/vagrant/provisioners/chef_solo_test.rb",
"test/vagrant/systems/linux_test.rb",
"test/vagrant/config_test.rb",
"test/vagrant/actions/base_test.rb",
"test/vagrant/actions/runner_test.rb",
"test/vagrant/actions/box/verify_test.rb",
"test/vagrant/actions/box/destroy_test.rb",
"test/vagrant/actions/box/add_test.rb",
"test/vagrant/actions/box/unpackage_test.rb",
"test/vagrant/actions/box/download_test.rb",
"test/vagrant/actions/collection_test.rb",
"test/vagrant/actions/vm/reload_test.rb",
"test/vagrant/actions/vm/suspend_test.rb",
"test/vagrant/actions/vm/boot_test.rb",
"test/vagrant/actions/vm/package_test.rb",
"test/vagrant/actions/vm/down_test.rb",
"test/vagrant/actions/vm/shared_folders_test.rb",
"test/vagrant/actions/vm/destroy_test.rb",
"test/vagrant/actions/vm/halt_test.rb",
"test/vagrant/actions/vm/import_test.rb",
"test/vagrant/actions/vm/customize_test.rb",
"test/vagrant/actions/vm/start_test.rb",
"test/vagrant/actions/vm/move_hard_drive_test.rb",
"test/vagrant/actions/vm/up_test.rb",
"test/vagrant/actions/vm/export_test.rb",
"test/vagrant/actions/vm/provision_test.rb",
"test/vagrant/actions/vm/resume_test.rb",
"test/vagrant/actions/vm/forward_ports_test.rb",
"test/vagrant/active_list_test.rb",
"test/vagrant/commands/base_test.rb",
"test/vagrant/commands/box/add_test.rb",
"test/vagrant/commands/box/list_test.rb",
"test/vagrant/commands/box/remove_test.rb",
"test/vagrant/commands/reload_test.rb",
"test/vagrant/commands/ssh_config_test.rb",
"test/vagrant/commands/suspend_test.rb",
"test/vagrant/commands/package_test.rb",
"test/vagrant/commands/status_test.rb",
"test/vagrant/commands/init_test.rb",
"test/vagrant/commands/destroy_test.rb",
"test/vagrant/commands/halt_test.rb",
"test/vagrant/commands/init_test.rb",
"test/vagrant/commands/package_test.rb",
"test/vagrant/commands/reload_test.rb",
"test/vagrant/commands/resume_test.rb",
"test/vagrant/commands/ssh_config_test.rb",
"test/vagrant/commands/ssh_test.rb",
"test/vagrant/commands/status_test.rb",
"test/vagrant/commands/suspend_test.rb",
"test/vagrant/commands/box/remove_test.rb",
"test/vagrant/commands/box/add_test.rb",
"test/vagrant/commands/box/list_test.rb",
"test/vagrant/commands/up_test.rb",
"test/vagrant/config_test.rb",
"test/vagrant/commands/resume_test.rb",
"test/vagrant/commands/ssh_test.rb",
"test/vagrant/downloaders/base_test.rb",
"test/vagrant/downloaders/file_test.rb",
"test/vagrant/downloaders/http_test.rb",
"test/vagrant/environment_test.rb",
"test/vagrant/provisioners/base_test.rb",
"test/vagrant/provisioners/chef_server_test.rb",
"test/vagrant/provisioners/chef_solo_test.rb",
"test/vagrant/provisioners/chef_test.rb",
"test/vagrant/ssh_test.rb",
"test/vagrant/systems/linux_test.rb",
"test/vagrant/util/progress_meter_test.rb",
"test/vagrant/util/stacked_proc_runner_test.rb",
"test/vagrant/util/progress_meter_test.rb",
"test/vagrant/util/template_renderer_test.rb",
"test/vagrant/util/translator_test.rb",
"test/vagrant/util_test.rb",
"test/vagrant/vm_test.rb"
"test/vagrant/ssh_test.rb"
]
if s.respond_to? :specification_version then