Do you manually back up every file before you change it? And do you invent creative file name extensions such as _me
and _you
when you try to collaborate a file? If you answer yes to any of these, it's time to rethink your processes.
A version control system (VCS) helps you stay sane when dealing with important files and collaborating with them.
Using version control is a fundamental part of any infrastructure automation. There are multiple solutions (some free, some paid) to manage 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 local workstation. 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 to store your repositories. However, 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 www.github.com and create an (free) account to follow the instructions given in this book. Make sure that you upload your Secure Shell (SSH) key by 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 have created your GitHub account, you should create your repository by visiting https://github.com while you're still logged in and using chef-repo
as the repository name.
Make sure you have wget
installed on your local workstation, in order to be able to download the required files from public servers.
Before you can write any cookbooks, you need to set up your initial Git repository on your development box. Chef Software, Inc. provides an empty Chef repository to get you started. Let's see how you can set up your own Chef repository with Git, using Chef's skeleton.
- Download Chef's skeleton repository as a tarball:
- Extract the downloaded tarball:
- Rename the directory:
- Change to your newly created Chef repository:
- Initialize a fresh Git repository:
- Connect your local repository to your remote repository on github.com. Make sure to replace
mmarschall
with your own GitHub username: - Configure Git with your user name and e-mail address:
- Add and commit Chef's default directory structure:
- Push your initialized repository to GitHub. This makes it available to all your co-workers to collaborate on:
You have downloaded a tarball containing Chef's skeleton repository. Then, you initialized chef-repo
and connected it to your own repository on GitHub.
After that, you 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.
Finally, you 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 downloaded on to your own laptop.
Pull your co-workers' changes from GitHub. This will merge their changes into your local copy of the repository. Use the pull
subcommand:
In case of any conflicting changes, Git will help you merge and resolve them.