From 602e09a72c6617ee01ca3febb519d3fd36bd2b8c Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Mon, 23 Nov 2015 19:59:02 -0800 Subject: dpkg multipackage and bonus fixes - multipackages dpkg_package - fixes edge conditions in load-current-resource around purging/removing packages that threw errors before - fixes the ability to purge packages that have been removed - adds a lot of functional tests for dpkg_package --- lib/chef/provider/package.rb | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'lib/chef/provider/package.rb') diff --git a/lib/chef/provider/package.rb b/lib/chef/provider/package.rb index 888b171fa4..c78d125f96 100644 --- a/lib/chef/provider/package.rb +++ b/lib/chef/provider/package.rb @@ -18,6 +18,7 @@ require 'chef/mixin/shell_out' require 'chef/mixin/command' +require 'chef/mixin/subclass_directive' require 'chef/log' require 'chef/file_cache' require 'chef/platform' @@ -27,6 +28,12 @@ class Chef class Package < Chef::Provider include Chef::Mixin::Command include Chef::Mixin::ShellOut + include Chef::Mixin::SubclassDirective + + # subclasses declare this if they want all their arguments as arrays of packages and names + subclass_directive :use_multipackage_api + # subclasses declare this if they want sources (filenames) pulled from their package names + subclass_directive :use_package_name_for_source # # Hook that subclasses use to populate the candidate_version(s) @@ -44,6 +51,8 @@ class Chef end def check_resource_semantics! + # FIXME: this is not universally true and subclasses are needing to override this and no-ops it. It should be turned into + # another "subclass_directive" and the apt and yum providers should declare that they need this behavior. if new_resource.package_name.is_a?(Array) && new_resource.source != nil raise Chef::Exceptions::InvalidResourceSpecification, "You may not specify both multipackage and source" end @@ -211,7 +220,7 @@ class Chef # @todo use composition rather than inheritance def multipackage_api_adapter(name, version) - if supports_arrays? + if use_multipackage_api? yield [name].flatten, [version].flatten else yield name, version @@ -242,7 +251,7 @@ class Chef raise( Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :reconfig" ) end - # this is heavily used by subclasses + # used by subclasses. deprecated. use #a_to_s instead. def expand_options(options) options ? " #{options}" : "" end @@ -298,24 +307,6 @@ class Chef [ thing ].flatten end - class << self - attr_accessor :supports_arrays - - def supports_arrays? - !!@supports_arrays - end - - private - - def use_multipackage_api - @supports_arrays = true - end - end - - def supports_arrays? - self.class.supports_arrays? - end - private # Returns the package names which need to be modified. If the resource was called with an array of packages @@ -528,6 +519,16 @@ class Chef end args end + + # Helper for sublcasses to convert an array of string args into a string. It + # will compact nil or empty strings in the array and will join the array elements + # with spaces, without introducing any double spaces for nil/empty elements. + # + # @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(" ") + end end end end -- cgit v1.2.1