How to Deploy Ruby on Rails With The Opscode Chef Application Cookbook

I already wrote about how to get started with the Opscode Chef Platform. In this article I want to show you a very elegant way to deploy a Ruby on Rails stack with Chef. One of the strengths of Chef is the decent set of available cookbooks. @jtimberman does an especially excellent job in writing them. His chef cookbooks really help you to configure your systems neatly. One of his cookbooks is the Application cookbook. It enables data driven application deployment. Currently, it supports Ruby on Rails apps. The preferred stack is currently Matz Ruby with Unicorn, but, in a later post, I’ll show you how to use it cleanly with Ruby Enterprise Edition (REE).
Let’s get started!

Use The Chef Command Line Tool knife to Pull Down Required Cookbooks

First of all we need to get all required cookbooks on our local workstation. I assume your local box is setup to develop chef cookbooks. The preferred way to use existing cookbooks is to “vendor” them:

$ knife cookbook site vendor application -d

The -d parameter tells knife to pull all dependencies. The application cookbook will pull down quite a lot of deprecated dependencies like passenger_enterprise or passenger_apache2. Even if you want to install a unicorn based stack, those cookbooks will be downloaded (but never used). Just ignore them.

You may use the same command to pull a new version of any cookbook (no -d required then).

Configure Your Application Using a Data Bag

One of the great features of opscode chef is the support of data bags. A data bag is a JSON file containing any data structures (like configurations, etc) for your recipes to use. The Application cookbook makes heavy use of a data bag called apps.

First, we need to create a data bag file. Just name the data bag file like your application (in this example I use “my_app”):

NOTE: Make sure your deploy key string is in one line with n</code as line breaks within:"deploy_key": "-----BEGIN DSA PRIVATE KEY-----nMIIBv...EW1auXCaVnb24T...TiaHIn...n-----END DSA PRIVATE KEY-----n",

Note the “apps” part before the actual file name. This is the name of the data bag and must be “apps” for the application cookbook to work correctly. You can load multiple application configurations into that same data bag (just use different JSON files).

Define Your Roles

You already put some role names into your data bag. Now, it’s time to create those roles to be able to assign them to nodes.