Chef for RightScale Quick Start

- - posted in Cloud computing | Comments

For the past few weeks I have been getting down and dirty writing Chef recipes for use in RightScale.

With Chef support still in beta on RightScale, and me being a complete Chef newbie it’s I’m still on the front side of the learning curve.

If you’re just getting started with Chef for RightScale, you might benefit from this quick start and some of the lessons I learned while getting started.

Mise en place

While you could just open up Notepad or TextEdit and start writing your Chef recipes, there are a few resources that will make the experience much more enjoyable.

Install Chef

The first thing you’re going to want to do is install the Chef Ruby Gem. This will add all of the libraries and binaries for Chef.

While we’re writing our recipes for use with RightScale we won’t need to worry about using the Chef Server, Chef Solo, or Chef Client, so don’t bother configuring any of those.

I’m developing on a Mac and the only suitable installation method seems to be using Ruby Gems to install. This mechanism should also work for most Linux and Debian machines which already have Ruby installed.

sudo gem install chef

Be sure that you run this as root, or with sudo or Chef will get installed at ~/.gem and won’t be in your $PATH or consequently accessible to other users.

Sharpen your knives

Now that you have all of the Chef goodies installed, you’ll have a binary named “knife”. The knife is a utility which we’ll be using as we develop our cookbooks and recipes which you’ll see later.

First though, we need to configure knife. Since we’ll only be using knife to act locally on our recipes and cookbooks we don’t need to give it legit values for a Chef server, but we do need to complete the configuration step to satisfy knife and prevent it from crashing when we use it.

To create a simple configuration file for knife, just run knife config, and accept all of the defaults.

knife configure
No knife configuration file found
Where should I put the config file? [~/.chef/knife.rb] 
Please enter the chef server URL: [http://localhost:4000] 
Please enter an existing username or clientname for the API: [rgeyer] 
Please enter the validation clientname: [chef-validator] 
Please enter the location of the validation key: [/etc/chef/validation.pem] 
Please enter the path to a chef repository (or leave blank): 
WARN: *****
WARN: You must place your client key in:
WARN:   /Users/rgeyer/.chef/rgeyer.pem
WARN: Before running commands with Knife!
WARN: *****
WARN: You must place your validation key in:
WARN:   /etc/chef/validation.pem
WARN: Before generating instance data with Knife!
WARN: *****
WARN: Configuration file written to /Users/rgeyer/.chef/knife.rb

You’re safe to ignore the warnings since they only pertain to actually connecting to a Chef server, which we won’t be doing.

That’s it for installing and configuring Chef if you’re using it with RightScale!

Prepare your pantry

Once you get going with Chef you’re going to need to store your cookbooks somewhere for RightScale to find them and run them.

RightScale will use svn or git repositories, as well as simply downloading a tar or zip file, but my personal preference is to use a git repository.

The quickest and easiest way to get started with git is to head over to github and sign up for a free account.

Once you’re setup, go ahead and create the repository we’ll be using for our Chef cookbooks, and note the SSH URL to your new repository. It should look something like<github_username>/<repo_name>.git. Write it down or keep the browser window open so you can reference it later.

Heat things up

Now that you’ve got your mise en place it’s time to get cooking for RightScale with Chef.

Create your repo

The first step is to get the file structure of your Chef repository setup. You could just take a look at an existing repository like the opscode cookbooks, or RightScale’s public repository and replicate it manually, but there is a better way.

Clone the OpsCode repo

OpsCode, (the folks behind Chef) have conveniently provided a public github repository that represents the skeleton of a fully operational Chef repository. You can clone this repository to get you started.

git clone git://

Once you’ve cloned the skeleton repository, you’ll want to configure git to use the new github repository we created earlier. Here’s where you’ll use the SSH URL I told you to take note of. Substitute that URL where you find <ssh_url> below.

cd chef-repo/
git remote rename origin opscode-github
git remote add origin <ssh_url>
git remote rm opscode-github
git push origin master

Create cookbook

With the repository skeleton all ready, we’re going to want to create our first cookbook and add a recipe to it.

Creating a cookbook is our first chance to use our freshly sharpened knife, like so.

knife cookbook create my-first-cookbook -o cookbooks/

This should create a nice new cookbook skeleton directory at <path_to_your_repo>/cookbooks/my-first-cookbook.

With that done, let’s put our first recipe in the cookbook. Using your favorite IDE, create and save a file named helloworld.rb inside the new cookbook directory.

EDIT: Many have correctly pointed out that the hello world file should actually go in <path_to_your_repo>/cookbooks/my-first-cookbook/recipes. Thanks all for the feedback!

log "Hello World"

With the recipe created, we need to add it to our metadata file so that Chef and RightScale can know it’s there. Here’s a copy of our default metadata.rb file. The highlighted line is added to define the recipe we’ve added.

metadata.rb mark:8
maintainer       "YOUR_COMPANY_NAME"
maintainer_email "YOUR_EMAIL"
license          "All rights reserved"
description      "Installs/Configures my-first-cookbook"
long_description, 'README.rdoc'))
version          "0.0.1"

recipe "my-first-cookbook::helloworld", "My first recipe, prints Hello World to the RightScale dashboard"

We only have one more step before we’re ready to commit our new cookbook and recipe to our new git repository.

RightScale reads the metadata to determine which recipes are available in the repository, and which inputs are available for those recipes. The file that RightScale evaluates though is a json file named metadata.json.

The contents of metadata.json are effectively the same as metadata.rb, but take heart, you only need maintain the metadata.rb file, then convert it using another Chef command. To generate metadata.json from metadata.rb just run the following command.

rake metadata

You can run this command either right at the root of your repository, or inside each cookbook. If you run it from the root directory of your repository, it will convert all the metadata files in all of the cookbooks.

Now we’re ready to commit our repository and try out our new cookbook.

git add cookbooks/
git commit -am "Added my-first-cookbook and helloworld recipe"
git push

RightScale Configuration

I’m going to assume that you already have some familiarity with the RightScale dashboard, and creating things like ServerTemplates and RightScripts.

What I’m going to cover here are steps are necessary to start using Chef with your ServerTemplates.

Tell RightScale about your Repo

The first thing you need to start using your cookbooks in RightScale is to tell the RightScale dashboard where to find your repo. To do that, you need to create a new RepoPath in the designer.

Screen shot 2010-11-28 at 7.33.05 PM

Screen shot 2010-11-28 at 7.33.35 PM

When you’re adding the RepoPath, for the URL field, supply the “Git Read-Only URL” for your repository, and make sure to specify the path to your cookbooks as cookbooks.

Screen shot 2010-11-28 at 7.34.37 PM

Add your RepoPath to a ServerTemplate

Now that you’ve added your repo to the RightScale dashboard, you need to add that RepoPath to a ServerTemplate. To do that, open the ServerTemplate you want to run Chef cookbooks on, and click the edit pencil on the Repos tab.

Then select the RepoPath we just made, and click OK.

Screen shot 2010-11-28 at 7.56.33 PM

Add a recipe to your ServerTemplate

Our very last step is to add our helloworld recipe to a our ServerTemplate so that we can run it on one of our instances. Go back to the Scripts tab, and in the script category of your choosing, click Add Recipe and select the helloworld recipe.

Screen shot 2010-11-28 at 8.02.27 PM

Screen shot 2010-11-28 at 8.02.34 PM

From an instance running with the ServerTemplate we just modified, we can run the recipe, and see “Hello World” appear in the RightScale dashboard.

Screen shot 2010-11-28 at 8.18.58 PM

Tips & Tricks

As stated before, Chef support in RightScale is still in Beta. Because of this there are some gotchas to watch out for.

Sanity Check Recipe

If there are any syntax errors or anything else amiss in your repository, you’ll find that not just the cookbook or recipe which you’re working on is failing, but absolutely every recipe in your repo (or any other repo that’s part of your RepoPath) will fail with this super informative error message.

*ERROR> Chef process failed with return code 1

Of course, you can also get that error message if you have some other problem in the recipe you’re working on.

Because of this I find it useful to keep a “smoke test” recipe in my repo, and assigned to the operational scripts on my ServerTemplate when I’m doing Chef development. This recipe should be something simple that you know will always succeed, assuming that everything else is in order.

The helloworld recipe that we created in this exercise is actually a perfect choice for this purpose.

Taste Test

So there it is, everything you need to know to get started using Chef on RightScale condensed from my few weeks of experience. I hope it helps you get started configuring your server instances with Chef, which brings a whole new dimension of power and flexibility to the RightScale ServerTemplates.

I’m excited to see how Chef support progresses and makes it to production status.

If you’re hungry for more info, check out some of the early documentation available about using Chef in RightScale here.