Skip to content

Instantly share code, notes, and snippets.

@chrisleavoy
Created August 6, 2015 01:35
Show Gist options
  • Save chrisleavoy/23aa8b44730fc08746d0 to your computer and use it in GitHub Desktop.
Save chrisleavoy/23aa8b44730fc08746d0 to your computer and use it in GitHub Desktop.
define misc::lexec::example () {
exec { 'notify':
command => '/bin/true',
unless => '/usr/bin/test -e /tmp/skip_notify',
}
misc::lexec { 'test_working':
command => '/usr/bin/test -e /tmp/ok',
lock => "/tmp/exec_lock",
subscribe => Exec['notify'],
notify_on_success => Exec['post_exec']
}
exec { 'post_exec':
command => '/bin/echo post',
refreshonly => true,
}
}
define misc::lexec::fuck_you_puppet () {
# this unexpectedly generates a "changed" resource instead of a "failed" resource
exec { 'test_refresh_fail':
command => "/bin/false",
refreshonly => true,
}
# annoyingly, this will get executed even though "test_refresh_fail" fails
exec { 'no_seriously_fuck_you_puppet':
command => "/bin/false",
refreshonly => true,
subscribe => Exec['test_refresh_fail'],
}
# this generates a proper "failed" resoure
exec { 'test_exec_fail':
command => '/bin/false',
creates => '/tmp/test_exec_fail',
}
}
# The purpose of misc::lexec is to provide a wrapper for Puppet's exec resource. It
# uses a lock file to ensure an exec that receives a refresh completes successfully.
# Without it, an exec with refreshonly=>true will always succeed, never failing the
# puppet run, or skipping for failed dependancies as one might expect it to. The
# desired behavior is to run once, and again after receiving a refresh, or repeatedly
# until the command returns successfully.
#
# See examples and usage above.
#
define misc::lexec (
$command,
$lock,
$create_lock = true,
# $creates not supported
$cwd = undef,
$environment = undef,
$group = undef,
$logoutput = on_failure,
# onlyif not supported
$path = undef,
$provider = undef,
# $refresh not supported
# $refreshonly not supported
$returns = 0,
$timeout = 300,
$tries = 1,
$try_sleep = undef,
$umask = undef,
# unless not supported
$user = undef,
# custom parameters:
$notify_on_success = undef,) {
#
exec { "${title}_del_lock":
command => "/bin/rm -f ${lock}",
refreshonly => true,
cwd => $cwd,
}
exec { $title:
command => $command,
creates => $lock,
cwd => $cwd,
environment => $environment,
group => $group,
logoutput => $logoutput,
path => $path,
provider => $provider,
returns => $returns,
timeout => $timeout,
tries => 1,
try_sleep => $try_sleep,
umask => $umask,
user => $user,
require => Exec["${title}_del_lock"],
notify => $notify_on_success,
}
if ($create_lock) {
file { "${title}_lock":
ensure => file,
path => $lock,
require => Exec[$title]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment