I've been working with system configuration utility Chef over the last few months and I was disappointed to learn that it didn't provide support for Mercurial (Hg) version control…so I did what any good open-sourcer would and built it myself.
This brief tutorial explains how to use the simple Hg package that I wrote for Chef.
Chef is a deployment tool that allows you to install and configure a system using a set of 'recipes'. These recipes make it an ideal solution for managing repeated server deployment and keeping track of the packages required to get things up and running.
I have spent the last couple months researching and executing a move to EC2 and it was a perfect opportunity to do some spring cleaning and manage the server configuration. All of the Napes. source code is maintained in Mercurial repositories, so the adoption of Chef was a shock. Only Git? Really? I developed this simple cookbook to ease deployment of Napes. code and I'm happy to share it with the Chef community in the hop that it will help others and that others will contribute to improving it.
The source code is hosted on Bitbucket at https://bitbucket.org/niallsco/chef-cookbooks and I've released it under the Apache 2.0 license.
I'll assume that you have a cookbook that you using to represent the configuration for a particular role or server. The following commands should be added to a recipe in that cookbook that corresponds to the function or service you're trying to provide. In our case we'll call the cookbook main and the recipe webapp.rb.
To get started, download the source code from Bitbucket and copy the cookbook chef-hg into your own cookbook collection (not the main cookbook, but the cookbooks directory).
The chef-hg resource is designed to access the Mercurial repository using ssh and a private key. You will need to ensure that the key is present on your host system. This can be achieved by using the cookbook_file resource. The cookbook_file resource copies a file from the cookbook onto the server at a specified location.
Create the directory files/default in the main cookbook if it does not already exist. Copy your ssh key into this directory. This should be the key you use to connect to the Mercurial repository you wish to sync with.
Copy the following code into the webapp.rb recipe.
cookbook_file "/home/site/.ssh/keyname" do
owner "site"
group "site"
source "keyname"
mode "0700"
end
Replace keyname with the filename of the key you copied into the files/default directory and update the path enclosed in quotes to the file path you'd like the key to be placed in, including the file name.
The next step is to checkout your repository, there are two options with chef-hg, :sync and :clone.
Copy the following code into the webapp.rb recipe:
hg "/home/site/checkouts/www" do
repository "ssh://hg@bitbucket.org/niallsco/chef-hg"
reference "tip"
key "/home/site/.ssh/keyname"
action :sync
end
Replace the path enclosed in quotes with the destination directory for the Mercurial clone. The repository should be set to the location of the Mercurial repository and key should be set to the directory specified in the key step above. Depending on the desired action, action should be set to either :sync or :clone as explained above.
Hopefully this will help fellow Mercurial fans in using Chef to its full potential. I've added a project page to help keep track of the chef-hg package. If you have any feedback, please add a comment below.
Comments
Post a commentThere aren't any comments for this entry yet - add one using the form below.
Post a Comment