Skip to content

Commit 8a6a1c6

Browse files
committed
add possibility to override exists command.
Fixes ccin2p3#37
1 parent 7be54af commit 8a6a1c6

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,19 @@ cpan {'DBD::DB2':
128128
}
129129
```
130130

131+
### Overriding exists code
132+
133+
Some modules like `Sys::RunAlone` don't return a code 0 when invoked with `perl -M$MODULE_NAME -e1`.
134+
This means there's no way for puppet to check if they're installed or not (in an agnostic way).
135+
Therefore you'll have to override the method. Use the `%` sign if you need the module name in the command line.
136+
137+
```puppet
138+
cpan {'Sys::RunAlone':
139+
ensure => 'present',
140+
exists_command => 'perl -M% -e"__END__"',
141+
}
142+
```
143+
131144
## Reference
132145

133146
## Classes

lib/puppet/provider/cpan/default.rb

+12-5
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ def create
4646
system("#{umask} yes | perl #{ll} -MCPAN -e 'CPAN::install #{resource[:name]}'")
4747
end
4848

49-
#cpan doesn't always provide the right exit code, so we double check
50-
system("perl #{ll} -M#{resource[:name]} -e1 > /dev/null 2>&1")
49+
exists_command = "perl #{ll} -M#{resource[:name]} -e1 > /dev/null 2>&1"
50+
if resource[:exists_command]
51+
exists_command = resource[:exists_command].gsub(/\%/,resource[:name])
52+
end
53+
system(exists_command)
5154
estatus = $?.exitstatus
5255

5356
if estatus != 0
@@ -88,8 +91,12 @@ def exists?
8891
if resource[:local_lib]
8992
ll = "-Mlocal::lib=#{resource[:local_lib]}"
9093
end
91-
Puppet.debug("perl #{ll} -M#{resource[:name]} -e1 > /dev/null 2>&1")
92-
output = `perl #{ll} -M#{resource[:name]} -e1 > /dev/null 2>&1`
94+
exists_command = "perl #{ll} -M#{resource[:name]} -e1 > /dev/null 2>&1"
95+
if resource[:exists_command]
96+
exists_command = resource[:exists_command].gsub(/\%/,resource[:name])
97+
end
98+
Puppet.debug(exists_command)
99+
output = `#{exists_command}`
93100
estatus = $?.exitstatus
94101

95102
case estatus
@@ -99,7 +106,7 @@ def exists?
99106
Puppet.debug("#{resource[:name]} not installed")
100107
false
101108
else
102-
raise Puppet::Error, "perl #{ll} -M#{resource[:name]} -e1 failed with error code #{estatus}: #{output}"
109+
raise Puppet::Error, "exists_command `#{exists_command}` failed with error code #{estatus}: #{output}"
103110
end
104111
end
105112

lib/puppet/type/cpan.rb

+7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@
3838
end
3939
end
4040

41+
newparam(:exists_command) do
42+
desc 'Custom command to test for module existence'
43+
validate do |value|
44+
raise ArgumentError, 'String expected for exists_command.' unless value.is_a?(String)
45+
end
46+
end
47+
4148
newparam(:force, :boolean => true, :parent => Puppet::Parameter::Boolean) do
4249
desc 'Enable/Disable to force the installation of the module. Disabled by default.'
4350
defaultto :false

test/exists_command.pp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
class { '::cpan':
3+
manage_package => false,
4+
manage_config => false,
5+
}
6+
7+
cpan { 'Sys::RunAlone':
8+
ensure => present,
9+
exists_command => 'perl -Mlocal::lib=/tmp/cpan -M% -e"__END__"',
10+
local_lib => '/tmp/cpan',
11+
}

0 commit comments

Comments
 (0)