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
|
# Get the provider configuration from the final loaded configuration
|
||||||
provider_config = config.vm.providers[provider].config
|
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
|
# Create the machine and cache it for future calls. This will also
|
||||||
# return the machine from this method.
|
# return the machine from this method.
|
||||||
@machines[cache_key] = Machine.new(name, provider_cls, provider_config,
|
@machines[cache_key] = Machine.new(name, provider_cls, provider_config,
|
||||||
config, box, self)
|
config, machine_data_path, box, self)
|
||||||
end
|
end
|
||||||
|
|
||||||
# This returns a list of the configured machines for this environment.
|
# 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))
|
@global_data ||= DataStore.new(File.expand_path("global_data.json", home_path))
|
||||||
end
|
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)
|
# The root path is the path where the top-most (loaded last)
|
||||||
# Vagrantfile resides. It can be considered the project root for
|
# Vagrantfile resides. It can be considered the project root for
|
||||||
# this environment.
|
# this environment.
|
||||||
|
|
|
@ -15,6 +15,11 @@ module Vagrant
|
||||||
# @return [Object]
|
# @return [Object]
|
||||||
attr_reader :config
|
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.
|
# The environment that this machine is a part of.
|
||||||
#
|
#
|
||||||
# @return [Environment]
|
# @return [Environment]
|
||||||
|
@ -50,19 +55,22 @@ module Vagrant
|
||||||
# @param [Object] provider_config The provider-specific configuration for
|
# @param [Object] provider_config The provider-specific configuration for
|
||||||
# this machine.
|
# this machine.
|
||||||
# @param [Object] config The 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 [Box] box The box that is backing this virtual machine.
|
||||||
# @param [Environment] env The environment that this machine is a
|
# @param [Environment] env The environment that this machine is a
|
||||||
# part of.
|
# 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 = Log4r::Logger.new("vagrant::machine")
|
||||||
@logger.info("Initializing machine: #{name}")
|
@logger.info("Initializing machine: #{name}")
|
||||||
@logger.info(" - Provider: #{provider_cls}")
|
@logger.info(" - Provider: #{provider_cls}")
|
||||||
@logger.info(" - Box: #{box}")
|
@logger.info(" - Box: #{box}")
|
||||||
|
|
||||||
@box = box
|
@box = box
|
||||||
@config = config
|
@config = config
|
||||||
@env = env
|
@data_dir = data_dir
|
||||||
@name = name
|
@env = env
|
||||||
|
@name = name
|
||||||
@provider_config = provider_config
|
@provider_config = provider_config
|
||||||
|
|
||||||
# Read the ID, which is usually in local storage
|
# Read the ID, which is usually in local storage
|
||||||
|
@ -72,7 +80,10 @@ module Vagrant
|
||||||
if base
|
if base
|
||||||
@id = name
|
@id = name
|
||||||
else
|
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
|
end
|
||||||
|
|
||||||
# Initializes the provider last so that it has access to all the
|
# Initializes the provider last so that it has access to all the
|
||||||
|
@ -176,20 +187,20 @@ module Vagrant
|
||||||
#
|
#
|
||||||
# @param [String] value The ID.
|
# @param [String] value The ID.
|
||||||
def id=(value)
|
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
|
if value
|
||||||
# Set the value
|
# Write the "id" file with the id given.
|
||||||
@env.local_data[:active][@name] = value
|
id_file.open("w+") do |f|
|
||||||
|
f.write(value)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
# Delete it from the active hash
|
# Delete the file, since the machine is now destroyed
|
||||||
@env.local_data[:active].delete(@name)
|
id_file.delete
|
||||||
end
|
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
|
# Store the ID locally
|
||||||
@id = value
|
@id = value
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require "pathname"
|
||||||
|
|
||||||
require File.expand_path("../../base", __FILE__)
|
require File.expand_path("../../base", __FILE__)
|
||||||
|
|
||||||
describe Vagrant::Machine do
|
describe Vagrant::Machine do
|
||||||
|
@ -13,6 +15,7 @@ describe Vagrant::Machine do
|
||||||
let(:provider_config) { Object.new }
|
let(:provider_config) { Object.new }
|
||||||
let(:box) { Object.new }
|
let(:box) { Object.new }
|
||||||
let(:config) { env.config_global }
|
let(:config) { env.config_global }
|
||||||
|
let(:data_dir) { Pathname.new(Tempdir.new.path) }
|
||||||
let(:env) do
|
let(:env) do
|
||||||
# We need to create a Vagrantfile so that this test environment
|
# We need to create a Vagrantfile so that this test environment
|
||||||
# has a proper root path
|
# has a proper root path
|
||||||
|
@ -28,7 +31,8 @@ describe Vagrant::Machine do
|
||||||
|
|
||||||
# Returns a new instance with the test data
|
# Returns a new instance with the test data
|
||||||
def new_instance
|
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
|
end
|
||||||
|
|
||||||
describe "initialization" do
|
describe "initialization" do
|
||||||
|
@ -57,7 +61,8 @@ describe Vagrant::Machine do
|
||||||
|
|
||||||
# Initialize a new machine and verify that we properly receive
|
# Initialize a new machine and verify that we properly receive
|
||||||
# the machine we expect.
|
# 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)
|
received_machine.should eql(instance)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue