Puppet Function: simplib::ip_to_cron

Defined in:
lib/puppet/functions/simplib/ip_to_cron.rb
Function type:
Ruby 4.x API

Overview

simplib::ip_to_cron(Optional[Integer[1]] $occurs, Optional[Integer[1]] $max_value, Optional[IpToCronAlgorithm] $algorithm, Optional[Simplib::IP] $ip)Array[Integer]

Transforms an IP address to one or more interval values for ‘cron`.

This can be used to avoid starting a certain cron job at the same
time on all servers.

Examples:

Generate one value for the ‘minute` cron interval

ip_to_cron()

Generate 2 values for the ‘hour` cron interval, using the

'sha256' algorithm and a provided IP address

ip_to_cron(2,23,'sha256','10.0.23.45')

Parameters:

  • occurs (Optional[Integer[1]])

    The occurrence within an interval, i.e., the number of values to be generated for the interval.

  • max_value (Optional[Integer[1]])

    The maximum value for the interval. The values generated will be in the inclusive range [0, max_value].

  • algorithm (Optional[IpToCronAlgorithm])

    When ‘ip_mod’, the modulus of the IP number is used as the basis for the returned values. This algorithm works well to create cron job intervals for multiple hosts, when the number of hosts exceeds the ‘max_value` and the hosts have largely, linearly- assigned IP addresses.

    When ‘sha256’, a random number generated using the IP address string is the basis for the returned values. This algorithm works well to create cron job intervals for multiple hosts, when the number of hosts is less than the ‘max_value` or the hosts do not have linearly-assigned IP addresses.

  • ip (Optional[Simplib::IP])

    The IP address to use as the basis for the generated values. When ‘nil`, the ’ipaddress’ fact (IPv4) is used.

Returns:

  • (Array[Integer])

    Array of integers suitable for use in the “minute“ or “hour“ cron field.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/puppet/functions/simplib/ip_to_cron.rb', line 4

Puppet::Functions.create_function(:'simplib::ip_to_cron') do

  local_types do
    type "IpToCronAlgorithm = Enum['ip_mod', 'sha256']"
  end

  # @param occurs
  #   The occurrence within an interval, i.e., the number of values to
  #   be generated for the interval.
  #
  # @param max_value
  #   The maximum value for the interval.  The values generated will
  #   be in the inclusive range [0, max_value].
  #
  # @param algorithm
  #   When 'ip_mod', the modulus of the IP number is used as the basis
  #   for the returned values.  This algorithm works well to create
  #   cron job intervals for multiple hosts, when the number of hosts
  #   exceeds the `max_value` and the hosts have largely, linearly-
  #   assigned IP addresses.
  #
  #   When 'sha256', a random number generated using the IP address
  #   string is the basis for the returned values.  This algorithm
  #   works well to create cron job intervals for multiple hosts,
  #   when the number of hosts is less than the `max_value` or the
  #   hosts do not have linearly-assigned IP addresses.
  #
  # @param ip
  #   The IP address to use as the basis for the generated values.
  #   When `nil`, the 'ipaddress' fact (IPv4) is used.
  #
  # @return [Array[Integer]] Array of integers suitable for use in the
  #   ``minute`` or ``hour`` cron field.
  #
  # @example Generate one value for the `minute` cron interval
  #   ip_to_cron()
  #
  # @example Generate 2 values for the `hour` cron interval, using the
  #   'sha256' algorithm and a provided IP address
  #
  #   ip_to_cron(2,23,'sha256','10.0.23.45')
  #

  dispatch :ip_to_cron do
    optional_param 'Integer[1]',        :occurs
    optional_param 'Integer[1]',        :max_value
    optional_param 'IpToCronAlgorithm', :algorithm
    optional_param 'Simplib::IP',       :ip
  end

  def ip_to_cron(occurs = 1, max_value = 59, algorithm = 'ip_mod', ip = nil)
    if ip.nil?
      scope = closure_scope
      ipaddr = scope['facts']['ipaddress']
    else
      ipaddr = ip.dup
    end

    call_function('simplib::rand_cron', ipaddr, algorithm, occurs, max_value)
  end
end