Do you manually back up every file before you change it? And do you invent creative filename extensions like _me
and _you
when you try to collaborate on a file? If you answer yes to any of the preceding questions, it's time to rethink your process.
A version control system (VCS) helps you stay sane when dealing with important files and collaborating on them.
Using version control is a fundamental part of any infrastructure automation. There are multiple solutions (some free, some paid) for managing source version control including Git, SVN, Mercurial, and Perforce. Due to its popularity among the Chef community, we will be using Git. However, you could easily use any other version control system with Chef.
Note
Don't even think about building your Infrastructure As Code without using a version control system to manage it!
You'll need Git installed on your box. Either use your operating system's package manager (such as Apt on Ubuntu or Homebrew on OS X), or simply download the installer from www.git-scm.org.
Git is a distributed version control system. This means that you don't necessarily need a central host for storing your repositories. But in practice, using GitHub as your central repository has proven to be very helpful. In this book, I'll assume that you're using GitHub. Therefore, you need to go to github.com and create a (free) account to follow the instructions given in this book. Make sure that you upload your SSH key following the instructions at https://help.github.com/articles/generating-ssh-keys, so that you're able to use the SSH protocol to interact with your GitHub account.
As soon as you've created your GitHub account, you should create your repository by visiting https://github.com/new and using chef-repo
as the repository name.
Before you can write any cookbooks, you need to set up your initial Git repository on your development box. Opscode provides an empty Chef repository to get you started. Let's see how you can set up your own Chef repository with Git using Opscode's skeleton.
Download Opscode's skeleton Chef repository as a tarball:
mma@laptop $ wget http://github.com/opscode/chef-repo/tarball/master
...TRUNCATED OUTPUT... 2013-07-05 20:54:24 (125 MB/s) - 'master' saved [9302/9302]
Extract the downloaded tarball:
mma@laptop $ tar zvf master
Rename the directory. Replace 2c42c6a with whatever your downloaded tarball contained in its name:
mma@laptop $ mv opscode-chef-repo-2c42c6a/ chef-repo
Change into your newly created Chef repository:
mma@laptop $ cd chef-repo/
Initialize a fresh Git repository:
mma@laptop:~/chef-repo $ git init . Initialized empty Git repository in /Users/mma/work/chef-repo/.git/
Connect your local repository to your remote repository on github.com. Make sure to replace
mmarschall
with your own GitHub username:mma@laptop:~/chef-repo $ git remote add origin [email protected]:mmarschall/chef-repo.git
Add and commit Opscode's default directory structure:
mma@laptop:~/chef-repo $ git add . mma@laptop:~/chef-repo $ git commit -m "initial commit"
[master (root-commit) 6148b20] initial commit 10 files changed, 339 insertions(+), 0 deletions(-) create mode 100644 .gitignore ...TRUNCATED OUTPUT... create mode 100644 roles/README.md
Push your initialized repository to GitHub. This makes it available to all your co-workers to collaborate on it.
mma@laptop:~/chef-repo $ git push -u origin master
...TRUNCATED OUTPUT... To [email protected]:mmarschall/chef-repo.git * [new branch] master -> master
You've downloaded a tarball containing Opscode's skeleton repository. Then, you've initialized your chef-repo
and connected it to your own repository on GitHub.
After that, you've added all the files from the tarball to your repository and committed them. This makes Git track your files and the changes you make later.
As a last step, you've pushed your repository to GitHub, so that your co-workers can use your code too.
Let's assume you're working on the same chef-repo
repository together with your co-workers. They cloned your repository, added a new cookbook called other_cookbook
, committed their changes locally, and pushed their changes back to GitHub. Now it's time for you to get the new cookbook down to your own laptop.
Pull your co-workers, changes from GitHub. This will merge their changes into your local copy of the repository.
mma@laptop:~/chef-repo $ git pull
From github.com:mmarschall/chef-repo * branch master -> FETCH_HEAD ...TRUNCATED OUTPUT... create mode 100644 cookbooks/other_cookbook/recipes/default.rb
In the case of any conflicting changes, Git will help you merge and resolve them.
Learn about Git basics at http://git-scm.com/videos
Walk through the basic steps using GitHub at https://help.github.com/categories/54/articles
The Downloading and integrating cookbooks as vendor branches into your Git repository section