Monday, August 7, 2017

Evaluating and troubleshooting Cookbooks and Chef runs

Developing cookbooks and making sure your nodes converge to the desired state is a complex endeavor.You need transparency about what is happening.From running basic checks on your cookbooks to a fully test-driven development approach,we will see what the Chef ecosystem has to offer.

Testing your Chef cookbook with cookstyle and Rubocop

You know how annoying this is: you tweak a cookbook,run Test Kitchen and boom! it fails.What's even more annoying is that it fails only because you missed  a mundane comma in the default recipe of the cookbook you just tweaked .Fortunately there a quick and easy way to find such simple glitches before you go and try to ru your cookbooks on Test kitchen.

Install the ntp cookbook by running the following command:

local@workstation:~/chef-repo $ knife cookbook site install ntp
Installing ntp to /Users/mmmmma/work/chef-repo/cookbooks
....TRUNCATED OUTPUT

Cookbook ntp version 3.2.0  successfully installed


How to do it?

Carry out the following steps  to test your cookbook; run cook style on the nap cookbook.

local@workstation:~/chef-repo $  cookstyle cookbook/ntp
Inspecting 5 files
...C
offenses:
cookbook/ntp/recipes/default.rb:25:1  C: Extra blank line detected.

5 files inspected, 1 offenses completed.

How it works?

Cookstyle is a wrapper around Rubocop  and executes a Ruby syntax check an all Ruby files with in the cookbook.Rubocop is a linting and style checking tool built for Ruby.Cookstyle defines same rules for Chef cookbooks.

There's more

There exist a whole ecosystem of additional tools  such as Chefspec  (behavior driven testing for chef) and an Test kitchen (an integration testing tool to run cookbooks on virtual servers).


References:

https://docs.chef.io/rubocop.html

source code for rubocop: https://github.com/bbatsov/rubocop/

more about cook styles: https://github.com/chef/cookstyle/


Flagging problems in your chef cookbooks with Foodcritic


You might wonder what are the proven ways to write a cookbooks are.Foodcritic tries to identify possible issues with the logic and style of your cookbooks.

How to use Foodcritic with your existing cookbooks?

Getting ready:

Install version 6.0.0 of the mysql cookbook by running the following code:


local@workstation:~/chef-repo $ knife cookbook site install mysql 6.0.0
Installing mysql to /Users/mma/chef/work/chef-repo/cookbooks/
TRUNCATED OUTPUT
Cookbook mysql version 6.0.0 successfully installed.

How to do it?

lets see how foodcritic reports findings


local@workstation:~/chef-repo $   foodcritic ./cookbooks/mysql
TRUNCATED OUTPUT
FC001: Use strings in preference to symbols to access node attributes
./cookbooks/mysql/libraries/helpers.rb:243
FC005: Avoid repetition of resource declarations
./cookbooks/mysql/libraries/provider_mysql_service.rb:77
TRUNCATED OUTPUT

2) Get a detailed list of the reported sections inside the mysql cookbook by using the C flag:

local@workstation:~/chef-repo $   foodcritic -C ./cookbooks/mysql
TRUNCATED OUTPUT
FC001: Use strings in preference to symbols to access node attributes
    @pkginfo.set[:suse][11'3]['5'5][:server_package] = 'mysql'
     @pkginfo
     end
./cookbooks/mysql/libraries/provider_mysql_service.rb:77
FC005: Avoid repetition of resource declarations
end

#support directories 
directory  "#{new_resource.name}  :create #{etc_dir}" do
path etc_dir
owner new_resource.run_user
group  new_resource.run_group

 How it works?

Foodcritic defines a set of rules and checks your recipes against each of them.It comes with rules concerning various areas: style, correctness,attributes,strings,portability,search,services,files,metadata and so on.Running Foodcritic against a cookbook tells you which of  its rules matched a certain part of your cookbook.By default,it gives you a short explanation of what you should do along the concerned file and line number.

If you run foodcritic -C,it displays excerpts of the places where it found the rules to match.

In the preceding example,it did not like it that the mysql cookbook version 6.0.0 uses symbols to access node attribute instead of strings:

@pkginfo.set[:suse]['11.3']['5.5'][:server_package] = 'mysql'

This could be rewritten as follows:

@pkginfo.sst['suse']['11.3']['5.5']['server_package']  = 'mysql'

Some of the rules,especially those from the styles section, are opinionated.You are able to exclude certain rules or complete sets of rules,such as style when running Foodcritic:


local@workstation:~/chef-repo $   foodcritic -t '~style' ./cookbooks/mysql
local@workstation:~/chef-repo $

 In this case,the tilde negates the tag selection to exclude all rules with the style tag.Running without the tilde would run the style rules exclusively.


local@workstation:~/chef-repo $ foodcritic -t style ./cookbooks/mysql


Note:

If you want to run foodcritic  in a continous integration (CI) environment,you can use the -f parameter  to indicate which rules  should fail the build:


local@workstation:~/chef-repo $ foodcritic -f style ./cookbook/mysql
...TRUNCATED OUTPUT....
FC001: Use strings in preference to symbols to access node attributes:
./cookbooks/mysql/libraries/helpers.rb:273
FC005: Avoid repetition of resource declarations
 ./cookbooks/mysql/libraries/providers_mysql_service.rb:77
 local@workstation:~/chef-repo $ echo$?

In this example,we  tell Foodcritic to fail if any rule in the style group fails.In our case,it  returns a non-zero exit code instead of zero,as it would if either no rule matches or we omit the -f parameter.That non-zero exit code would fail your build on your continous integration server.You can use -f any if you want foodcritic to fail on all warnings/



References:

Foodcritic and its rules at http://www.foodcritic.io
Testing your chef cookbooks with cookstyle and rubocop receipe.
Checkout strainer,a tool to simultaneously test multiple things,such as foodcritic,knife test and other stuff  at http://github.com/customink/strainer



 









No comments:

Post a Comment