Chef 简明教程

Chef - Foodcritic

编写没有任何问题的优秀菜谱是一项相当困难的任务。但是,有一些方法可以帮助识别陷阱。Chef 菜谱中可以进行标记。Foodcritic 是实现此目的的最佳方式之一,它尝试识别菜谱逻辑和样式中的可能问题。

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 − 添加 Foodcritic gem。

Step 1 − Add Foodcritic gem.

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

Step 2 − 安装 gem。

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 − 对菜谱运行 Foodcritic。

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 − 生成一份详细的报告。

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 定义了一组规则,并检查每个食谱代理。它带有针对各个领域的多个规则:样式、关联性、属性、字符串、概率、搜索、服务、文件、元数据等。

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.