~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to lib/puppet/provider/package/portage.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Puppet::Type.type(:package).provide :portage do
 
1
require 'puppet/provider/package'
 
2
 
 
3
Puppet::Type.type(:package).provide :portage, :parent => Puppet::Provider::Package do
2
4
    desc "Provides packaging support for Gentoo's portage system."
3
5
 
4
 
    commands :emerge => "/usr/bin/emerge", :eix => "/usr/bin/eix"
 
6
    has_feature :versionable
 
7
 
 
8
    commands :emerge => "/usr/bin/emerge", :eix => "/usr/bin/eix", :update_eix => "/usr/bin/update-eix"
5
9
 
6
10
    defaultfor :operatingsystem => :gentoo
7
11
 
8
 
    def self.list
9
 
        search_format = /(\S+) (\S+) \[(.*)\] \[(\S*)\] ([\S]*) (.*)/
10
 
        result_fields = [:category, :name, :ensure, :version_available,
11
 
                :vendor, :description]
12
 
        command = "#{command(:eix)} --format \"{installedversions}<category> <name> [<installedversions>] [<best>] <homepage> <description>{}\""
 
12
    def self.instances
 
13
        result_format = /(\S+) (\S+) \[(?:([0-9.a-zA-Z]+(?:_(?:alpha|beta|pre|rc|p)[0-9]*)*(?:-r[0-9]*)?)(?:\([^\)]+\))?(?:\[([^\]]+)\])?[ ]*)*\] \[(?:(?:\{M\})?(?:\([~*]+\))?([0-9.a-zA-Z]+(?:_(?:alpha|beta|pre|rc|p)[0-9]*)*(?:-r[0-9]*)?)(?:\(([^\)]+)\))?(?:![mf])*(?:\[([^\]]+)\])?)?\] ([\S]*) (.*)/
 
14
        result_fields = [:category, :name, :ensure, :ensure_overlay, :version_available, :slot, :overlay, :vendor, :description]
 
15
 
 
16
        search_format = "{installedversionsshort}<category> <name> [<installedversionsshort>] [<best>] <homepage> <description>{}"
13
17
 
14
18
        begin
15
 
            search_output = execute( command )
 
19
            if !FileUtils.uptodate?("/var/cache/eix", %w(/usr/bin/eix /usr/portage/metadata/timestamp))
 
20
                update_eix
 
21
            end
 
22
            search_output = eix "--nocolor", "--format", search_format
16
23
 
17
24
            packages = []
18
25
            search_output.each do |search_result|
19
 
                match = search_format.match( search_result )
 
26
                match = result_format.match( search_result )
20
27
 
21
 
                if( match )
 
28
                if match
22
29
                    package = {}
23
 
                    result_fields.zip( match.captures ) { |field, value| package[field] = value unless value.empty? }
 
30
                    result_fields.zip(match.captures) { |field, value|
 
31
                        package[field] = value unless !value or value.empty?
 
32
                    }
24
33
                    package[:provider] = :portage
25
34
                    package[:ensure] = package[:ensure].split.last
26
35
 
27
 
                    packages.push( Puppet.type(:package).installedpkg(package) )
 
36
                    packages << new(package)
28
37
                end
29
38
            end
30
39
 
31
40
            return packages
32
41
        rescue Puppet::ExecutionFailure => detail
33
 
            raise Puppet::PackageError.new(detail)
 
42
            raise Puppet::Error.new(detail)
34
43
        end
35
44
    end
36
45
 
37
46
    def install
38
 
        if @model.should( :ensure ) == :present || @model.should( :ensure ) == :latest
39
 
            package_name = "#{@model[:category]}/#{@model[:name]}"
40
 
        else
 
47
        should = @resource.should(:ensure)
 
48
        name = package_name
 
49
        unless should == :present or should == :latest
41
50
            # We must install a specific version
42
 
            package_name = "=#{@model[:category]}/#{@model[:name]}-#{@model.should( :ensure )}"
 
51
            name = "=%s-%s" % [name, should]
43
52
        end
44
 
        command = "#{command(:emerge)} #{package_name}"
 
53
        emerge name
 
54
    end
45
55
 
46
 
        output = execute( command )
 
56
    # The common package name format.
 
57
    def package_name
 
58
        @resource[:category] ? "%s/%s" % [@resource[:category], @resource[:name]] : @resource[:name]
47
59
    end
48
60
 
49
61
    def uninstall
50
 
        package_name = "#{@model[:category]}/#{@model[:name]}"
51
 
        command ="#{command(:emerge)} --unmerge #{package_name}"
52
 
        begin
53
 
            output = execute( command )
54
 
        rescue Puppet::ExecutionFailure => detail
55
 
            raise Puppet::PackageError.new(detail)
56
 
        end
 
62
        emerge "--unmerge", package_name
57
63
    end
58
64
 
59
65
    def update
61
67
    end
62
68
 
63
69
    def query
64
 
        search_format = /(\S+) (\S+) \[(.*)\] \[(\S*)\] ([\S]*) (.*)/
65
 
        result_fields = [:category, :name, :ensure, :version_available, :vendor, :description]
 
70
        result_format = /(\S+) (\S+) \[(?:([0-9.a-zA-Z]+(?:_(?:alpha|beta|pre|rc|p)[0-9]*)*(?:-r[0-9]*)?)(?:\([^\)]+\))?(?:\[([^\]]+)\])?[ ]*)*\] \[(?:(?:\{M\})?(?:\([~*]+\))?([0-9.a-zA-Z]+(?:_(?:alpha|beta|pre|rc|p)[0-9]*)*(?:-r[0-9]*)?)(?:\(([^\)]+)\))?(?:![mf])*(?:\[([^\]]+)\])?)?\] ([\S]*) (.*)/
 
71
        result_fields = [:category, :name, :ensure, :ensure_overlay, :version_available, :slot, :overlay, :vendor, :description]
66
72
 
67
 
        search_field = @model[:name].include?( '/' ) ? "--category-name" : "--name"
68
 
        command = "#{command(:eix)} --format \"<category> <name> [<installedversions>] [<best>] <homepage> <description>\" --exact #{search_field} #{@model[:name]}"
 
73
        search_field = @resource[:category] ? "--category-name" : "--name"
 
74
        search_value = package_name
 
75
        search_format = "<category> <name> [<installedversionsshort>] [<best>] <homepage> <description>"
69
76
 
70
77
        begin
71
 
            search_output = execute( command )
 
78
            if !FileUtils.uptodate?("/var/cache/eix", %w(/usr/bin/eix /usr/portage/metadata/timestamp))
 
79
                update_eix
 
80
            end
 
81
            search_output = eix "--nocolor", "--format", search_format, "--exact", search_field, search_value
72
82
 
73
83
            packages = []
74
84
            search_output.each do |search_result|
75
 
                match = search_format.match( search_result )
 
85
                match = result_format.match( search_result )
76
86
 
77
87
                if( match )
78
88
                    package = {}
79
 
                    result_fields.zip( match.captures ) { |field, value| package[field] = value unless value.empty? }
 
89
                    result_fields.zip( match.captures ) { |field, value| package[field] = value unless !value or value.empty? }
80
90
                    if package[:ensure]
81
91
                        package[:ensure] = package[:ensure].split.last
82
92
                    else
88
98
 
89
99
            case packages.size
90
100
                when 0
91
 
                    raise Puppet::PackageError.new( "No package found with the specified name [#{@model[:name]}]" )
 
101
                    not_found_value = "%s/%s" % [@resource[:category] ? @resource[:category] : "<unspecified category>", @resource[:name]]
 
102
                    raise Puppet::Error.new("No package found with the specified name [#{not_found_value}]")
92
103
                when 1
93
104
                    return packages[0]
94
105
                else
95
 
                    raise Puppet::PackageError.new( "More than one package with the specified name [#{@model[:name]}], please use category/name to disambiguate" )
 
106
                    raise Puppet::Error.new("More than one package with the specified name [#{search_value}], please use the category parameter to disambiguate")
96
107
            end
97
108
        rescue Puppet::ExecutionFailure => detail
98
 
            raise Puppet::PackageError.new(detail)
 
109
            raise Puppet::Error.new(detail)
99
110
        end
100
111
    end
101
112
 
102
113
    def latest
103
114
        return self.query[:version_available]
104
115
    end
105
 
 
106
 
    def versionable?
107
 
        true
108
 
    end
109
116
end
110
117
 
111
 
# $Id: portage.rb 1811 2006-10-18 14:39:33Z luke $