Module: Chef::ResourceHelpers::CronValidations

Extended by:
CronValidations
Included in:
Chef::Resource::ChefClientCron, CronValidations
Defined in:
lib/chef/resource/helpers/cron_validations.rb

Overview

a collection of methods for validating cron times. Used in the various cron-like resources

Instance Method Summary collapse

Instance Method Details

#validate_day(spec) ⇒ Boolean

validate the day of the month is 1-31

Parameters:

  • spec

    the value to validate

Returns:

  • (Boolean)

    valid or not?



80
81
82
# File 'lib/chef/resource/helpers/cron_validations.rb', line 80

def validate_day(spec)
  validate_numeric(spec, 1, 31)
end

#validate_dow(spec) ⇒ Boolean

validate the provided day of the week is sun-sat, sunday-saturday, 0-7, or * Added crontab param to check cron resource

Parameters:

  • spec

    the value to validate

Returns:

  • (Boolean)

    valid or not?



69
70
71
72
73
74
75
# File 'lib/chef/resource/helpers/cron_validations.rb', line 69

def validate_dow(spec)
  spec = spec.to_s
  spec == "*" ||
    validate_numeric(spec, 0, 7) ||
    %w{sun mon tue wed thu fri sat}.include?(spec.downcase) ||
    %w{sunday monday tuesday wednesday thursday friday saturday}.include?(spec.downcase)
end

#validate_hour(spec) ⇒ Boolean

validate the hour is 0-23

Parameters:

  • spec

    the value to validate

Returns:

  • (Boolean)

    valid or not?



87
88
89
# File 'lib/chef/resource/helpers/cron_validations.rb', line 87

def validate_hour(spec)
  validate_numeric(spec, 0, 23)
end

#validate_minute(spec) ⇒ Boolean

validate the minute is 0-59

Parameters:

  • spec

    the value to validate

Returns:

  • (Boolean)

    valid or not?



94
95
96
# File 'lib/chef/resource/helpers/cron_validations.rb', line 94

def validate_minute(spec)
  validate_numeric(spec, 0, 59)
end

#validate_month(spec) ⇒ Boolean

validate the provided month value to be jan - dec, 1 - 12, or *

Parameters:

  • spec

    the value to validate

Returns:

  • (Boolean)

    valid or not?



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/chef/resource/helpers/cron_validations.rb', line 49

def validate_month(spec)
  return true if spec == "*"

  if spec.respond_to? :to_int
    validate_numeric(spec, 1, 12)
  elsif spec.respond_to? :to_str
    # Named abbreviations are permitted but not as part of a range or with stepping
    return true if %w{jan feb mar apr may jun jul aug sep oct nov dec}.include? spec.downcase

    # 1-12 are legal for months
    validate_numeric(spec, 1, 12)
  else
    false
  end
end

#validate_numeric(spec, min, max) ⇒ Boolean

validate a provided value is between two other provided values we also allow * as a valid input

Parameters:

  • spec

    the value to validate

  • min

    the lowest value allowed

  • max

    the highest value allowed

Returns:

  • (Boolean)

    valid or not?



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chef/resource/helpers/cron_validations.rb', line 28

def validate_numeric(spec, min, max)
  return true if spec == "*"

  if spec.respond_to? :to_int
    return spec >= min && spec <= max
  end

  # Lists of individual values, ranges, and step values all share the validity range for type
  spec.split(%r{\/|-|,}).each do |x|
    next if x == "*"
    return false unless /^\d+$/.match?(x)

    x = x.to_i
    return false unless x >= min && x <= max
  end
  true
end