|
Cookbook Development Workflow for Chef CookbooksThis page describes development workflow for Chef cookbooks using Git as the version control system.
This page will walk through five examples:
Pre-requisitesThe user should already have cloned the skeleton Chef Repository from Opscode's GitHub repository. git clone Opscode's chef-repo skeleton The remaining examples will be done from the chef-repo directory. Work from chef-repo As this is already a git repository, it's already set up for you to use with Git. However, the remote origin is still Opscode's repository. See the remote repositories section of this article for information on remote repositories in Git. |
|
New CookbookA cookbook is a directory which contains the recipes and supporting code and assets. Create a new cookbook with knife. knife cookbook create working_with_git This will create a predefined directory structure of all the various components of a Cookbook.
As it has created files, the git working copy will have uncommitted changes. Show that we have uncommitted changes Here's what the cookbook directory contains: Contents of the cookbook's directory
Next, let's edit the default recipe to do something. vi cookbooks/working_with_git/recipes/default.rb Now write the changes to the git repository. This is done in two steps. First, we add a list of files with git-add. Tell git what files will be committed We can view the changes that will be written with git-status. Show what will be committed Next use git-commit to write the changes to the repository. Write the commit (changes) to the repository
You've created a cookbook and written the changes to the local git repository. What is metadata.json?If you are using a Chef Server, after doing the knife cookbook upload, you might notice there's a new file, metadata.json, in the cookbook. Show generated metadata.json file This is the generated JSON file from the upload. This is dynamically generated, and if you modify the metadata.rb, it will change every time the cookbook is uploaded. You can add metadata.json to the .gitignore file to ignore these generated files, but make sure to always use the metadata.rb file for modifying the cookbook's metadata. Use .gitignore to ignore metadata.json files Notice that we only added the .gitignore file, the metadata.json file is still there, Git just won't show it as uncommitted anymore. Read more about cookbook Metadata. Read more about gitignore(5) Download CookbookOpscode provides a centralized location for the Chef Community to share and download cookbooks. Knife can be used to retrieve cookbooks from the Community site and automatically integrate them into the local Git repository. Make sure that the local repository has all changes committed: Use Git Status to show clean repository For this example we're going to download the chef-client cookbook and integrate it into the local repository using the "vendor branch" pattern. Output of filenames in the cookbook are truncated; you can view the full output by running the same command. knife cookbook site install chef-client This command does the following:
By default, the cookbook's dependencies are also downloaded. The -D option can be used to prohibit dependency download. Learn more about cookbook dependencies.
We can now view some changes from this command in the repository. First, a new git-branch(1) is created: git branch The "*" in front of master indicates it is the currently checked out branch. Also, a git-tag(1) is created: git tag Modify CookbookAfter you have created a new cookbook, or installed a cookbook in the repository with the install command, you may want to make modifications. Edit the files as desired and commit your changes to the Git repository. The filenames and contents changed are not important. Assuming we've editted a recipe and saved changes to the file: Edit files and save changes git status to show uncommitted changes git diff to see file content changes Add the file to the commit and create the commit. git add, git commit Tracking Upstream ChangesOne advantage of using the vendor branch pattern described above is it becomes easy to make local changes to a cookbook, and still be able to retrieve upstream improvements and other changes. Once your local changes are committed, you can download a new version again with the same command. knife cookbook site install Note: there weren't any updates, since we had just downloaded the cookbook of the same version already. If we did make changes and there were updates on the Community Site, then they would be merged in. If there are any conflicts between local changes and the upstream's update, then we would handle those by editing the files, or modifying the local copy in the repository. Read more about branching and merging, "Resolving a Merge" section of the online Git Community Book. Version TrackingAll git repos have version tracking, so even if you are just committing changes locally you can still go back and see what was changed. You can use git log to see the log of commits. This command would show you the last 3 commits for example: git log You could then see more information on a specific commit with git show. For example, to see more information on the last commit you could take the commit id and use it in this command: git show For more information on the show and log commands, reference the git-show and git-log manual pages. The command `man gittutorial` can also give you further information on how these can be used. Testing CookbooksTesting cookbooks is not directly related to Git, but depending on how you're using Chef determines what you need to do to test changes you've made to cookbooks. If you're using Chef SoloYou'll need to copy the cookbooks you want to use to a system with Chef Solo, configured to use the cookbooks directory. You'll also need to pass a JSON file to chef-solo with the run_list of recipes to use. See the Chef Solo page for information about how to set up a system to run chef-solo. If you're using Chef Client/ServerUpload the cookbook to the Chef Server with Knife: knife cookbook upload Then apply the recipe to a node or role and run Chef on the desired system. Remote RepositoriesBy default, the remote origin is Opscode's repository. This means that you can make commits to the local repo for version tracking and other features, but not the remote repo so you can access externally. If you would like to add a remote origin for your personal git, you will first want to add a new repo to your git server. Once that is done you can add the remote origin with a command like this: git remote Then, whenever you want to push commits to your remote git you can do it with this command, after using git add and git commit: git push to remote You can use any name in place of github in these examples, it is just a name for the remote connection. For more information on remote, read the git-remote manual page. Here's an example of how your workflow may look, once this is all setup: 1. Create the cookbook directory skeleton with knife cookbook create. Keep in mind that due to how git actually works, we can't predict what the proper behavior for #5 and #6 will be.
|

