summaryrefslogtreecommitdiff
path: root/lib/chef/provider/package/rpm.rb
blob: 131587e066ab0756fbe9f83b1d4793c4fa1bd02a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#
# Author:: Joshua Timberman (<joshua@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'chef/provider/package'
require 'chef/mixin/command'
require 'chef/resource/package'
require 'chef/mixin/get_source_from_package'

class Chef
  class Provider
    class Package
      class Rpm < Chef::Provider::Package

        provides :rpm_package, os: [ "linux", "aix" ]

        include Chef::Mixin::GetSourceFromPackage

        def define_resource_requirements
          super

          requirements.assert(:all_actions) do |a|
            a.assertion { @package_source_exists }
            a.failure_message Chef::Exceptions::Package, "Package #{@new_resource.name} not found: #{@new_resource.source}"
            a.whyrun "Assuming package #{@new_resource.name} would have been made available."
          end
          requirements.assert(:all_actions) do |a|
            a.assertion { !@rpm_status.nil? && (@rpm_status.exitstatus == 0 || @rpm_status.exitstatus == 1) }
            a.failure_message Chef::Exceptions::Package, "Unable to determine current version due to RPM failure. Detail: #{@rpm_status.inspect}"
            a.whyrun "Assuming current version would have been determined for package#{@new_resource.name}."
          end
        end

        def load_current_resource
          @package_source_provided = true
          @package_source_exists = true

          @current_resource = Chef::Resource::Package.new(@new_resource.name)
          @current_resource.package_name(@new_resource.package_name)
          @new_resource.version(nil)

          if @new_resource.source
            unless ::File.exists?(@new_resource.source)
              @package_source_exists = false
              return
            end

            Chef::Log.debug("#{@new_resource} checking rpm status")
            status = popen4("rpm -qp --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' #{@new_resource.source}") do |pid, stdin, stdout, stderr|
              stdout.each do |line|
                case line
                when /^([\w\d+_.-]+)\s([\w\d_.-]+)$/
                  @current_resource.package_name($1)
                  @new_resource.version($2)
                  @candidate_version = $2
                end
              end
            end
          else
            if Array(@new_resource.action).include?(:install)
              @package_source_exists = false
              return
            end
          end

          Chef::Log.debug("#{@new_resource} checking install state")
          @rpm_status = popen4("rpm -q --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' #{@current_resource.package_name}") do |pid, stdin, stdout, stderr|
            stdout.each do |line|
              case line
              when /^([\w\d+_.-]+)\s([\w\d_.-]+)$/
                Chef::Log.debug("#{@new_resource} current version is #{$2}")
                @current_resource.version($2)
              end
            end
          end

          @current_resource
        end

        def install_package(name, version)
          unless @current_resource.version
            shell_out!( "rpm #{@new_resource.options} -i #{@new_resource.source}" )
          else
            shell_out!( "rpm #{@new_resource.options} -U #{@new_resource.source}" )
          end
        end

        alias_method :upgrade_package, :install_package

        def remove_package(name, version)
          if version
            shell_out!( "rpm #{@new_resource.options} -e #{name}-#{version}" )
          else
            shell_out!( "rpm #{@new_resource.options} -e #{name}" )
          end
        end

      end
    end
  end
end