diff options
author | danielsdeleo <dan@chef.io> | 2016-02-08 15:00:29 -0800 |
---|---|---|
committer | danielsdeleo <dan@chef.io> | 2016-02-16 15:26:43 -0800 |
commit | c5416667d86a47f00b4664e297fb589f2c7bf54b (patch) | |
tree | f20f7130c72c997fe2c2a332d7c4749feab0eab8 /lib/chef/chef_fs | |
parent | c5481bf4b86fd10ba634df02793446d88f7c1f88 (diff) | |
download | chef-c5416667d86a47f00b4664e297fb589f2c7bf54b.tar.gz |
Extract cheffs dir behavior, move data bag classes to own files
Diffstat (limited to 'lib/chef/chef_fs')
6 files changed, 312 insertions, 282 deletions
diff --git a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_data_bags_dir.rb b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_data_bags_dir.rb deleted file mode 100644 index 11072557f5..0000000000 --- a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_data_bags_dir.rb +++ /dev/null @@ -1,280 +0,0 @@ -# -# Author:: John Keiser (<jkeiser@chef.io>) -# Copyright:: Copyright 2013-2016, Chef Software 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 "chef/chef_fs/file_system/repository/chef_repository_file_system_entry" -require "chef/chef_fs/data_handler/data_bag_item_data_handler" - -class Chef - module ChefFS - module FileSystem - module Repository - - class ChefRepositoryFileSystemDataBagsDir - - attr_reader :name - attr_reader :parent - attr_reader :path - attr_reader :file_path - - def initialize(name, parent, file_path) - @parent = parent - @name = name - @path = Chef::ChefFS::PathUtils::join(parent.path, name) - @file_path = file_path - end - - # ChefFS API: - - # Public api called by multiplexed_dir - def can_have_child?(name, is_dir) - is_dir && make_child_entry(name).name_valid? - end - - def path_for_printing - file_path - end - - def children - dir_ls.sort. - map { |child_name| make_child_entry(child_name) } - rescue Errno::ENOENT => e - raise Chef::ChefFS::FileSystem::NotFoundError.new(self, e) - end - - def create_child(child_name, file_contents = nil) - make_child_entry(child_name).tap { |c| c.create } - end - - # An empty children array is an empty dir - def empty? - children.empty? - end - - def child(name) - possible_child = make_child_entry(name) - if possible_child.name_valid? - possible_child - else - NonexistentFSObject.new(name, self) - end - end - - def root - parent.root - end - - # File system wrappers - - def dir_ls - Dir.entries(file_path).select { |p| !p.start_with?(".") } - end - - def delete(recurse) - if exists? - if !recurse - raise MustDeleteRecursivelyError.new(self, $!) - end - FileUtils.rm_r(file_path) - else - raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) - end - end - - def exists? - File.exists?(file_path) - end - - protected - - def make_child_entry(child_name) - ChefRepositoryFileSystemDataBagEntry.new(child_name, self) - end - - end - - class ChefRepositoryFileSystemDataBagEntry - - attr_reader :name - attr_reader :parent - attr_reader :path - attr_reader :data_handler - attr_reader :file_path - - def initialize(name, parent) - @parent = parent - @name = name - @path = Chef::ChefFS::PathUtils::join(parent.path, name) - @file_path = "#{parent.file_path}/#{name}" - end - - def create - if exists? - raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, child) - end - begin - Dir.mkdir(file_path) - rescue Errno::EEXIST - raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, child) - end - end - - def name_valid? - !name.start_with?(".") - end - - def can_have_child?(name, is_dir) - !name.start_with?(".") - end - - def path_for_printing - file_path - end - - def children - begin - dir_ls.sort. - map { |child_name| make_child_entry(child_name) } - rescue Errno::ENOENT - raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) - end - end - - def create_child(child_name, file_contents = nil) - make_child_entry(child_name).tap { |c| c.create(file_contents) } - end - - # An empty children array is an empty dir - def empty? - children.empty? - end - - def child(name) - if can_have_child?(name, true) || can_have_child?(name, false) - result = make_child_entry(name) - end - result || NonexistentFSObject.new(name, self) - end - - def root - parent.root - end - - # File system wrappers - - def dir_ls - Dir.entries(file_path).select { |p| !p.start_with?(".") } - end - - def delete(recurse) - if exists? - if !recurse - raise MustDeleteRecursivelyError.new(self, $!) - end - FileUtils.rm_r(file_path) - else - raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) - end - end - - def exists? - File.exists?(file_path) - end - - protected - - def make_child_entry(child_name) - ChefRepositoryFileSystemDataBagItemEntry.new(child_name, self) - end - - end - - class ChefRepositoryFileSystemDataBagItemEntry - - attr_reader :name - attr_reader :parent - attr_reader :path - attr_reader :ruby_only - attr_reader :recursive - attr_reader :file_path - - def initialize(name, parent) - @parent = parent - @name = name - @path = Chef::ChefFS::PathUtils::join(parent.path, name) - @data_handler = Chef::ChefFS::DataHandler::DataBagItemDataHandler.new - @file_path = "#{parent.file_path}/#{name}" - end - - def create(file_contents) - if exists? - raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, self) - else - write(file_contents) - end - end - - def can_have_child?(name, is_dir) - false - end - - def write_pretty_json=(value) - @write_pretty_json = value - end - - def write_pretty_json - @write_pretty_json.nil? ? root.write_pretty_json : @write_pretty_json - end - - def path_for_printing - file_path - end - - def delete(recurse) - File.delete(file_path) - rescue Errno::ENOENT - raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) - end - - def exists? - File.exists?(file_path) - end - - def read - begin - File.open(file_path, "rb") { |f| f.read } - rescue Errno::ENOENT - raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) - end - end - - def write(content) - File.open(file_path, "wb") do |file| - file.write(content) - end - end - - def root - parent.root - end - - end - - end - end - end -end diff --git a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb index 5f238888d5..a1237cc429 100644 --- a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb @@ -20,7 +20,7 @@ require "chef/chef_fs/file_system/base_fs_dir" require "chef/chef_fs/file_system/repository/chef_repository_file_system_acls_dir" require "chef/chef_fs/file_system/repository/chef_repository_file_system_cookbooks_dir" require "chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_artifacts_dir" -require "chef/chef_fs/file_system/repository/chef_repository_file_system_data_bags_dir" +require "chef/chef_fs/file_system/repository/data_bags_dir" require "chef/chef_fs/file_system/repository/chef_repository_file_system_entry" require "chef/chef_fs/file_system/repository/chef_repository_file_system_policies_dir" require "chef/chef_fs/file_system/repository/chef_repository_file_system_versioned_cookbooks_dir" @@ -175,7 +175,7 @@ class Chef when "policies" dirs = paths.map { |path| ChefRepositoryFileSystemPoliciesDir.new(name, self, path) } when "data_bags" - dirs = paths.map { |path| ChefRepositoryFileSystemDataBagsDir.new(name, self, path) } + dirs = paths.map { |path| DataBagsDir.new(name, self, path) } when "acls" dirs = paths.map { |path| ChefRepositoryFileSystemAclsDir.new(name, self, path) } else diff --git a/lib/chef/chef_fs/file_system/repository/data_bag.rb b/lib/chef/chef_fs/file_system/repository/data_bag.rb new file mode 100644 index 0000000000..0c1b155bd1 --- /dev/null +++ b/lib/chef/chef_fs/file_system/repository/data_bag.rb @@ -0,0 +1,41 @@ +# +# Author:: John Keiser (<jkeiser@chef.io>) +# Copyright:: Copyright 2013-2016, Chef Software 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 "chef/chef_fs/file_system/repository/directory" +require "chef/chef_fs/file_system/repository/data_bag_item" + +class Chef + module ChefFS + module FileSystem + module Repository + + # Represents REPO_ROOT/data_bags/data_bag Children of this are data bag + # items. + class DataBag < Repository::Directory + + def make_child_entry(child_name) + DataBagItem.new(child_name, self) + end + + end + end + end + end +end + + diff --git a/lib/chef/chef_fs/file_system/repository/data_bag_item.rb b/lib/chef/chef_fs/file_system/repository/data_bag_item.rb new file mode 100644 index 0000000000..8b25338134 --- /dev/null +++ b/lib/chef/chef_fs/file_system/repository/data_bag_item.rb @@ -0,0 +1,105 @@ +# +# Author:: John Keiser (<jkeiser@chef.io>) +# Copyright:: Copyright 2013-2016, Chef Software 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 "chef/chef_fs/data_handler/data_bag_item_data_handler" + +class Chef + module ChefFS + module FileSystem + module Repository + + class DataBagItem + + attr_reader :name + attr_reader :parent + attr_reader :path + attr_reader :ruby_only + attr_reader :recursive + attr_reader :file_path + + def initialize(name, parent) + @parent = parent + @name = name + @path = Chef::ChefFS::PathUtils::join(parent.path, name) + @data_handler = Chef::ChefFS::DataHandler::DataBagItemDataHandler.new + @file_path = "#{parent.file_path}/#{name}" + end + + def name_valid? + !name.start_with?(".") + end + + def create(file_contents) + if exists? + raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, self) + else + write(file_contents) + end + end + + def can_have_child?(name, is_dir) + false + end + + def write_pretty_json=(value) + @write_pretty_json = value + end + + def write_pretty_json + @write_pretty_json.nil? ? root.write_pretty_json : @write_pretty_json + end + + def path_for_printing + file_path + end + + def delete(recurse) + File.delete(file_path) + rescue Errno::ENOENT + raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) + end + + def exists? + File.exists?(file_path) + end + + def read + begin + File.open(file_path, "rb") { |f| f.read } + rescue Errno::ENOENT + raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) + end + end + + def write(content) + File.open(file_path, "wb") do |file| + file.write(content) + end + end + + def root + parent.root + end + + end + + end + end + end +end + diff --git a/lib/chef/chef_fs/file_system/repository/data_bags_dir.rb b/lib/chef/chef_fs/file_system/repository/data_bags_dir.rb new file mode 100644 index 0000000000..666fede62b --- /dev/null +++ b/lib/chef/chef_fs/file_system/repository/data_bags_dir.rb @@ -0,0 +1,39 @@ +# +# Author:: John Keiser (<jkeiser@chef.io>) +# Copyright:: Copyright 2013-2016, Chef Software 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 "chef/chef_fs/file_system/repository/directory" +require "chef/chef_fs/file_system/repository/data_bag" + +class Chef + module ChefFS + module FileSystem + module Repository + + # Represents the REPO_ROOT/data_bags directory. Children of this are + # data bags. + class DataBagsDir < Repository::Directory + + def make_child_entry(child_name) + DataBag.new(child_name, self) + end + + end + end + end + end +end diff --git a/lib/chef/chef_fs/file_system/repository/directory.rb b/lib/chef/chef_fs/file_system/repository/directory.rb new file mode 100644 index 0000000000..1a8ea29a9a --- /dev/null +++ b/lib/chef/chef_fs/file_system/repository/directory.rb @@ -0,0 +1,125 @@ +# +# Author:: John Keiser (<jkeiser@chef.io>) +# Copyright:: Copyright 2013-2016, Chef Software 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. +# + +class Chef + module ChefFS + module FileSystem + module Repository + + class Directory + + attr_reader :name + attr_reader :parent + attr_reader :path + attr_reader :file_path + + def initialize(name, parent, file_path = nil) + @parent = parent + @name = name + @path = Chef::ChefFS::PathUtils::join(parent.path, name) + @file_path = file_path || "#{parent.file_path}/#{name}" + end + + def name_valid? + !name.start_with?(".") + end + + # ChefFS API: + + # Public api called by multiplexed_dir + def can_have_child?(name, is_dir) + is_dir && make_child_entry(name).name_valid? + end + + def path_for_printing + file_path + end + + def children + dir_ls.sort. + map { |child_name| make_child_entry(child_name) } + rescue Errno::ENOENT => e + raise Chef::ChefFS::FileSystem::NotFoundError.new(self, e) + end + + def create_child(child_name, file_contents = nil) + make_child_entry(child_name).tap { |c| c.create(file_contents) } + end + + # An empty children array is an empty dir + def empty? + children.empty? + end + + def child(name) + possible_child = make_child_entry(name) + if possible_child.name_valid? + possible_child + else + NonexistentFSObject.new(name, self) + end + end + + def root + parent.root + end + + # File system wrappers + + def create(file_contents = nil) + if exists? + raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, child) + end + begin + Dir.mkdir(file_path) + rescue Errno::EEXIST + raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, child) + end + end + + def dir_ls + Dir.entries(file_path).select { |p| !p.start_with?(".") } + end + + def delete(recurse) + if exists? + if !recurse + raise MustDeleteRecursivelyError.new(self, $!) + end + FileUtils.rm_r(file_path) + else + raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) + end + end + + def exists? + File.exists?(file_path) + end + + protected + + def make_child_entry(child_name) + raise "Not Implemented" + end + + end + end + end + end +end + |