diff options
Diffstat (limited to 'lib/chef/chef_fs/chef_fs_data_store.rb')
-rw-r--r-- | lib/chef/chef_fs/chef_fs_data_store.rb | 158 |
1 files changed, 79 insertions, 79 deletions
diff --git a/lib/chef/chef_fs/chef_fs_data_store.rb b/lib/chef/chef_fs/chef_fs_data_store.rb index 07cb3d2cd5..99a668d50d 100644 --- a/lib/chef/chef_fs/chef_fs_data_store.rb +++ b/lib/chef/chef_fs/chef_fs_data_store.rb @@ -16,15 +16,15 @@ # limitations under the License. # -require 'chef/cookbook_manifest' -require 'chef_zero/data_store/memory_store' -require 'chef_zero/data_store/data_already_exists_error' -require 'chef_zero/data_store/data_not_found_error' -require 'chef/chef_fs/file_pattern' -require 'chef/chef_fs/file_system' -require 'chef/chef_fs/file_system/not_found_error' -require 'chef/chef_fs/file_system/memory/memory_root' -require 'fileutils' +require "chef/cookbook_manifest" +require "chef_zero/data_store/memory_store" +require "chef_zero/data_store/data_already_exists_error" +require "chef_zero/data_store/data_not_found_error" +require "chef/chef_fs/file_pattern" +require "chef/chef_fs/file_system" +require "chef/chef_fs/file_system/not_found_error" +require "chef/chef_fs/file_system/memory/memory_root" +require "fileutils" class Chef module ChefFS @@ -185,15 +185,15 @@ class Chef if use_memory_store?(path) @memory_store.create(path, name, data, *options) - elsif path[0] == 'cookbooks' && path.length == 2 + elsif path[0] == "cookbooks" && path.length == 2 # Do nothing. The entry gets created when the cookbook is created. # create [/organizations/ORG]/users/NAME (with content '{}') # Manipulate the `members.json` file that contains a list of all users - elsif is_org? && path == [ 'users' ] - update_json('members.json', []) do |members| + elsif is_org? && path == [ "users" ] + update_json("members.json", []) do |members| # Format of each entry: { "user": { "username": "jkeiser" } } - if members.any? { |member| member['user']['username'] == name } + if members.any? { |member| member["user"]["username"] == name } raise ChefZero::DataStore::DataAlreadyExistsError.new(path, entry) end @@ -203,10 +203,10 @@ class Chef # create [/organizations/ORG]/association_requests/NAME (with content '{}') # Manipulate the `invitations.json` file that contains a list of all users - elsif is_org? && path == [ 'association_requests' ] - update_json('invitations.json', []) do |invitations| + elsif is_org? && path == [ "association_requests" ] + update_json("invitations.json", []) do |invitations| # Format of each entry: { "id" => "jkeiser-chef", 'username' => 'jkeiser' } - if invitations.any? { |member| member['username'] == name } + if invitations.any? { |member| member["username"] == name } raise ChefZero::DataStore::DataAlreadyExistsError.new(path) end @@ -233,8 +233,8 @@ class Chef if use_memory_store?(path) @memory_store.get(path) - elsif path[0] == 'file_store' && path[1] == 'repo' - entry = Chef::ChefFS::FileSystem.resolve_path(chef_fs, path[2..-1].join('/')) + elsif path[0] == "file_store" && path[1] == "repo" + entry = Chef::ChefFS::FileSystem.resolve_path(chef_fs, path[2..-1].join("/")) begin entry.read rescue Chef::ChefFS::FileSystem::NotFoundError => e @@ -242,10 +242,10 @@ class Chef end # /policy_groups/NAME/policies/POLICYNAME: return the revision of the given policy - elsif path[0] == 'policy_groups' && path[2] == 'policies' && path.length == 4 + elsif path[0] == "policy_groups" && path[2] == "policies" && path.length == 4 with_entry(path[0..1]) do |entry| policy_group = Chef::JSONCompat.parse(entry) - if !policy_group['policies'] || !policy_group['policies'][path[3]] || !policy_group['policies'][path[3]] + if !policy_group["policies"] || !policy_group["policies"][path[3]] || !policy_group["policies"][path[3]] raise ChefZero::DataStore::DataNotFoundError.new(path, entry) end # The policy group looks like: @@ -254,30 +254,30 @@ class Chef # "x": { "revision_id": "10" } # } # } - Chef::JSONCompat.to_json_pretty(policy_group['policies'][path[3]]["revision_id"]) + Chef::JSONCompat.to_json_pretty(policy_group["policies"][path[3]]["revision_id"]) end # GET [/organizations/ORG]/users/NAME -> /users/NAME # Manipulates members.json - elsif is_org? && path[0] == 'users' && path.length == 2 - if get_json('members.json', []).any? { |member| member['user']['username'] == path[1] } - '{}' + elsif is_org? && path[0] == "users" && path.length == 2 + if get_json("members.json", []).any? { |member| member["user"]["username"] == path[1] } + "{}" else raise ChefZero::DataStore::DataNotFoundError.new(path) end # GET [/organizations/ORG]/association_requests/NAME -> /users/NAME # Manipulates invites.json - elsif is_org? && path[0] == 'association_requests' && path.length == 2 - if get_json('invites.json', []).any? { |member| member['user']['username'] == path[1] } - '{}' + elsif is_org? && path[0] == "association_requests" && path.length == 2 + if get_json("invites.json", []).any? { |member| member["user"]["username"] == path[1] } + "{}" else raise ChefZero::DataStore::DataNotFoundError.new(path) end else with_entry(path) do |entry| - if path[0] == 'cookbooks' && path.length == 3 + if path[0] == "cookbooks" && path.length == 3 # get /cookbooks/NAME/version result = nil begin @@ -289,15 +289,15 @@ class Chef result.each_pair do |key, value| if value.is_a?(Array) value.each do |file| - if file.is_a?(Hash) && file.has_key?('checksum') - relative = ['file_store', 'repo', 'cookbooks'] + if file.is_a?(Hash) && file.has_key?("checksum") + relative = ["file_store", "repo", "cookbooks"] if chef_fs.versioned_cookbooks relative << "#{path[1]}-#{path[2]}" else relative << path[1] end - relative = relative + file[:path].split('/') - file['url'] = ChefZero::RestBase::build_uri(request.base_uri, relative) + relative = relative + file[:path].split("/") + file["url"] = ChefZero::RestBase::build_uri(request.base_uri, relative) end end end @@ -324,7 +324,7 @@ class Chef end # Write out the files! - if path[0] == 'cookbooks' && path.length == 3 + if path[0] == "cookbooks" && path.length == 3 write_cookbook(path, data, *options) else with_dir(path[0..-2]) do |parent| @@ -345,9 +345,9 @@ class Chef # DELETE [/organizations/ORG]/users/NAME # Manipulates members.json - elsif is_org? && path[0] == 'users' && path.length == 2 - update_json('members.json', []) do |members| - result = members.reject { |member| member['user']['username'] == path[1] } + elsif is_org? && path[0] == "users" && path.length == 2 + update_json("members.json", []) do |members| + result = members.reject { |member| member["user"]["username"] == path[1] } if result.size == members.size raise ChefZero::DataStore::DataNotFoundError.new(path) end @@ -356,9 +356,9 @@ class Chef # DELETE [/organizations/ORG]/users/NAME # Manipulates members.json - elsif is_org? && path[0] == 'association_requests' && path.length == 2 - update_json('invitations.json', []) do |invitations| - result = invitations.reject { |invitation| invitation['username'] == path[1] } + elsif is_org? && path[0] == "association_requests" && path.length == 2 + update_json("invitations.json", []) do |invitations| + result = invitations.reject { |invitation| invitation["username"] == path[1] } if result.size == invitations.size raise ChefZero::DataStore::DataNotFoundError.new(path) end @@ -368,7 +368,7 @@ class Chef else with_entry(path) do |entry| begin - if path[0] == 'cookbooks' && path.length >= 3 + if path[0] == "cookbooks" && path.length >= 3 entry.delete(true) else entry.delete(false) @@ -398,18 +398,18 @@ class Chef if use_memory_store?(path) @memory_store.list(path) - elsif path[0] == 'policy_groups' && path.length == 2 + elsif path[0] == "policy_groups" && path.length == 2 with_entry(path) do |entry| - [ 'policies' ] + [ "policies" ] end - elsif path[0] == 'policy_groups' && path[2] == 'policies' && path.length == 3 - with_entry([ 'policy_groups', path[1] ]) do |entry| + elsif path[0] == "policy_groups" && path[2] == "policies" && path.length == 3 + with_entry([ "policy_groups", path[1] ]) do |entry| policy_group = Chef::JSONCompat.parse(entry.read) - (policy_group['policies'] || {}).keys + (policy_group["policies"] || {}).keys end - elsif path[0] == 'cookbooks' && path.length == 1 + elsif path[0] == "cookbooks" && path.length == 1 with_entry(path) do |entry| begin if chef_fs.versioned_cookbooks @@ -424,9 +424,9 @@ class Chef end end - elsif path[0] == 'cookbooks' && path.length == 2 + elsif path[0] == "cookbooks" && path.length == 2 if chef_fs.versioned_cookbooks - result = with_entry([ 'cookbooks' ]) do |entry| + result = with_entry([ "cookbooks" ]) do |entry| # list /cookbooks/name = filter /cookbooks/name-version down to name entry.children.map { |child| split_name_version(child.name) }. select { |name, version| name == path[1] }. @@ -476,13 +476,13 @@ class Chef def exists_dir?(path) if use_memory_store?(path) @memory_store.exists_dir?(path) - elsif path[0] == 'cookbooks' && path.length == 2 + elsif path[0] == "cookbooks" && path.length == 2 list([ path[0] ]).include?(path[1]) # /policy_groups/NAME/policies - elsif path[0] == 'policy_groups' && path[2] == 'policies' && path.length == 3 + elsif path[0] == "policy_groups" && path[2] == "policies" && path.length == 3 exists_dir?(path[0..1]) # /policy_groups/NAME/policies/POLICYNAME - elsif path[0] == 'policy_groups' && path[2] == 'policies' && path.length == 4 + elsif path[0] == "policy_groups" && path[2] == "policies" && path.length == 4 exists_dir?(path[0..1]) && list(path[0..2]).include?(path[3]) else Chef::ChefFS::FileSystem.resolve_path(chef_fs, to_chef_fs_path(path)).exists? @@ -492,34 +492,34 @@ class Chef private def use_memory_store?(path) - return path[0] == 'sandboxes' || path[0] == 'file_store' && path[1] == 'checksums' || path == [ 'environments', '_default' ] + return path[0] == "sandboxes" || path[0] == "file_store" && path[1] == "checksums" || path == [ "environments", "_default" ] end def write_cookbook(path, data, *options) if chef_fs.versioned_cookbooks - cookbook_path = File.join('cookbooks', "#{path[1]}-#{path[2]}") + cookbook_path = File.join("cookbooks", "#{path[1]}-#{path[2]}") else - cookbook_path = File.join('cookbooks', path[1]) + cookbook_path = File.join("cookbooks", path[1]) end # Create a little Chef::ChefFS memory filesystem with the data - cookbook_fs = Chef::ChefFS::FileSystem::Memory::MemoryRoot.new('uploading') + cookbook_fs = Chef::ChefFS::FileSystem::Memory::MemoryRoot.new("uploading") cookbook = Chef::JSONCompat.parse(data) cookbook.each_pair do |key, value| if value.is_a?(Array) value.each do |file| - if file.is_a?(Hash) && file.has_key?('checksum') - file_data = @memory_store.get(['file_store', 'checksums', file['checksum']]) - cookbook_fs.add_file(File.join(cookbook_path, file['path']), file_data) + if file.is_a?(Hash) && file.has_key?("checksum") + file_data = @memory_store.get(["file_store", "checksums", file["checksum"]]) + cookbook_fs.add_file(File.join(cookbook_path, file["path"]), file_data) end end end end # Create the .uploaded-cookbook-version.json - cookbooks = chef_fs.child('cookbooks') + cookbooks = chef_fs.child("cookbooks") if !cookbooks.exists? - cookbooks = chef_fs.create_child('cookbooks') + cookbooks = chef_fs.create_child("cookbooks") end # We are calling a cookbooks-specific API, so get multiplexed_dirs out of the way if it is there if cookbooks.respond_to?(:multiplexed_dirs) @@ -529,14 +529,14 @@ class Chef end def split_name_version(entry_name) - name_version = entry_name.split('-') - name = name_version[0..-2].join('-') + name_version = entry_name.split("-") + name = name_version[0..-2].join("-") version = name_version[-1] [name,version] end def to_chef_fs_path(path) - _to_chef_fs_path(path).join('/') + _to_chef_fs_path(path).join("/") end def chef_fs_filename(path) @@ -544,18 +544,18 @@ class Chef end def _to_chef_fs_path(path) - if path[0] == 'data' + if path[0] == "data" path = path.dup - path[0] = 'data_bags' + path[0] = "data_bags" if path.length >= 3 path[2] = "#{path[2]}.json" end - elsif path[0] == 'policies' + elsif path[0] == "policies" path = path.dup if path.length >= 3 path[2] = "#{path[2]}.json" end - elsif path[0] == 'cookbooks' + elsif path[0] == "cookbooks" if path.length == 2 raise ChefZero::DataStore::DataNotFoundError.new(path) elsif chef_fs.versioned_cookbooks @@ -575,10 +575,10 @@ class Chef end end - elsif path[0] == 'acls' + elsif path[0] == "acls" # /acls/containers|nodes|.../x.json # /acls/organization.json - if path.length == 3 || path == [ 'acls', 'organization' ] + if path.length == 3 || path == [ "acls", "organization" ] path = path.dup path[-1] = "#{path[-1]}.json" end @@ -595,15 +595,15 @@ class Chef end def to_zero_path(entry) - path = entry.path.split('/')[1..-1] - if path[0] == 'data_bags' + path = entry.path.split("/")[1..-1] + if path[0] == "data_bags" path = path.dup - path[0] = 'data' + path[0] = "data" if path.length >= 3 path[2] = path[2][0..-6] end - elsif path[0] == 'cookbooks' + elsif path[0] == "cookbooks" if chef_fs.versioned_cookbooks # cookbooks/name-version/... -> cookbooks/name/version/... if path.length >= 2 @@ -618,7 +618,7 @@ class Chef end end - elsif path.length == 2 && path[0] != 'cookbooks' + elsif path.length == 2 && path[0] != "cookbooks" path = path.dup path[1] = path[1][0..-6] end @@ -643,7 +643,7 @@ class Chef def with_dir(path) # Do not automatically create data bags - create = !(path[0] == 'data' && path.size >= 2) + create = !(path[0] == "data" && path.size >= 2) begin yield get_dir(_to_chef_fs_path(path), create) @@ -655,7 +655,7 @@ class Chef end def get_dir(path, create=false) - result = Chef::ChefFS::FileSystem.resolve_path(chef_fs, path.join('/')) + result = Chef::ChefFS::FileSystem.resolve_path(chef_fs, path.join("/")) if result.exists? result elsif create @@ -666,9 +666,9 @@ class Chef end def get_single_cookbook_version(path) - dir = Chef::ChefFS::FileSystem.resolve_path(chef_fs, path[0..1].join('/')) + dir = Chef::ChefFS::FileSystem.resolve_path(chef_fs, path[0..1].join("/")) metadata = ChefZero::CookbookData.metadata_from(dir, path[1], nil, []) - metadata[:version] || '0.0.0' + metadata[:version] || "0.0.0" end def update_json(path, default_value) @@ -694,7 +694,7 @@ class Chef end def is_org? - repo_mode == 'hosted_everything' + repo_mode == "hosted_everything" end end end |