Remove Vagrant::DataStore
We just don't use it yet and the old implementation was sketchy. I was not happy with it.
This commit is contained in:
parent
b15a6dee0e
commit
c0c3e7bf43
|
@ -67,7 +67,6 @@ module Vagrant
|
|||
autoload :CLI, 'vagrant/cli'
|
||||
autoload :Command, 'vagrant/command'
|
||||
autoload :Config, 'vagrant/config'
|
||||
autoload :DataStore, 'vagrant/data_store'
|
||||
autoload :Downloaders, 'vagrant/downloaders'
|
||||
autoload :Driver, 'vagrant/driver'
|
||||
autoload :Easy, 'vagrant/easy'
|
||||
|
|
|
@ -1,92 +0,0 @@
|
|||
require 'pathname'
|
||||
|
||||
require 'log4r'
|
||||
|
||||
module Vagrant
|
||||
# The Vagrant data store is a key-value store which is persisted
|
||||
# as JSON in a local file which is specified in the initializer.
|
||||
# The data store itself is accessed via typical hash accessors: `[]`
|
||||
# and `[]=`. If a key is set to `nil`, then it is removed from the
|
||||
# datastore. The data store is only updated on disk when {#commit}
|
||||
# is called on the data store itself.
|
||||
#
|
||||
# The data store is a hash with indifferent access, meaning that
|
||||
# while all keys are persisted as strings in the JSON, you can access
|
||||
# them back as either symbols or strings. Note that this is only true
|
||||
# for the top-level data store. As soon as you set a hash inside the
|
||||
# data store, unless you explicitly use a {Util::HashWithIndifferentAccess},
|
||||
# it will be a regular hash.
|
||||
class DataStore < Util::HashWithIndifferentAccess
|
||||
attr_reader :file_path
|
||||
|
||||
def initialize(file_path)
|
||||
@logger = Log4r::Logger.new("vagrant::datastore")
|
||||
|
||||
if file_path
|
||||
@logger.info("Created: #{file_path}")
|
||||
|
||||
@file_path = Pathname.new(file_path)
|
||||
if @file_path.exist?
|
||||
raise Errors::DotfileIsDirectory if @file_path.directory?
|
||||
|
||||
begin
|
||||
merge!(JSON.parse(@file_path.read))
|
||||
rescue JSON::ParserError
|
||||
# Ignore if the data is invalid in the file.
|
||||
@logger.error("Data store contained invalid JSON. Ignoring.")
|
||||
end
|
||||
end
|
||||
else
|
||||
@logger.info("No file path. In-memory data store.")
|
||||
@file_path = nil
|
||||
end
|
||||
end
|
||||
|
||||
# Commits any changes to the data to disk. Even if the data
|
||||
# hasn't changed, it will be reserialized and written to disk.
|
||||
def commit
|
||||
if !@file_path
|
||||
raise StandardError, "In-memory data stores can't be committed."
|
||||
end
|
||||
|
||||
clean_nil_and_empties
|
||||
|
||||
if empty?
|
||||
# Delete the file since an empty data store is not useful
|
||||
@logger.info("Deleting data store since we're empty: #{@file_path}")
|
||||
@file_path.delete if @file_path.exist?
|
||||
else
|
||||
@logger.info("Committing data to data store: #{@file_path}")
|
||||
|
||||
@file_path.open("w") do |f|
|
||||
f.write(to_json)
|
||||
f.fsync
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# Removes the "nil" and "empty?" values from the hash (children
|
||||
# included) so that the final output JSON is cleaner.
|
||||
def clean_nil_and_empties(hash=self)
|
||||
# Clean depth first
|
||||
hash.each do |k, v|
|
||||
clean_nil_and_empties(v) if v.is_a?(Hash)
|
||||
end
|
||||
|
||||
# Clean ourselves, knowing that any children have already been
|
||||
# cleaned up
|
||||
bad_keys = hash.inject([]) do |acc, data|
|
||||
k,v = data
|
||||
acc.push(k) if v.nil?
|
||||
acc.push(k) if v.respond_to?(:empty?) && v.empty?
|
||||
acc
|
||||
end
|
||||
|
||||
bad_keys.each do |key|
|
||||
hash.delete(key)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -311,17 +311,6 @@ module Vagrant
|
|||
end
|
||||
end
|
||||
|
||||
# Loads on initial access and reads data from the global data store.
|
||||
# The global data store is global to Vagrant everywhere (in every environment),
|
||||
# so it can be used to store system-wide information. Note that "system-wide"
|
||||
# typically means "for this user" since the location of the global data
|
||||
# store is in the home directory.
|
||||
#
|
||||
# @return [DataStore]
|
||||
def global_data
|
||||
@global_data ||= DataStore.new(File.expand_path("global_data.json", home_path))
|
||||
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.
|
||||
|
|
|
@ -1,79 +0,0 @@
|
|||
require File.expand_path("../../base", __FILE__)
|
||||
|
||||
require 'pathname'
|
||||
|
||||
describe Vagrant::DataStore do
|
||||
include_context "unit"
|
||||
|
||||
let(:db_file) do
|
||||
# We create a tempfile and force an explicit close/unlink
|
||||
# but save the path so that we can re-use it multiple times
|
||||
temp = Tempfile.new("vagrant")
|
||||
result = Pathname.new(temp.path)
|
||||
temp.close
|
||||
temp.unlink
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
let(:instance) { described_class.new(db_file) }
|
||||
|
||||
it "initializes a new DB file" do
|
||||
instance[:data] = true
|
||||
instance.commit
|
||||
instance[:data].should == true
|
||||
|
||||
test = described_class.new(db_file)
|
||||
test[:data].should == true
|
||||
end
|
||||
|
||||
it "initializes empty if the file contains invalid data" do
|
||||
db_file.open("w+") { |f| f.write("NOPE!") }
|
||||
described_class.new(db_file).should be_empty
|
||||
end
|
||||
|
||||
it "initializes empty if the file doesn't exist" do
|
||||
described_class.new("NOPENOPENOPENOPENPEPEPEPE").should be_empty
|
||||
end
|
||||
|
||||
it "raises an error if the path given is a directory" do
|
||||
db_file.delete if db_file.exist?
|
||||
db_file.mkdir
|
||||
|
||||
expect { described_class.new(db_file) }.
|
||||
to raise_error(Vagrant::Errors::DotfileIsDirectory)
|
||||
end
|
||||
|
||||
it "cleans nil and empties when committing" do
|
||||
instance[:data] = { :bar => nil }
|
||||
instance[:another] = {}
|
||||
instance.commit
|
||||
|
||||
# The instance is now empty because the data was nil
|
||||
instance.should be_empty
|
||||
end
|
||||
|
||||
it "deletes the data file if the store is empty when saving" do
|
||||
instance[:data] = true
|
||||
instance.commit
|
||||
|
||||
another = described_class.new(db_file)
|
||||
another[:data] = nil
|
||||
another.commit
|
||||
|
||||
# The file should no longer exist
|
||||
db_file.should_not be_exist
|
||||
end
|
||||
|
||||
it "works if the DB file is nil" do
|
||||
store = described_class.new(nil)
|
||||
store[:foo] = "bar"
|
||||
store[:foo].should == "bar"
|
||||
end
|
||||
|
||||
it "throws an exception if attempting to commit a data store with no file" do
|
||||
store = described_class.new(nil)
|
||||
expect { store.commit }.
|
||||
to raise_error(StandardError)
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue