pacman.rb

Pacman Package Provider for Archlinux - Miah Johnson, 01/07/2009 06:12 pm

Download (2.2 kB)

 
1
require 'puppet/provider/package'
2

    
3
Puppet.type(:package).provide :pacman, :parent => Puppet::Provider::Package do
4
    desc "Support for the Package Manager Utility (pacman) used in Archlinux."
5

    
6
    commands :pacman => "/usr/bin/pacman"
7
    defaultfor :operatingsystem => :archlinux
8
    confine    :operatingsystem => :archlinux
9

    
10
    # Install a package using 'pacman'.
11
    # Installs quietly, without confirmation or progressbar, updates package
12
    # list from servers defined in pacman.conf.
13
        def install
14
                pacman "--noconfirm", "--noprogressbar", "-S", @resource[:name]
15

    
16
                unless self.query
17
                        raise Puppet::ExecutionFailure.new(
18
                        "Could not find package %s" % self.name
19
                        )
20
                end
21
        end
22

    
23
        def self.listcmd
24
                [command(:pacman), " -Q"]
25
        end
26

    
27
# Fetch the list of packages currently installed on the system.
28
        def self.instances
29
                packages = []
30
                        begin execpipe(listcmd()) do |process|
31
                        # pacman -Q output is 'packagename version-rel'
32
                        regex = %r{^(\S+)\s(\S+)}
33
                        fields = [:name, :ensure]
34
                        hash = {}
35

    
36
                        process.each { |line|
37
                                if match = regex.match(line)
38
                                        fields.zip(match.captures) { |field,value|
39
                                                hash[field] = value
40
                                        }
41

    
42
                                        name = hash[:name]
43
                                        hash[:provider] = self.name
44
                                        
45
                                        packages << new(hash)
46
                                        hash = {}
47
                                else
48
                                        warning("Failed to match line %s" % line)
49
                        end
50
                        }
51
                end
52
                rescue Puppet::ExecutionFailure
53
                        return nil
54
                end
55
                return packages
56
        end
57

    
58
    # Because Archlinux is a rolling release based distro, installing a package
59
    # should always result in the newest release.
60
    def update
61
        # Install in pacman can be used for update, too
62
        self.install
63
    end
64

    
65
    # Querys the pacman master list for information about the package.
66
    def query
67
     begin
68
      hash = {}
69
      output = pacman("-Qi", @resource[:name])
70

    
71
      if output =~ /Version.*:\s(.+)/
72
              hash[:ensure] = $1
73
      end
74
                        rescue Puppet::ExecutionFailure
75
                                return {:ensure => :purged, :status => 'missing',
76
                                        :name => @resource[:name], :error => 'ok'}
77
     end
78
      return hash
79
    end
80

    
81
    # Removes a package from the system.
82
    def uninstall
83
            pacman "--noconfirm", "--noprogressbar", "-R", @resource[:name]
84
    end
85
end
86

    
87
# vim: set ts=2 noet: