diff options
author | Nigel Kersten <nigelk@google.com> | 2010-05-17 16:38:19 -0700 |
---|---|---|
committer | test branch <puppet-dev@googlegroups.com> | 2010-02-17 06:50:53 -0800 |
commit | e0e6b642c4eefc1b5f44b44638d2db24f4845ef1 (patch) | |
tree | 48f3e9c1c442c7ffaa351564a97af1ac8e387be0 /lib/puppet | |
parent | c8ca19a4e7d48d5c45fd67efb981c1c3d3b80e5c (diff) | |
download | puppet-e0e6b642c4eefc1b5f44b44638d2db24f4845ef1.tar.gz puppet-e0e6b642c4eefc1b5f44b44638d2db24f4845ef1.tar.xz puppet-e0e6b642c4eefc1b5f44b44638d2db24f4845ef1.zip |
Provides #3723. Add ability for execs to have several attempts at a successful
execution and fix minor bug with logoutput and returns as an array..
* Add 'tries' and 'try_sleep' parameters
* Fix bug where returns is specified as an array and logoutput on
* failure.
* unit tests for both cases above.
Diffstat (limited to 'lib/puppet')
-rwxr-xr-x | lib/puppet/type/exec.rb | 62 |
1 files changed, 58 insertions, 4 deletions
diff --git a/lib/puppet/type/exec.rb b/lib/puppet/type/exec.rb index 448a56714..0cb345af1 100755 --- a/lib/puppet/type/exec.rb +++ b/lib/puppet/type/exec.rb @@ -111,9 +111,20 @@ module Puppet dir = self.resource[:cwd] || Dir.pwd event = :executed_command + tries = self.resource[:tries] + try_sleep = self.resource[:try_sleep] begin - @output, status = @resource.run(self.resource[:command]) + tries.times do |try| + # Only add debug messages for tries > 1 to reduce log spam. + debug("Exec try #{try+1}/#{tries}") if tries > 1 + @output, @status = @resource.run(self.resource[:command]) + break if self.should.include?(@status.exitstatus.to_s) + if try_sleep > 0 and tries > 1 + debug("Sleeping for #{try_sleep} seconds between tries") + sleep try_sleep + end + end rescue Timeout::Error self.fail "Command exceeded timeout" % value.inspect end @@ -123,7 +134,7 @@ module Puppet when :true log = @resource[:loglevel] when :on_failure - if status.exitstatus.to_s != self.should.to_s + unless self.should.include?(@status.exitstatus.to_s) log = @resource[:loglevel] else log = :false @@ -136,9 +147,9 @@ module Puppet end end - unless self.should.include?(status.exitstatus.to_s) + unless self.should.include?(@status.exitstatus.to_s) self.fail("%s returned %s instead of one of [%s]" % - [self.resource[:command], status.exitstatus, self.should.join(",")]) + [self.resource[:command], @status.exitstatus, self.should.join(",")]) end return event @@ -282,6 +293,49 @@ module Puppet defaultto 300 end + newparam(:tries) do + desc "The number of times execution of the command should be tried. + Defaults to '1'. This many attempts will be made to execute + the command until an acceptable return code is returned. + Note that the timeout paramater applies to each try rather than + to the complete set of tries." + + munge do |value| + if value.is_a?(String) + unless value =~ /^[\d]+$/ + raise ArgumentError, "Tries must be an integer" + end + value = Integer(value) + end + if value < 1 + raise ArgumentError, "Tries must be an integer >= 1" + end + value + end + + defaultto 1 + end + + newparam(:try_sleep) do + desc "The time to sleep in seconds between 'tries'." + + munge do |value| + if value.is_a?(String) + unless value =~ /^[-\d.]+$/ + raise ArgumentError, "try_sleep must be a number" + end + value = Float(value) + end + if value < 0 + raise ArgumentError, "try_sleep cannot be a negative number" + end + value + end + + defaultto 0 + end + + newcheck(:refreshonly) do desc "The command should only be run as a refresh mechanism for when a dependent object is changed. It only |