diff options
author | Joshua C. Burt <joshburt@shapeandshare.com> | 2016-05-11 08:22:26 -0600 |
---|---|---|
committer | Joshua C. Burt <joshburt@shapeandshare.com> | 2016-05-11 08:22:26 -0600 |
commit | 632e99689cbdc8108231ec28b781412d2481bd0a (patch) | |
tree | 1dd19747dbc477992b4e7d311c8b61db44317cce /lib | |
parent | 5d91f332d1435edd4d44a855c60dc4340a006af3 (diff) | |
parent | 6fe31094c894780116d8438c040e04632d9d01c0 (diff) | |
download | chef-632e99689cbdc8108231ec28b781412d2481bd0a.tar.gz |
Merge branch 'master' into jb_git_new_resource_destination_to_cwd
Diffstat (limited to 'lib')
44 files changed, 293 insertions, 197 deletions
diff --git a/lib/chef/chef_fs/command_line.rb b/lib/chef/chef_fs/command_line.rb index 4a9a712c20..c824bc90df 100644 --- a/lib/chef/chef_fs/command_line.rb +++ b/lib/chef/chef_fs/command_line.rb @@ -63,6 +63,13 @@ class Chef end when :deleted + # This is kind of a kludge - because the "new" entry isn't there, we can't predict + # it's true file name, because we've not got enough information. So because we know + # the two entries really ought to have the same extension, we'll just grab the old one + # and use it. (This doesn't affect cookbook files, since they'll always have extensions) + if File.extname(old_path) != File.extname(new_path) + new_path += File.extname(old_path) + end next if diff_filter && diff_filter !~ /D/ if output_mode == :name_only yield "#{new_path}\n" diff --git a/lib/chef/chef_fs/config.rb b/lib/chef/chef_fs/config.rb index 6e3cc50ac1..63a1363724 100644 --- a/lib/chef/chef_fs/config.rb +++ b/lib/chef/chef_fs/config.rb @@ -242,7 +242,7 @@ class Chef # Print the given server path, relative to the current directory def format_path(entry) - server_path = entry.path + server_path = entry.respond_to?(:display_path) ? entry.display_path : entry.path if base_path && server_path[0, base_path.length] == base_path if server_path == base_path return "." diff --git a/lib/chef/chef_fs/data_handler/data_handler_base.rb b/lib/chef/chef_fs/data_handler/data_handler_base.rb index b30ae9c708..b34aff4b98 100644 --- a/lib/chef/chef_fs/data_handler/data_handler_base.rb +++ b/lib/chef/chef_fs/data_handler/data_handler_base.rb @@ -24,15 +24,14 @@ class Chef object end - # - # Takes a name like blah.json and removes the .json from it. - # - def remove_dot_json(name) - if name.length < 5 || name[-5, 5] != ".json" - raise "Invalid name #{path}: must end in .json" + def remove_file_extension(name, ext = ".*") + if %w{ .rb .json }.include?(File.extname(name)) + File.basename(name, ext) + else + name end - name[0, name.length - 5] end + alias_method :remove_dot_json, :remove_file_extension # # Return true if minimize() should preserve a key even if it is the same @@ -109,8 +108,10 @@ class Chef # # Bring in an instance of this object from Ruby. (Like roles/x.rb) # - def from_ruby(ruby) - chef_class.from_file(ruby).to_hash + def from_ruby(path) + r = chef_class.new + r.from_file(path) + r.to_hash end # @@ -192,7 +193,7 @@ class Chef # @yield [s] callback to handle errors # @yieldparam [s<string>] error message def verify_integrity(object, entry) - base_name = remove_dot_json(entry.name) + base_name = remove_file_extension(entry.name) if object["name"] != base_name yield("Name must be '#{base_name}' (is '#{object['name']}')") end diff --git a/lib/chef/chef_fs/data_handler/environment_data_handler.rb b/lib/chef/chef_fs/data_handler/environment_data_handler.rb index 8d066764be..68f6daee9a 100644 --- a/lib/chef/chef_fs/data_handler/environment_data_handler.rb +++ b/lib/chef/chef_fs/data_handler/environment_data_handler.rb @@ -7,7 +7,7 @@ class Chef class EnvironmentDataHandler < DataHandlerBase def normalize(environment, entry) normalize_hash(environment, { - "name" => remove_dot_json(entry.name), + "name" => remove_file_extension(entry.name), "description" => "", "cookbook_versions" => {}, "default_attributes" => {}, diff --git a/lib/chef/chef_fs/data_handler/role_data_handler.rb b/lib/chef/chef_fs/data_handler/role_data_handler.rb index 74533cff05..b09c146a5d 100644 --- a/lib/chef/chef_fs/data_handler/role_data_handler.rb +++ b/lib/chef/chef_fs/data_handler/role_data_handler.rb @@ -7,7 +7,7 @@ class Chef class RoleDataHandler < DataHandlerBase def normalize(role, entry) result = normalize_hash(role, { - "name" => remove_dot_json(entry.name), + "name" => remove_file_extension(entry.name), "description" => "", "json_class" => "Chef::Role", "chef_type" => "role", diff --git a/lib/chef/chef_fs/file_system.rb b/lib/chef/chef_fs/file_system.rb index 2b553b19a7..69dce54f00 100644 --- a/lib/chef/chef_fs/file_system.rb +++ b/lib/chef/chef_fs/file_system.rb @@ -52,13 +52,13 @@ class Chef def list_from(entry, &block) # Include self in results if it matches - if pattern.match?(entry.path) + if pattern.match?(entry.display_path) yield(entry) end - if pattern.could_match_children?(entry.path) + if pattern.could_match_children?(entry.display_path) # If it's possible that our children could match, descend in and add matches. - exact_child_name = pattern.exact_child_name_under(entry.path) + exact_child_name = pattern.exact_child_name_under(entry.display_path) # If we've got an exact name, don't bother listing children; just grab the # child with the given name. @@ -186,16 +186,16 @@ class Chef # Make sure everything on the server is also on the filesystem, and diff found_paths = Set.new Chef::ChefFS::FileSystem.list(a_root, pattern).each do |a| - found_paths << a.path - b = Chef::ChefFS::FileSystem.resolve_path(b_root, a.path) + found_paths << a.display_path + b = Chef::ChefFS::FileSystem.resolve_path(b_root, a.display_path) yield [ a, b ] end # Check the outer regex pattern to see if it matches anything on the # filesystem that isn't on the server Chef::ChefFS::FileSystem.list(b_root, pattern).each do |b| - if !found_paths.include?(b.path) - a = Chef::ChefFS::FileSystem.resolve_path(a_root, b.path) + if !found_paths.include?(b.display_path) + a = Chef::ChefFS::FileSystem.resolve_path(a_root, b.display_path) yield [ a, b ] end end @@ -222,14 +222,14 @@ class Chef result = [] a_children_names = Set.new a.children.each do |a_child| - a_children_names << a_child.name - result << [ a_child, b.child(a_child.name) ] + a_children_names << a_child.bare_name + result << [ a_child, b.child(a_child.bare_name) ] end # Check b for children that aren't in a b.children.each do |b_child| - if !a_children_names.include?(b_child.name) - result << [ a.child(b_child.name), b_child ] + if !a_children_names.include?(b_child.bare_name) + result << [ a.child(b_child.bare_name), b_child ] end end result @@ -327,8 +327,8 @@ class Chef if options[:dry_run] ui.output "Would create #{dest_path}" if ui else - new_dest_parent.create_child(src_entry.name, src_entry.read) - ui.output "Created #{dest_path}" if ui + child = new_dest_parent.create_child(src_entry.name, src_entry.read) + ui.output "Created #{format_path.call(child)}" if ui end end end @@ -392,6 +392,8 @@ class Chef end end end + rescue RubyFileError => e + ui.warn "#{format_path.call(e.entry)} #{e.reason}." if ui rescue DefaultEnvironmentCannotBeModifiedError => e ui.warn "#{format_path.call(e.entry)} #{e.reason}." if ui rescue OperationFailedError => e diff --git a/lib/chef/chef_fs/file_system/base_fs_object.rb b/lib/chef/chef_fs/file_system/base_fs_object.rb index 6abbcf343f..9767b5b1ba 100644 --- a/lib/chef/chef_fs/file_system/base_fs_object.rb +++ b/lib/chef/chef_fs/file_system/base_fs_object.rb @@ -40,6 +40,10 @@ class Chef attr_reader :parent attr_reader :path + alias_method :display_path, :path + alias_method :display_name, :name + alias_method :bare_name, :name + # Override this if you have a special comparison algorithm that can tell # you whether this entry is the same as another--either a quicker or a # more reliable one. Callers will use this to decide whether to upload, diff --git a/lib/chef/chef_fs/file_system/chef_server/acl_dir.rb b/lib/chef/chef_fs/file_system/chef_server/acl_dir.rb index 9f4d13f5a9..006098a0c9 100644 --- a/lib/chef/chef_fs/file_system/chef_server/acl_dir.rb +++ b/lib/chef/chef_fs/file_system/chef_server/acl_dir.rb @@ -35,7 +35,7 @@ class Chef end def can_have_child?(name, is_dir) - name =~ /\.json$/ && !is_dir + !is_dir end def children diff --git a/lib/chef/chef_fs/file_system/chef_server/acl_entry.rb b/lib/chef/chef_fs/file_system/chef_server/acl_entry.rb index 68df3704bf..f4655412fa 100644 --- a/lib/chef/chef_fs/file_system/chef_server/acl_entry.rb +++ b/lib/chef/chef_fs/file_system/chef_server/acl_entry.rb @@ -30,6 +30,15 @@ class Chef "#{super}/_acl" end + def display_path + pth = if parent.name == "acls" + "/acls/#{name}" + else + "/acls/#{parent.name}/#{name}" + end + File.extname(pth).empty? ? pth + ".json" : pth + end + def delete(recurse) raise Chef::ChefFS::FileSystem::OperationNotAllowedError.new(:delete, self, nil, "ACLs cannot be deleted") end diff --git a/lib/chef/chef_fs/file_system/chef_server/data_bag_dir.rb b/lib/chef/chef_fs/file_system/chef_server/data_bag_dir.rb index 5ad0063807..ee0ecd3b40 100644 --- a/lib/chef/chef_fs/file_system/chef_server/data_bag_dir.rb +++ b/lib/chef/chef_fs/file_system/chef_server/data_bag_dir.rb @@ -17,6 +17,7 @@ # require "chef/chef_fs/file_system/chef_server/rest_list_dir" +require "chef/chef_fs/file_system/chef_server/data_bag_entry" require "chef/chef_fs/file_system/exceptions" require "chef/chef_fs/data_handler/data_bag_item_data_handler" @@ -63,6 +64,11 @@ class Chef end end end + + def make_child_entry(name, exists = nil) + @children.find { |child| child.name == name } if @children + DataBagEntry.new(name, self, exists) + end end end end diff --git a/lib/chef/chef_fs/file_system/chef_server/data_bag_entry.rb b/lib/chef/chef_fs/file_system/chef_server/data_bag_entry.rb new file mode 100644 index 0000000000..c0093058b7 --- /dev/null +++ b/lib/chef/chef_fs/file_system/chef_server/data_bag_entry.rb @@ -0,0 +1,19 @@ +require "chef/chef_fs/file_system/chef_server/rest_list_entry" +require "chef/chef_fs/data_handler/data_bag_item_data_handler" + +class Chef + module ChefFS + module FileSystem + module ChefServer + # /policies/NAME-REVISION.json + # Represents the actual data at /organizations/ORG/policies/NAME/revisions/REVISION + class DataBagEntry < RestListEntry + def display_path + pth = "/data_bags/#{parent.name}/#{name}" + File.extname(pth).empty? ? pth + ".json" : pth + end + end + end + end + end +end diff --git a/lib/chef/chef_fs/file_system/chef_server/environments_dir.rb b/lib/chef/chef_fs/file_system/chef_server/environments_dir.rb index 81ae81ac0d..e4714cf089 100644 --- a/lib/chef/chef_fs/file_system/chef_server/environments_dir.rb +++ b/lib/chef/chef_fs/file_system/chef_server/environments_dir.rb @@ -26,7 +26,7 @@ class Chef module ChefServer class EnvironmentsDir < RestListDir def make_child_entry(name, exists = nil) - if name == "_default.json" + if File.basename(name, ".*") == "_default" DefaultEnvironmentEntry.new(name, self, exists) else super diff --git a/lib/chef/chef_fs/file_system/chef_server/nodes_dir.rb b/lib/chef/chef_fs/file_system/chef_server/nodes_dir.rb index 4f83b89711..df2388f1df 100644 --- a/lib/chef/chef_fs/file_system/chef_server/nodes_dir.rb +++ b/lib/chef/chef_fs/file_system/chef_server/nodes_dir.rb @@ -30,7 +30,7 @@ class Chef def children begin @children ||= root.get_json(env_api_path).keys.sort.map do |key| - make_child_entry("#{key}.json", true) + make_child_entry(key, true) end rescue Timeout::Error => e raise Chef::ChefFS::FileSystem::OperationFailedError.new(:children, self, e, "Timeout retrieving children: #{e}") diff --git a/lib/chef/chef_fs/file_system/chef_server/org_entry.rb b/lib/chef/chef_fs/file_system/chef_server/org_entry.rb index 87be36b932..7253de3449 100644 --- a/lib/chef/chef_fs/file_system/chef_server/org_entry.rb +++ b/lib/chef/chef_fs/file_system/chef_server/org_entry.rb @@ -17,6 +17,10 @@ class Chef parent.api_path end + def display_path + "/org.json" + end + def exists? parent.exists? end diff --git a/lib/chef/chef_fs/file_system/chef_server/organization_invites_entry.rb b/lib/chef/chef_fs/file_system/chef_server/organization_invites_entry.rb index 37b7af8b5e..adaffb99a7 100644 --- a/lib/chef/chef_fs/file_system/chef_server/organization_invites_entry.rb +++ b/lib/chef/chef_fs/file_system/chef_server/organization_invites_entry.rb @@ -27,6 +27,10 @@ class Chef File.join(parent.api_path, "association_requests") end + def display_path + "/invitations.json" + end + def exists? parent.exists? end diff --git a/lib/chef/chef_fs/file_system/chef_server/organization_members_entry.rb b/lib/chef/chef_fs/file_system/chef_server/organization_members_entry.rb index 2e45b74450..7e9c7141c4 100644 --- a/lib/chef/chef_fs/file_system/chef_server/organization_members_entry.rb +++ b/lib/chef/chef_fs/file_system/chef_server/organization_members_entry.rb @@ -27,6 +27,10 @@ class Chef File.join(parent.api_path, "users") end + def display_path + "/members.json" + end + def exists? parent.exists? end diff --git a/lib/chef/chef_fs/file_system/chef_server/policy_revision_entry.rb b/lib/chef/chef_fs/file_system/chef_server/policy_revision_entry.rb index d083383a0e..325b18e429 100644 --- a/lib/chef/chef_fs/file_system/chef_server/policy_revision_entry.rb +++ b/lib/chef/chef_fs/file_system/chef_server/policy_revision_entry.rb @@ -14,6 +14,10 @@ class Chef "#{parent.api_path}/#{policy_name}/revisions/#{revision_id}" end + def display_path + "/policies/#{policy_name}-#{revision_id}.json" + end + def write(file_contents) raise OperationNotAllowedError.new(:write, self, nil, "cannot be updated: policy revisions are immutable once uploaded. If you want to change the policy, create a new revision with your changes") end diff --git a/lib/chef/chef_fs/file_system/chef_server/rest_list_dir.rb b/lib/chef/chef_fs/file_system/chef_server/rest_list_dir.rb index 276f7760b5..dfd26a0241 100644 --- a/lib/chef/chef_fs/file_system/chef_server/rest_list_dir.rb +++ b/lib/chef/chef_fs/file_system/chef_server/rest_list_dir.rb @@ -35,7 +35,7 @@ class Chef attr_reader :data_handler def can_have_child?(name, is_dir) - name =~ /\.json$/ && !is_dir + !is_dir end # @@ -74,7 +74,7 @@ class Chef begin # Grab the names of the children, append json, and make child entries @children ||= root.get_json(api_path).keys.sort.map do |key| - make_child_entry("#{key}.json", true) + make_child_entry(key, true) end rescue Timeout::Error => e raise Chef::ChefFS::FileSystem::OperationFailedError.new(:children, self, e, "Timeout retrieving children: #{e}") @@ -85,7 +85,7 @@ class Chef if parent.is_a?(ChefServerRootDir) # GET /organizations/ORG/<container> returned 404, but that just might be because # we are talking to an older version of the server that doesn't support policies. - # Do GET /orgqanizations/ORG to find out if the org exists at all. + # Do GET /organizations/ORG to find out if the org exists at all. # TODO use server API version instead of a second network request. begin root.get_json(parent.api_path) diff --git a/lib/chef/chef_fs/file_system/chef_server/rest_list_entry.rb b/lib/chef/chef_fs/file_system/chef_server/rest_list_entry.rb index 9b16bd80de..b8ec5f8524 100644 --- a/lib/chef/chef_fs/file_system/chef_server/rest_list_entry.rb +++ b/lib/chef/chef_fs/file_system/chef_server/rest_list_entry.rb @@ -37,16 +37,27 @@ class Chef end def api_child_name - if name.length < 5 || name[-5, 5] != ".json" - raise "Invalid name #{path}: must end in .json" + if %w{ .rb .json }.include? File.extname(name) + File.basename(name, ".*") + else + name end - name[0, name.length - 5] end def api_path "#{parent.api_path}/#{api_child_name}" end + def display_path + pth = api_path.start_with?("/") ? api_path : "/#{api_path}" + File.extname(pth).empty? ? pth + ".json" : pth + end + alias_method :path_for_printing, :display_path + + def display_name + File.basename(display_path) + end + def org parent.org end @@ -58,7 +69,7 @@ class Chef def exists? if @exists.nil? begin - @exists = parent.children.any? { |child| child.name == name } + @exists = parent.children.any? { |child| child.api_child_name == api_child_name } rescue Chef::ChefFS::FileSystem::NotFoundError @exists = false end diff --git a/lib/chef/chef_fs/file_system/exceptions.rb b/lib/chef/chef_fs/file_system/exceptions.rb index cf4916e4c8..2a1baba8f5 100644 --- a/lib/chef/chef_fs/file_system/exceptions.rb +++ b/lib/chef/chef_fs/file_system/exceptions.rb @@ -86,6 +86,13 @@ class Chef class CookbookFrozenError < AlreadyExistsError; end + class RubyFileError < OperationNotAllowedError + def reason + result = super + result + " (can't safely update ruby files)" + end + end + class DefaultEnvironmentCannotBeModifiedError < OperationNotAllowedError def reason result = super diff --git a/lib/chef/chef_fs/file_system/multiplexed_dir.rb b/lib/chef/chef_fs/file_system/multiplexed_dir.rb index e143dde9e8..d3951edd51 100644 --- a/lib/chef/chef_fs/file_system/multiplexed_dir.rb +++ b/lib/chef/chef_fs/file_system/multiplexed_dir.rb @@ -24,7 +24,7 @@ class Chef multiplexed_dirs.each do |dir| dir.children.each do |child| if seen[child.name] - Chef::Log.warn("Child with name '#{child.name}' found in multiple directories: #{seen[child.name].path_for_printing} and #{child.path_for_printing}") + Chef::Log.warn("Child with name '#{child.name}' found in multiple directories: #{seen[child.name].path_for_printing} and #{child.path_for_printing}") unless seen[child.name].path_for_printing == child.path_for_printing else result << child seen[child.name] = child @@ -41,7 +41,7 @@ class Chef child_entry = dir.child(name) if child_entry.exists? if result - Chef::Log.warn("Child with name '#{child_entry.name}' found in multiple directories: #{result.parent.path_for_printing} and #{child_entry.parent.path_for_printing}") + Chef::Log.debug("Child with name '#{child_entry.name}' found in multiple directories: #{result.parent.path_for_printing} and #{child_entry.parent.path_for_printing}") unless seen[child.name].path_for_printing == child.path_for_printing else result = child_entry end diff --git a/lib/chef/chef_fs/file_system/nonexistent_fs_object.rb b/lib/chef/chef_fs/file_system/nonexistent_fs_object.rb index b78dcb6c0b..1a48d23047 100644 --- a/lib/chef/chef_fs/file_system/nonexistent_fs_object.rb +++ b/lib/chef/chef_fs/file_system/nonexistent_fs_object.rb @@ -23,10 +23,6 @@ class Chef module ChefFS module FileSystem class NonexistentFSObject < BaseFSObject - def initialize(name, parent) - super - end - def exists? false end diff --git a/lib/chef/chef_fs/file_system/repository/acl.rb b/lib/chef/chef_fs/file_system/repository/acl.rb index e2ba2e8771..023ae11379 100644 --- a/lib/chef/chef_fs/file_system/repository/acl.rb +++ b/lib/chef/chef_fs/file_system/repository/acl.rb @@ -31,6 +31,13 @@ class Chef super end + def bare_name + if name == "organization" && parent.kind_of?(AclDir) + "organization.json" + else + name + end + end end end end diff --git a/lib/chef/chef_fs/file_system/repository/acls_sub_dir.rb b/lib/chef/chef_fs/file_system/repository/acls_sub_dir.rb index 8eee8e1d70..70c7d77480 100644 --- a/lib/chef/chef_fs/file_system/repository/acls_sub_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/acls_sub_dir.rb @@ -26,10 +26,6 @@ class Chef module Repository class AclsSubDir < Repository::Directory - def can_have_child?(name, is_dir) - !is_dir && File.extname(name) == ".json" - end - protected def make_child_entry(child_name) diff --git a/lib/chef/chef_fs/file_system/repository/base_file.rb b/lib/chef/chef_fs/file_system/repository/base_file.rb index d5ef26887e..a768bcf971 100644 --- a/lib/chef/chef_fs/file_system/repository/base_file.rb +++ b/lib/chef/chef_fs/file_system/repository/base_file.rb @@ -29,23 +29,48 @@ class Chef attr_reader :file_path attr_reader :data_handler + alias_method :display_path, :path + alias_method :display_name, :name + def initialize(name, parent) @parent = parent + + if %w{ .rb .json }.include? File.extname(name) + name = File.basename(name, ".*") + end + + file_path = "#{parent.file_path}/#{name}" + + Chef::Log.debug "BaseFile: Detecting file extension for #{name}" + ext = File.exist?(file_path + ".rb") ? ".rb" : ".json" + name += ext + file_path += ext + + Chef::Log.debug "BaseFile: got a file path of #{file_path} for #{name}" @name = name @path = Chef::ChefFS::PathUtils.join(parent.path, name) - @file_path = "#{parent.file_path}/#{name}" + @file_path = file_path end def dir? false end + # Used to compare names on disk to the API, for diffing. + def bare_name + File.basename(name, ".*") + end + def is_json_file? - File.extname(name) == ".json" + File.extname(file_path) == ".json" + end + + def is_ruby_file? + File.extname(file_path) == ".rb" end def name_valid? - !name.start_with?(".") && is_json_file? + !name.start_with?(".") && (is_json_file? || is_ruby_file?) end def fs_entry_valid? @@ -91,12 +116,19 @@ class Chef end def read - File.open(file_path, "rb") { |f| f.read } + if is_ruby_file? + data_handler.from_ruby(file_path).to_json + else + File.open(file_path, "rb") { |f| f.read } + end rescue Errno::ENOENT raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) end def write(content) + if is_ruby_file? + raise Chef::ChefFS::FileSystem::RubyFileError.new(:write, self) + end if content && write_pretty_json && is_json_file? content = minimize(content, self) end diff --git a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb index 703c0fc635..9d1538e46e 100644 --- a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb +++ b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb @@ -37,6 +37,10 @@ class Chef attr_reader :recursive attr_reader :file_path + alias_method :display_path, :path + alias_method :display_name, :name + alias_method :bare_name, :name + def initialize(name, parent, file_path = nil, ruby_only = false, recursive = false) @parent = parent @name = name diff --git a/lib/chef/chef_fs/file_system/repository/client_keys_sub_dir.rb b/lib/chef/chef_fs/file_system/repository/client_keys_sub_dir.rb index d231e41e78..6aafcfe294 100644 --- a/lib/chef/chef_fs/file_system/repository/client_keys_sub_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/client_keys_sub_dir.rb @@ -26,10 +26,6 @@ class Chef module Repository class ClientKeysSubDir < Repository::Directory - def can_have_child?(name, is_dir) - !is_dir && File.extname(name) == ".json" - end - protected def make_child_entry(child_name) diff --git a/lib/chef/chef_fs/file_system/repository/clients_dir.rb b/lib/chef/chef_fs/file_system/repository/clients_dir.rb index 31aa80a28f..01027f83ac 100644 --- a/lib/chef/chef_fs/file_system/repository/clients_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/clients_dir.rb @@ -26,9 +26,6 @@ class Chef module FileSystem module Repository class ClientsDir < Repository::Directory - def can_have_child?(name, is_dir) - !is_dir && File.extname(name) == ".json" - end def make_child_entry(child_name) Client.new(child_name, self) diff --git a/lib/chef/chef_fs/file_system/repository/containers_dir.rb b/lib/chef/chef_fs/file_system/repository/containers_dir.rb index 0fe541417a..2af496f418 100644 --- a/lib/chef/chef_fs/file_system/repository/containers_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/containers_dir.rb @@ -27,10 +27,6 @@ class Chef module Repository class ContainersDir < Repository::Directory - def can_have_child?(name, is_dir) - !is_dir && File.extname(name) == ".json" - end - def make_child_entry(child_name) Container.new(child_name, self) end diff --git a/lib/chef/chef_fs/file_system/repository/directory.rb b/lib/chef/chef_fs/file_system/repository/directory.rb index 20f6f82108..dae467993a 100644 --- a/lib/chef/chef_fs/file_system/repository/directory.rb +++ b/lib/chef/chef_fs/file_system/repository/directory.rb @@ -28,6 +28,10 @@ class Chef attr_reader :path attr_reader :file_path + alias_method :display_path, :path + alias_method :display_name, :name + alias_method :bare_name, :name + def initialize(name, parent, file_path = nil) @parent = parent @name = name diff --git a/lib/chef/chef_fs/file_system/repository/environments_dir.rb b/lib/chef/chef_fs/file_system/repository/environments_dir.rb index 2c9b31b29b..4d04348d6e 100644 --- a/lib/chef/chef_fs/file_system/repository/environments_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/environments_dir.rb @@ -27,10 +27,6 @@ class Chef module Repository class EnvironmentsDir < Repository::Directory - def can_have_child?(name, is_dir) - !is_dir && File.extname(name) == ".json" - end - def make_child_entry(child_name) Environment.new(child_name, self) end diff --git a/lib/chef/chef_fs/file_system/repository/groups_dir.rb b/lib/chef/chef_fs/file_system/repository/groups_dir.rb index e307000ad8..20728d1248 100644 --- a/lib/chef/chef_fs/file_system/repository/groups_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/groups_dir.rb @@ -27,10 +27,6 @@ class Chef module Repository class GroupsDir < Repository::Directory - def can_have_child?(name, is_dir) - !is_dir && File.extname(name) == ".json" - end - def make_child_entry(child_name) Group.new(child_name, self) end diff --git a/lib/chef/chef_fs/file_system/repository/nodes_dir.rb b/lib/chef/chef_fs/file_system/repository/nodes_dir.rb index d04bea0de2..33ca7ca709 100644 --- a/lib/chef/chef_fs/file_system/repository/nodes_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/nodes_dir.rb @@ -27,10 +27,6 @@ class Chef module Repository class NodesDir < Repository::Directory - def can_have_child?(name, is_dir) - !is_dir && File.extname(name) == ".json" - end - def make_child_entry(child_name) Node.new(child_name, self) end diff --git a/lib/chef/chef_fs/file_system/repository/policy_groups_dir.rb b/lib/chef/chef_fs/file_system/repository/policy_groups_dir.rb index 020464a634..4db85a93f7 100644 --- a/lib/chef/chef_fs/file_system/repository/policy_groups_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/policy_groups_dir.rb @@ -27,10 +27,6 @@ class Chef module Repository class PolicyGroupsDir < Repository::Directory - def can_have_child?(name, is_dir) - !is_dir && File.extname(name) == ".json" - end - def make_child_entry(child_name) PolicyGroup.new(child_name, self) end diff --git a/lib/chef/chef_fs/file_system/repository/roles_dir.rb b/lib/chef/chef_fs/file_system/repository/roles_dir.rb index f59af093bb..42f4376e71 100644 --- a/lib/chef/chef_fs/file_system/repository/roles_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/roles_dir.rb @@ -27,10 +27,6 @@ class Chef module Repository class RolesDir < Repository::Directory - def can_have_child?(name, is_dir) - !is_dir && File.extname(name) == ".json" - end - def make_child_entry(child_name) Role.new(child_name, self) end diff --git a/lib/chef/chef_fs/file_system/repository/users_dir.rb b/lib/chef/chef_fs/file_system/repository/users_dir.rb index 32cebf842d..9e8621575b 100644 --- a/lib/chef/chef_fs/file_system/repository/users_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/users_dir.rb @@ -27,10 +27,6 @@ class Chef module Repository class UsersDir < Repository::Directory - def can_have_child?(name, is_dir) - !is_dir && File.extname(name) == ".json" - end - def make_child_entry(child_name) User.new(child_name, self) end diff --git a/lib/chef/knife/cookbook_site_install.rb b/lib/chef/knife/cookbook_site_install.rb index 802fdd792b..45f3061d87 100644 --- a/lib/chef/knife/cookbook_site_install.rb +++ b/lib/chef/knife/cookbook_site_install.rb @@ -141,10 +141,15 @@ class Chef def extract_cookbook(upstream_file, version) ui.info("Uncompressing #{@cookbook_name} version #{version}.") - # FIXME: Detect if we have the bad tar from git on Windows: https://github.com/opscode/chef/issues/1753 extract_command = "tar zxvf \"#{convert_path upstream_file}\"" if Chef::Platform.windows? - extract_command << " --force-local" + tar_version = shell_out("tar --version").stdout.tr("\n", " ") + if tar_version =~ /GNU tar/ + Chef::Log.debug("GNU tar detected, adding --force-local") + extract_command << " --force-local" + else + Chef::Log.debug("non-GNU tar detected, not adding --force-local") + end end shell_out!(extract_command, :cwd => @install_path) end diff --git a/lib/chef/knife/list.rb b/lib/chef/knife/list.rb index 3d1583b270..fcfde0eb45 100644 --- a/lib/chef/knife/list.rb +++ b/lib/chef/knife/list.rb @@ -44,7 +44,6 @@ class Chef patterns = name_args.length == 0 ? [""] : name_args # Get the top-level matches - args = pattern_args_from(patterns) all_results = parallelize(pattern_args_from(patterns)) do |pattern| pattern_results = Chef::ChefFS::FileSystem.list(config[:local] ? local_fs : chef_fs, pattern).to_a @@ -95,7 +94,7 @@ class Chef printed_something = true end output "#{format_path(result)}:" - print_results(children.map { |result| maybe_add_slash(result.name, result.dir?) }.sort, "") + print_results(children.map { |result| maybe_add_slash(result.display_name, result.dir?) }.sort, "") end exit self.exit_code if self.exit_code diff --git a/lib/chef/monkey_patches/win32/registry.rb b/lib/chef/monkey_patches/win32/registry.rb index 1c1a103432..a08d67becf 100644 --- a/lib/chef/monkey_patches/win32/registry.rb +++ b/lib/chef/monkey_patches/win32/registry.rb @@ -22,6 +22,17 @@ require "win32/registry" module Win32 class Registry + # ::Win32::Registry#export_string is used when enumerating child + # keys and values and re encodes a UTF-16LE to the local codepage. + # This can result in encoding incompatibilities if the native codepage + # does not support the characters in the registry. There is an open bug + # in ruby at https://bugs.ruby-lang.org/issues/11410. Rather than converting + # the UTF-16LE originally returned by the win32 api, we encode to UTF-8 + # which will likely not result in any conversion error. + def export_string(str, enc = Encoding.default_internal || "utf-8") + str.encode(enc) + end + module API extend Chef::ReservedNames::Win32::API::Registry diff --git a/lib/chef/provider/package.rb b/lib/chef/provider/package.rb index 6d67cdbbb2..cac0fb6eb0 100644 --- a/lib/chef/provider/package.rb +++ b/lib/chef/provider/package.rb @@ -596,7 +596,7 @@ class Chef # @param args [String] variable number of string arguments # @return [String] nicely concatenated string or empty string def a_to_s(*args) - args.reject { |i| i.nil? || i == "" }.join(" ") + args.flatten.reject { |i| i.nil? || i == "" }.join(" ") end end end diff --git a/lib/chef/provider/package/apt.rb b/lib/chef/provider/package/apt.rb index ac730202b8..8af089e14a 100644 --- a/lib/chef/provider/package/apt.rb +++ b/lib/chef/provider/package/apt.rb @@ -17,132 +17,64 @@ # require "chef/provider/package" -require "chef/mixin/command" -require "chef/resource/package" +require "chef/resource/apt_package" class Chef class Provider class Package class Apt < Chef::Provider::Package + use_multipackage_api provides :package, platform_family: "debian" provides :apt_package, os: "linux" - # return [Hash] mapping of package name to Boolean value - attr_accessor :is_virtual_package - def initialize(new_resource, run_context) super - @is_virtual_package = {} end def load_current_resource - @current_resource = Chef::Resource::Package.new(@new_resource.name) - @current_resource.package_name(@new_resource.package_name) - check_all_packages_state(@new_resource.package_name) - @current_resource + @current_resource = Chef::Resource::AptPackage.new(new_resource.name) + current_resource.package_name(new_resource.package_name) + current_resource.version(get_current_versions) + current_resource end def define_resource_requirements super requirements.assert(:all_actions) do |a| - a.assertion { !@new_resource.source } + a.assertion { !new_resource.source } a.failure_message(Chef::Exceptions::Package, "apt package provider cannot handle source attribute. Use dpkg provider instead") end end - def default_release_options - # Use apt::Default-Release option only if provider supports it - "-o APT::Default-Release=#{@new_resource.default_release}" if @new_resource.respond_to?(:default_release) && @new_resource.default_release + def package_data + @package_data ||= Hash.new do |hash, key| + hash[key] = package_data_for(key) + end end - def check_package_state(pkg) - is_virtual_package = false - installed = false - installed_version = nil - candidate_version = nil - - shell_out_with_timeout!("apt-cache#{expand_options(default_release_options)} policy #{pkg}").stdout.each_line do |line| - case line - when /^\s{2}Installed: (.+)$/ - installed_version = $1 - if installed_version == "(none)" - Chef::Log.debug("#{@new_resource} current version is nil") - installed_version = nil - else - Chef::Log.debug("#{@new_resource} current version is #{installed_version}") - installed = true - end - when /^\s{2}Candidate: (.+)$/ - candidate_version = $1 - if candidate_version == "(none)" - # This may not be an appropriate assumption, but it shouldn't break anything that already worked -- btm - is_virtual_package = true - showpkg = shell_out_with_timeout!("apt-cache showpkg #{pkg}").stdout - providers = Hash.new - showpkg.rpartition(/Reverse Provides: ?#{$/}/)[2].each_line do |line| - provider, version = line.split - providers[provider] = version - end - # Check if the package providing this virtual package is installed - num_providers = providers.length - raise Chef::Exceptions::Package, "#{@new_resource.package_name} has no candidate in the apt-cache" if num_providers == 0 - # apt will only install a virtual package if there is a single providing package - raise Chef::Exceptions::Package, "#{@new_resource.package_name} is a virtual package provided by #{num_providers} packages, you must explicitly select one to install" if num_providers > 1 - # Check if the package providing this virtual package is installed - Chef::Log.info("#{@new_resource} is a virtual package, actually acting on package[#{providers.keys.first}]") - ret = check_package_state(providers.keys.first) - installed = ret[:installed] - installed_version = ret[:installed_version] - else - Chef::Log.debug("#{@new_resource} candidate version is #{$1}") - end - end + def get_current_versions + package_name_array.map do |package_name| + package_data[package_name][:current_version] end - - return { - installed_version: installed_version, - installed: installed, - candidate_version: candidate_version, - is_virtual_package: is_virtual_package, - } end - def check_all_packages_state(package) - installed_version = {} - candidate_version = {} - installed = {} - - [package].flatten.each do |pkg| - ret = check_package_state(pkg) - is_virtual_package[pkg] = ret[:is_virtual_package] - installed[pkg] = ret[:installed] - installed_version[pkg] = ret[:installed_version] - candidate_version[pkg] = ret[:candidate_version] + def get_candidate_versions + package_name_array.map do |package_name| + package_data[package_name][:candidate_version] end + end - if package.is_a?(Array) - @candidate_version = [] - final_installed_version = [] - [package].flatten.each do |pkg| - @candidate_version << candidate_version[pkg] - final_installed_version << installed_version[pkg] - end - @current_resource.version(final_installed_version) - else - @candidate_version = candidate_version[package] - @current_resource.version(installed_version[package]) - end + def candidate_version + @candidate_version ||= get_candidate_versions end def install_package(name, version) - name_array = [ name ].flatten - version_array = [ version ].flatten - package_name = name_array.zip(version_array).map do |n, v| - is_virtual_package[n] ? n : "#{n}=#{v}" - end.join(" ") - run_noninteractive("apt-get -q -y#{expand_options(default_release_options)}#{expand_options(@new_resource.options)} install #{package_name}") + package_name = name.zip(version).map do |n, v| + package_data[n][:virtual] ? n : "#{n}=#{v}" + end + run_noninteractive("apt-get -q -y", default_release_options, new_resource.options, "install", package_name) end def upgrade_package(name, version) @@ -150,24 +82,27 @@ class Chef end def remove_package(name, version) - package_name = [ name ].flatten.join(" ") - run_noninteractive("apt-get -q -y#{expand_options(@new_resource.options)} remove #{package_name}") + package_name = name.map do |n| + package_data[n][:virtual] ? resolve_virtual_package_name(n) : n + end + run_noninteractive("apt-get -q -y", new_resource.options, "remove", package_name) end def purge_package(name, version) - package_name = [ name ].flatten.join(" ") - run_noninteractive("apt-get -q -y#{expand_options(@new_resource.options)} purge #{package_name}") + package_name = name.map do |n| + package_data[n][:virtual] ? resolve_virtual_package_name(n) : n + end + run_noninteractive("apt-get -q -y", new_resource.options, "purge", package_name) end def preseed_package(preseed_file) - Chef::Log.info("#{@new_resource} pre-seeding package installation instructions") - run_noninteractive("debconf-set-selections #{preseed_file}") + Chef::Log.info("#{new_resource} pre-seeding package installation instructions") + run_noninteractive("debconf-set-selections", preseed_file) end def reconfig_package(name, version) - package_name = [ name ].flatten.join(" ") - Chef::Log.info("#{@new_resource} reconfiguring") - run_noninteractive("dpkg-reconfigure #{package_name}") + Chef::Log.info("#{new_resource} reconfiguring") + run_noninteractive("dpkg-reconfigure", name) end private @@ -175,8 +110,67 @@ class Chef # Runs command via shell_out with magic environment to disable # interactive prompts. Command is run with default localization rather # than forcing locale to "C", so command output may not be stable. - def run_noninteractive(command) - shell_out_with_timeout!(command, :env => { "DEBIAN_FRONTEND" => "noninteractive", "LC_ALL" => nil }) + def run_noninteractive(*args) + shell_out_with_timeout!(a_to_s(*args), :env => { "DEBIAN_FRONTEND" => "noninteractive" }) + end + + def default_release_options + # Use apt::Default-Release option only if provider supports it + "-o APT::Default-Release=#{new_resource.default_release}" if new_resource.respond_to?(:default_release) && new_resource.default_release + end + + def resolve_package_versions(pkg) + current_version = nil + candidate_version = nil + run_noninteractive("apt-cache", default_release_options, "policy", pkg).stdout.each_line do |line| + case line + when /^\s{2}Installed: (.+)$/ + current_version = ( $1 != "(none)" ) ? $1 : nil + Chef::Log.debug("#{new_resource} installed version for #{pkg} is #{$1}") + when /^\s{2}Candidate: (.+)$/ + candidate_version = ( $1 != "(none)" ) ? $1 : nil + Chef::Log.debug("#{new_resource} candidate version for #{pkg} is #{$1}") + end + end + [ current_version, candidate_version ] + end + + def resolve_virtual_package_name(pkg) + showpkg = run_noninteractive("apt-cache showpkg", pkg).stdout + partitions = showpkg.rpartition(/Reverse Provides: ?#{$/}/) + return nil if partitions[0] == "" && partitions[1] == "" # not found in output + set = partitions[2].lines.each_with_object(Set.new) do |line, acc| + # there may be multiple reverse provides for a single package + acc.add(line.split[0]) + end + if set.size > 1 + raise Chef::Exceptions::Package, "#{new_resource.package_name} is a virtual package provided by multiple packages, you must explicitly select one" + end + return set.to_a.first + end + + def package_data_for(pkg) + virtual = false + current_version = nil + candidate_version = nil + + current_version, candidate_version = resolve_package_versions(pkg) + + if candidate_version.nil? + newpkg = resolve_virtual_package_name(pkg) + + if newpkg + virtual = true + Chef::Log.info("#{new_resource} is a virtual package, actually acting on package[#{newpkg}]") + current_version, candidate_version = resolve_package_versions(newpkg) + end + end + + return { + current_version: current_version, + candidate_version: candidate_version, + virtual: virtual, + } end end diff --git a/lib/chef/provider/package/chocolatey.rb b/lib/chef/provider/package/chocolatey.rb index 44fb1de235..ebd3f987cd 100644 --- a/lib/chef/provider/package/chocolatey.rb +++ b/lib/chef/provider/package/chocolatey.rb @@ -256,6 +256,7 @@ EOS def parse_list_output(*args) list = [] choco_command(*args).stdout.each_line do |line| + next if line.start_with?("Chocolatey v") name, version = line.split("|") list << [ name.downcase, version.chomp ] end diff --git a/lib/chef/provider/package/yum.rb b/lib/chef/provider/package/yum.rb index dfd32fde55..64f1b2043c 100644 --- a/lib/chef/provider/package/yum.rb +++ b/lib/chef/provider/package/yum.rb @@ -46,7 +46,7 @@ class Chef lead = 0 tail = evr.size - if evr =~ %r{^([\d]+):} + if %r{^([\d]+):}.match(evr) # rubocop:disable Performance/RedundantMatch epoch = $1.to_i lead = $1.length + 1 elsif evr[0].ord == ":".ord @@ -54,7 +54,7 @@ class Chef lead = 1 end - if evr =~ %r{:?.*-(.*)$} + if %r{:?.*-(.*)$}.match(evr) # rubocop:disable Performance/RedundantMatch release = $1 tail = evr.length - release.length - lead - 1 @@ -443,7 +443,7 @@ class Chef # "mtr >= 2:0.71-3.0" # "mta" def self.parse(string) - if string =~ %r{^(\S+)\s+(>|>=|=|==|<=|<)\s+(\S+)$} + if %r{^(\S+)\s+(>|>=|=|==|<=|<)\s+(\S+)$}.match(string) # rubocop:disable Performance/RedundantMatch name = $1 if $2 == "=" flag = :== diff --git a/lib/chef/version.rb b/lib/chef/version.rb index 1afc811da1..19f3c78f13 100644 --- a/lib/chef/version.rb +++ b/lib/chef/version.rb @@ -21,7 +21,7 @@ class Chef CHEF_ROOT = File.expand_path("../..", __FILE__) - VERSION = "12.10.33" + VERSION = "12.10.43" end # |