Puppet: System Administration Automated

Support

Ticket #718: cpan.rb

File cpan.rb, 1.5 kB (added by jblomo, 1 year ago)

provider-package-cpan.rb

Line 
1 # PERL CPAN support
2 Puppet::Type.type(:package).provide :cpan do
3         desc "PERL CPAN support."
4
5         # has_feature :versionable
6
7         commands :cpancmd => "perl"
8         confine :exists   => "/etc/perl/CPAN/Config.pm" # may be Debian specific
9
10         def install(useversion = true)
11                 name = @model[:name]
12                 if (! @model.should(:ensure).is_a? Symbol) and useversion
13                         # we have to figure out the CPAN file name to install
14                         response = self.class.info(name)
15                         name = response['CPAN_FILE'].sub(/\d+\.\d+/, @model.should(:ensure))
16                 end
17
18                 command = command(:cpancmd) + " -MCPAN -e 'CPAN::Shell->install(\"#{name}\")'"
19                 execute(command);
20         end
21
22         def latest
23                 response = self.class.info(@model[:name])
24                 if response['CPAN_VERSION']
25                         return {:name => @model[:name], :ensure => response['CPAN_VERSION'], :provider => :cpan}
26                         else
27                                 raise Puppet::Error, "CPAN module #{@model[:name]} not found"
28                 end
29         end
30
31         def query
32                 response = self.class.info(@model[:name])
33
34                 return {:name => @model[:name], :ensure => response['INST_VERSION'], :provider => :cpan}
35         end
36
37         def self.info(name)
38                 command = command(:cpancmd) + " -MCPAN -e 'CPAN::Shell->i(\"#{name}\")'"
39                 response = {}
40                 begin
41                         execute(command).split("\n").each do |line|
42                                 response[$1] = $2 if /\s{4}(\S+)\s+(.*)/.match(line)
43                         end
44                 rescue Puppet::ExecutionFailure => detail
45                         raise Puppet::Error, "Could not list CPAN modules: %s" % detail
46                 end
47                 return response
48         end
49
50         def update
51                 self.install(false)
52         end
53
54         def versionable?
55                 true
56         end
57
58 end
59
60 # $Id$