Deploying Rails Apps with Chef and Capistrano

Preview:

DESCRIPTION

A walkthrough of how to deploy rails apps with Chef and Capistrano, from SmartLogic's Dan Ivovich at BohConf 2013.

Citation preview

Guided ExplorationDeploying Rails apps with Chef and Capistrano

Dan Ivovich

BohConf Baltimore 20137/19/2013

What is the goal?

● Build a machine that can run the application

● Do so quickly and repeatedly

● Make deploying the app, upgrading components, adding components, etc seamless and easy

How?

● Chef - Infrastructure Management

● Capistrano - Application Deployment

Who does what?● Chef

○ Users / SSH Keys○ Web server○ Database○ Postfix○ Redis / Memcached○ Monit○ NewRelic Server monitoring○ /etc/hosts○ rbenv & Ruby○ Binary dependencies (e.g. Sphinx)

Who does what?

● Capistrano○ Virtual Hosts○ Unicorn init.d script○ Unicorn.rb○ Monit process monitors○ Normal Capistrano Stuff

Why both?

● Use each for what it is best at● Chef is for infrastructure● Capistrano is for the app● Could have more than one Capistrano app with

the same Chef config● Chef config changes infrequently, Capistrano

config could change more frequently

How? - Chef

● Standard Recipes● Custom Recipes● Recipes assigned to Roles● Roles assigned to Nodes● Nodes with attributes to tailor the install

How? - Capistrano

● Standard Tasks● Custom Tasks● Templating files

To The Code!

git clone git@github.com:smartlogic/chef-cap-demo.git

See the README or follow along the following git tags

git checkout rails_app

Basic Rails Application

1. Displays some data from a database, includes a rake task to add some data to the database

2. No deployment code of any kind at this point

git checkout meet_the_chef

Basic Chef Configuration

1. Web Server2. Database Server3. Dependencies4. Nothing application specific

git checkout app_cookbook

Application Cookbook

1. User account to run the application as2. Folder structure to store the application3. Application specific dependencies4. rbenv5. The application database

git checkout cap_setup

Capistrano Setup

1. Virtual hosts configuration2. Unicorn configuration3. Monit configuration4. Application setup (linking database.yml)

git checkout cap_data

Capistrano Tasks

1. Simple to expose your rake tasks as capistrano tasks

2. This task helps us see the impact of running a deploy3. Easy to insert into the capistrano execution chain

From the top!

Ready?!? Here we go!

1. vagrant destroy2. vagrant up3. bundle exec knife bootstrap -p 2222 -x vagrant

\ -d precise32_vagrant chef_cap_demo4. bundle exec knife cook

vagrant@chef_cap_demo5. cap staging deploy:setup deploy:migrations

Thoughts....● Vagrant and VMs are you friend. Rinse and repeat

● It is ok to tweak your Chef stuff and re-cook, but I always

like to restart with a fresh VM once I think I'm done

● Capistrano tweaks should be easy to apply, especially with

tasks like nginx:setup, unicorn:setup etc.

● Chef issues are harder to debug and more frustrating than

Capistrano issues, another reason to put more app specific

custom stuff in Capistrano and do standard things in Chef

Recommended