Table of contents
1.
Introduction
2.
BlockGaurdWithOnlyString
2.1.
Examples
3.
ChefApplicationFatal
3.1.
Examples
4.
ConditionalRubyShellout
4.1.
Examples
5.
IncorrectLibraryInjection:
5.1.
Examples
6.
DnfPackageAllowDowngrades:
6.1.
Examples
7.
CookbookUsesNodeSave:
7.1.
Examples
8.
InvalidCookbookName:
8.1.
Examples
9.
InvalidDefaultAction:
9.1.
Examples
10.
InvalidNotificationResource
10.1.
Examples
11.
InvalidNotificationTiming
11.1.
Examples
12.
InvalidPlatformFamilyHelper
12.1.
Examples
13.
InvalidPlatformFamilyInCase
13.1.
Examples
14.
InvalidPlatformHelper
14.1.
Examples
15.
Frequently Asked Questions
15.1.
What is Progress Chef?
15.2.
What is Cookstyle? 
15.3.
What are Policyfiles?
16.
Conclusion
Last Updated: Mar 27, 2024

CookStyle Cops - Chef/Correctness Section

Author Anmol Punetha
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
OG Image

Introduction

Chef Apps

Source: Chef

Progress Chef is a configuration management tool with a complete automation solution for applications and infrastructure.

We use the Chef Infra platform to transfer the infrastructure to code.

Cookstyle is a linting tool that helps improve the cookbook code by automatically correcting syntax, style, and logical errors in your code.

BlockGaurdWithOnlyString

The complete name of the cop is: Chef/Correctness/BlockGaurdWithOnlyString

BlockGaurdWithOnlyString is a cookstyle cop that is enabled by Default and supports autocorrection. It is a cop that can be used in all chef versions. 

A resource guard, i.e. (only_if/not_if) if it is a string, should not be wrapped in {} as doing so will cause it to be executed as Ruby code that always returns a true and not a shell command that runs.

Examples

Incorrect:

template '/etc/foo' do
 mode '0644'
 source 'foo.erb'
 only_if { 'test -f /etc/foo' }
End

 

Correct:

template '/etc/foo' do
 mode '0644'
 source 'foo.erb'
only_if 'test -f /etc/foo'
end

 

Configurable Attributes

 

 

ChefApplicationFatal

The complete name of the cop is: Chef/Correctness/ChefApplicationFatal

ChefApplicationFatal is a cookstyle cop that is enabled by Default and supports autocorrection. It is a cop that can be used in all chef versions. 

We use raise to force the Chef Infra Client to fail rather than using Chef::Application.fatal, as it masks the full stack trace of the failure and makes debugging harder.

Examples

Incorrect:

Chef::Application.fatal!('Something horrible happened!')

 

Correct:

raise "Something horrible happened!"

 

Configurable Attributes:

 

 

ConditionalRubyShellout

The complete name of the cop is: Chef/Correctness/ConditionalRubyShellout

ConditionalRubyShellout is a cookstyle cop that is enabled by Default and supports autocorrection. It is a cop that can be used in all chef versions. 

We don’t use Ruby to shell out in case of only_if / not_if conditionals because any string value will get executed in your system’s shell and the return code is the result for only_if / not_if determination.

Examples

Incorrect:

cookbook_file '/logs/foo/error.log' do
source 'error.log'
only_if { system('wget https://www.bar.com/foobar.txt -O /dev/null') }
end


cookbook_file '/logs/foo/error.log' do
source 'error.log'
 only_if { shell_out('wget https://www.bar.com/foobar.txt -O /dev/null').exitstatus == 0 }
end

 

Correct:

cookbook_file '/logs/foo/error.log' do
source 'error.log'
only_if 'wget https://www.bar.com/foobar.txt -O /dev/null'
end

 

Configurable Attributes:

 

IncorrectLibraryInjection:

 

The complete name of the cop is: Chef/Correctness/IncorrectLibraryInjection

IncorrectLibraryInjection is a cookstyle cop that is enabled by Default and supports autocorrection. It is a cop that can be used in all chef versions. 

The libraries being injected should be injected into the Chef::DSL::Recipe class and not Chef::Recipe or Chef::Provider classes.

Examples

Incorrect:

::Chef::Recipe.send(:include, Filebeat::Helpers)

::Chef::Provider.send(:include, Filebeat::Helpers)

::Chef::Recipe.include Filebeat::Helpers

::Chef::Provider.include Filebeat::Helpers

 

Correct:

::Chef::DSL::Recipe.send(:include, Filebeat::Helpers) # covers previous Recipe & Provider classes

 

Configurable Attributes:

 

DnfPackageAllowDowngrades:

The complete name of the cop is: Chef/Correctness/DnfPackageAllowDowngrades

DnfPackageAllowDowngrades is a cookstyle cop that is enabled by Default and supports autocorrection. It is a cop that can be used in all chef versions. 

The resource of dnf_package doesn’t support the allow_downgrades property.

Examples

Incorrect:

dnf_package 'nginx' do
version '1.2.3'
allow_downgrades true
end

 

Correct:

dnf_package 'nginx' do
version '1.2.3'
end

 

Configurable Attributes:

 

CookbookUsesNodeSave:

The complete name of the cop is: Chef/Correctness/CookbookUsesNodeSave

CookbookUsesNodeSave is a cookstyle cop that is enabled by Default and it does not support autocorrection. It is a cop that can be used in all chef versions. 

Never use node.save to save partial data to the server mid-run unless it’s a requirement of the cookbook design that’s inevitable as it can result in failed runs appearing in search as well as it increases the load on the chef infra server.

Examples

 

Incorrect:

node.save

 

Configurable Attributes:

 


 

 

InvalidCookbookName:

The complete name of the cop is: Chef/Correctness/InvalidCookbookName

InvalidCookbookName is a cookstyle cop that is enabled by Default and it does not support autocorrection. It is a cop that can be used in all chef versions. 

The Cookbook name shouldn’t contain any invalid characters like periods or commas.

Examples

Incorrect:

name 'foo.bar'

 

Correct:

name 'foo_bar'

 

Configurable Attributes:

 

InvalidDefaultAction:

The complete name of the cop is: Chef/Correctness/InvalidDefaultAction

InvalidDefaultAction is a cookstyle cop that is enabled by Default and it does not support autocorrection. It is a cop that can be used in all chef versions. 

The default actions should be symbols/arrays of symbols.

Examples

Incorrect:

default_action 'create'

 

Correct:

default_action :create

 

Configurable Attributes:

 

InvalidNotificationResource

The complete name of the cop is: Chef/Correctness/InvalidNotificationResource

InvalidNotificationResource is a cookstyle cop that is enabled by Default and it does not support autocorrection. It is a cop that can be used in all chef versions. 

The resource used to call notifies or subscribes should be a string.

Examples

Incorrect:

template '/etc/www/configures-apache.conf' do
notifies :restart, service['apache'], :immediately
end
template '/etc/www/configures-apache.conf' do
notifies :restart, service[apache], :immediately
end

 

Correct:

template '/etc/www/configures-apache.conf' do
notifies :restart, 'service[apache]', :immediately
end

 

Configurable Attributes:

 


InvalidNotificationTiming

The complete name of the cop is: Chef/Correctness/InvalidNotificationTiming

InvalidNotificationTiming is a cookstyle cop that is enabled by Default and it does not support autocorrection. It is a cop that can be used in all chef versions. 

The valid notification timings possible are :immediate, :immediately, :delayed & :before

Examples

Incorrect:

template '/etc/www/configures-apache.conf' do
notifies :restart, 'service[apache]', :nope
end

 

Correct:

template '/etc/www/configures-apache.conf' do
notifies :restart, 'service[apache]', :immediately
end

 

Configurable Attributes: 

 

Must Read Apache Server

InvalidPlatformFamilyHelper

The complete name of the cop is: Chef/Correctness/InvalidPlatformFamilyHelper

InvalidPlatformFamilyHelper is a cookstyle cop that is enabled by Default and it supports autocorrection. It is a cop that can be used in all chef versions. 

Pass valid platform families to the platform_family? helper.

Examples

Incorrect:

platform_family?('redhat')
platform_family?('sles')


#### incorrect


```ruby
platform_family?('rhel')
platform_family?('suse')

 

Configurable Attributes:

 


InvalidPlatformFamilyInCase

The complete name of the cop is: Chef/Correctness/InvalidPlatformFamilyInCase

InvalidPlatformFamilyInCase is a cookstyle cop that is enabled by Default and it supports autocorrection. It is a cop that can be used in all chef versions. 

The valid platform family values have to be in case statements to be used.

Examples

Incorrect:

case node['platform_family']
when 'redhat'
puts "I'm on a RHEL-like system"
end

 

Configurable Attributes: 

 

InvalidPlatformHelper

The complete name of the cop is: Chef/Correctness/InvalidPlatformHelper

InvalidPlatformHelper is a cookstyle cop that is enabled by Default and it does not support autocorrection. It is a cop that can be used in all chef versions. 

Only pass valid platforms to the platform? helper.

Examples

Incorrect:

platform?('darwin')
platform?('rhel')
platform?('sles')

 

Correct:

platform?('mac_os_x')
platform?('redhat')
platform?('suse')

 

Configurable Attributes:

 

Frequently Asked Questions

What is Progress Chef?

Progress Chef is a configuration management tool that provides a way to define infrastructure as code, i.e., a method to manage infrastructure without using manual processes. Chef uses pure Ruby, Domain Specific Language, to write system configurations.

What is Cookstyle? 

Cookstyle is a linting tool that helps improve the cookbook code by automatically correcting syntax, style, and logical errors in your code.

What are Policyfiles?

Policyfiles are a way to create an immutable collection of cookbooks, their dependencies, and attributes that are defined in a single document uploaded to the Chef Infra server.

Conclusion

So, with this, we saw about the Chef/Correctness section of the Cookstyle tool. I hope the blog was informative. 

See Basics of C++ with Data StructureDBMSOperating System by Coding Ninjas, and keep practicing on our platform Coding Ninjas Studio.

If you think you are ready for the tech giants company, check out the mock test series on code studio.

You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in domains like Data Structures and AlgorithmsCompetitive ProgrammingAptitude, and many more! You can also prepare for tech giants companies like Amazon, Microsoft, Uber, etc., by looking for the questions asked by them in recent interviews. If you want to prepare for placements, refer to the interview bundle. If you are nervous about your interviews, you can see interview experiences to get ideas about these companies' questions.

Nevertheless, you may consider our premium courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass