Store machine ID in "id" file in data directory.
Instead of storing an "active" hash in the local_data of an Environment, we now place the ID of a machine in the "id" file of the machine data directory. This file is read upon re-instantiation in order to load the proper state.
This commit is contained in:
parent
d18edc3ce5
commit
3baa31460f
|
@ -223,10 +223,15 @@ module Vagrant
|
|||
# Get the provider configuration from the final loaded configuration
|
||||
provider_config = config.vm.providers[provider].config
|
||||
|
||||
# Determine the machine data directory and pass it to the machine.
|
||||
# XXX: Permissions error here.
|
||||
machine_data_path = @local_data_path.join("machines/#{name}/#{provider}")
|
||||
FileUtils.mkdir_p(machine_data_path)
|
||||
|
||||
# Create the machine and cache it for future calls. This will also
|
||||
# return the machine from this method.
|
||||
@machines[cache_key] = Machine.new(name, provider_cls, provider_config,
|
||||
config, box, self)
|
||||
config, machine_data_path, box, self)
|
||||
end
|
||||
|
||||
# This returns a list of the configured machines for this environment.
|
||||
|
@ -317,16 +322,6 @@ module Vagrant
|
|||
@global_data ||= DataStore.new(File.expand_path("global_data.json", home_path))
|
||||
end
|
||||
|
||||
# Loads (on initial access) and reads data from the local data
|
||||
# store. This file is always at the root path as the file "~/.vagrant"
|
||||
# and contains a JSON dump of a hash. See {DataStore} for more
|
||||
# information.
|
||||
#
|
||||
# @return [DataStore]
|
||||
def local_data
|
||||
@local_data ||= DataStore.new(@local_data_path.join("environment_data"))
|
||||
end
|
||||
|
||||
# The root path is the path where the top-most (loaded last)
|
||||
# Vagrantfile resides. It can be considered the project root for
|
||||
# this environment.
|
||||
|
|
|
@ -15,6 +15,11 @@ module Vagrant
|
|||
# @return [Object]
|
||||
attr_reader :config
|
||||
|
||||
# Directory where machine-specific data can be stored.
|
||||
#
|
||||
# @return [Pathname]
|
||||
attr_reader :data_dir
|
||||
|
||||
# The environment that this machine is a part of.
|
||||
#
|
||||
# @return [Environment]
|
||||
|
@ -50,19 +55,22 @@ module Vagrant
|
|||
# @param [Object] provider_config The provider-specific configuration for
|
||||
# this machine.
|
||||
# @param [Object] config The configuration for this machine.
|
||||
# @param [Pathname] data_dir The directory where machine-specific data
|
||||
# can be stored. This directory is ensured to exist.
|
||||
# @param [Box] box The box that is backing this virtual machine.
|
||||
# @param [Environment] env The environment that this machine is a
|
||||
# part of.
|
||||
def initialize(name, provider_cls, provider_config, config, box, env, base=false)
|
||||
def initialize(name, provider_cls, provider_config, config, data_dir, box, env, base=false)
|
||||
@logger = Log4r::Logger.new("vagrant::machine")
|
||||
@logger.info("Initializing machine: #{name}")
|
||||
@logger.info(" - Provider: #{provider_cls}")
|
||||
@logger.info(" - Box: #{box}")
|
||||
|
||||
@box = box
|
||||
@config = config
|
||||
@env = env
|
||||
@name = name
|
||||
@box = box
|
||||
@config = config
|
||||
@data_dir = data_dir
|
||||
@env = env
|
||||
@name = name
|
||||
@provider_config = provider_config
|
||||
|
||||
# Read the ID, which is usually in local storage
|
||||
|
@ -72,7 +80,10 @@ module Vagrant
|
|||
if base
|
||||
@id = name
|
||||
else
|
||||
@id = @env.local_data[:active][@name.to_s] if @env.local_data[:active]
|
||||
# Read the id file from the data directory if it exists as the
|
||||
# ID for the pre-existing physical representation of this machine.
|
||||
id_file = @data_dir.join("id")
|
||||
@id = id_file.read if id_file.file?
|
||||
end
|
||||
|
||||
# Initializes the provider last so that it has access to all the
|
||||
|
@ -176,20 +187,20 @@ module Vagrant
|
|||
#
|
||||
# @param [String] value The ID.
|
||||
def id=(value)
|
||||
@env.local_data[:active] ||= {}
|
||||
# The file that will store the id if we have one. This allows the
|
||||
# ID to persist across Vagrant runs.
|
||||
id_file = @data_dir.join("id")
|
||||
|
||||
if value
|
||||
# Set the value
|
||||
@env.local_data[:active][@name] = value
|
||||
# Write the "id" file with the id given.
|
||||
id_file.open("w+") do |f|
|
||||
f.write(value)
|
||||
end
|
||||
else
|
||||
# Delete it from the active hash
|
||||
@env.local_data[:active].delete(@name)
|
||||
# Delete the file, since the machine is now destroyed
|
||||
id_file.delete
|
||||
end
|
||||
|
||||
# Commit the local data so that the next time Vagrant is initialized,
|
||||
# it realizes the VM exists (or doesn't).
|
||||
@env.local_data.commit
|
||||
|
||||
# Store the ID locally
|
||||
@id = value
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require "pathname"
|
||||
|
||||
require File.expand_path("../../base", __FILE__)
|
||||
|
||||
describe Vagrant::Machine do
|
||||
|
@ -13,6 +15,7 @@ describe Vagrant::Machine do
|
|||
let(:provider_config) { Object.new }
|
||||
let(:box) { Object.new }
|
||||
let(:config) { env.config_global }
|
||||
let(:data_dir) { Pathname.new(Tempdir.new.path) }
|
||||
let(:env) do
|
||||
# We need to create a Vagrantfile so that this test environment
|
||||
# has a proper root path
|
||||
|
@ -28,7 +31,8 @@ describe Vagrant::Machine do
|
|||
|
||||
# Returns a new instance with the test data
|
||||
def new_instance
|
||||
described_class.new(name, provider_cls, provider_config, config, box, env)
|
||||
described_class.new(name, provider_cls, provider_config,
|
||||
config, data_dir, box, env)
|
||||
end
|
||||
|
||||
describe "initialization" do
|
||||
|
@ -57,7 +61,8 @@ describe Vagrant::Machine do
|
|||
|
||||
# Initialize a new machine and verify that we properly receive
|
||||
# the machine we expect.
|
||||
instance = described_class.new(name, provider_cls, provider_config, config, box, env)
|
||||
instance = described_class.new(name, provider_cls, provider_config,
|
||||
config, data_dir, box, env)
|
||||
received_machine.should eql(instance)
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue