summaryrefslogtreecommitdiff
path: root/lib/chef/provider/package/openbsd.rb
blob: f0931d755588a63d2117114a764e366651a5a4f3 (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
#
# Authors:: Bryan McLellan (btm@loftninjas.org)
#           Matthew Landauer (matthew@openaustralia.org)
#           Richard Manyanza (liseki@nyikacraftsmen.com)
#           Scott Bonds (scott@ggr.com)
# Copyright:: Copyright (c) 2009 Bryan McLellan, Matthew Landauer
# Copyright:: Copyright (c) 2014 Richard Manyanza, Scott Bonds
# 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/resource/package'
require 'chef/provider/package'
require 'chef/mixin/shell_out'
require 'chef/mixin/get_source_from_package'

class Chef
  class Provider
    class Package
      class Openbsd < Chef::Provider::Package

        provides :package, os: "openbsd"

        include Chef::Mixin::ShellOut
        include Chef::Mixin::GetSourceFromPackage

        def initialize(*args)
          super
          @current_resource = Chef::Resource::Package.new(@new_resource.name)
          @new_resource.source(pkg_path) if !@new_resource.source
        end

        def load_current_resource
          @current_resource.package_name(@new_resource.package_name)
          @current_resource.version(installed_version)
          @current_resource
        end

        def install_package(name, version)
          unless @current_resource.version
            version_string  = ''
            version_string += "-#{version}" if version
            if parts = name.match(/^(.+?)--(.+)/) # use double-dash for stems with flavors, see man page for pkg_add
              name = parts[1]
            end
            shell_out!("pkg_add -r #{name}#{version_string}", :env => {"PKG_PATH" => @new_resource.source}).status
            Chef::Log.debug("#{@new_resource} installed from: #{@new_resource.source}")
          end
        end

        def remove_package(name, version)
          version_string  = ''
          version_string += "-#{version}" if version
          if parts = name.match(/^(.+?)--(.+)/)
            name = parts[1]
          end
          shell_out!("pkg_delete #{name}#{version_string}", :env => nil).status
        end

        private

        def installed_version
          if parts = @new_resource.package_name.match(/^(.+?)--(.+)/)
            name = parts[1]
          else
            name = @new_resource.package_name
          end
          pkg_info = shell_out!("pkg_info -e \"#{name}->0\"", :env => nil, :returns => [0,1])
          result = pkg_info.stdout[/^inst:#{Regexp.escape(name)}-(.+?)\s/, 1]
          Chef::Log.debug("installed_version of '#{@new_resource.package_name}' is '#{result}'")
          result
        end

        def candidate_version
          @candidate_version ||= begin
            version_string  = ''
            version_string += "-#{version}" if @new_resource.version
            pkg_info = shell_out!("pkg_info -I \"#{@new_resource.package_name}#{version_string}\"", :env => nil, :returns => [0,1])
            if parts = @new_resource.package_name.match(/^(.+?)--(.+)/)
              result = pkg_info.stdout[/^#{Regexp.escape(parts[1])}-(.+?)\s/, 1]
            else
              result = pkg_info.stdout[/^#{Regexp.escape(@new_resource.package_name)}-(.+?)\s/, 1]
            end
            Chef::Log.debug("candidate_version of '#{@new_resource.package_name}' is '#{result}'")
            result
          end
        end

        def pkg_path
          ENV['PKG_PATH'] || "http://ftp.OpenBSD.org/pub/#{node.kernel.name}/#{node.kernel.release}/packages/#{node.kernel.machine}/"
        end

      end
    end
  end
end