diff options
Diffstat (limited to 'lib/chef/provider/package/pacman.rb')
-rw-r--r-- | lib/chef/provider/package/pacman.rb | 74 |
1 files changed, 32 insertions, 42 deletions
diff --git a/lib/chef/provider/package/pacman.rb b/lib/chef/provider/package/pacman.rb index bd8028d881..2de2d12579 100644 --- a/lib/chef/provider/package/pacman.rb +++ b/lib/chef/provider/package/pacman.rb @@ -16,9 +16,8 @@ # limitations under the License. # -require "chef/provider/package" -require "chef/mixin/command" -require "chef/resource/package" +require_relative "../package" +require_relative "../../resource/package" class Chef class Provider @@ -26,64 +25,55 @@ class Chef class Pacman < Chef::Provider::Package provides :package, platform: "arch" - provides :pacman_package, os: "linux" + provides :pacman_package - def load_current_resource - @current_resource = Chef::Resource::Package.new(@new_resource.name) - @current_resource.package_name(@new_resource.package_name) - - Chef::Log.debug("#{@new_resource} checking pacman for #{@new_resource.package_name}") - status = shell_out_with_timeout("pacman -Qi #{@new_resource.package_name}") - status.stdout.each_line do |line| - case line - when /^Version(\s?)*: (.+)$/ - Chef::Log.debug("#{@new_resource} current version is #{$2}") - @current_resource.version($2) - end - end - - unless status.exitstatus == 0 || status.exitstatus == 1 - raise Chef::Exceptions::Package, "pacman failed - #{status.inspect}!" - end + use_multipackage_api + allow_nils - @current_resource - end - - def candidate_version - return @candidate_version if @candidate_version + def load_current_resource + @current_resource = Chef::Resource::Package.new(new_resource.name) + current_resource.package_name(new_resource.package_name) + current_resource.version = [] repos = %w{extra core community} - if ::File.exists?("/etc/pacman.conf") + if ::File.exist?("/etc/pacman.conf") pacman = ::File.read("/etc/pacman.conf") repos = pacman.scan(/\[(.+)\]/).flatten end - package_repos = repos.map { |r| Regexp.escape(r) }.join("|") - - status = shell_out_with_timeout("pacman -Sl") - status.stdout.each_line do |line| - case line - when /^(#{package_repos}) #{Regexp.escape(@new_resource.package_name)} (.+)$/ - # $2 contains a string like "4.4.0-1" or "3.10-4 [installed]" - # simply split by space and use first token - @candidate_version = $2.split(" ").first - end - end + repos = Regexp.union(repos) + status = shell_out("pacman", "-Sl") unless status.exitstatus == 0 || status.exitstatus == 1 raise Chef::Exceptions::Package, "pacman failed - #{status.inspect}!" end - unless @candidate_version - raise Chef::Exceptions::Package, "pacman does not have a version of package #{@new_resource.package_name}" + pkg_db_data = status.stdout + @candidate_version = [] + package_name_array.each do |pkg| + pkg_data = pkg_db_data.match(/(#{repos}) #{pkg} (?<candidate>.*?-[0-9]+)(?<installed> \[.*?( (?<current>.*?-[0-9]+))?\])?\n/m) + unless pkg_data + raise Chef::Exceptions::Package, "pacman does not have a version of package #{pkg}" + end + + @candidate_version << pkg_data[:candidate] + if pkg_data[:installed] + current_resource.version << (pkg_data[:current] || pkg_data[:candidate]) + else + current_resource.version << nil + end end + current_resource + end + + def candidate_version @candidate_version end def install_package(name, version) - shell_out_with_timeout!( "pacman --sync --noconfirm --noprogressbar#{expand_options(@new_resource.options)} #{name}" ) + shell_out!("pacman", "--sync", "--noconfirm", "--noprogressbar", options, *name) end def upgrade_package(name, version) @@ -91,7 +81,7 @@ class Chef end def remove_package(name, version) - shell_out_with_timeout!( "pacman --remove --noconfirm --noprogressbar#{expand_options(@new_resource.options)} #{name}" ) + shell_out!("pacman", "--remove", "--noconfirm", "--noprogressbar", options, *name) end def purge_package(name, version) |