diff options
author | Adam Jacob <adam@hjksolutions.com> | 2009-01-06 20:56:13 -0800 |
---|---|---|
committer | Adam Jacob <adam@hjksolutions.com> | 2009-01-06 20:56:13 -0800 |
commit | 9a0c67615947fab5156ec51927dda9bf300aeea6 (patch) | |
tree | 62dbfa0c5f323cb85e5aa73dc0562d7a995cf681 | |
parent | c651822bcb495d7cc59ab5db6da68436bdcad612 (diff) | |
download | chef-9a0c67615947fab5156ec51927dda9bf300aeea6.tar.gz |
Removing the use of the FileStore altogether - only using the FileCache, as it's more straightforward
-rw-r--r-- | chef/lib/chef/client.rb | 5 | ||||
-rw-r--r-- | chef/lib/chef/file_store.rb | 135 | ||||
-rw-r--r-- | chef/spec/unit/file_store_spec.rb | 105 |
3 files changed, 2 insertions, 243 deletions
diff --git a/chef/lib/chef/client.rb b/chef/lib/chef/client.rb index 16bb1f3069..68ee04eaeb 100644 --- a/chef/lib/chef/client.rb +++ b/chef/lib/chef/client.rb @@ -163,8 +163,7 @@ class Chef end if @registration - reg = Chef::FileStore.load("registration", @safe_name) - @secret = reg["secret"] + @secret = Chef::FileCache.load(File.join("registration", @safe_name)) else create_registration end @@ -178,7 +177,7 @@ class Chef # true:: Always returns true def create_registration @secret = random_password(500) - Chef::FileStore.store("registration", @safe_name, { "secret" => @secret }) + Chef::FileCache.store(File.join("registration", @safe_name), @secret) @rest.post_rest("registrations", { :id => @safe_name, :password => @secret, :validation_token => @validation_token }) true end diff --git a/chef/lib/chef/file_store.rb b/chef/lib/chef/file_store.rb deleted file mode 100644 index 7e8f89f946..0000000000 --- a/chef/lib/chef/file_store.rb +++ /dev/null @@ -1,135 +0,0 @@ -# -# Author:: Adam Jacob (<adam@opscode.com>) -# Copyright:: Copyright (c) 2008 OpsCode, Inc. -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -require File.join(File.dirname(__FILE__), "mixin", "params_validate") -require File.join(File.dirname(__FILE__), "mixin", "create_path") -require 'digest/sha2' -require 'json' - -class Chef - class FileStore - class << self - include Chef::Mixin::ParamsValidate - include Chef::Mixin::CreatePath - - def store(obj_type, name, object) - validate( - { - :obj_type => obj_type, - :name => name, - :object => object, - }, - { - :object => { :respond_to => :to_json }, - } - ) - - store_path = create_store_path(obj_type, name) - io = File.open(store_path, "w") - io.puts object.to_json - io.close - end - - def load(obj_type, name) - validate( - { - :obj_type => obj_type, - :name => name, - }, - { - :obj_type => { :kind_of => String }, - :name => { :kind_of => String }, - } - ) - store_path = create_store_path(obj_type, name) - raise "Cannot find #{store_path} for #{obj_type} #{name}!" unless File.exists?(store_path) - object = JSON.parse(IO.read(store_path)) - end - - def delete(obj_type, name) - validate( - { - :obj_type => obj_type, - :name => name, - }, - { - :obj_type => { :kind_of => String }, - :name => { :kind_of => String }, - } - ) - store_path = create_store_path(obj_type, name) - if File.exists?(store_path) - File.unlink(store_path) - end - end - - def list(obj_type, inflate=false) - validate( - { - :obj_type => obj_type, - }, - { - :obj_type => { :kind_of => String } - } - ) - keys = Array.new - Dir[File.join(Chef::Config[:file_store_path], obj_type, '**', '*')].each do |f| - if File.file?(f) - if inflate - keys << load(obj_type, File.basename(f)) - else - keys << File.basename(f) - end - end - end - keys - end - - def has_key?(obj_type, name) - validate( - { - :obj_type => obj_type, - :name => name, - }, - { - :obj_type => { :kind_of => String }, - :name => { :kind_of => String }, - } - ) - Dir[File.join(Chef::Config[:file_store_path], obj_type, '**', '*')].each do |f| - if File.file?(f) - return true if File.basename(f) == name - end - end - return false - end - - def create_store_path(obj_type, key) - shadigest = Digest::SHA2.hexdigest("#{obj_type}#{key}") - - file_path = [ - Chef::Config[:file_store_path], - obj_type, - shadigest[0,1], - shadigest[1,3] - ] - File.join(create_path(file_path), key) - end - - end - end -end
\ No newline at end of file diff --git a/chef/spec/unit/file_store_spec.rb b/chef/spec/unit/file_store_spec.rb deleted file mode 100644 index 25cd8d3935..0000000000 --- a/chef/spec/unit/file_store_spec.rb +++ /dev/null @@ -1,105 +0,0 @@ -# -# Author:: Adam Jacob (<adam@opscode.com>) -# Copyright:: Copyright (c) 2008 OpsCode, Inc. -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper")) - -class Fakestore - attr_accessor :name - - def to_json(*a) - { :name => @name }.to_json(*a) - end - - def self.json_create(o) - new_fakestore = new - new_fakestore.name = o[:name] - new_fakestore - end -end - -describe Chef::FileStore do - before(:each) do - Chef::Config[:file_store_path] = "/tmp/chef-test" - @fakestore = Fakestore.new - @fakestore.name = "Landslide" - @fakestore_digest = "a56a428bddac69e505731708ba206da0bb75e8de883bb4d5ef6be9b327da556a" - end - - it "should return a path to a file given a type and key" do - Dir.stub!(:mkdir).and_return(true) - File.stub!(:directory?).and_return(true) - path = Chef::FileStore.create_store_path("fakestore", @fakestore.name) - path.should eql("/tmp/chef-test/fakestore/a/56a/Landslide") - end - - it "should create directories for the path if needed" do - File.stub!(:directory?).and_return(false) - Dir.should_receive(:mkdir).exactly(4).times.and_return(true) - Chef::FileStore.create_store_path("fakestore", @fakestore.name) - end - - it "should store an object with a type and key" do - Chef::FileStore.should_receive(:create_store_path).with("fakestore", @fakestore.name).and_return("/monkey") - File.stub!(:directory?).and_return(true) - ioobj = mock("IO", :null_object => true) - ioobj.should_receive(:puts).with(@fakestore.to_json) - ioobj.should_receive(:close).once.and_return(true) - File.should_receive(:open).with("/monkey", "w").and_return(ioobj) - Chef::FileStore.store("fakestore", @fakestore.name, @fakestore) - end - - it "should load an object from the store with type and key" do - Chef::FileStore.should_receive(:create_store_path).with("fakestore", @fakestore.name).and_return("/monkey") - File.stub!(:exists?).and_return(true) - IO.should_receive(:read).once.and_return(true) - JSON.should_receive(:parse).and_return(true) - Chef::FileStore.load("fakestore", @fakestore.name) - end - - it "should through an exception if it cannot load a file from the store" do - Chef::FileStore.should_receive(:create_store_path).and_return("/tmp") - File.stub!(:exists?).and_return(false) - lambda { Chef::FileStore.load("fakestore", @fakestore.name) }.should raise_error(RuntimeError) - end - - it "should delete a file from the store if it exists" do - Chef::FileStore.should_receive(:create_store_path).with("node", "nothing").and_return("/tmp/foolio") - File.stub!(:exists?).and_return(true) - File.should_receive(:unlink).with("/tmp/foolio").and_return(1) - Chef::FileStore.delete("node", "nothing") - end - - it "should list all the keys of a particular type" do - Dir.should_receive(:[]).with("/tmp/chef-test/node/**/*").and_return(["pool"]) - File.should_receive(:file?).with("pool").and_return(true) - Chef::FileStore.list("node").should eql(["pool"]) - end - - it "should return all the documents of a particular type with list and inflate" do - Dir.stub!(:[]).and_return(["/foo/pool"]) - File.stub!(:file?).and_return(true) - Chef::FileStore.should_receive(:load).with("node", "pool").and_return("monkey") - Chef::FileStore.list("node", true).should eql(["monkey"]) - end - - it "should let you test whether a key doesnt exist for an object type with has_key?" do - Dir.should_receive(:[]).with("/tmp/chef-test/node/**/*").and_return(["pool"]) - File.should_receive(:file?).with("pool").and_return(true) - Chef::FileStore.has_key?("node", "snake").should eql(false) - end -end
\ No newline at end of file |