Remove the ActiveList. This will make a comeback using DataStore in the future.

This commit is contained in:
Mitchell Hashimoto 2010-09-02 13:00:47 -07:00
parent d74d95d0cd
commit 2d80c32479
5 changed files with 20 additions and 318 deletions

View File

@ -1,84 +0,0 @@
module Vagrant
# This class represents the active list of vagrant virtual
# machines.
class ActiveList
FILENAME = "active.json"
@@list = nil
# The environment this active list belongs to
attr_accessor :env
# Creates the instance of the ActiveList, with the given environment
# if specified
def initialize(env=nil)
@list = nil
@env = env
end
# Parses and returns the list of UUIDs from the active VM
# JSON file. This will cache the result, which can be reloaded
# by setting the `reload` parameter to true.
#
# @return [Array<String>]
def list(reload=false)
return @list unless @list.nil? || reload
@list ||= {}
return @list unless File.file?(path)
File.open(path, "r") do |f|
begin
@list = JSON.parse(f.read)
rescue Exception
@list = {}
end
# This forces earlier versions of Vagrant to use the new hash
# format. Clearing out the old data isn't a big deal since it
# was never used.
@list = {} unless @list.is_a?(Hash)
end
@list
end
# Returns an array of UUIDs filtered so each is verified to exist.
def filter_list
list.each do |uuid, data|
list.delete(uuid) unless Vagrant::VM.find(uuid, env)
end
list
end
# Adds a virtual environment to the list of active virtual machines
def add(vm)
list[vm.uuid] = {
:path => env.root_path,
:created_at => Time.now.to_i
}
save
end
# Remove a virtual environment from the list of active virtual machines
def remove(vm)
vm = vm.uuid if vm.is_a?(Vagrant::VM)
list.delete(vm)
save
end
# Persists the list down to the JSON file.
def save
File.open(path, "w+") do |f|
f.write(filter_list.to_json)
end
end
# Returns the path to the JSON file which holds the UUIDs of the
# active virtual machines managed by Vagrant.
def path
File.join(env.home_path, FILENAME)
end
end
end

View File

@ -19,8 +19,6 @@ module Vagrant
attr_reader :host attr_reader :host
attr_reader :box attr_reader :box
attr_accessor :vm attr_accessor :vm
attr_reader :active_list
attr_reader :logger
attr_reader :actions attr_reader :actions
attr_writer :ui attr_writer :ui
@ -159,6 +157,13 @@ module Vagrant
@local_data ||= DataStore.new(dotfile_path) @local_data ||= DataStore.new(dotfile_path)
end end
# Accesses the logger for Vagrant. This logger is a _detailed_
# logger which should be used to log internals only. For outward
# facing information, use {#ui}.
def logger
@logger ||= Util::ResourceLogger.new(resource, self)
end
#--------------------------------------------------------------- #---------------------------------------------------------------
# Load Methods # Load Methods
#--------------------------------------------------------------- #---------------------------------------------------------------
@ -167,7 +172,6 @@ module Vagrant
# such as `vm`, `config`, etc. on this environment. The order this # such as `vm`, `config`, etc. on this environment. The order this
# method calls its other methods is very particular. # method calls its other methods is very particular.
def load! def load!
load_logger!
load_root_path! load_root_path!
load_config! load_config!
load_home_directory! load_home_directory!
@ -176,7 +180,6 @@ module Vagrant
load_config! load_config!
self.class.check_virtualbox! self.class.check_virtualbox!
load_vm! load_vm!
load_active_list!
load_actions! load_actions!
self self
end end
@ -241,17 +244,7 @@ module Vagrant
@config = Config.execute! @config = Config.execute!
# (re)load the logger # (re)load the logger
load_logger! @logger = nil
end
# Loads the logger for this environment. This is called by
# {#load_config!} so that the logger is only loaded after
# configuration information is available. The logger is also
# loaded early on in the load chain process so that the various
# references to logger won't throw nil exceptions, but by default
# the logger will just send the log data to a black hole.
def load_logger!
@logger = Util::ResourceLogger.new(resource, self)
end end
# Loads the home directory path and creates the necessary subdirectories # Loads the home directory path and creates the necessary subdirectories
@ -329,11 +322,6 @@ module Vagrant
end end
end end
# Loads the activelist for this environment
def load_active_list!
@active_list = ActiveList.new(self)
end
# Loads the instance of {Action} for this environment. This allows # Loads the instance of {Action} for this environment. This allows
# users of the instance to run action sequences in the context of # users of the instance to run action sequences in the context of
# this environment. # this environment.
@ -364,9 +352,6 @@ module Vagrant
f.write(data.to_json) f.write(data.to_json)
end end
end end
# Also add to the global store
# active_list.add(vm)
end end
end end
end end

View File

@ -73,7 +73,6 @@ class Test::Unit::TestCase
# Setup the logger. We create it then reset it so that subsequent # Setup the logger. We create it then reset it so that subsequent
# calls will recreate it for us. # calls will recreate it for us.
environment.load_logger!
environment.logger.class.reset_singleton_logger! environment.logger.class.reset_singleton_logger!
environment.logger.stubs(:flush_progress) environment.logger.stubs(:flush_progress)
environment.logger.stubs(:cl_reset).returns("") environment.logger.stubs(:cl_reset).returns("")

View File

@ -1,173 +0,0 @@
require "test_helper"
class ActiveListTest < Test::Unit::TestCase
setup do
@env = mock_environment
@list = Vagrant::ActiveList.new(@env)
end
context "initializing" do
should "set the environment to nil if not specified" do
assert_nothing_raised {
list = Vagrant::ActiveList.new
assert list.env.nil?
}
end
should "set the environment to the given parameter if specified" do
env = mock("env")
list = Vagrant::ActiveList.new(env)
assert_equal env, list.env
end
end
context "listing" do
setup do
@path = "foo"
@list.stubs(:path).returns(@path)
end
should "load if reload is given" do
File.stubs(:file?).returns(true)
File.expects(:open).once
@list.list(true)
end
should "not load if the active json file doesn't exist" do
File.expects(:file?).with(@list.path).returns(false)
File.expects(:open).never
assert_equal Hash.new, @list.list(true)
end
should "parse the JSON by reading the file" do
file = mock("file")
data = mock("data")
result = { :hey => :yep }
File.expects(:file?).returns(true)
File.expects(:open).with(@list.path, 'r').once.yields(file)
file.expects(:read).returns(data)
JSON.expects(:parse).with(data).returns(result)
assert_equal result, @list.list(true)
end
should "not load if reload flag is false and already loaded" do
File.expects(:file?).once.returns(false)
result = @list.list(true)
assert result.equal?(@list.list)
assert result.equal?(@list.list)
assert result.equal?(@list.list)
end
should "be an empty hash if JSON parsing raises an exception" do
file = mock("file")
file.stubs(:read)
File.expects(:file?).returns(true)
File.expects(:open).with(@list.path, 'r').once.yields(file)
JSON.expects(:parse).raises(Exception)
assert_nothing_raised do
assert_equal Hash.new, @list.list(true)
end
end
end
context "filter list" do
should "remove nonexistent VMs" do
list = {}
result = {}
5.times do |i|
vm = mock("vm#{i}")
vm.stubs(:uuid).returns(i)
list[vm.uuid] = {}
found_vm = i % 2 ? nil : vm
Vagrant::VM.stubs(:find).with(vm.uuid, @env).returns(found_vm)
results[vm.uuid] = {} if found_vm
end
@list.stubs(:list).returns(list)
assert_equal result, @list.filter_list
end
end
context "adding a VM to the list" do
setup do
@the_list = {}
@list.stubs(:list).returns(@the_list)
@list.stubs(:save)
@uuid = "foo"
@vm = mock("vm")
@vm.stubs(:uuid).returns(@uuid)
end
should "add the VMs UUID to the list" do
@list.add(@vm)
assert @the_list[@uuid]
assert @the_list[@uuid].is_a?(Hash)
end
should "save after adding" do
save_seq = sequence('save')
@the_list.expects(:[]=).in_sequence(save_seq)
@list.expects(:save).in_sequence(save_seq)
@list.add(@vm)
end
end
context "deleting a VM from the list" do
setup do
@the_list = ["bar"]
@list.stubs(:list).returns(@the_list)
@list.stubs(:save)
@uuid = "bar"
@vm = mock("vm")
@vm.stubs(:uuid).returns(@uuid)
@vm.stubs(:is_a?).with(Vagrant::VM).returns(true)
end
should "delete the uuid from the list of a VM" do
@list.remove(@vm)
assert @the_list.empty?
end
should "delete just the string if a string is given" do
@the_list << "zoo"
@list.remove("zoo")
assert !@the_list.include?("zoo")
end
should "save after removing" do
save_seq = sequence('save')
@the_list.expects(:delete).in_sequence(save_seq)
@list.expects(:save).in_sequence(save_seq)
@list.remove(@vm)
end
end
context "saving" do
setup do
@filtered = ["zoo"]
@list.stubs(:filter_list).returns(@filtered)
end
should "open the JSON path and save to it" do
file = mock("file")
File.expects(:open).with(@list.path, "w+").yields(file)
file.expects(:write).with(@filtered.to_json)
@list.save
end
end
context "path" do
setup do
@env.stubs(:home_path).returns("foo")
end
should "return the active file within the home path" do
assert_equal File.join(@env.home_path, Vagrant::ActiveList::FILENAME), @list.path
end
end
end

View File

@ -237,6 +237,17 @@ class EnvironmentTest < Test::Unit::TestCase
end end
end end
context "loading logger" do
should "lazy load the logger only once" do
result = Vagrant::Util::ResourceLogger.new("vagrant", mock_environment)
Vagrant::Util::ResourceLogger.expects(:new).returns(result).once
@env = mock_environment
assert_equal result, @env.logger
assert_equal result, @env.logger
assert_equal result, @env.logger
end
end
context "loading" do context "loading" do
setup do setup do
@env = mock_environment @env = mock_environment
@ -245,7 +256,6 @@ class EnvironmentTest < Test::Unit::TestCase
context "overall load method" do context "overall load method" do
should "load! should call proper sequence and return itself" do should "load! should call proper sequence and return itself" do
call_seq = sequence("call_sequence") call_seq = sequence("call_sequence")
@env.expects(:load_logger!).once.in_sequence(call_seq)
@env.expects(:load_root_path!).once.in_sequence(call_seq) @env.expects(:load_root_path!).once.in_sequence(call_seq)
@env.expects(:load_config!).once.in_sequence(call_seq) @env.expects(:load_config!).once.in_sequence(call_seq)
@env.expects(:load_home_directory!).once.in_sequence(call_seq) @env.expects(:load_home_directory!).once.in_sequence(call_seq)
@ -254,7 +264,6 @@ class EnvironmentTest < Test::Unit::TestCase
@env.expects(:load_config!).once.in_sequence(call_seq) @env.expects(:load_config!).once.in_sequence(call_seq)
@klass.expects(:check_virtualbox!).once.in_sequence(call_seq) @klass.expects(:check_virtualbox!).once.in_sequence(call_seq)
@env.expects(:load_vm!).once.in_sequence(call_seq) @env.expects(:load_vm!).once.in_sequence(call_seq)
@env.expects(:load_active_list!).once.in_sequence(call_seq)
@env.expects(:load_actions!).once.in_sequence(call_seq) @env.expects(:load_actions!).once.in_sequence(call_seq)
assert_equal @env, @env.load! assert_equal @env, @env.load!
end end
@ -329,7 +338,6 @@ class EnvironmentTest < Test::Unit::TestCase
@home_path = "/bar" @home_path = "/bar"
@env.stubs(:root_path).returns(@root_path) @env.stubs(:root_path).returns(@root_path)
@env.stubs(:home_path).returns(@home_path) @env.stubs(:home_path).returns(@home_path)
@env.stubs(:load_logger!)
@parent_env = mock_environment @parent_env = mock_environment
@ -425,28 +433,8 @@ class EnvironmentTest < Test::Unit::TestCase
should "reload the logger after executing" do should "reload the logger after executing" do
load_seq = sequence("load_seq") load_seq = sequence("load_seq")
Vagrant::Config.expects(:execute!).once.returns(nil).in_sequence(load_seq) Vagrant::Config.expects(:execute!).once.returns(nil).in_sequence(load_seq)
@env.expects(:load_logger!).once.in_sequence(load_seq)
@env.load_config! @env.load_config!
end assert @env.instance_variable_get(:@logger).nil?
end
context "loading logger" do
setup do
@env = mock_environment
@env.stubs(:vm_name).returns(nil)
end
should "use 'vagrant' by default" do
assert @env.vm_name.nil? # sanity
@env.load_logger!
assert_equal "vagrant", @env.logger.resource
end
should "use the vm name if available" do
name = "foo"
@env.stubs(:vm_name).returns(name)
@env.load_logger!
assert_equal name, @env.logger.resource
end end
end end
@ -632,19 +620,6 @@ class EnvironmentTest < Test::Unit::TestCase
end end
end end
context "loading the active list" do
setup do
@env = mock_environment
end
should "initialize the ActiveList object with the given environment" do
active_list = mock("active_list")
Vagrant::ActiveList.expects(:new).with(@env).returns(active_list)
@env.load_active_list!
assert_equal active_list, @env.active_list
end
end
context "loading actions" do context "loading actions" do
setup do setup do
@env = mock_environment @env = mock_environment