Project

General

Profile

« Previous | Next » 

Revision 450be5b5

Added by Greg Sutcliffe about 12 years ago

Cleanup crontab if we switch to daemon mode

View differences:

lib/parser/functions/ip_to_cron.rb
# provides a "random" value to cron based on the last bit of the machine IP address.
# used to avoid starting a certain cron job at the same time on all servers.
# if used with no parameters, it will return a single value between 0-59
# first argument is the occournce within a timeframe, for example if you want it to run 2 times per hour
# the second argument is the timeframe, by default its 60 minutes, but it could also be 24 hours etc
#
# example usage
# ip_to_cron() - returns one value between 0..59
# ip_to_cron(2) - returns an array of two values between 0..59
# ip_to_cron(2,24) - returns an array of two values between 0..23
module Puppet::Parser::Functions
newfunction(:ip_to_cron, :type => :rvalue) do |args|
occours = (args[0] || 1).to_i
scope = (args[1] || 60).to_i
ip = lookupvar('ipaddress').to_s.split('.')[3].to_i
base = ip % scope
if occours == 1
base
else
(1..occours).map { |i| (base - (scope / occours * i)) % scope }.sort
end
end
end
lib/puppet/parser/functions/ip_to_cron.rb
# provides a "random" value to cron based on the last bit of the machine IP address.
# used to avoid starting a certain cron job at the same time on all servers.
# if used with no parameters, it will return a single value between 0-59
# first argument is the occournce within a timeframe, for example if you want it to run 2 times per hour
# the second argument is the timeframe, by default its 60 minutes, but it could also be 24 hours etc
#
# example usage
# ip_to_cron() - returns one value between 0..59
# ip_to_cron(2) - returns an array of two values between 0..59
# ip_to_cron(2,24) - returns an array of two values between 0..23
module Puppet::Parser::Functions
newfunction(:ip_to_cron, :type => :rvalue) do |args|
occours = (args[0] || 1).to_i
scope = (args[1] || 60).to_i
ip = lookupvar('ipaddress').to_s.split('.')[3].to_i
base = ip % scope
if occours == 1
base
else
(1..occours).map { |i| (base - (scope / occours * i)) % scope }.sort
end
end
end
manifests/cron.pp
ensure => stopped,
}
cron {'puppet':
cron { 'puppet':
command => "sleep $((RANDOM%59)) && /usr/sbin/puppet agent --config ${puppet::params::dir}/puppet.conf --onetime --no-daemonize",
user => root,
minute => ip_to_cron($puppet::params::cron_interval, $puppet::params::cron_range),
}
}
manifests/daemon.pp
class puppet::cron inherits puppet::service {
class puppet::daemon inherits puppet::service {
Service['puppet'] {
enable => true,
ensure => running,
}
cron { 'puppet': ensure => absent }
}

Also available in: Unified diff