deploying rails apps with chef and capistrano

25
Guided Exploration Deploying Rails apps with Chef and Capistrano Dan Ivovich BohConf Baltimore 2013 7/19/2013

Upload: smartlogic

Post on 22-Nov-2014

6.868 views

Category:

Technology


2 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Deploying Rails Apps with Chef and Capistrano

Guided ExplorationDeploying Rails apps with Chef and Capistrano

Dan Ivovich

BohConf Baltimore 20137/19/2013

Page 3: Deploying Rails Apps with Chef and Capistrano

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

Page 4: Deploying Rails Apps with Chef and Capistrano

How?

● Chef - Infrastructure Management

● Capistrano - Application Deployment

Page 5: Deploying Rails Apps with Chef and Capistrano

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)

Page 6: Deploying Rails Apps with Chef and Capistrano

Who does what?

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

Page 7: Deploying Rails Apps with Chef and Capistrano

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

Page 8: Deploying Rails Apps with Chef and Capistrano

How? - Chef

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

Page 9: Deploying Rails Apps with Chef and Capistrano

How? - Capistrano

● Standard Tasks● Custom Tasks● Templating files

Page 10: Deploying Rails Apps with Chef and Capistrano

To The Code!

Page 11: Deploying Rails Apps with Chef and Capistrano

git clone [email protected]:smartlogic/chef-cap-demo.git

See the README or follow along the following git tags

Page 12: Deploying Rails Apps with Chef and Capistrano

git checkout rails_app

Page 13: Deploying Rails Apps with Chef and Capistrano

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

Page 14: Deploying Rails Apps with Chef and Capistrano

git checkout meet_the_chef

Page 15: Deploying Rails Apps with Chef and Capistrano

Basic Chef Configuration

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

Page 16: Deploying Rails Apps with Chef and Capistrano

git checkout app_cookbook

Page 17: Deploying Rails Apps with Chef and Capistrano

Application Cookbook

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

Page 18: Deploying Rails Apps with Chef and Capistrano

git checkout cap_setup

Page 19: Deploying Rails Apps with Chef and Capistrano

Capistrano Setup

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

Page 20: Deploying Rails Apps with Chef and Capistrano

git checkout cap_data

Page 21: Deploying Rails Apps with Chef and Capistrano

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

Page 22: Deploying Rails Apps with Chef and Capistrano

From the top!

Page 23: Deploying Rails Apps with Chef and Capistrano

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

Page 24: Deploying Rails Apps with Chef and Capistrano

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