]> git.pld-linux.org Git - packages/chef.git/blob - poldek.patch
up to 14.13.16
[packages/chef.git] / poldek.patch
1 poldek package manager support
2
3 https://github.com/chef/chef/pull/7482
4
5 diff --git a/lib/chef/provider/package/poldek.rb b/lib/chef/provider/package/poldek.rb
6 new file mode 100644
7 index 000000000..c7dc21862
8 --- /dev/null
9 +++ b/lib/chef/provider/package/poldek.rb
10 @@ -0,0 +1,137 @@
11 +#
12 +# Author:: Elan Ruusamäe (glen@pld-linux.org)
13 +# Copyright:: Copyright (c) 2013,2018 Elan Ruusamäe
14 +# License:: Apache License, Version 2.0
15 +#
16 +# Licensed under the Apache License, Version 2.0 (the "License");
17 +# you may not use this file except in compliance with the License.
18 +# You may obtain a copy of the License at
19 +#
20 +#     http://www.apache.org/licenses/LICENSE-2.0
21 +#
22 +# Unless required by applicable law or agreed to in writing, software
23 +# distributed under the License is distributed on an "AS IS" BASIS,
24 +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 +# See the License for the specific language governing permissions and
26 +# limitations under the License.
27 +#
28 +
29 +require 'digest/md5'
30 +require 'chef/provider/package'
31 +require 'chef/mixin/shell_out'
32 +require 'chef/resource/package'
33 +
34 +class Chef
35 +  class Provider
36 +    class Package
37 +      class Poldek < Chef::Provider::Package
38 +        include Chef::Mixin::ShellOut
39 +
40 +        allow_nils
41 +        use_multipackage_api
42 +
43 +        provides :package, platform_family: "pld"
44 +
45 +        def load_current_resource
46 +          logger.debug("#{new_resource} loading current resource")
47 +          @current_resource = Chef::Resource::Package.new(new_resource.name)
48 +          current_resource.package_name(new_resource.package_name)
49 +          current_resource.version(get_current_versions)
50 +          current_resource
51 +        end
52 +
53 +        def candidate_version
54 +          @candidate_version ||= get_candidate_versions
55 +        end
56 +
57 +        def get_current_versions
58 +          names = package_name_array
59 +          logger.debug("#{new_resource} checking current version: #{names}")
60 +
61 +          # rpm works as expected: output is returned in order as input given, even duplicates
62 +          cmd = rpm("-q", "--qf", "%{NAME} %{VERSION}\n", names)
63 +          versions_from_name_list(cmd.stdout, names)
64 +        end
65 +
66 +        def get_candidate_versions
67 +          names = package_name_array
68 +          logger.debug("#{new_resource} check candidate version");
69 +
70 +          update_indexes
71 +
72 +          # poldek works unexpectedly: packages that don't exist are printed as errors first, and names are de-duplicated
73 +          cmd = poldek(%w{--uniq --skip-installed} + options.to_a + ["--cmd", "ls --qf '%{NAME} %{VERSION}\n'", names])
74 +          versions_from_name_list(cmd.stdout, names)
75 +        end
76 +
77 +        def install_package(names, versions)
78 +          logger.trace("#{new_resource} installing package #{names} version #{versions}")
79 +          update_indexes
80 +          poldek("-u", names)
81 +        end
82 +
83 +        def upgrade_package(names, versions)
84 +          logger.trace("#{new_resource} upgrading package #{names} version #{versions}")
85 +          install_package(names, versions)
86 +        end
87 +
88 +        def remove_package(names, versions)
89 +          logger.trace("#{new_resource} removing package #{names} version #{versions}")
90 +          poldek("-e", names)
91 +        end
92 +
93 +        private
94 +        @@updated = Hash.new
95 +
96 +        def update_indexes()
97 +          checksum = Digest::MD5.hexdigest(opts).to_s
98 +
99 +          if @@updated[checksum]
100 +              return
101 +          end
102 +
103 +          logger.debug("#{new_resource} updating package indexes")
104 +          poldek("--up")
105 +          @@updated[checksum] = true
106 +        end
107 +
108 +        def opts
109 +          expand_options(options)
110 +        end
111 +
112 +        def versions_from_name_list(input, names)
113 +          packages = extract_packages(input)
114 +          versions = match_versions(names, packages)
115 +          versions
116 +        end
117 +
118 +        def extract_packages(output)
119 +          packages = {}
120 +          output.each_line do |line|
121 +            case line.rstrip
122 +            when /^package (.+) is not installed$/
123 +            when /(.+): no such package or directory$/
124 +            when /^(.+?) (.+)$/
125 +              packages[$1] = $2
126 +            end
127 +          end
128 +          packages
129 +        end
130 +
131 +        def match_versions(names, packages)
132 +          names.map do |name|
133 +            packages[name]
134 +          end
135 +        end
136 +
137 +        def rpm(*args)
138 +          shell_out_compact_timeout!("rpm", *args, env: nil, returns: [0, 1])
139 +        end
140 +
141 +        def poldek(*args)
142 +          shell_out_compact_timeout!(%w{poldek -q --noask}, options, *args, env: nil, returns: [0, 1, 255])
143 +        end
144 +      end
145 +    end
146 +  end
147 +end
148 diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb
149 index cd265b061..075ffd7e8 100644
150 --- a/lib/chef/providers.rb
151 +++ b/lib/chef/providers.rb
152 @@ -74,6 +74,7 @@ require "chef/provider/package/macports"
153  require "chef/provider/package/openbsd"
154  require "chef/provider/package/pacman"
155  require "chef/provider/package/portage"
156 +require 'chef/provider/package/poldek'
157  require "chef/provider/package/paludis"
158  require "chef/provider/package/rpm"
159  require "chef/provider/package/rubygems"
160 diff --git a/lib/chef/resource/poldek_package.rb b/lib/chef/resource/poldek_package.rb
161 new file mode 100644
162 index 000000000..4db9aa824
163 --- /dev/null
164 +++ b/lib/chef/resource/poldek_package.rb
165 @@ -0,0 +1,41 @@
166 +#
167 +# Author:: Elan Ruusamäe (glen@pld-linux.org)
168 +# Copyright:: Copyright (c) 2013,2018 Elan Ruusamäe
169 +# License:: Apache License, Version 2.0
170 +#
171 +# Licensed under the Apache License, Version 2.0 (the "License");
172 +# you may not use this file except in compliance with the License.
173 +# You may obtain a copy of the License at
174 +#
175 +#     http://www.apache.org/licenses/LICENSE-2.0
176 +#
177 +# Unless required by applicable law or agreed to in writing, software
178 +# distributed under the License is distributed on an "AS IS" BASIS,
179 +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
180 +# See the License for the specific language governing permissions and
181 +# limitations under the License.
182 +#
183 +
184 +require 'chef/resource/package'
185 +require 'chef/provider/package/poldek'
186 +
187 +class Chef
188 +  class Resource
189 +    class PoldekPackage < Chef::Resource::Package
190 +      extend Chef::Mixin::Which
191 +
192 +      resource_name :poldek_package
193 +
194 +      provides :package do
195 +        which("poldek")
196 +      end
197 +
198 +      provides :poldek_package
199 +
200 +      description "Use the poldek_package resource to install, upgrade, and remove packages with poldek."
201 +      introduced "14.3"
202 +
203 +      allowed_actions :install, :upgrade, :remove
204 +    end
205 +  end
206 +end
This page took 0.045633 seconds and 3 git commands to generate.