R&D

Use Chef to Fast Track Your Remote Rails Environment Setup

In this tutorial, we will walk through the process of setting up Ruby on Rails server deployment with Chef – in order to automate remote server configuration. Chef is a tool that allows us to manage configurations, similar to Puppet, but it’s considerably more popular as it’s written in Ruby. This tool can help you manage your infrastructure dependencies, create folder structures (with ‘knife’) and bootstrap the entire system. It can even update configurations with just a few commands.

Step 1: Ruby, Ruby Version Manager and Gemset

Supposedly, you already have some Ruby Version Manager (RVM or rbenv most likely) and Ruby. The Ruby version used in this tutorial is Ruby 2.2.1. It is recommended that you create a separate gemset for the Chef configuration. In RVM, you can do this using the following command:

To use a newly created gemset, run the following command:

This will separate your Chef gems from the other gems, which can save you from the trouble later. If everything went right, you should have RVM, Ruby and the new gemset on your local machine. To display the new gemset on the list, run the following command:

The new gemset will be marked with an asterisk (*) if it is currently used. The next item you need is a bundler gem. You can easily install it using the following command:

Step 2: Knife, Chef and Berkshelf Gems

The workbench is prepared in the previous step. To start creating your Chef repo, few tools are required.

At first, you will need to create the Gemfile where the Ruby version, gemset (that we want to use) and all the needed gems will be specified. Start by creating a directory for the repo. You can name it in your own way, for example:

Now, with the help of the bundler gem, initialize a Gemfile:

Edit your Gemfile and then add the following Gems:

Run the following command:

Step 3: Copy SSH Key to the Target Server

Assuming that you already have the ssh-key setup, we will skip the process of creating a new ssh-key here. Start by copying and pasting your ssh-key to the target server:

Note: This will copy the public key of your default identity (use “-i identity_file” for other identities) to the remote host.

Step 4: Creating a Chef Repo

At first, you need to initialize a Chef repo. Go to your console, make sure that you are in the chefrepo_ directory and execute the following command:

Your console will output these lines while creating the repo structure:

Now, let’s see how the Chef repo structure looks; your chefrepo_ should have the following content:

To add cookbooks, open Berksfile. It should be populated with:

This is the site where the cookbooks will be downloaded. Now, start populating the Berksfile with cookbook names, and repositories where these cookbooks can be found. Add the following lines just below “site :opscode“:

This set will be sufficient for setting up the Rails environment. With the cookbooks in place, it is the time to set up some roles.