~ubuntu-branches/ubuntu/karmic/chef/karmic

« back to all changes in this revision

Viewing changes to chef/lib/chef/provider/package/macports.rb

  • Committer: Bazaar Package Importer
  • Author(s): Joshua Timberman
  • Date: 2009-08-12 12:18:48 UTC
  • Revision ID: james.westby@ubuntu.com-20090812121848-yl38hpd7nfsl51xz
Tags: upstream-0.7.8
ImportĀ upstreamĀ versionĀ 0.7.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
class Chef
 
2
  class Provider
 
3
    class Package
 
4
      class Macports < Chef::Provider::Package
 
5
        def load_current_resource
 
6
          @current_resource = Chef::Resource::Package.new(@new_resource.name)
 
7
          @current_resource.package_name(@new_resource.package_name)
 
8
 
 
9
          @current_resource.version(current_installed_version)
 
10
          Chef::Log.debug("Current version is #{@current_resource.version}") if @current_resource.version
 
11
 
 
12
          @candidate_version = macports_candidate_version
 
13
 
 
14
          if !@new_resource.version and !@candidate_version
 
15
            raise Chef::Exceptions::Package, "Could not get a candidate version for this package -- #{@new_resource.name} does not seem to be a valid package!"
 
16
          end
 
17
 
 
18
          Chef::Log.debug("MacPorts candidate version is #{@candidate_version}") if @candidate_version
 
19
 
 
20
          @current_resource
 
21
        end
 
22
 
 
23
        def current_installed_version
 
24
          command = "port installed #{@new_resource.package_name}"
 
25
          output = get_response_from_command(command)
 
26
 
 
27
          response = nil
 
28
          output.each_line do |line|
 
29
            match = line.match(/^.+ @([^\s]+) \(active\)$/)
 
30
            response = match[1] if match
 
31
          end
 
32
          response
 
33
        end
 
34
 
 
35
        def macports_candidate_version
 
36
          command = "port info --version #{@new_resource.package_name}"
 
37
          output = get_response_from_command(command)
 
38
 
 
39
          match = output.match(/^version: (.+)$/)
 
40
 
 
41
          match ? match[1] : nil
 
42
        end
 
43
 
 
44
        def install_package(name, version)
 
45
          unless @current_resource.version == version
 
46
            command = "port install #{name}"
 
47
            command << " @#{version}" if version and !version.empty? 
 
48
            run_command(
 
49
              :command => command
 
50
            )
 
51
          end
 
52
        end
 
53
 
 
54
        def purge_package(name, version)
 
55
          command = "port uninstall #{name}"
 
56
          command << " @#{version}" if version and !version.empty?
 
57
          run_command(
 
58
            :command => command
 
59
          )
 
60
        end
 
61
 
 
62
        def remove_package(name, version)
 
63
          command = "port deactivate #{name}"
 
64
          command << " @#{version}" if version and !version.empty?
 
65
 
 
66
          run_command(
 
67
            :command => command
 
68
          )
 
69
        end
 
70
 
 
71
        def upgrade_package(name, version)
 
72
          # Saving this to a variable -- weird rSpec behavior
 
73
          # happens otherwise...
 
74
          current_version = @current_resource.version
 
75
 
 
76
          if current_version.nil? or current_version.empty?
 
77
            # Macports doesn't like when you upgrade a package
 
78
            # that hasn't been installed.
 
79
            install_package(name, version)
 
80
          elsif current_version != version
 
81
            run_command(
 
82
              :command => "port upgrade #{name} @#{version}"
 
83
            )
 
84
          end
 
85
        end
 
86
 
 
87
        private
 
88
        def get_response_from_command(command)
 
89
          output = nil
 
90
          status = popen4(command) do |pid, stdin, stdout, stderr|
 
91
            begin
 
92
              output = stdout.read
 
93
            rescue Exception
 
94
              raise Chef::Exceptions::Package, "Could not read from STDOUT on command: #{command}"
 
95
            end
 
96
          end
 
97
          unless status.exitstatus == 0 || status.exitstatus == 1
 
98
            raise Chef::Exceptions::Package, "#{command} failed - #{status.insect}!"
 
99
          end
 
100
          output
 
101
        end
 
102
      end
 
103
    end
 
104
  end
 
105
end