[packages/chef/chef-14] poldek package provider rewritten to support chef-12 package array

glen glen at pld-linux.org
Sat Jul 21 22:27:52 CEST 2018


commit bcb6b0f645538e5cbb225ce4c43fb03a9a9bc552
Author: Elan Ruusamäe <glen at pld-linux.org>
Date:   Sat Jul 21 23:23:46 2018 +0300

    poldek package provider rewritten to support chef-12 package array

 chef.spec    |   2 +-
 poldek.patch | 165 +++++++++++++++++++++++++++++++++--------------------------
 2 files changed, 93 insertions(+), 74 deletions(-)
---
diff --git a/chef.spec b/chef.spec
index d05ebad..0d36dfa 100644
--- a/chef.spec
+++ b/chef.spec
@@ -8,7 +8,7 @@
 Summary:	A systems integration framework, built to bring the benefits of configuration management to your entire infrastructure
 Name:		chef
 Version:	14.1.36
-Release:	0.7
+Release:	0.8
 License:	Apache v2.0
 Group:		Networking/Admin
 Source0:	https://github.com/chef/chef/archive/v%{version}/%{name}-%{version}.tar.gz
diff --git a/poldek.patch b/poldek.patch
index 61c1c95..adfe888 100644
--- a/poldek.patch
+++ b/poldek.patch
@@ -4,10 +4,10 @@ https://github.com/chef/chef/pull/7482
 
 diff --git a/lib/chef/provider/package/poldek.rb b/lib/chef/provider/package/poldek.rb
 new file mode 100644
-index 000000000..63e3961a0
+index 000000000..a4d17ba67
 --- /dev/null
 +++ b/lib/chef/provider/package/poldek.rb
-@@ -0,0 +1,125 @@
+@@ -0,0 +1,137 @@
 +#
 +# Author:: Elan Ruusamäe (glen at pld-linux.org)
 +# Copyright:: Copyright (c) 2013,2018 Elan Ruusamäe
@@ -30,105 +30,117 @@ index 000000000..63e3961a0
 +require 'chef/provider/package'
 +require 'chef/mixin/shell_out'
 +require 'chef/resource/package'
-+require 'chef/mixin/get_source_from_package'
 +
 +class Chef
 +  class Provider
 +    class Package
 +      class Poldek < Chef::Provider::Package
 +        include Chef::Mixin::ShellOut
-+        attr_accessor :is_virtual_package
++
++        allow_nils
++        use_multipackage_api
 +
 +        provides :package, platform_family: "pld"
 +
 +        def load_current_resource
-+            Chef::Log.debug("#{@new_resource} loading current resource")
-+            @current_resource = Chef::Resource::Package.new(@new_resource.name)
-+            @current_resource.package_name(@new_resource.package_name)
-+            @current_resource.version(nil)
-+            check_package_state
-+            @current_resource # modified by check_package_state
++          logger.debug("#{new_resource} loading current resource")
++          @current_resource = Chef::Resource::Package.new(new_resource.name)
++          current_resource.package_name(new_resource.package_name)
++          current_resource.version(get_current_versions)
++          current_resource
 +        end
 +
-+        def check_package_state()
-+            Chef::Log.debug("#{@new_resource} checking package #{@new_resource.package_name}")
-+
-+            installed = false
-+            @current_resource.version(nil)
++        def candidate_version
++          @candidate_version ||= get_candidate_versions
++        end
 +
-+            out = shell_out!("rpm -q #{@new_resource.package_name}", :env => nil, :returns => [0,1])
-+            if out.stdout
-+                Chef::Log.debug("rpm STDOUT: #{out.stdout}");
-+                version = version_from_nvra(out.stdout)
-+                if version
-+                    @current_resource.version(version)
-+                    installed = true
-+                end
-+            end
++        def get_current_versions
++          names = package_name_array
++          logger.debug("#{new_resource} checking current version: #{names}")
 +
-+            return installed
++          # rpm works as expected: output is returned in order as input given, even duplicates
++          cmd = rpm("-q", "--qf", "%{NAME} %{VERSION}\n", names)
++          versions_from_name_list(cmd.stdout, names)
 +        end
 +
-+        def candidate_version
-+            Chef::Log.debug("poldek check candidate version for #{@new_resource.package_name}");
-+            return @candidate_version if @candidate_version
-+
-+            update_indexes
-+            cmd = "poldek -q --uniq --skip-installed #{expand_options(@new_resource.options)} --cmd 'ls #{@new_resource.package_name}'"
-+            out = shell_out!(cmd, :env => nil, :returns => [0,1,255])
-+            if out.stdout
-+                Chef::Log.debug("poldek STDOUT: #{out.stdout}");
-+                version = version_from_nvra(out.stdout)
-+                if version
-+                    @candidate_version = version
-+                end
-+            end
-+            unless @candidate_version
-+                raise Chef::Exceptions::Package, "poldek does not have a version of package #{@new_resource.package_name}"
-+            end
-+            @candidate_version
-+        end
++        def get_candidate_versions
++          names = package_name_array
++          logger.debug("#{new_resource} check candidate version");
++
++          update_indexes
 +
-+        def install_package(name, version)
-+            Chef::Log.debug("#{@new_resource} installing package #{name}-#{version}")
-+            package = "#{name}-#{version}"
-+            update_indexes
-+            out = shell_out!("poldek --noask #{expand_options(@new_resource.options)} -u #{package}", :env => nil)
++          # poldek works unexpectedly: packages that don't exist are printed as errors first, and names are de-duplicated
++          cmd = poldek(%w{--uniq --skip-installed} + options.to_a + ["--cmd", "ls --qf '%{NAME} %{VERSION}\n'", names])
++          versions_from_name_list(cmd.stdout, names)
 +        end
 +
-+        def upgrade_package(name, version)
-+            Chef::Log.debug("#{@new_resource} upgrading package #{name}-#{version}")
-+            install_package(name, version)
++        def install_package(names, versions)
++          logger.trace("#{new_resource} installing package #{names} version #{versions}")
++          update_indexes
++          poldek(options, "-u", names)
 +        end
 +
-+        def remove_package(name, version)
-+            Chef::Log.debug("#{@new_resource} removing package #{name}-#{version}")
-+            package = "#{name}-#{version}"
-+            out = shell_out!("poldek --noask #{expand_options(@new_resource.options)} -e #{package}", :env => nil)
++        def upgrade_package(names, versions)
++          logger.trace("#{new_resource} upgrading package #{names} version #{versions}")
++          install_package(names, versions)
 +        end
 +
-+        def purge_package(name, version)
-+            remove_package(name, version)
++        def remove_package(names, versions)
++          logger.trace("#{new_resource} removing package #{names} version #{versions}")
++          poldek(options, "-e", names)
 +        end
 +
 +        private
 +        @@updated = Hash.new
 +
-+        def version_from_nvra(stdout)
-+            stdout[/^#{Regexp.escape(@new_resource.package_name)}-(.+)/, 1]
-+        end
-+
 +        def update_indexes()
-+            Chef::Log.debug("#{@new_resource} call update indexes #{expand_options(@new_resource.options)}")
-+            checksum = Digest::MD5.hexdigest(@new_resource.options || '').to_s
++            checksum = Digest::MD5.hexdigest(opts).to_s
 +
 +            if @@updated[checksum]
 +                return
 +            end
-+            Chef::Log.debug("#{@new_resource} updating package indexes: #{expand_options(@new_resource.options)}")
-+            shell_out!("poldek --up #{expand_options(@new_resource.options)}", :env => nil)
++
++            logger.debug("#{@new_resource} updating package indexe")
++            poldek("--up", options, :env => nil)
 +            @@updated[checksum] = true
 +        end
++
++        def opts
++          expand_options(options)
++        end
++
++        def versions_from_name_list(input, names)
++          packages = extract_packages(input)
++          versions = match_versions(names, packages)
++          versions
++        end
++
++        def extract_packages(output)
++          packages = {}
++          output.each_line do |line|
++            case line.rstrip
++            when /^package (.+) is not installed$/
++            when /(.+): no such package or directory$/
++            when /^(.+?) (.+)$/
++              packages[$1] = $2
++            end
++          end
++          packages
++        end
++
++        def match_versions(names, packages)
++          names.map do |name|
++            packages[name]
++          end
++        end
++
++        def rpm(*args)
++          shell_out_compact_timeout!("rpm", *args, env: nil, returns: [0, 1])
++        end
++
++        def poldek(*args)
++          shell_out_compact_timeout!(%w{poldek -q --noask}, *args, env: nil, returns: [0, 1, 255])
++        end
 +      end
 +    end
 +  end
@@ -147,13 +159,13 @@ index cd265b061..075ffd7e8 100644
  require "chef/provider/package/rubygems"
 diff --git a/lib/chef/resource/poldek_package.rb b/lib/chef/resource/poldek_package.rb
 new file mode 100644
-index 000000000..cdc8f9209
+index 000000000..4db9aa824
 --- /dev/null
 +++ b/lib/chef/resource/poldek_package.rb
-@@ -0,0 +1,34 @@
+@@ -0,0 +1,41 @@
 +#
 +# Author:: Elan Ruusamäe (glen at pld-linux.org)
-+# Copyright:: Copyright (c) 2013 Elan Ruusamäe
++# Copyright:: Copyright (c) 2013,2018 Elan Ruusamäe
 +# License:: Apache License, Version 2.0
 +#
 +# Licensed under the Apache License, Version 2.0 (the "License");
@@ -175,13 +187,20 @@ index 000000000..cdc8f9209
 +class Chef
 +  class Resource
 +    class PoldekPackage < Chef::Resource::Package
++      extend Chef::Mixin::Which
 +
-+      def initialize(name, run_context=nil)
-+        super
-+        @resource_name = :poldek_package
-+        @provider = Chef::Provider::Package::Poldek
++      resource_name :poldek_package
++
++      provides :package do
++        which("poldek")
 +      end
 +
++      provides :poldek_package
++
++      description "Use the poldek_package resource to install, upgrade, and remove packages with poldek."
++      introduced "14.3"
++
++      allowed_actions :install, :upgrade, :remove
 +    end
 +  end
 +end
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/packages/chef.git/commitdiff/bcb6b0f645538e5cbb225ce4c43fb03a9a9bc552



More information about the pld-cvs-commit mailing list