summaryrefslogtreecommitdiff
path: root/lib/chef/provider/package/rpm.rb
blob: d3f9b2f5f0bfe4ba03c484a5a9a40ba7e0d43d72 (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
115
116
117
118
119
120
121
122
123
124
125
126
#
# Author:: Joshua Timberman (<joshua@chef.io>)
# Copyright:: Copyright 2008-2016, Chef Software 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/resource/package"
require "chef/mixin/get_source_from_package"
require "chef/provider/package/yum/rpm_utils"

class Chef
  class Provider
    class Package
      class Rpm < Chef::Provider::Package
        provides :rpm_package

        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)

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

            Chef::Log.debug("#{new_resource} checking rpm status")
            shell_out_compact_timeout!("rpm", "-qp", "--queryformat", "%{NAME} %{VERSION}-%{RELEASE}\n", new_resource.source).stdout.each_line do |line|
              case line
              when /^(\S+)\s(\S+)$/
                current_resource.package_name($1)
                new_resource.version($2)
                @candidate_version = $2
              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 = shell_out_compact_timeout("rpm", "-q", "--queryformat", "%{NAME} %{VERSION}-%{RELEASE}\n", current_resource.package_name)
          @rpm_status.stdout.each_line do |line|
            case line
            when /^(\S+)\s(\S+)$/
              Chef::Log.debug("#{new_resource} current version is #{$2}")
              current_resource.version($2)
            end
          end

          current_resource
        end

        def install_package(name, version)
          if current_resource.version
            if allow_downgrade
              shell_out_compact_timeout!("rpm", options, "-U", "--oldpackage", new_resource.source)
            else
              shell_out_compact_timeout!("rpm", options, "-U", new_resource.source)
            end
          else
            shell_out_compact_timeout!("rpm", options, "-i", new_resource.source)
          end
        end

        alias upgrade_package install_package

        def remove_package(name, version)
          if version
            shell_out_compact_timeout!("rpm", options, "-e", "#{name}-#{version}")
          else
            shell_out_compact_timeout!("rpm", options, "-e", name)
          end
        end

        private

        def version_compare(v1, v2)
          Chef::Provider::Package::Yum::RPMVersion.parse(v1) <=> Chef::Provider::Package::Yum::RPMVersion.parse(v2)
        end

        def uri_scheme?(str)
          scheme = URI.split(str).first
          return false unless scheme
          %w{http https ftp file}.include?(scheme.downcase)
        rescue URI::InvalidURIError
          return false
        end
      end
    end
  end
end