Вы находитесь на странице: 1из 59

Riding the Rails on OS X Leopard

IT854

Brian Pitts
polibyte.com

1/18/2008
Introduction
Ruby
Rails
Deployment
Administration
Conclusion

Outline

1 Introduction 4 Deployment
2 Ruby Overview
Overview Mongrel
Rake Apache
RubyGems Misc
3 Rails 5 Administration
History Capistrano
Philosophy Maintenance
Anatomy Scaling
6 Conclusion

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Overview
Rails
Rake
Deployment
RubyGems
Administration
Conclusion

Ruby History

An object-oriented, interpreted programming language


First release by Yukihiro Matsumoto in 1995
Inspired by Perl and Smalltalk

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Overview
Rails
Rake
Deployment
RubyGems
Administration
Conclusion

Ruby in Leopard

Latest stable release: ruby 1.8.6


In framework form:
/System/Library/Frameworks/Ruby.framework/
Interpreter and library are universal binaries
Library is 64-bit too

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Overview
Rails
Rake
Deployment
RubyGems
Administration
Conclusion

Rake Overview

Alternative to the ’make’ program


Rakefiles are written in pure Ruby
Used to automate many housekeepings tasks in Rails

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Overview
Rails
Rake
Deployment
RubyGems
Administration
Conclusion

Rake Example

/Users/brian/Rakefile
namespace :macworld do
desc "This task will submit a session proposal"
task :submit_proposal do
puts "Submitted Proposal"
end

desc "This task will give the talk"


task :give_talk => :submit_proposal do
puts "Gave Talk"
end
end

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Overview
Rails
Rake
Deployment
RubyGems
Administration
Conclusion

Rake Example

Output
$ rake macworld:give_talk
(in /Users/brian)
Submitted Proposal
Gave Talk

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Overview
Rails
Rake
Deployment
RubyGems
Administration
Conclusion

RubyGems Overview

Ruby’s packaging system


RubyForge is Ruby’s’ equivalent to Perl’s CPAN
Organized with one gem per directory

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Overview
Rails
Rake
Deployment
RubyGems
Administration
Conclusion

RubyGems Examples

List installed gems: gem query –local


Search gems: gem search –remote FOO
Install the latest version of a gem: gem install –remote
–include-dependencies FOO
Install a specific gem version: add –version ”BAR” to above
Update an installed gem: gem update FOO
Show details of a gem: gem specification FOO
Uninstall a gem: gem uninstall FOO
Uninstall old gems: gem clean

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Overview
Rails
Rake
Deployment
RubyGems
Administration
Conclusion

RubyGems on Leopard

RubyGems 0.9.4 is installed


Pre-installed Gems are under
/System/Library/Frameworks/Ruby.framework/
Gems you install are in /Library/Ruby/Gems/1.8

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Overview
Rails
Rake
Deployment
RubyGems
Administration
Conclusion

Building Gems

Most gems are pure Ruby, but some involve C


Have the Xcode Developer Tools installed
By default Leopard builds universal gems
You can specify the architecture with ARCHFLAGS=”-arch
i386” or ”-arch ppc”

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

Rails History

Extracted by David Heinemeier Hansson from his work on


Basecamp
First public release in July 2004
Agile Web Development with Rails published in September
2005
Version 2.0 released December 17 2007

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

Rails Users

A few sites on Rails include


Penny Arcade
CNET’s Chow
YellowPages.com
and of course, Twitter

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

Rails Philosophy

Model-View-Controller (MVC)
Convention over Configuration (CoC)
Don’t Repeat Yourself (DRY)

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

Anatomy of Rails

Active Record
Active Resource
Action Pack
Active Support
Action Mailer

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

Anatomy of a Rails App

Standard directory structure


Three environments
Development
Test
Production

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

app/

Contains subdirectories with the code for


Controllers
Helpers
Models
Views

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

config/

Important configuration parameters


database.yml
environment.rb
environment/

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

config/database.yml

Database connection parameters for the three environments


Sample
production:
adapter: mysql
database: chunky_production
username: chunky
host: db.bacon.com

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

Database Adapters

Available database adapters


DB2
Firebird
Frontbase
MySQL
Openbase
Oracle
Postgres
SQLite
SQL Server
Sybase
Configuration for each varies
Brian Pitts polibyte.com Riding the Rails on OS X Leopard
Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

Installing MySQL

Download and install package from mysql.com


Run MySQLStartupItem.pkg
Set environment to ppc or i386
gem install mysql –with-mysql-dir=/usr/local/mysql

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

Creating Databases

Create databases
CREATE DATABASE chunky development;
CREATE DATABASE chunky test;
CREATE DATABASE chunky production;
Assign permissions

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

config/environment.rb

Application-wide settings, such as


Version of Rails
Session Store
Log level

Sample
RAILS_GEM_VERSION = ’1.2.6’
config.log_level = :debug
# create the session table with ’rake db:sessions:create’
config.action_controller.session_store = :active_record_sto

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

Sessions

PStore - default, flat files of serialized data


active record store - database
drb store - DRb server
mem cache store - memcached

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

environments/

3 files, one for each environment


Specify options only for that environment
Sample

environments/production.rb
config.action controller.consider all request local = false

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

db/

Autogenerated schema.rb
Migrations

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

Migrations

Programatically define changes to database


Run with rake db:migrate
Applied in order
001 create users.rb
class AddUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column :name, :string
t.column :age, :integer
end
end
def self.down
drop_table :users
end
Brian Pitts polibyte.com Riding the Rails on OS X Leopard
Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

doc/

Application documentation in HTML


Generated with ’rake doc:app’

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

lib/

Code that isn’t in MVC


Often used for shared code
tasks/ for application-specific rake tasks

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

log/

Log file for each environment


Logs contain exception reports, database queries, profiling
information, and more
Log level can be debug, info, error, or fatal

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

public/

Document root for Apache

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

script/

Miscellaneous scripts such as


breakpointer
console
runner
process/ - used to control FastCGI
performance/ - benchmarker and profiler

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

test/

Programs and supporting files for


Unit
Functional
Integration
tests.
Run the tests with ’rake test’
First time create the test database with ’rake db:test:prepare’

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

tmp/

Temporary files

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
History
Rails
Philosophy
Deployment
Anatomy
Administration
Conclusion

vendor/

plugins/
Rails
rake rails:freeze:gems
rake rails:freeze:edge
rake rails:unfreeze

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby Overview
Rails Mongrel
Deployment Apache
Administration Misc
Conclusion

Deployment Overview

Considered more complicated than writing a simple Rails app


No working mod ruby for Rails
cgi = unusably slow
First common approach was FastCGI, but this was unreliable
Current favorite is cluster of mongrel web servers behind a
load balancer

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby Overview
Rails Mongrel
Deployment Apache
Administration Misc
Conclusion

Mongrel Overview

Created by Zed Shaw


HTTP server written in 2500 lines of C and Ruby
Intended for use as a Rails application server
Start one up with ’mongrel rails start -e production -d -p
3000’
Connect at http://bacon.com:3000

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby Overview
Rails Mongrel
Deployment Apache
Administration Misc
Conclusion

Mongrel Clustering

1 Mongrel = 1 Request
A cluster of mongrels = Many requests
mongrel rails cluster::configure -p 3000 -e production -a
127.0.0.1 -N 3 -c /var/chunky/current/
Produces config/mongrel cluster.yml which can be further
tweaked
Start cluster with ’mongrel rails cluster::start’
Mongrels are on 3000, 3001, and 3002

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby Overview
Rails Mongrel
Deployment Apache
Administration Misc
Conclusion

Launchd for Mongrel Cluster

Credit to Jake Schutz

<key>Program</key>
<string>/opt/local/bin/daemondo</string>
<key>ProgramArguments</key>
<array>
<string>--label=mongrel_cluster</string>
<string>--start-cmd</string>
<string>/usr/local/bin/mongrel_rails</string>
<string>cluster::start</string>
<string>-C</string>
<string>(Path to your Mongrel Cluster config.yml)</string
<string>;</string>

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby Overview
Rails Mongrel
Deployment Apache
Administration Misc
Conclusion

Launchd for Mongrel Cluster

<string>--stop-cmd</string>
<string>/usr/local/bin/mongrel_rails</string>
<string>cluster::stop</string>
<string>-C</string>
<string>(Path to your Mongrel Cluster config.yml)</string
<string>;</string>
<string>--restart-cmd</string>
<string>/usr/local/bin/mongrel_rails</string>
<string>cluster::restart</string>
<string>-C</string>
<string>(Path to your Mongrel Cluster config.yml)</string
<string>;</string>
<string>--pid=none</string>
</array>
Brian Pitts polibyte.com Riding the Rails on OS X Leopard
Introduction
Ruby Overview
Rails Mongrel
Deployment Apache
Administration Misc
Conclusion

How Many Mongrels?

Average request time * peak request


10 per CPU core
Leave some free RAM

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby Overview
Rails Mongrel
Deployment Apache
Administration Misc
Conclusion

Apache Overview

Swiss army knife of web servers


Included in OS X
mod proxy balancer
Fast at serving static content

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby Overview
Rails Mongrel
Deployment Apache
Administration Misc
Conclusion

Proxy Balancer Definition

<Proxy balancer://chunky>
BalancerMember http://127.0.0.1.3000
BalancerMember http://127.0.0.1.3001
BalancerMember http://127.0.0.1.3002
</Proxy>

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby Overview
Rails Mongrel
Deployment Apache
Administration Misc
Conclusion

Proxy Configuration

<VirtualHost *:80>
ServerName www.bacon.com
RequestHeader set X_FORWARDED_HOST ’www.bacon.com’
DocumentRoot /var/chunky/current/public
<Directory /var/chunky/current/public>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby Overview
Rails Mongrel
Deployment Apache
Administration Misc
Conclusion

Proxy Configuration

RewriteEngine On
# Check for static index and cached pages.
RewriteRule ^/$ /index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
# No static file exists, let Mongrel handle the request
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://chunky%{REQUEST_URI} [P,QSA,
</VirtualHost>

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby Overview
Rails Mongrel
Deployment Apache
Administration Misc
Conclusion

Apps outside of document root

Set mongrels prefix option


If app at http://bacon.com/chunky, prefix is chunky/
Add appropriate rewrite rule

RewriteRule ^/chunky(.*)$ balancer://chunky%{REQUEST_URI} [

This allows multiple apps under the same domain

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Capistrano
Rails
Maintenance
Deployment
Scaling
Administration
Conclusion

Capistrano Overview

Deployment tool written in Ruby


Allows you to deploy or rollback releases with one command
Each deployment goes into releases/, current/ is symlink to
latest deployment
Retrieves application from SCM, default is subversion
Communicates with servers using ssh

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Capistrano
Rails
Maintenance
Deployment
Scaling
Administration
Conclusion

Capistrano Setup

If config/deploy.rb doesn’t exist, run ’capify /Users/fox/chunky


chunky’

require ’mongrel_cluster/recipes’
set :application, "chunky"
set :repository, "http://svn.bacon.com/#{application}"
set :scm_username, ’fox’
set :scm_password, proc{Capistrano::CLI.password_prompt(’SV
set :deploy_to, "/var/#{application}"
set :mongrel_conf, "#{current_path}/config/mongrel_cluster.
role :web, "bacon.com"
role :app, "bacon.com"
role :db, "bacon.com" , :primary => true

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Capistrano
Rails
Maintenance
Deployment
Scaling
Administration
Conclusion

Running Capistrano

Make sure you can connect to your SCM from your servers
Make sure the user apache and mongrel run as can ready files
created by the user who runs cap
The first time, ’cap deploy:setup’
cap deploy
cap deploy:migrations
cap deploy:rollback

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Capistrano
Rails
Maintenance
Deployment
Scaling
Administration
Conclusion

Capistrano Callbacks

You can write cap tasks yourself. E.G. perform more tasks after a
deployment
deploy.rb
task :symlink_database_yml do
run "ln -sf #{shared_path}/config/database.yml
#{release_path}/config/database.yml"
end
after ’deploy:update_code’, ’symlink_database_yml’

deploy.rb

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Capistrano
Rails
Maintenance
Deployment
Scaling
Administration
Conclusion

Email Notifications

You can get email notifications of application errors by


Configuring Active Mailer
Installing the exception notification plugin
Modifying the ApplicationController Class

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Capistrano
Rails
Maintenance
Deployment
Scaling
Administration
Conclusion

Configuring Active Mailer

In config/environment.rb
Example
config.action_mailer.delivery_method = :smtp
config.action_mailer.server_settings = {
:address => "mail.bacon.com",
:port => 25,
:domain => "bacon.com",
}

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Capistrano
Rails
Maintenance
Deployment
Scaling
Administration
Conclusion

Configuring exception notification

ruby script/plugin install exception notification


Add ’include ExceptionNotifiable’ to
app/controllers/application.rb
Add ’ExceptionNotifier.exception.recipients =
[”chunky@bacon.com”, ”two@foxes.com”]’ to
config/environment.rb

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Capistrano
Rails
Maintenance
Deployment
Scaling
Administration
Conclusion

Housekeeping

Script and automate


Log rollover
Clearing sessions

DB Example
RAILS_ENV=PRODUCTION ./script/runner \
’ActiveRecord::Base.connection.delete(
"DELETE FROM sessions WHERE updated_at < now() - 3600")’

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Capistrano
Rails
Maintenance
Deployment
Scaling
Administration
Conclusion

Multiple Apps, One Host

Seperate
mongrel cluster
proxy balancer://
rewrite rule
virtualhost (if multiple domains)
for each app.

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Capistrano
Rails
Maintenance
Deployment
Scaling
Administration
Conclusion

One App, Multiple Hosts

Optimize the app


Dedicated database server, more mongrels on your application
server
The path from here varies
Multiple application servers and dedicated front-end load
balancer
Beefy database server or replication

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Capistrano
Rails
Maintenance
Deployment
Scaling
Administration
Conclusion

Trends to watch

Patches to mongrel (evented mongrel)


Alternate web servers (lighthttp, nginx, swiftiply)
Alternate load balancers (pen, pound, balance, haproxy,
hardware)
Alternate Ruby implementations (JRuby, Rubinius)

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


Introduction
Ruby
Rails
Deployment
Administration
Conclusion

Further Reading

Download the presentation (pdf and tex) at


http://polibyte.com
Find related materials tagged as IT854 on del.icio.us
’Deploying Rails Applications’ by Ezra Zygmuntowicz, Bruce
A. Tate and Clinton Begin
’Agile Web Development with Rails’ by Dave Thomas and
DHH

Brian Pitts polibyte.com Riding the Rails on OS X Leopard


I’ll stick around for questions.

Вам также может понравиться