class Chef::Resource::CronD

Public Class Methods

validate_dow(spec) click to toggle source

validate the provided day of the week is sun-sat, 0-7, or * @param spec the value to validate @return [Boolean] valid or not?

# File lib/chef/resource/cron_d.rb, line 75
def self.validate_dow(spec)
  return true if spec == "*"
  if spec.respond_to? :to_int
    validate_numeric(spec, 0, 7)
  elsif spec.respond_to? :to_str
    return true if spec == "*"
    # Named abbreviations are permitted but not as part of a range or with stepping
    return true if %w{sun mon tue wed thu fri sat}.include? spec.downcase
    # 0-7 are legal for days of week
    validate_numeric(spec, 0, 7)
  else
    false
  end
end
validate_month(spec) click to toggle source

validate the provided month value to be jan - dec, 1 - 12, or * @param spec the value to validate @return [Boolean] valid or not?

# File lib/chef/resource/cron_d.rb, line 57
def self.validate_month(spec)
  return true if spec == "*"
  if spec.respond_to? :to_int
    validate_numeric(spec, 1, 12)
  elsif spec.respond_to? :to_str
    return true if spec == "*"
    # 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) click to toggle source

validate a provided value is between two other provided values we also allow * as a valid input @param spec the value to validate @param min the lowest value allowed @param max the highest value allowed @return [Boolean] valid or not?

# File lib/chef/resource/cron_d.rb, line 36
def self.validate_numeric(spec, min, max)
  return true if spec == "*"
  #  binding.pry
  if spec.respond_to? :to_int
    return false unless spec >= min && spec <= max
    return true
  end

  # Lists of invidual values, ranges, and step values all share the validity range for type
  spec.split(%r{\/|-|,}).each do |x|
    next if x == "*"
    return false unless x =~ /^\d+$/
    x = x.to_i
    return false unless x >= min && x <= max
  end
  true
end

Public Instance Methods

after_created() click to toggle source

warn if someone passes the deprecated cookbook property

# File lib/chef/resource/cron_d.rb, line 165
def after_created
  raise ArgumentError, "The 'cookbook' property for the cron_d resource is no longer supported now that it ships as a core resource." if cookbook
end
create_template(create_action) click to toggle source
# File lib/chef/resource/cron_d.rb, line 201
def create_template(create_action)
  # cleanup the legacy named job if it exists
  file "#{new_resource.cron_name} legacy named cron.d file" do
    path "/etc/cron.d/#{new_resource.cron_name}"
    action :delete
    only_if { new_resource.cron_name != sanitized_name }
  end

  # @todo this is Chef 12 era cleanup. Someday we should remove it all
  template "/etc/cron.d/#{sanitized_name}" do
    source ::File.expand_path("../support/cron.d.erb", __FILE__)
    local true
    mode new_resource.mode
    variables(
      name: sanitized_name,
      predefined_value: new_resource.predefined_value,
      minute: new_resource.minute,
      hour: new_resource.hour,
      day: new_resource.day,
      month: new_resource.month,
      weekday: new_resource.weekday,
      command: new_resource.command,
      user: new_resource.user,
      mailto: new_resource.mailto,
      path: new_resource.path,
      home: new_resource.home,
      shell: new_resource.shell,
      comment: new_resource.comment,
      random_delay: new_resource.random_delay,
      environment: new_resource.environment
    )
    action create_action
  end
end
sanitized_name() click to toggle source

@return [String] cron_name property with . replaced with -

# File lib/chef/resource/cron_d.rb, line 197
def sanitized_name
  new_resource.cron_name.tr(".", "-")
end