Automatically Updating Cookbooks with Cookstyle

Infrastructure code, just like your infrastructure, is in constant flux. New services need to be updated and deployed, configuration changes need to be propagated, and security vulnerabilities mitigated. There’s a never-ending stream of code changes to make and once you’ve written your infrastructure code you just want it to work. There’s no time to update your code for new tools, but every month brings a Chef Infra Client release with new features and best practices. How do you know the code you wrote years ago will continue to function and fully utilize these new releases?

That’s why we’ve written Cookstyle: to make sure all of your Chef Infra code always meets the latest best practices. Powered by the RuboCop linting engine, Cookstyle is a code linting tool that helps you to write better Chef Infra cookbooks by detecting and automatically correcting cookbook code. RuboCop ships with over three-hundred rules, or “cops”, designed to detect common Ruby coding mistakes and enforce a common coding style. 

We’ve customized Cookstyle with a subset of RuboCop’s built-in cops that we believe are perfectly tailored for cookbook development. There are also over 100 Chef Infra specific cops shipped with Cookstyle that catch common cookbook coding mistakes, cleanup portions of code that are no longer necessary, and detect deprecations that prevent cookbooks from running on the latest releases of Chef Infra Client. Running Cookstyle on your infrastructure code not only ensures that everyone on your team is following a common coding style, it also ensures that you’re utilizing the latest functionality the Chef Infra Client offers.

Cookstyle ships as part of Chef Workstation, so each update to Chef Workstation brings with it additional Chef Infra cops for keeping your infrastructure up to date. With Chef Workstation installed you can run Cookstyle against a cookbook or an entire repository of cookbooks just by typing `cookstyle` at your command prompt. Cookstyle can even integrate with popular editors like Visual Studio Code to alert you to issues in your cookbooks as you write them.

We how you are as excited about Cookstyle as we are! If you have any questions, or want to get started, reach out to us at awesome@chef.io. Happy Automating!

Avatar
Tim Smith

Tim is a Software Development Engineer working on Chef Infra and Workstation at Chef.