Using version control
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 on 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 on them.
Using version control is a fundamental part of any infrastructure automation. There are multiple solutions 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!
Getting ready
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 account (which is free) 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 and using chef-repo
as the repository name.
Make sure you have wget
installed on your local workstation, to be able to download the required files from public servers.
How to do it…
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:
mma@laptop $ wget http://github.com/chef/chef-repo/tarball/master ...TRUNCATED OUTPUT... 2016-09-28 20:54:41 (9.26 MB/s) - 'master' saved [7332/7332]
- Extract the downloaded tarball:
mma@laptop $ tar xzvf master
- Rename the directory:
mma@laptop:~ $ mv chef-boneyard-chef-repo-* chef-repo
- Change to your newly created Chef repository:
mma@laptop:~ $ cd chef-repo/
- Initialize a fresh Git repository:
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 git@github.com:mmarschall/chef-repo.git
- Configure Git with your user name and e-mail address:
mma@laptop:~/chef-repo $ git config --global user.email "you@example.com" mma@laptop:~/chef-repo $ git config --global user.name "Your Name"
- Add and commit Chef'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 11 files changed, 545 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:
mma@laptop:~/chef-repo $ git push -u origin master ...TRUNCATED OUTPUT... To git@github.com:mmarschall/chef-repo.git * [new branch] master -> master
How it works…
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.
There's more...
Let's assume you're working on the same chef-repo repository, together with your co-workers. They cloned the repository, added a new cookbook called other_cookbook
, committed their changes locally, and pushed to GitHub. Now, it's time for you to get the new cookbook downloaded 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:
mma@laptop:~/chef-repo $ git pull --rebase From github.com:mmarschall/chef-repo * branch master -> FETCH_HEAD ...TRUNCATED OUTPUT... create mode 100644 cookbooks/other_cookbook/recipes/default.rb
In the event of any conflicting changes, Git will help you merge and resolve them.
See also
- Learn about Git basics at http://git-scm.com/videos
- Walk through the basic steps using GitHub at https://help.github.com/categories/bootcamp