Академический Документы
Профессиональный Документы
Культура Документы
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
Ruby History
Ruby in Leopard
Rake Overview
Rake Example
/Users/brian/Rakefile
namespace :macworld do
desc "This task will submit a session proposal"
task :submit_proposal do
puts "Submitted Proposal"
end
Rake Example
Output
$ rake macworld:give_talk
(in /Users/brian)
Submitted Proposal
Gave Talk
RubyGems Overview
RubyGems Examples
RubyGems on Leopard
Building Gems
Rails History
Rails Users
Rails Philosophy
Model-View-Controller (MVC)
Convention over Configuration (CoC)
Don’t Repeat Yourself (DRY)
Anatomy of Rails
Active Record
Active Resource
Action Pack
Active Support
Action Mailer
app/
config/
config/database.yml
Database Adapters
Installing MySQL
Creating Databases
Create databases
CREATE DATABASE chunky development;
CREATE DATABASE chunky test;
CREATE DATABASE chunky production;
Assign permissions
config/environment.rb
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
Sessions
environments/
environments/production.rb
config.action controller.consider all request local = false
db/
Autogenerated schema.rb
Migrations
Migrations
doc/
lib/
log/
public/
script/
test/
tmp/
Temporary files
vendor/
plugins/
Rails
rake rails:freeze:gems
rake rails:freeze:edge
rake rails:unfreeze
Deployment Overview
Mongrel Overview
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
<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>
<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
Apache Overview
<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>
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>
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>
Capistrano Overview
Capistrano Setup
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
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
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
Email Notifications
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",
}
Housekeeping
DB Example
RAILS_ENV=PRODUCTION ./script/runner \
’ActiveRecord::Base.connection.delete(
"DELETE FROM sessions WHERE updated_at < now() - 3600")’
Seperate
mongrel cluster
proxy balancer://
rewrite rule
virtualhost (if multiple domains)
for each app.
Trends to watch
Further Reading