Now that you’ve written some cookbooks after learning about some of the config management primitives and running through the standard “deploy a website with apache” walkthroughs you’re finding that you make some of the same customizations to every cookbook.
Instead of (sometimes, hopefully remembering to…wait what about that one thing?) manually making those same modifications for every cookbook, drop those configurations and overrides in a generator cookbook with chef generate generator base_origin
.
Now every object that you would create with the chef generate
command (app, cookbook, recipe, attribute, resource, etc.) references the generator recipe you wrote, including all the modifications and customizations that you added. And this doesn’t benefit just you alone. Generator cookbooks give your team a standardized set of configurations and expectations for every cookbook and recipe, letting everyone learn from each other’s past discoveries and mistakes.
Let’s get down to business
First you have to generate the generator (replace base_origin with whatever you want your generator cookbook to be named).
chef generate generator base_origin
Next make whatever changes you want to your baseline generator. We’ll go over a few possible modifications shortly.
You have to reference the generator cookbook in your config.rb.
if File.basename($PROGRAM_NAME).eql?('chef') && ARGV[0].eql?('generate')
chefdk.generator.license= "all_rights"
chefdk.generator.copyright_holder = "<Yournamehere>"
chefdk.generator.email = "<yourname>@<yourdomain>.com"
chefdk.generator_cookbook = "/vagrant/configs/base_origin"
end
Before you start using your generator cookbook overrides in the chef development process, generate a few test cookbooks and inspect the results to verify the changes you made do exactly what you intended:
chef generate cookbook test_cookbook
What to put in a generator cookbook
Alright so what might make sense to override in a generator cookbook? Here are a few ideas to inspire.
Chefspec spec_helper defaults
- Add a unit test coverage meter (although that feature will reportedly be deprecated soon)
- Add a shell_out stub detector guard (to safely run shell_out commands in chefspec runs)
- Add some output color to chefspec results
Then apply any modifications you want to see in your generator to the files/default/spec_helper.rb
file in the generator cookbook.
Chefspec configuration defaults
- Define the default chefspec platform and platform version (eg. CentOS 7.5.1804)
- Use ServerRunner instead of SoloRunner
- Create databag and environment skeleton defaults
- Assign default environment to node
- Override fauxhai hostname
- Add shell command stub skeleton defaults
Then apply any modifications you want to see in your generator to the templates/default/recipe_spec.rb.erb
file in the generator cookbook.
Test kitchen configuration defaults
- Set paths for environments, data_bags, roles
- Set default driver and privilege level
- Apply platform and driver configurations
Then apply any modifications you want to see in your generator to the templates/default/kitchen.yml.erb
file in the generator cookbook.
Generally speaking, if you find yourself making the same modifications to every cookbook or recipe that you write then that is a good candidate for a generator override.