From 0f8280aa100aa60238f39f9df66ecba700fdc16f Mon Sep 17 00:00:00 2001 From: Thom May Date: Thu, 15 Feb 2018 15:10:07 +0000 Subject: chefstyle 0.6 Signed-off-by: Thom May --- lib/chef_zero/chef_data/acl_path.rb | 4 +- lib/chef_zero/chef_data/cookbook_data.rb | 298 +++++++++++---------- lib/chef_zero/chef_data/default_creator.rb | 10 +- lib/chef_zero/data_store/default_facade.rb | 16 +- lib/chef_zero/data_store/memory_store_v2.rb | 28 +- lib/chef_zero/data_store/raw_file_store.rb | 28 +- lib/chef_zero/data_store/v1_to_v2_adapter.rb | 20 +- lib/chef_zero/data_store/v2_to_v1_adapter.rb | 12 +- lib/chef_zero/endpoints/actor_endpoint.rb | 2 +- lib/chef_zero/endpoints/actors_endpoint.rb | 6 +- .../endpoints/cookbook_artifact_endpoint.rb | 2 +- .../cookbook_artifact_identifier_endpoint.rb | 42 ++- .../endpoints/cookbook_artifacts_endpoint.rb | 2 +- .../endpoints/cookbook_version_endpoint.rb | 2 +- lib/chef_zero/endpoints/cookbooks_base.rb | 2 +- .../environment_cookbook_versions_endpoint.rb | 4 +- lib/chef_zero/endpoints/not_found_endpoint.rb | 2 +- .../organization_association_request_endpoint.rb | 1 - lib/chef_zero/endpoints/policies_endpoint.rb | 2 +- lib/chef_zero/endpoints/policy_endpoint.rb | 4 +- .../endpoints/policy_group_policy_endpoint.rb | 2 +- .../endpoints/policy_revision_endpoint.rb | 4 +- .../endpoints/policy_revisions_endpoint.rb | 2 +- lib/chef_zero/endpoints/rest_object_endpoint.rb | 4 +- lib/chef_zero/endpoints/search_endpoint.rb | 8 +- lib/chef_zero/rest_base.rb | 8 +- lib/chef_zero/rest_request.rb | 2 +- lib/chef_zero/rspec.rb | 6 +- lib/chef_zero/server.rb | 2 +- lib/chef_zero/socketless_server_map.rb | 2 +- lib/chef_zero/solr/query/term.rb | 2 +- lib/chef_zero/solr/solr_parser.rb | 2 +- spec/run_oc_pedant.rb | 4 +- spec/search_spec.rb | 2 +- 34 files changed, 260 insertions(+), 277 deletions(-) diff --git a/lib/chef_zero/chef_data/acl_path.rb b/lib/chef_zero/chef_data/acl_path.rb index dd162bd..6286287 100644 --- a/lib/chef_zero/chef_data/acl_path.rb +++ b/lib/chef_zero/chef_data/acl_path.rb @@ -104,14 +104,13 @@ module ChefZero end end - private - # /acls/root -> nil # /acls/containers/containers -> /acls/root # /acls/TYPE/X -> /acls/containers/TYPE # # Method *assumes* acl_data_path is valid. # Returns nil if the path is /acls/root + private_class_method def self.partition_parent_acl_data_path(acl_data_path) if acl_data_path.size == 3 if acl_data_path == %w{acls containers containers} @@ -124,6 +123,7 @@ module ChefZero end end + private_class_method def self.partition_acl_data_path(path, data_types) if path.size == 0 %w{acls root} diff --git a/lib/chef_zero/chef_data/cookbook_data.rb b/lib/chef_zero/chef_data/cookbook_data.rb index 2766162..d41948b 100644 --- a/lib/chef_zero/chef_data/cookbook_data.rb +++ b/lib/chef_zero/chef_data/cookbook_data.rb @@ -4,53 +4,158 @@ require "hashie" module ChefZero module ChefData module CookbookData - def self.to_hash(cookbook, name, version = nil) - frozen = false - if cookbook.has_key?(:frozen) - frozen = cookbook[:frozen] - cookbook = cookbook.dup - cookbook.delete(:frozen) - end - - result = files_from(cookbook) - recipe_names = result[:all_files].select do |file| - file[:name].start_with?("recipes/") - end.map do |recipe| - recipe_name = recipe[:name][0..-2] - recipe_name == "default" ? name : "#{name}::#{recipe_name}" - end - result[:metadata] = metadata_from(cookbook, name, version, recipe_names) - result[:name] = "#{name}-#{result[:metadata][:version]}" - result[:json_class] = "Chef::CookbookVersion" - result[:cookbook_name] = name - result[:version] = result[:metadata][:version] - result[:chef_type] = "cookbook_version" - result[:frozen?] = true if frozen - result - end + class << self + def to_hash(cookbook, name, version = nil) + frozen = false + if cookbook.has_key?(:frozen) + frozen = cookbook[:frozen] + cookbook = cookbook.dup + cookbook.delete(:frozen) + end + + result = files_from(cookbook) + recipe_names = result[:all_files].select do |file| + file[:name].start_with?("recipes/") + end.map do |recipe| + recipe_name = recipe[:name][0..-2] + recipe_name == "default" ? name : "#{name}::#{recipe_name}" + end + result[:metadata] = metadata_from(cookbook, name, version, recipe_names) + result[:name] = "#{name}-#{result[:metadata][:version]}" + result[:json_class] = "Chef::CookbookVersion" + result[:cookbook_name] = name + result[:version] = result[:metadata][:version] + result[:chef_type] = "cookbook_version" + result[:frozen?] = true if frozen + result + end + + def metadata_from(directory, name, version, recipe_names) + metadata = PretendCookbookMetadata.new(PretendCookbook.new(name, recipe_names)) + # If both .rb and .json exist, read .json + if has_child(directory, "metadata.json") + metadata.from_json(read_file(directory, "metadata.json")) + elsif has_child(directory, "metadata.rb") + begin + file = filename(directory, "metadata.rb") || "(#{name}/metadata.rb)" + metadata.instance_eval(read_file(directory, "metadata.rb"), file) + rescue + ChefZero::Log.error("Error loading cookbook #{name}: #{$!}\n #{$!.backtrace.join("\n ")}") + end + end + result = {} + metadata.to_hash.each_pair do |key, value| + result[key.to_sym] = value + end + result[:version] = version if version + result + end + + private - def self.metadata_from(directory, name, version, recipe_names) - metadata = PretendCookbookMetadata.new(PretendCookbook.new(name, recipe_names)) - # If both .rb and .json exist, read .json - if has_child(directory, "metadata.json") - metadata.from_json(read_file(directory, "metadata.json")) - elsif has_child(directory, "metadata.rb") - begin - file = filename(directory, "metadata.rb") || "(#{name}/metadata.rb)" - metadata.instance_eval(read_file(directory, "metadata.rb"), file) - rescue - ChefZero::Log.error("Error loading cookbook #{name}: #{$!}\n #{$!.backtrace.join("\n ")}") + def files_from(directory) + # TODO some support .rb only + result = load_files(directory) + + set_specificity(result, :templates) + set_specificity(result, :files) + + result = { + all_files: result, + } + result + end + + def has_child(directory, name) + if directory.is_a?(Hash) + directory.has_key?(name) + else + directory.child(name).exists? end end - result = {} - metadata.to_hash.each_pair do |key, value| - result[key.to_sym] = value + + def read_file(directory, name) + if directory.is_a?(Hash) + directory[name] + else + directory.child(name).read + end + end + + def filename(directory, name) + if directory.respond_to?(:file_path) + File.join(directory.file_path, name) + else + nil + end + end + + def get_directory(directory, name) + if directory.is_a?(Hash) + directory[name].is_a?(Hash) ? directory[name] : nil + else + result = directory.child(name) + result.dir? ? result : nil + end + end + + def list(directory) + if directory.is_a?(Hash) + directory.keys + else + directory.children.map { |c| c.name } + end + end + + def load_child_files(parent, key, recursive, part) + result = load_files(get_directory(parent, key), recursive, part) + result.each do |file| + file[:path] = "#{key}/#{file[:path]}" + end + result + end + + def load_files(directory, recursive = true, part = nil) + result = [] + if directory + list(directory).each do |child_name| + dir = get_directory(directory, child_name) + if dir + child_part = child_name if part.nil? + if recursive + result += load_child_files(directory, child_name, recursive, child_part) + end + else + result += load_file(read_file(directory, child_name), child_name, part) + end + end + end + result + end + + def load_file(value, name, part = nil) + specific_name = part ? "#{part}/#{name}" : name + [{ + :name => specific_name, + :path => name, + :checksum => Digest::MD5.hexdigest(value), + :specificity => "default", + }] end - result[:version] = version if version - result - end - private + def set_specificity(files, type) + files.each do |file| + next unless file[:name].split("/")[0] == type.to_s + + parts = file[:path].split("/") + file[:specificity] = if parts.size == 2 + "default" + else + parts[1] + end + end + end + end # Just enough cookbook to make a Metadata object class PretendCookbook @@ -67,15 +172,15 @@ module ChefZero class PretendCookbookMetadata < Hash # @param [String] path def initialize(cookbook) - self.name(cookbook.name) - self.recipes(cookbook.fully_qualified_recipe_names) + name(cookbook.name) + recipes(cookbook.fully_qualified_recipe_names) %w{attributes grouping dependencies supports recommendations suggestions conflicting providing replacing recipes}.each do |hash_arg| self[hash_arg.to_sym] = Hashie::Mash.new end end def from_json(json) - self.merge!(FFI_Yajl::Parser.parse(json)) + merge!(FFI_Yajl::Parser.parse(json)) end private @@ -121,109 +226,6 @@ module ChefZero end end end - - def self.files_from(directory) - # TODO some support .rb only - result = load_files(directory) - - set_specificity(result, :templates) - set_specificity(result, :files) - - result = { - all_files: result, - } - result - end - - def self.has_child(directory, name) - if directory.is_a?(Hash) - directory.has_key?(name) - else - directory.child(name).exists? - end - end - - def self.read_file(directory, name) - if directory.is_a?(Hash) - directory[name] - else - directory.child(name).read - end - end - - def self.filename(directory, name) - if directory.respond_to?(:file_path) - File.join(directory.file_path, name) - else - nil - end - end - - def self.get_directory(directory, name) - if directory.is_a?(Hash) - directory[name].is_a?(Hash) ? directory[name] : nil - else - result = directory.child(name) - result.dir? ? result : nil - end - end - - def self.list(directory) - if directory.is_a?(Hash) - directory.keys - else - directory.children.map { |c| c.name } - end - end - - def self.load_child_files(parent, key, recursive, part) - result = load_files(get_directory(parent, key), recursive, part) - result.each do |file| - file[:path] = "#{key}/#{file[:path]}" - end - result - end - - def self.load_files(directory, recursive = true, part = nil) - result = [] - if directory - list(directory).each do |child_name| - dir = get_directory(directory, child_name) - if dir - child_part = child_name if part.nil? - if recursive - result += load_child_files(directory, child_name, recursive, child_part) - end - else - result += load_file(read_file(directory, child_name), child_name, part) - end - end - end - result - end - - def self.load_file(value, name, part = nil) - specific_name = part ? "#{part}/#{name}" : name - [{ - :name => specific_name, - :path => name, - :checksum => Digest::MD5.hexdigest(value), - :specificity => "default", - }] - end - - def self.set_specificity(files, type) - files.each do |file| - next unless file[:name].split("/")[0] == type.to_s - - parts = file[:path].split("/") - file[:specificity] = if parts.size == 2 - "default" - else - parts[1] - end - end - end end end diff --git a/lib/chef_zero/chef_data/default_creator.rb b/lib/chef_zero/chef_data/default_creator.rb index e70b2c2..46c0165 100644 --- a/lib/chef_zero/chef_data/default_creator.rb +++ b/lib/chef_zero/chef_data/default_creator.rb @@ -455,17 +455,17 @@ module ChefZero when 0, 1 return true when 2 - return path[0] == "organizations" || (path[0] == "acls" && path[1] != "root") + path[0] == "organizations" || (path[0] == "acls" && path[1] != "root") when 3 # If it has a container, it is a directory. - return path[0] == "organizations" && - (path[2] == "acls" || data.exists?(path[0..1] + [ "containers", path[2] ])) + path[0] == "organizations" && + (path[2] == "acls" || data.exists?(path[0..1] + [ "containers", path[2] ])) when 4 - return path[0] == "organizations" && ( + path[0] == "organizations" && ( (path[2] == "acls" && path[1] != "root") || %w{cookbooks cookbook_artifacts data policies policy_groups}.include?(path[2])) else - return false + false end end end diff --git a/lib/chef_zero/data_store/default_facade.rb b/lib/chef_zero/data_store/default_facade.rb index 5e932d5..d906446 100644 --- a/lib/chef_zero/data_store/default_facade.rb +++ b/lib/chef_zero/data_store/default_facade.rb @@ -66,15 +66,13 @@ module ChefZero end def get(path, request = nil) - begin - real_store.get(path, request) - rescue DataNotFoundError - result = default_creator.get(path) - if result - FFI_Yajl::Encoder.encode(result, :pretty => true) - else - raise - end + real_store.get(path, request) + rescue DataNotFoundError + result = default_creator.get(path) + if result + FFI_Yajl::Encoder.encode(result, :pretty => true) + else + raise end end diff --git a/lib/chef_zero/data_store/memory_store_v2.rb b/lib/chef_zero/data_store/memory_store_v2.rb index c2f2d8e..b35d490 100644 --- a/lib/chef_zero/data_store/memory_store_v2.rb +++ b/lib/chef_zero/data_store/memory_store_v2.rb @@ -111,27 +111,23 @@ module ChefZero end def exists?(path, options = {}) - begin - value = _get(path) - if value.is_a?(Hash) && !options[:allow_dirs] - raise "exists? does not work with directories (#{path} = #{value.class})" - end - return true - rescue DataNotFoundError - return false + value = _get(path) + if value.is_a?(Hash) && !options[:allow_dirs] + raise "exists? does not work with directories (#{path} = #{value.class})" end + return true + rescue DataNotFoundError + return false end def exists_dir?(path) - begin - dir = _get(path) - if !dir.is_a? Hash - raise "exists_dir? only works with directories (#{path} = #{dir.class})" - end - return true - rescue DataNotFoundError - return false + dir = _get(path) + if !dir.is_a? Hash + raise "exists_dir? only works with directories (#{path} = #{dir.class})" end + return true + rescue DataNotFoundError + return false end private diff --git a/lib/chef_zero/data_store/raw_file_store.rb b/lib/chef_zero/data_store/raw_file_store.rb index 120fc1b..8b4c442 100644 --- a/lib/chef_zero/data_store/raw_file_store.rb +++ b/lib/chef_zero/data_store/raw_file_store.rb @@ -80,11 +80,9 @@ module ChefZero end def get(path, request = nil) - begin - return IO.read(path_to(path)) - rescue Errno::ENOENT - raise DataNotFoundError.new(path) - end + return IO.read(path_to(path)) + rescue Errno::ENOENT + raise DataNotFoundError.new(path) end def set(path, data, *options) @@ -105,11 +103,9 @@ module ChefZero end def delete(path) - begin - File.delete(path_to(path)) - rescue Errno::ENOENT - raise DataNotFoundError.new(path) - end + File.delete(path_to(path)) + rescue Errno::ENOENT + raise DataNotFoundError.new(path) end def delete_dir(path, *options) @@ -128,19 +124,17 @@ module ChefZero end def list(path) - begin - Dir.entries(path_to(path)).select { |entry| entry != "." && entry != ".." }.to_a - rescue Errno::ENOENT - raise DataNotFoundError.new(path) - end + Dir.entries(path_to(path)).select { |entry| entry != "." && entry != ".." }.to_a + rescue Errno::ENOENT + raise DataNotFoundError.new(path) end def exists?(path, options = {}) - File.exists?(path_to(path)) + File.exist?(path_to(path)) end def exists_dir?(path) - File.exists?(path_to(path)) + File.exist?(path_to(path)) end end end diff --git a/lib/chef_zero/data_store/v1_to_v2_adapter.rb b/lib/chef_zero/data_store/v1_to_v2_adapter.rb index dba35b3..874c05a 100644 --- a/lib/chef_zero/data_store/v1_to_v2_adapter.rb +++ b/lib/chef_zero/data_store/v1_to_v2_adapter.rb @@ -109,17 +109,15 @@ module ChefZero private def fix_exceptions - begin - yield - rescue DataAlreadyExistsError => e - err = DataAlreadyExistsError.new([ "organizations", single_org ] + e.path, e) - err.set_backtrace(e.backtrace) - raise err - rescue DataNotFoundError => e - err = DataNotFoundError.new([ "organizations", single_org ] + e.path, e) - err.set_backtrace(e.backtrace) - raise e - end + yield + rescue DataAlreadyExistsError => e + err = DataAlreadyExistsError.new([ "organizations", single_org ] + e.path, e) + err.set_backtrace(e.backtrace) + raise err + rescue DataNotFoundError => e + err = DataNotFoundError.new([ "organizations", single_org ] + e.path, e) + err.set_backtrace(e.backtrace) + raise e end def skip_organizations?(path, name = nil) diff --git a/lib/chef_zero/data_store/v2_to_v1_adapter.rb b/lib/chef_zero/data_store/v2_to_v1_adapter.rb index f2cbce0..38f2bc1 100644 --- a/lib/chef_zero/data_store/v2_to_v1_adapter.rb +++ b/lib/chef_zero/data_store/v2_to_v1_adapter.rb @@ -90,13 +90,11 @@ module ChefZero protected def fix_exceptions - begin - yield - rescue DataAlreadyExistsError => e - raise DataAlreadyExistsError.new(e.path[2..-1], e) - rescue DataNotFoundError => e - raise DataNotFoundError.new(e.path[2..-1], e) - end + yield + rescue DataAlreadyExistsError => e + raise DataAlreadyExistsError.new(e.path[2..-1], e) + rescue DataNotFoundError => e + raise DataNotFoundError.new(e.path[2..-1], e) end def fix_path(path) diff --git a/lib/chef_zero/endpoints/actor_endpoint.rb b/lib/chef_zero/endpoints/actor_endpoint.rb index 8d90c13..4cba31a 100644 --- a/lib/chef_zero/endpoints/actor_endpoint.rb +++ b/lib/chef_zero/endpoints/actor_endpoint.rb @@ -156,7 +156,7 @@ module ChefZero def client?(request, rest_path = nil) rest_path ||= request.rest_path - request.rest_path[2] == "clients" + rest_path[2] == "clients" end # Return the data store keys path for the request client or user, e.g. diff --git a/lib/chef_zero/endpoints/actors_endpoint.rb b/lib/chef_zero/endpoints/actors_endpoint.rb index 4a6d8c9..f4a2160 100644 --- a/lib/chef_zero/endpoints/actors_endpoint.rb +++ b/lib/chef_zero/endpoints/actors_endpoint.rb @@ -10,9 +10,9 @@ module ChefZero # apply query filters: if one applies, stop processing rest # (precendence matches chef-server: https://github.com/chef/chef-server/blob/268a0c9/src/oc_erchef/apps/chef_objects/src/chef_user.erl#L554-L559) - if value = request.query_params["external_authentication_uid"] + if (value = request.query_params["external_authentication_uid"]) response[2] = filter("external_authentication_uid", value, request, response[2]) - elsif value = request.query_params["email"] + elsif (value = request.query_params["email"]) response[2] = filter("email", value, request, response[2], insensitive: true) end @@ -97,7 +97,7 @@ module ChefZero end def is_equal(a, b, ignore_case) - ignore_case ? a.casecmp(b).zero? : a == b + ignore_case ? a.casecmp(b) == 0 : a == b end end end diff --git a/lib/chef_zero/endpoints/cookbook_artifact_endpoint.rb b/lib/chef_zero/endpoints/cookbook_artifact_endpoint.rb index baf80d3..d0cf822 100644 --- a/lib/chef_zero/endpoints/cookbook_artifact_endpoint.rb +++ b/lib/chef_zero/endpoints/cookbook_artifact_endpoint.rb @@ -17,7 +17,7 @@ module ChefZero response_data[cookbook_name] = { url: cookbook_url, versions: versions } - return json_response(200, response_data) + json_response(200, response_data) end end end diff --git a/lib/chef_zero/endpoints/cookbook_artifact_identifier_endpoint.rb b/lib/chef_zero/endpoints/cookbook_artifact_identifier_endpoint.rb index e586e94..9f7b973 100644 --- a/lib/chef_zero/endpoints/cookbook_artifact_identifier_endpoint.rb +++ b/lib/chef_zero/endpoints/cookbook_artifact_identifier_endpoint.rb @@ -10,7 +10,7 @@ module ChefZero # GET /organizations/ORG/cookbook_artifacts/NAME/IDENTIFIER def get(request) cookbook_data = normalize(request, get_data(request)) - return json_response(200, cookbook_data) + json_response(200, cookbook_data) end # PUT /organizations/ORG/cookbook_artifacts/COOKBOOK/IDENTIFIER @@ -22,34 +22,32 @@ module ChefZero cb_data = normalize(request, request.body) set_data(request, nil, to_json(cb_data), :create_dir, :create) - return already_json_response(201, request.body) + already_json_response(201, request.body) end # DELETE /organizations/ORG/cookbook_artifacts/COOKBOOK/IDENTIFIER def delete(request) - begin - doomed_cookbook_json = get_data(request) - identified_cookbook_data = normalize(request, doomed_cookbook_json) - delete_data(request) + doomed_cookbook_json = get_data(request) + identified_cookbook_data = normalize(request, doomed_cookbook_json) + delete_data(request) - # go through the recipes and delete stuff in the file store. - hoover_unused_checksums(get_checksums(doomed_cookbook_json), request) + # go through the recipes and delete stuff in the file store. + hoover_unused_checksums(get_checksums(doomed_cookbook_json), request) - # if this was the last revision, delete the directory so future requests will 404, instead of - # returning 200 with an empty list. - # Last one out turns out the lights: delete /organizations/ORG/cookbooks/COOKBOOK if it no longer has versions - cookbook_path = request.rest_path[0..3] - if exists_data_dir?(request, cookbook_path) && list_data(request, cookbook_path).size == 0 - delete_data_dir(request, cookbook_path) - end + # if this was the last revision, delete the directory so future requests will 404, instead of + # returning 200 with an empty list. + # Last one out turns out the lights: delete /organizations/ORG/cookbooks/COOKBOOK if it no longer has versions + cookbook_path = request.rest_path[0..3] + if exists_data_dir?(request, cookbook_path) && list_data(request, cookbook_path).size == 0 + delete_data_dir(request, cookbook_path) + end - json_response(200, identified_cookbook_data) - rescue RestErrorResponse => ex - if ex.response_code == 404 - error(404, "not_found") - else - raise - end + json_response(200, identified_cookbook_data) + rescue RestErrorResponse => ex + if ex.response_code == 404 + error(404, "not_found") + else + raise end end diff --git a/lib/chef_zero/endpoints/cookbook_artifacts_endpoint.rb b/lib/chef_zero/endpoints/cookbook_artifacts_endpoint.rb index a105664..bfe2ff5 100644 --- a/lib/chef_zero/endpoints/cookbook_artifacts_endpoint.rb +++ b/lib/chef_zero/endpoints/cookbook_artifacts_endpoint.rb @@ -27,7 +27,7 @@ module ChefZero data[cookbook_artifact] = { url: cookbook_url, versions: versions } end - return json_response(200, data) + json_response(200, data) end end end diff --git a/lib/chef_zero/endpoints/cookbook_version_endpoint.rb b/lib/chef_zero/endpoints/cookbook_version_endpoint.rb index 8afde32..615b03e 100644 --- a/lib/chef_zero/endpoints/cookbook_version_endpoint.rb +++ b/lib/chef_zero/endpoints/cookbook_version_endpoint.rb @@ -99,7 +99,7 @@ module ChefZero begin data_store.list(request.rest_path[0..1] + [cookbook_type, cookbook_name]).each do |version| cookbook = data_store.get(request.rest_path[0..1] + [cookbook_type, cookbook_name, version], request) - deleted_checksums = deleted_checksums - get_checksums(cookbook) + deleted_checksums -= get_checksums(cookbook) end rescue ChefZero::DataStore::DataNotFoundError end diff --git a/lib/chef_zero/endpoints/cookbooks_base.rb b/lib/chef_zero/endpoints/cookbooks_base.rb index 55b79ca..d8af80a 100644 --- a/lib/chef_zero/endpoints/cookbooks_base.rb +++ b/lib/chef_zero/endpoints/cookbooks_base.rb @@ -65,7 +65,7 @@ module ChefZero def recipe_names(cookbook_name, cookbook) cookbook["all_files"].inject([]) do |acc, file| part, name = file["name"].split("/") - next unless part == "recipes" || File.extname(name) != ".rb" + next acc unless part == "recipes" || File.extname(name) != ".rb" if name == "default.rb" acc << cookbook_name else diff --git a/lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb b/lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb index 669be9a..ddd44d3 100644 --- a/lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb +++ b/lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb @@ -82,7 +82,7 @@ module ChefZero # If the dep is not already in the list, add it to the list to solve # and bring in all environment-allowed cookbook versions to desired_versions if !new_desired_versions.has_key?(dep_name) - new_unsolved = new_unsolved + [dep_name] + new_unsolved += [dep_name] # If the dep is missing, we will try other versions of the cookbook that might not have the bad dep. if !exists_data_dir?(request, request.rest_path[0..1] + ["cookbooks", dep_name]) @last_missing_dep = dep_name.to_s @@ -101,7 +101,7 @@ module ChefZero result = depsolve(request, new_unsolved, new_desired_versions, environment_constraints) return result if result end - return nil + nil end def sort_versions(versions) diff --git a/lib/chef_zero/endpoints/not_found_endpoint.rb b/lib/chef_zero/endpoints/not_found_endpoint.rb index 4c23800..582272b 100644 --- a/lib/chef_zero/endpoints/not_found_endpoint.rb +++ b/lib/chef_zero/endpoints/not_found_endpoint.rb @@ -4,7 +4,7 @@ module ChefZero module Endpoints class NotFoundEndpoint def call(request) - return [404, { "Content-Type" => "application/json" }, FFI_Yajl::Encoder.encode({ "error" => ["Object not found: #{request.env['REQUEST_PATH']}"] }, :pretty => true)] + [404, { "Content-Type" => "application/json" }, FFI_Yajl::Encoder.encode({ "error" => ["Object not found: #{request.env['REQUEST_PATH']}"] }, :pretty => true)] end end end diff --git a/lib/chef_zero/endpoints/organization_association_request_endpoint.rb b/lib/chef_zero/endpoints/organization_association_request_endpoint.rb index 0402893..c73d816 100644 --- a/lib/chef_zero/endpoints/organization_association_request_endpoint.rb +++ b/lib/chef_zero/endpoints/organization_association_request_endpoint.rb @@ -13,7 +13,6 @@ module ChefZero end username = $1 path = request.rest_path[0..-2] + [username] - data = FFI_Yajl::Parser.parse(get_data(request, path)) delete_data(request, path) json_response(200, { "id" => id, "username" => username }) end diff --git a/lib/chef_zero/endpoints/policies_endpoint.rb b/lib/chef_zero/endpoints/policies_endpoint.rb index f830c5c..3a8deea 100644 --- a/lib/chef_zero/endpoints/policies_endpoint.rb +++ b/lib/chef_zero/endpoints/policies_endpoint.rb @@ -19,7 +19,7 @@ module ChefZero } end - return json_response(200, response_data) + json_response(200, response_data) end end end diff --git a/lib/chef_zero/endpoints/policy_endpoint.rb b/lib/chef_zero/endpoints/policy_endpoint.rb index 5c77a7c..1e80278 100644 --- a/lib/chef_zero/endpoints/policy_endpoint.rb +++ b/lib/chef_zero/endpoints/policy_endpoint.rb @@ -8,7 +8,7 @@ module ChefZero def get(request) revisions = list_data(request, request.rest_path + ["revisions"]) data = { revisions: hashify_list(revisions) } - return json_response(200, data) + json_response(200, data) end # DELETE /organizations/ORG/policies/NAME @@ -17,7 +17,7 @@ module ChefZero data = { revisions: hashify_list(revisions) } delete_data_dir(request, nil, :recursive) - return json_response(200, data) + json_response(200, data) end end end diff --git a/lib/chef_zero/endpoints/policy_group_policy_endpoint.rb b/lib/chef_zero/endpoints/policy_group_policy_endpoint.rb index 2643dd3..a34da32 100644 --- a/lib/chef_zero/endpoints/policy_group_policy_endpoint.rb +++ b/lib/chef_zero/endpoints/policy_group_policy_endpoint.rb @@ -76,7 +76,7 @@ module ChefZero full_policy_doc = parse_json(get_data(request, policy_path)) full_policy_doc = ChefData::DataNormalizer.normalize_policy(full_policy_doc, policy_name, current_revision_id) - return json_response(200, full_policy_doc) + json_response(200, full_policy_doc) end end end diff --git a/lib/chef_zero/endpoints/policy_revision_endpoint.rb b/lib/chef_zero/endpoints/policy_revision_endpoint.rb index 64e2dca..c825510 100644 --- a/lib/chef_zero/endpoints/policy_revision_endpoint.rb +++ b/lib/chef_zero/endpoints/policy_revision_endpoint.rb @@ -8,7 +8,7 @@ module ChefZero def get(request) data = parse_json(get_data(request)) data = ChefData::DataNormalizer.normalize_policy(data, request.rest_path[3], request.rest_path[5]) - return json_response(200, data) + json_response(200, data) end # DELETE /organizations/ORG/policies/NAME/revisions/REVISION @@ -16,7 +16,7 @@ module ChefZero policyfile_data = parse_json(get_data(request)) policyfile_data = ChefData::DataNormalizer.normalize_policy(policyfile_data, request.rest_path[3], request.rest_path[5]) delete_data(request) - return json_response(200, policyfile_data) + json_response(200, policyfile_data) end end end diff --git a/lib/chef_zero/endpoints/policy_revisions_endpoint.rb b/lib/chef_zero/endpoints/policy_revisions_endpoint.rb index 27e019e..d755f81 100644 --- a/lib/chef_zero/endpoints/policy_revisions_endpoint.rb +++ b/lib/chef_zero/endpoints/policy_revisions_endpoint.rb @@ -8,7 +8,7 @@ module ChefZero def post(request) policyfile_data = parse_json(request.body) create_data(request, request.rest_path, policyfile_data["revision_id"], request.body, :create_dir) - return already_json_response(201, request.body) + already_json_response(201, request.body) end end end diff --git a/lib/chef_zero/endpoints/rest_object_endpoint.rb b/lib/chef_zero/endpoints/rest_object_endpoint.rb index c35133f..887905e 100644 --- a/lib/chef_zero/endpoints/rest_object_endpoint.rb +++ b/lib/chef_zero/endpoints/rest_object_endpoint.rb @@ -20,7 +20,7 @@ module ChefZero def put(request) # We grab the old body to trigger a 404 if it doesn't exist - old_body = get_data(request) + get_data(request) # If it's a rename, check for conflict and delete the old value if is_rename?(request) @@ -69,7 +69,7 @@ module ChefZero # Does this request change the value of the identity key? def is_rename?(request) - return false unless key = identity_key_value(request) + return false unless (key = identity_key_value(request)) key != request.rest_path[-1] end end diff --git a/lib/chef_zero/endpoints/search_endpoint.rb b/lib/chef_zero/endpoints/search_endpoint.rb index b808ce9..e2f30ce 100644 --- a/lib/chef_zero/endpoints/search_endpoint.rb +++ b/lib/chef_zero/endpoints/search_endpoint.rb @@ -96,8 +96,8 @@ module ChefZero end end end - value.each_pair do |key, value| - result[key] = value unless %w{default normal override automatic}.include?(key) + value.each_pair do |key, val| + result[key] = val unless %w{default normal override automatic}.include?(key) end result @@ -155,8 +155,6 @@ module ChefZero } end - private - # Deep Merge core documentation. # deep_merge! method permits merging of arbitrary child elements. The two top level # elements must be hashes. These hashes can contain unlimited (to stack limit) levels @@ -194,7 +192,7 @@ module ChefZero end when Array if dest.kind_of?(Array) - dest = dest | source + dest |= source else dest = source end diff --git a/lib/chef_zero/rest_base.rb b/lib/chef_zero/rest_base.rb index 74ca014..e6114c4 100644 --- a/lib/chef_zero/rest_base.rb +++ b/lib/chef_zero/rest_base.rb @@ -46,8 +46,8 @@ module ChefZero return response unless response.nil? method = request.method.downcase.to_sym - if !self.respond_to?(method) - accept_methods = [:get, :put, :post, :delete].select { |m| self.respond_to?(m) } + if !respond_to?(method) + accept_methods = [:get, :put, :post, :delete].select { |m| respond_to?(m) } accept_methods_str = accept_methods.map { |m| m.to_s.upcase }.join(", ") return [405, { "Content-Type" => "text/plain", "Allow" => accept_methods_str }, "Bad request method for '#{request.env['REQUEST_PATH']}': #{request.env['REQUEST_METHOD']}"] end @@ -56,7 +56,7 @@ module ChefZero end # Dispatch to get()/post()/put()/delete() begin - self.send(method, request) + send(method, request) rescue RestErrorResponse => e ChefZero::Log.debug("#{e.inspect}\n#{e.backtrace.join("\n")}") error(e.response_code, e.error) @@ -72,7 +72,7 @@ module ChefZero # This parses as per http://tools.ietf.org/html/rfc7231#section-5.3 return true if !request.env["HTTP_ACCEPT"] accepts = request.env["HTTP_ACCEPT"].split(/,\s*/).map { |x| x.split(";", 2)[0].strip } - return accepts.include?("#{category}/#{type}") || accepts.include?("#{category}/*") || accepts.include?("*/*") + accepts.include?("#{category}/#{type}") || accepts.include?("#{category}/*") || accepts.include?("*/*") end def get_data(request, rest_path = nil, *options) diff --git a/lib/chef_zero/rest_request.rb b/lib/chef_zero/rest_request.rb index 127ec09..864c9b1 100644 --- a/lib/chef_zero/rest_request.rb +++ b/lib/chef_zero/rest_request.rb @@ -70,7 +70,7 @@ module ChefZero def to_s result = "#{method} #{rest_path.join('/')}" if query_params.size > 0 - result << "?#{query_params.map { |k, v| "#{k}=#{v}" }.join('&') }" + result << "?#{query_params.map { |k, v| "#{k}=#{v}" }.join('&')}" end if body.chomp != "" result << "\n--- #{method} BODY ---\n" diff --git a/lib/chef_zero/rspec.rb b/lib/chef_zero/rspec.rb index bb3a927..765ec92 100644 --- a/lib/chef_zero/rspec.rb +++ b/lib/chef_zero/rspec.rb @@ -48,6 +48,7 @@ module ChefZero # Take the passed-in options + # rubocop:disable Lint/UnderscorePrefixedVariableName define_singleton_method(:chef_server_options) do @chef_server_options ||= begin _chef_server_options = { port: 8900, signals: false, log_requests: true } @@ -55,11 +56,12 @@ module ChefZero _chef_server_options = _chef_server_options.freeze end end + # rubocop:enable Lint/UnderscorePrefixedVariableName # Merge in chef_server_options from let(:chef_server_options) - def chef_server_options + def chef_server_options # rubocop:disable Lint/NestedMethodDefinition chef_server_options = self.class.chef_server_options.dup - chef_server_options = chef_server_options.merge(chef_zero_opts) if self.respond_to?(:chef_zero_opts) + chef_server_options = chef_server_options.merge(chef_zero_opts) if respond_to?(:chef_zero_opts) chef_server_options end diff --git a/lib/chef_zero/server.rb b/lib/chef_zero/server.rb index 4771412..61f3962 100644 --- a/lib/chef_zero/server.rb +++ b/lib/chef_zero/server.rb @@ -493,7 +493,7 @@ module ChefZero if contents[cookbook_type] contents[cookbook_type].each_pair do |name_version, cookbook| if cookbook_type == "cookbook_artifacts" - name, dash, identifier = name_version.rpartition("-") + name, _, identifier = name_version.rpartition("-") cookbook_data = ChefData::CookbookData.to_hash(cookbook, name, identifier) elsif name_version =~ /(.+)-(\d+\.\d+\.\d+)$/ cookbook_data = ChefData::CookbookData.to_hash(cookbook, $1, $2) diff --git a/lib/chef_zero/socketless_server_map.rb b/lib/chef_zero/socketless_server_map.rb index 655047a..28b7b59 100644 --- a/lib/chef_zero/socketless_server_map.rb +++ b/lib/chef_zero/socketless_server_map.rb @@ -41,7 +41,7 @@ module ChefZero include Singleton - def initialize() + def initialize reset! end diff --git a/lib/chef_zero/solr/query/term.rb b/lib/chef_zero/solr/query/term.rb index e106519..b98d281 100644 --- a/lib/chef_zero/solr/query/term.rb +++ b/lib/chef_zero/solr/query/term.rb @@ -23,7 +23,7 @@ module ChefZero raise "~ unsupported" else if term[index] == '\\' - index = index + 1 + index += 1 if index >= term.length raise "Backslash at end of string '#{term}'" end diff --git a/lib/chef_zero/solr/solr_parser.rb b/lib/chef_zero/solr/solr_parser.rb index 980a75f..cec1b34 100644 --- a/lib/chef_zero/solr/solr_parser.rb +++ b/lib/chef_zero/solr/solr_parser.rb @@ -67,7 +67,7 @@ module ChefZero def peek_term_token return nil if @query_string[@index] =~ /\s/ op = peek_operator_token - return !op || op == "-" + !op || op == "-" end def peek_operator_token diff --git a/spec/run_oc_pedant.rb b/spec/run_oc_pedant.rb index 0ee94b5..068cb4d 100644 --- a/spec/run_oc_pedant.rb +++ b/spec/run_oc_pedant.rb @@ -49,12 +49,12 @@ def start_cheffs_server(chef_repo_path) require "chef/chef_fs/chef_fs_data_store" require "chef_zero/server" - Dir.mkdir(chef_repo_path) if !File.exists?(chef_repo_path) + Dir.mkdir(chef_repo_path) if !File.exist?(chef_repo_path) # 11.6 and below had a bug where it couldn't create the repo children automatically if Chef::VERSION.to_f < 11.8 %w{clients cookbooks data_bags environments nodes roles users}.each do |child| - Dir.mkdir("#{chef_repo_path}/#{child}") if !File.exists?("#{chef_repo_path}/#{child}") + Dir.mkdir("#{chef_repo_path}/#{child}") if !File.exist?("#{chef_repo_path}/#{child}") end end diff --git a/spec/search_spec.rb b/spec/search_spec.rb index 34ea5b9..f0b9b5e 100644 --- a/spec/search_spec.rb +++ b/spec/search_spec.rb @@ -2,7 +2,7 @@ require "chef_zero/solr/solr_parser" require "chef_zero/solr/solr_doc" describe ChefZero::Solr::SolrParser do - let (:all_docs) do + let(:all_docs) do docs = [] [{ "foo" => "a" }, { "foo" => "d" }].each_with_index do |h, i| -- cgit v1.2.1