summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Barczyński <romke@estrefa.pl>2011-03-03 03:09:55 +0100
committerRoman Barczyński <romke@estrefa.pl>2011-03-03 03:09:55 +0100
commit13098ff934b3ba88ef5e4ba9ce2b7232c32b9082 (patch)
tree5a90c35853bad2f556a27a69cdc8f30db4ae4a20
parentb305bbeac7a0560a271f34026f936b88b88da477 (diff)
downloadpuppet-stdlib-13098ff934b3ba88ef5e4ba9ce2b7232c32b9082.tar.gz
puppet-stdlib-13098ff934b3ba88ef5e4ba9ce2b7232c32b9082.tar.bz2
cronrand - stateful (between puppet runs) randomizer eg. for cron minutes for equal load distribution
-rw-r--r--cronrand.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/cronrand.rb b/cronrand.rb
new file mode 100644
index 0000000..3dd6810
--- /dev/null
+++ b/cronrand.rb
@@ -0,0 +1,31 @@
+# vim: set ts=2 sw=2 et :
+# TODO: path should not be hardcoded here
+#
+# USAGE:
+# $minutes = cronrand("puppet-run", $fqdn, 59)
+# file { "puppet-cron":
+# name => /etc/cron.d/puppet-run",
+# content => "$minutes * * * * root /usr/sbin/puppetd --onetime --no-daemonize --logdest syslog > /dev/null 2>&1\n"
+# }
+# ---
+# minutes will be chosen random and saved for each $fqdn,
+# second puppet run on same host will create same content as first one.
+
+module Puppet::Parser::Functions
+ newfunction(:cronrand, :type => :rvalue) do |args|
+ job = args[0]
+ host = args[1]
+ minutes = (args[2].to_i < 60) ? args[2].to_i : 59
+ filename = "/etc/puppet/modules/puppet/state/cronminutes-#{job}-#{host}"
+ value = 0
+
+ if FileTest.exists?(filename)
+ File.open(filename, 'r') { |fd| value = fd.gets.chomp.to_i }
+ else
+ value = rand(minutes)
+ File.open(filename, 'w') { |fd| fd.puts value }
+ end
+ value
+ end
+end
+