Chef – Foodcritic

  • Post author:
  • Post category:Chef
  • Post comments:0 Comments

Writing good cookbooks without any issue is quite a difficult task. But there are ways which can help in identifying the pitfalls. Flagging in Chef Cookbook is possible. Foodcritic is one of the best way of archiving it, which tries to identify possible issues with the logic and style of cookbooks.

Foodcritic Setup

Step 1 − Add Foodcritic gem.

vipin@laptop:~/chef-repo $ subl Gemfile 
source 'https://rubygems.org' 
gem 'foodcritic', '~>2.2.0'

Step 2 − Install the gem.

vipin@laptop:~/chef-repo $ bundle install 
Fetching gem metadata from https://rubygems.org/ 
...TRUNCATED OUTPUT... 
Installing foodcritic (2.2.0) 

Foodcritic Gem

Step 1 − Run Foodcritic on the cookbook.

vipin@laptop:~/chef-repo $ foodcritic ./cookbooks/<Cookbook Name> 
FC002: Avoid string interpolation where not required: ./cookbooks/ 
mysql/attributes/server.rb:220 
...TRUNCATED OUTPUT... 
FC024: Consider adding platform equivalents: ./cookbooks/<Cookbook Name>/ 
recipes/server.rb:132 

Step 2 − Generate a detailed report.

vipin@laptop:~/chef-repo $ foodcritic -C ./cookbooks/mysql 
cookbooks/<cookbook Name>/attributes/server.rb 
FC002: Avoid string interpolation where not required 
[...] 
85| default['<Cookbook Name>']['conf_dir'] = "#{mysql['basedir']}" 
[...] 
cookbooks/<Cookbook Name>/recipes/client.rb 
FC007: Ensure recipe dependencies are reflected in cookbook 
metadata 
40| end 
41|when "mac_os_x" 
42| include_recipe 'homebrew' 
43|end 
44|

Working Method

Foodcritic defines a set of rules and checks recipe agents, each one of them. It comes with multiple rules concerning various areas: styles, connectedness, attributes, string, probability, search, services, files, metadata, and so on.

Leave a Reply