Class: RuboCop::Cop::Chef::Modernize::CronDFileOrTemplate

Inherits:
Base
  • Object
show all
Extended by:
TargetChefVersion
Includes:
RuboCop::Chef::CookbookHelpers
Defined in:
lib/rubocop/cop/chef/modernize/cron_d_file_or_template.rb

Overview

Use the cron_d resource that ships with Chef Infra Client 14.4+ instead of manually creating the file with template, file, or cookbook_file resources.

Examples:


### incorrect
template '/etc/cron.d/backup' do
  source 'cron_backup_job.erb'
  owner 'root'
  group 'root'
  mode '644'
end

cookbook_file '/etc/cron.d/backup' do
  owner 'root'
  group 'root'
  mode '644'
end

file '/etc/cron.d/backup' do
  content '*/30 * * * * backup /usr/local/bin/backup_script.sh'
  owner 'root'
  group 'root'
  mode '644'
end

file '/etc/cron.d/blogs' do
  action :delete
end

file "/etc/cron.d/#{job_name}" do
  action :delete
end

file File.join('/etc/cron.d', job) do
  action :delete
end

file 'delete old cron job' do
  path '/etc/cron.d/backup'
  action :delete
end

file 'delete old cron job' do
  path "/etc/cron.d/#{job}"
  action :delete
end

file 'delete old cron job' do
  path ::File.join('/etc/cron.d', job)
  action :delete
end

### correct
cron_d 'backup' do
  minute '1'
  hour '1'
  mailto 'sysadmins@example.com'
  command '/usr/local/bin/backup_script.sh'
end

cron_d 'blogs' do
  action :delete
end

Constant Summary collapse

MSG =
'Use the cron_d resource that ships with Chef Infra Client 14.4+ instead of manually creating the file with template, file, or cookbook_file resources'

Instance Method Summary collapse

Methods included from TargetChefVersion

minimum_target_chef_version, required_minimum_chef_version, support_target_chef_version?

Methods included from RuboCop::Chef::CookbookHelpers

#match_property_in_resource?, #match_resource_type?, #method_arg_ast_to_string, #resource_block_name_if_string

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_block(node) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rubocop/cop/chef/modernize/cron_d_file_or_template.rb', line 102

def on_block(node)
  file_or_template?(node) do |file_name|
    break unless file_name.start_with?(%r{/etc/cron\.d\b}i)
    add_offense(node, severity: :refactor)
  end

  match_property_in_resource?(%i(template file cookbook_file), 'path', node) do |code_property|
    # instead of using CookbookHelpers#method_arg_ast_to_string, walk the property's descendants
    # and check if their value contains '/etc/cron.d'
    # covers the case where the argument to the path property is provided via a method like File.join
    code_property.each_descendant do |d|
      add_offense(node, severity: :refactor) if d.respond_to?(:value) && d.value.match?(%r{/etc/cron\.d\b}i)
    end
  end
end