summaryrefslogtreecommitdiff
path: root/lib/chef/provider/package/freebsd/pkgng.rb
blob: de7bea63872938ecb557a4543daf5dc4d5eaa188 (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
#
# Authors:: Richard Manyanza (liseki@nyikacraftsmen.com)
# Copyright:: Copyright 2014-2016, Richard Manyanza
# 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/freebsd/base"

class Chef
  class Provider
    class Package
      module Freebsd
        class Pkgng < Base

          def install_package(name, version)
            unless @current_resource.version
              case @new_resource.source
              when /^(http|ftp|\/)/
                shell_out_with_timeout!("pkg add#{expand_options(@new_resource.options)} #{@new_resource.source}", :env => { "LC_ALL" => nil }).status
                Chef::Log.debug("#{@new_resource} installed from: #{@new_resource.source}")

              else
                shell_out_with_timeout!("pkg install -y#{expand_options(@new_resource.options)} #{name}", :env => { "LC_ALL" => nil }).status
              end
            end
          end

          def remove_package(name, version)
            options = @new_resource.options && @new_resource.options.sub(repo_regex, "")
            options && !options.empty? || options = nil
            shell_out_with_timeout!("pkg delete -y#{expand_options(options)} #{name}#{version ? '-' + version : ''}", :env => nil).status
          end

          def current_installed_version
            pkg_info = shell_out_with_timeout!("pkg info \"#{@new_resource.package_name}\"", :env => nil, :returns => [0, 70])
            pkg_info.stdout[/^Version +: (.+)$/, 1]
          end

          def candidate_version
            @new_resource.source ? file_candidate_version : repo_candidate_version
          end

          private

          def file_candidate_version
            @new_resource.source[/#{Regexp.escape(@new_resource.package_name)}-(.+)\.txz/, 1]
          end

          def repo_candidate_version
            if @new_resource.options && @new_resource.options.match(repo_regex)
              options = $1
            end

            pkg_query = shell_out_with_timeout!("pkg rquery#{expand_options(options)} '%v' #{@new_resource.package_name}", :env => nil)
            pkg_query.exitstatus.zero? ? pkg_query.stdout.strip.split(/\n/).last : nil
          end

          def repo_regex
            /(-r\s?\S+)\b/
          end

        end
      end
    end
  end
end