diff --git a/lib/puppet/parser/functions/assert_empty_hash.rb b/lib/puppet/parser/functions/assert_empty_hash.rb new file mode 100644 index 0000000..fb90c2c --- /dev/null +++ b/lib/puppet/parser/functions/assert_empty_hash.rb @@ -0,0 +1,18 @@ +module Puppet::Parser::Functions +newfunction(:assert_empty_hash, :type => :rvalue, :doc => <<-EOS +This function checks an input struct for undefined hashes in key => hash and assigns {}. This is only a helper function to make a hash.each work if one of the values is undefined +EOS +)do |args| + fail "Must receive one argument." if args.empty? + args.each do |arg| + h = Hash.new + arg.each_pair do |host, hash| + unless hash.is_a? Hash + hash = {} + end + h[host] = hash + end + return h + end + end +end diff --git a/lib/puppet/parser/functions/rand_from_array.rb b/lib/puppet/parser/functions/rand_from_array.rb new file mode 100644 index 0000000..416c463 --- /dev/null +++ b/lib/puppet/parser/functions/rand_from_array.rb @@ -0,0 +1,37 @@ +module Puppet::Parser::Functions +newfunction(:rand_from_array, :type => :rvalue, :doc => <<-EOS +This function takes either an int or an array as input and returns the int or a +random element from the array +EOS +)do |args| + fail "Must receive two argument." if args.empty? +#++ this works if the input is an array + # return args.sample +# +++++++++++++++++++++++++++++++++++++ +# args.flatten! + params = [] + params << args[0] + params.flatten! + arr = [] + salt = args[1].sum % 60 + rnd = Random.new(salt) +# rnd = Random.new() + params.each do |key| + if key.is_a?(String) and key =~ /\d\.\.\d/ + k = key.split('..') + r = Range.new(k[0],k[1]).to_a + arr << r + arr.flatten! + elsif key.is_a?(String) + arr << key + arr.flatten! + elsif key.is_a?(Integer) + arr << key.to_s + arr.flatten! + end + end + last_i = arr.length - 1 + return arr[rnd.rand(0..last_i)] + +end +end diff --git a/manifests/config.pp b/manifests/config.pp index 79586b5..a9b8672 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -16,7 +16,6 @@ class rsnapshot::config ( ensure => 'directory', } - #$foo = ['1', '2', '3', '20..21'] # $foo='*/5' #$qax = rand_from_array($foo) #notice("QAX is $qax ") @@ -34,6 +33,7 @@ class rsnapshot::config ( $snapshot_root = pick($hash['snapshot_root'], $rsnapshot::params::config_snapshot_root) $backup_user = pick($hash['backup_user'], $rsnapshot::params::config_backup_user) $default_backup_dirs = pick($rsnapshot::default_backup, $rsnapshot::params::config_default_backup) + $backup_levels = pick($hash['backup_levels'], $rsnapshot::params::config_backup_levels, 'weekly') $backup = $hash['backup'] $backup_defaults = pick($hash['backup_defaults'], $rsnapshot::params::config_backup_defaults) $cmd_cp = pick($hash['cmd_cp'], $rsnapshot::params::config_cmd_cp) @@ -114,6 +114,37 @@ class rsnapshot::config ( file { $config: content => template('rsnapshot/rsnapshot.erb') } + + + ########################### CRON #################################################### + #30 1 * * * /usr/bin/rsnapshot -c /etc/rsnapshot/cmweb1.rsnapshot.conf daily + #15 1 * * 0 /usr/bin/rsnapshot -c /etc/rsnapshot/cmweb1.rsnapshot.conf weekly + # 00 1 1 * * /usr/bin/rsnapshot -c /etc/rsnapshot/cmweb1.rsnapshot.conf monthly + $cronfile = "/tmp/rsnapshot.d/cron/${host}" + $cron = pick_default($hash['cron'], $rsnapshot::params::cron, 'absent') + + concat { "${cronfile}": + # replace => false, + } + + $backup_levels.each |String $level| { + # allow to globally override ranges, create random numbers for backup_levels daily, weekly, monthly + $c_min = pick($cron['minute'], $rsnapshot::params::cron['minute'], '*') + $c_hour = pick($cron['hour'], $rsnapshot::params::cron['hour'], '*') + $c_monthday = pick($cron['monthday'], $rsnapshot::params::cron['monthday'], '*') + $c_month = pick($cron['month'], $rsnapshot::params::cron['month'], '*') + $c_weekday = pick($cron['weekday'], $rsnapshot::params::cron['weekday'], '*') + + $minute = rand_from_array($c_min, "${host}.${level}") + $hour = rand_from_array($c_hour, "${host}.${level}") + $monthday = rand_from_array($c_monthday, "${host}.${level}") + $month = rand_from_array($c_month, "${host}.${level}") + $weekday = rand_from_array($c_weekday, "${host}.${level}") + concat::fragment { "${host}.${level}": + target => "${cronfile}", + content => template('rsnapshot/cron.erb'), + } + } } } diff --git a/manifests/cron.pp b/manifests/cron.pp new file mode 100644 index 0000000..0b2a868 --- /dev/null +++ b/manifests/cron.pp @@ -0,0 +1,7 @@ +# this class sets up cron +# * todo +# let the user specify a range of hours and randomize cron creation in this range use rand for #args and return arg[rand] + +class rsnapshot::cron { + +} diff --git a/manifests/params.pp b/manifests/params.pp index 72e5507..f63bc41 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -7,6 +7,7 @@ class rsnapshot::params { $config_backup_user = 'root' $package_name = 'rsnapshot' $package_ensure = 'present' + $config_backup_levels = [ 'daily', 'weekly', ] $config_backup_defaults = true $config_version = '1.2' $config_cmd_cp = '/bin/cp' @@ -60,5 +61,11 @@ class rsnapshot::params { } $config_backup_scripts = {} + $cron = { + 'minute' => ['0..20'], + 'hour' => ['21..23','0..4','5'], + 'monthday' => '*', + 'month' => '*', + 'weekday' => '*', + } } - diff --git a/templates/cron.erb b/templates/cron.erb new file mode 100644 index 0000000..cd764e3 --- /dev/null +++ b/templates/cron.erb @@ -0,0 +1 @@ +<%= @minute %> <%= @hour %> <%= @monthday %> <%= @weekday %> root /usr/bin/rsnapshot -c <%= @config %> <%= @level %>