Community cookbooks and further resources
week6 – Community cookbooks and further resources
Problems Area –
Our infrastructure has many cookbooks that aim to be reusable, primarily through encapsulating behaviour in LWRPs. This led to an explosion of LWRPs and sometimes the documentation didn’t keep up or did just not exist.
Solution 1: Follow Best Practices
This command will create a README.rdoc by default, and I prefer Markdown, so I specify the -r md option.
> knife cookbook create smartmontools -r md
Solution 2: Automate using plugins
README.md which should be generated automatcially must contain information about the recipes, attributes, platform compatibility and cookbook requirements (i.e. depends, recommends, suggests etc).
Mathias Lafeldt wrote a knife plugin that generates an initial README.md from the metadata.rb file in a a cookbook.
Link –
Extension of Mathias Lafeldt was done here with regenerate README.md from the cookbook source code
Please refer these useful blogs as well which is referenced in order to write this article…
What is Metadata in chef?
In order to understand cookbooks without evaluating them every files, we generate metadata about cookbooks
What is metadata.rb?
Before unerstanding about metadata.json, we need to understand about metadata.rb.
Every cookbook requires a small amount of metadata. Metadata is stored in a file called metadata.rb that lives at the top of each cookbook’s directory. The contents of the metadata.rb file provides hints to the Chef server so that cookbooks are deployed to each node correctly.
A metadata.rb file is never interpreted directly. Rather, the metadata.rb file provides a simple location to store and edit data that is then compiled by the Chef server and stored as JSON data.
Metadata is compiled whenever ….
1. a cookbook is uploaded to the Chef server or
2. when the knife cookbook metadata subcommand is run. knife creates a metadata.rb file automatically whenever the knife cookbook create subcommand is run.
metadata.json
metadata.json is generated from metadata.rb. to generate or regenerate the Chef cookbook metadata, which creates a new ‘metadata.json’ file from the ‘metadata.rb’ file.
From the command line of your Chef development server, enter the following command:
# knife cookbook metadata my-cookbook -o <COOKBOOK_PATH>
Example: # knife cookbook metadata my-cookbook-name -o /opt/development/cookbooks/
A metadata.json file can be edited directly, should temporary changes be required. Any subsequent upload or action that generates metadata will cause the existing metadata.json file to be overwritten with the newly generated metadata. Therefore, any permanent change to metadata that is required should only be made in the metadata.rb file.
What if metadata.rb and metadata.json both are present in cookbooks?
If a cookbook contains both a metadata.rb and metadata.json at cookbook load time, the metadata.rb will be preferred. This should be swapped around to ensure that dynamically built metadata which has been compiled is read properly without error.
knife cookbook upload reads metadata.rb and ignores metadata.json. It generates the data from the .rb file, and uploads that. It does not create a metadata.json file, on disk or in the cookbook itself–this is just metadata in the cookbook object.