Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Cookstyle is a code linting tool that aids in the improvement of your Chef Infra cookbooks by identifying and automatically fixing any errors in style, grammar, and logic. Cookstyle was created to make it possible to upgrade the RuboCop engine while maintaining backward compatibility with the configuration and avoiding the need to update all of the recipes. Any new cops in the file that are enabled or disabled must have comments explaining the justification for the change.
This blog explains the details of CookStyle Cops - Chef/Modernize Section along with the details of its various departments and examples.
Without further ado, let's get started.
ActionMethodInResource
Department : ActionMethodInResource
Full name of the cop: Chef/Modernize/ActionMethodInResource
It is enabled by default and supports autocorrection. Instead of constructing actions with methods, use the action:my_action blocks of the custom resource language.
Examples
Incorrect:
def action_create
# :create action code here
end
Correct:
action:create do
# :create action code here
end
AllowedActionsFromInitialize
Department : AllowedActionsFromInitialize
Full name of the cop: Chef/Modernize/AllowedActionsFromInitialize
It is enabled by default and supports autocorrection. Instead of utilising the @actions or @allowed actions variables in the resource's initialise method, the allowed_actions can now be defined using the allowed actions helper. In general, we advise against developing HWRPs, but if you must, you should make the most of the resource DSL.
Examples
Incorrect:
def initialize(*args)
super
@actions = [ :create, :add ]
end
# also bad
def initialize(*args)
super
@allowed_actions = [ :create, :add ]
end
Correct:
allowed_actions [ :create, :add ]
ChefGemNokogiri
Department : ChefGemNokogiri
Full name of the cop: Chef/Modernize/ChefGemNokogiri
It is enabled by default and supports autocorrection. Chef Infra Client 12+ includes the nokogiri gem out of the box; it does not require installation to utilise.
Examples
Incorrect:
chef_gem 'nokogiri'
ClassEvalActionClass
Department : ClassEvalActionClass
Full name of the cop: Chef/Modernize/ClassEvalActionClass
It is enabled by default and supports autocorrection. It is no longer essential to call the class eval method on the action class block in Chef Infra Client 12.9 and later.
Examples
Incorrect:
action_class.class_eval do
foo
end
Correct:
action_class do
foo
end
ConditionalUsingTest
Department : ConditionalUsingTest
Full name of the cop: Chef/Modernize/ConditionalUsingTests
It is enabled by default and supports autocorrection. Use ::File.exist? ('/foo/bar') rather than the slower, more shell out 'test -f /foo/bar'
Examples
Incorrect:
only_if 'test -f /bin/foo'
Correct:
only_if { ::File.exist?('bin/foo') }
CronDFileOrTemplate
Department : CronDFileOrTemplate
Full name of the cop: Chef/Modernize/CronDFileOrTemplate
It is enabled by default but does not support autocorrection. Instead of manually creating the file with the template, file, or cookbook_file resources, use the cron_d resource that is included with Chef Infra Client 14.4+.
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
CronManageResource
Department : CronManageResource
Full name of the cop: Chef/Modernize/CronManageResource
It is enabled by default and supports autocorrection. The target chef version is 14.4+. In the cron cookbook's 6.1 release and later included in Chef Infra Client 14.4, the cron_manage resource was changed to cron_access. Use the updated resource name.
Examples
Incorrect:
cron_manage 'mike'
Correct:
cron_access 'mike'
CustomResourceWithAttributes
Department : CustomResourceWithAttributes
Full name of the cop: Chef/Modernize/CustomResourceWithAttributes
It is enabled by default and supports autocorrection. You defined attributes in HWRPs and LWRPs, but custom resources altered the name of the concept to properties in order to avoid confusion with chef recipe attributes. Despite the fact that they are aliased, they should be referred to as properties when constructing a custom resource.
Examples
Incorrect:
attribute :something, String
action :create do
# some action code because we're in a custom resource
end
Correct:
property :something, String
action :create do
# some action code because we're in a custom resource
end
DatabagHelpers
Department : DatabagHelpers
Full name of the cop: Chef/Modernize/DatabagHelpers
It is enabled by default and supports autocorrection. Instead of using Chef::DataBagItem.load or Chef::EncryptedDataBagItem.load, use the data_bag_item helper.
Full name of the cop: Chef/Modernize/DeclareActionClass
It is enabled by default and supports autocorrection. The target Chef version is 12.9+. The action_class can be used in place of declare_action_class with Chef Infra Client 12.9 and later.
Examples
Incorrect:
declare_action_class do
foo
end
Correct:
action_class do
foo
end
DefaultActionFromInitialize
Department : DefaultActionFromInitialize
Full name of the cop: Chef/Modernize/DefaultActionFromInitialize
It is enabled by default and supports autocorrection. The @action variable in the resource provider initialise method can now be replaced with the default action helper to specify default_actions. In general, we advise against developing HWRPs, but if you must, you should make the most of the resource DSL.
Examples
Incorrect:
def initialize(*args)
super
@action = :create
end
Correct:
default_action :create
DefinesChefSpecMatchers
Department : DefinesChefSpecMatchers
Full name of the cop: Chef/Modernize/DefinesChefSpecMatchers
It is enabled by default and supports autocorrection. Later versions( ChefSpec 7.1+) of ChefSpec automatically create ChefSpec matchers. Cookbooks can now have their matchers deleted.
Examples
Incorrect:
if defined?(ChefSpec)
def create_yum_repository(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:yum_repository, :create, resource_name)
end
end
Definitions
Department : Definitions
Full name of the cop: Chef/Modernize/Definitions
It is enabled by default but does not support auto correction. Custom Resources were introduced in 2016 with Chef Infra Client 12.5 as a mechanism to create reusable resource code that could be distributed in cookbooks. In addition to unit testing using ChefSpec, input validation, actions, common attributes like not_if/only_if, and resource reporting, custom resources have several advantages over historical definitions.
DependsOnChefVaultCookbook
Department : DependsOnChefVaultCookbook
Full name of the cop: Chef/Modernize/DependsOnChefVaultCookbook
It is enabled by default and supports autocorrection. The target chef version in 16.0. It is independent of the Chef Infra Client 16.0-outdated Chef Vault cookbook. The Chef Infra Client now includes the Chef Vault Gem and Helpers.
Examples
Incorrect:
depends 'chef-vault'
DependsOnChocolateyCookbooks
Department : DependsOnChocolateyCookbooks
Full name of the cop: Chef/Modernize/DependsOnChocolateyCookbooks
It is enabled by default and supports autocorrection. The target chef version in 14.3+. It is independent of the outdated Chef Infrastructure Client 14.3 cookbooks chocolatey_source and chocolatey_config. The Chef Infra Client now includes the chocolatey_source and chocolatey_config resources.
Full name of the cop: Chef/Modernize/DependsOnKernelModuleCookbook
It is enabled by default and supports autocorrection. The target chef version in 14.3+. It is independent of the kernel_module cookbook, which Chef Infra Client 14.3 rendered obsolete. The Chef Infra Client now contains the kernel_module resource.
Examples
Incorrect:
depends 'kernel_module'
DependsOnLocaleCookbook
Department :DependsOnLocaleCookbook
Full name of the cop: Chef/Modernize/DependsOnLocaleCookbook
It is enabled by default and supports autocorrection. The target chef version in 14.5+.
It is independent of the local cookbook, which Chef Infra Client 14.5 rendered obsolete. The Chef Infra Client now contains the locale resource.
Examples
Incorrect:
depends 'locale'
DependsOnOpensslCookbook
Department :DependsOnOpensslCookbook
Full name of the cop: Chef/Modernize/DependsOnOpensslCookbook
It is enabled by default and supports autocorrection. The target chef version in 14.4+. It is independent on the openssl cookbook, which Chef Infra Client 14.4 rendered obsolete. Now, Chef Infra Client directly includes all openssl_* resources.
Examples
Incorrect:
depends 'openssl'
Frequently Asked Questions
What is Cookstyle?
Cookstyle is a code linting tool that aids in the improvement of your Chef Infra cookbooks by identifying and automatically fixing any errors in style, grammar, and logic.
Which command is used for Autocorrecting Cookstyle Warnings ?
The cookstyle -a command is used for Autocorrecting Cookstyle Warnings.
What is RuboCop?
RuboCop is a powerful and adaptable Ruby code style checker and formatter that can find and, in many situations, correct issues in your code.
Conclusion
In this article, we have extensively discussed the details of CookStyle Cops - Chef/Modernize Section along with the details of its various departments and examples.