class Chef::Provider::Cron

Constants

CRON_ATTRIBUTES
CRON_PATTERN
ENVIRONMENT_PROPERTIES
ENV_PATTERN
SPECIAL_PATTERN
SPECIAL_TIME_VALUES
Solaris

Just to create an alias so ‘Chef::Provider::Cron::Solaris’ is exposed and accessible to existing consumers of class.

Attributes

cron_empty[RW]
cron_exists[RW]

Public Class Methods

new(new_resource, run_context) click to toggle source
Calls superclass method Chef::Provider::new
# File lib/chef/provider/cron.rb, line 34
def initialize(new_resource, run_context)
  super(new_resource, run_context)
  @cron_exists = false
  @cron_empty = false
end

Public Instance Methods

cron_different?() click to toggle source
# File lib/chef/provider/cron.rb, line 89
def cron_different?
  CRON_ATTRIBUTES.any? do |cron_var|
    if new_resource.send(cron_var).class == current_resource.send(cron_var).class
      new_resource.send(cron_var) != current_resource.send(cron_var)
    else
      new_resource.send(cron_var).to_s != current_resource.send(cron_var).to_s
    end
  end
end
load_current_resource() click to toggle source
# File lib/chef/provider/cron.rb, line 41
def load_current_resource
  crontab_lines = []
  @current_resource = Chef::Resource::Cron.new(new_resource.name)
  current_resource.user(new_resource.user)
  @cron_exists = false
  if crontab = read_crontab
    cron_found = false
    crontab.each_line do |line|
      case line.chomp
      when "# Chef Name: #{new_resource.name}"
        logger.trace("Found cron '#{new_resource.name}'")
        cron_found = true
        @cron_exists = true
        next
      when ENV_PATTERN
        set_environment_var($1, $2) if cron_found
        next
      when SPECIAL_PATTERN
        if cron_found
          current_resource.time($2.to_sym)
          current_resource.command($3)
          cron_found = false
        end
      when CRON_PATTERN
        if cron_found
          current_resource.minute($1)
          current_resource.hour($2)
          current_resource.day($3)
          current_resource.month($4)
          current_resource.weekday($5)
          current_resource.command($6)
          cron_found = false
        end
        next
      else
        cron_found = false # We've got a Chef comment with no following crontab line
        next
      end
    end
    logger.trace("Cron '#{new_resource.name}' not found") unless @cron_exists
  else
    logger.trace("Cron empty for '#{new_resource.user}'")
    @cron_empty = true
  end

  current_resource
end

Private Instance Methods

cmd_str() click to toggle source

@return [String] The command to be executed. The new line at the end has been added purposefully.

# File lib/chef/provider/cron.rb, line 266
def cmd_str
  " #{new_resource.command}\n"
end
duration_str() click to toggle source

@return [String] The Cron time string consisting five fields that Cron converts into a time interval.

# File lib/chef/provider/cron.rb, line 240
def duration_str
  if new_resource.time
    "@#{new_resource.time}"
  else
    "#{new_resource.minute} #{new_resource.hour} #{new_resource.day} #{new_resource.month} #{new_resource.weekday}"
  end
end
env_var_str() click to toggle source

@return [String] The string of Env Variables containing line breaks.

# File lib/chef/provider/cron.rb, line 215
def env_var_str
  str = []
  %i{mailto path shell home}.each do |v|
    str << "#{v.to_s.upcase}=\"#{new_resource.send(v)}\"" if new_resource.send(v)
  end
  new_resource.environment.each do |name, value|
    if ENVIRONMENT_PROPERTIES.include?(name)
      unless new_resource.property_is_set?(name.downcase)
        logger.warn("#{new_resource.name}: the environment property contains the '#{name}' variable, which should be set separately as a property.")
        new_resource.send(name.downcase.to_sym, value.gsub(/^"|"$/, ""))
        new_resource.environment.delete(name)
        str << "#{name.to_s.upcase}=\"#{value}\""
      else
        raise Chef::Exceptions::Cron, "#{new_resource.name}: the '#{name}' property is set and environment property also contains the '#{name}' variable. Remove the variable from the environment property."
      end
    else
      str << "#{name}=#{value}"
    end
  end
  str.join("\n")
end
get_crontab_entry() click to toggle source

Concatenates various information and formulates a complete string that could be written in the crontab

@return [String] A crontab string formed as per the user inputs.

# File lib/chef/provider/cron.rb, line 275
def get_crontab_entry
  # Initialize
  newcron = []
  newcron << "# Chef Name: #{new_resource.name}"
  newcron << env_var_str unless env_var_str.empty?
  newcron << duration_str + time_out_str + cmd_str

  newcron.join("\n")
end
read_crontab() click to toggle source
# File lib/chef/provider/cron.rb, line 196
def read_crontab
  so = shell_out!("crontab -l -u #{new_resource.user}", returns: [0, 1])
  return nil if so.exitstatus == 1

  so.stdout
rescue => e
  raise Chef::Exceptions::Cron, "Error determining state of #{new_resource.name}, error: #{e}"
end
set_environment_var(attr_name, attr_value) click to toggle source
# File lib/chef/provider/cron.rb, line 188
def set_environment_var(attr_name, attr_value)
  if ENVIRONMENT_PROPERTIES.include?(attr_name)
    current_resource.send(attr_name.downcase.to_sym, attr_value.gsub(/^"|"$/, ""))
  else
    current_resource.environment(current_resource.environment.merge(attr_name => attr_value))
  end
end
time_out_str() click to toggle source

@return [String] The timeout command string formed as per time_out property.

# File lib/chef/provider/cron.rb, line 251
def time_out_str
  return "" if new_resource.time_out.empty?

  str = " timeout"
  str << " --preserve-status" if new_resource.time_out["preserve-status"].to_s.casecmp("true") == 0
  str << " --foreground" if new_resource.time_out["foreground"].to_s.casecmp("true") == 0
  str << " --kill-after #{new_resource.time_out["kill-after"]}" if new_resource.time_out["kill-after"]
  str << " --signal #{new_resource.time_out["signal"]}" if new_resource.time_out["signal"]
  str << " #{new_resource.time_out["duration"]};"
  str
end
write_crontab(crontab) click to toggle source
# File lib/chef/provider/cron.rb, line 205
def write_crontab(crontab)
  write_exception = false
  so = shell_out!("crontab -u #{new_resource.user} -", input: crontab)
rescue => e
  raise Chef::Exceptions::Cron, "Error updating state of #{new_resource.name}, error: #{e}"
end