summaryrefslogtreecommitdiff
path: root/chef/lib/chef/file_store.rb
diff options
context:
space:
mode:
Diffstat (limited to 'chef/lib/chef/file_store.rb')
-rw-r--r--chef/lib/chef/file_store.rb135
1 files changed, 135 insertions, 0 deletions
diff --git a/chef/lib/chef/file_store.rb b/chef/lib/chef/file_store.rb
new file mode 100644
index 0000000000..6fe13e5ad6
--- /dev/null
+++ b/chef/lib/chef/file_store.rb
@@ -0,0 +1,135 @@
+#
+# Author:: Adam Jacob (<adam@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# 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