Config/deploy.rb
CS290F Fall 2006 - UCSB Computer Science - Thorsten von Eicken
Copy and past the deploy.eb below into your project's config directory. Then edit to match your project. If you are finding problems with this deploy.rb, please post in the discussion page.
require 'capistrano'
require 'mongrel_cluster/recipes'
# Portions of this recipe have been borrowed from various sources
# Special thanks to Slingshot Hosting for their initial version of this.
set :web_name, 'www.aws-console.com' # <====
role :web, "domu-12-31-33-00-02-12.usma1.compute.amazonaws.com" # <====
role :app, "domu-12-31-33-00-02-12.usma1.compute.amazonaws.com"
role :db, "domu-12-31-33-00-02-12.usma1.compute.amazonaws.com", :primary => true
# Configure the ssh information
ssh_options[:keys] = "config/deploy-key"
ssh_options[:username] = 'tve' # <====
# Configure user for application processes and database
set :user, ssh_options[:username]
set :dbuser, 'prod'
set :dbpassword, '!?password!?' # <====
set :deploy_to, "/home/rails/my_project" # <====
set :web_server, "httpd"
set :path_to_web_server, "/etc/httpd/"
# set this to the correct db adapter
set :database, "mysql"
# saves space by only keeping last 3 when running cleanup
set :keep_releases, 3
# SVN set-up
set :application, "my_project" # <====
set :repository, "http://wush.net/svn/syslitics/web/trunk" # <====
set :checkout, "export"
set :svn_username, "my_project" # <====
set :svn_password, "?!yes!?" # <====
# mongrel_cluster configuration:
set :mongrel_conf, '/etc/mongrel_cluster/my_project.yml' # <====
set :mongrel_prefix, "/usr/bin/" # path for mongrel_rails executable
set :mongrel_servers, 4
set :mongrel_start_port, 8000
# =============================================================================
# END OF CONFIGURATION VARIABLES
# =============================================================================
desc "List the files that would be updated or changed in a deploy."
task :svn_st_up, :roles => :app do
run "cd #{current_path} && " + "svn st -u"
end
# =============================================================================
# TASKS FOR INITIAL SETUP
# =============================================================================
desc "Tasks to execute before initial setup"
task :before_setup do
sudo "mkdir -p /mnt/rails"
sudo "ln -nfs /mnt/rails /home/rails"
sudo "chown -R #{user}:#{user} /mnt/rails/"
sudo "mkdir -p /etc/mongrel_cluster"
sudo "mkdir -p /etc/rails"
end
desc "Tasks to execute after initial setup"
task :after_setup do
mongrel_configuration = render :template => <<-EOF
---
cwd: #{deploy_to}/current
user: #{user}
group: #{user}
port: #{mongrel_start_port}
environment: production
address: 127.0.0.1
pid_file: #{deploy_to}/current/log/mongrel.pid
log_file: #{deploy_to}/current/log/mongrel.log
servers: #{mongrel_servers}
prefix: #{mongrel_prefix}
EOF
database_configuration = render :template => <<-EOF
# Deployment database.yml
login: &login
adapter: #{database}
host: localhost
username: <%= "#{dbuser}" %>
password: <%= "#{dbpassword}" %>
development:
database: <%= "#{application}_development" %>
<<: *login
test:
database: <%= "#{application}_test" %>
<<: *login
production:
database: <%= "#{application}_production" %>
<<: *login
EOF
# web server configuration (apache specific)
apache2_rails_conf = <<-EOF
<VirtualHost *:80>
Include /etc/rails/#{application}.common
ErrorLog #{path_to_web_server}logs/#{application}_errors_log
CustomLog #{path_to_web_server}logs/#{application}_log combined
</VirtualHost>
<Proxy balancer://#{application}_mongrel_cluster>
EOF
# builds the following as an example with start port 8000 and servers = 3:
# <Proxy balancer://mongrel_cluster>
# BalancerMember http://127.0.0.1:8000
# BalancerMember http://127.0.0.1:8001
# BalancerMember http://127.0.0.1:8002
# </Proxy>
(0..mongrel_servers-1).each { |server|
apache2_rails_conf += " BalancerMember http://127.0.0.1:#{mongrel_start_port + server}\n"
}
apache2_rails_conf +=<<-EOF
</Proxy>
Listen #{mongrel_start_port + 80}
<VirtualHost *:#{mongrel_start_port + 80}>
<Location />
SetHandler balancer-manager
Deny from all
Allow from localhost
</Location>
</VirtualHost>
EOF
apache2_rails_configuration = render :template => apache2_rails_conf
apache2_rails_common = render :template => <<-EOF
ServerName #{web_name}
DocumentRoot #{deploy_to}/current/public
<Directory "#{deploy_to}/current/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
RewriteEngine On
# Uncomment for rewrite debugging
#RewriteLog logs/#{application}_rewrite_log
#RewriteLogLevel 9
# Check for maintenance file and redirect all requests
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]
# Rewrite index to check for static
RewriteRule ^/$ /index.html [QSA]
# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]
# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://#{application}_mongrel_cluster%{REQUEST_URI} [P,QSA,L]
# Deflate
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
# Uncomment for deflate debugging
#DeflateFilterNote Input input_info
#DeflateFilterNote Output output_info
#DeflateFilterNote Ratio ratio_info
#LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
#CustomLog logs/#{application}_deflate_log deflate
EOF
run "mkdir -p #{deploy_to}/#{shared_dir}/config"
put mongrel_configuration, "#{deploy_to}/#{shared_dir}/config/mongrel_cluster.yml"
put database_configuration, "#{deploy_to}/#{shared_dir}/config/database.yml"
put apache2_rails_configuration, "#{deploy_to}/#{shared_dir}/system/#{application}.conf"
put apache2_rails_common, "#{deploy_to}/#{shared_dir}/system/#{application}.common"
run "mkdir -p #{deploy_to}/#{shared_dir}/tmp"
run "mkdir -p #{deploy_to}/#{shared_dir}/tmp/cache"
run "mkdir -p #{deploy_to}/#{shared_dir}/tmp/sessions"
run "mkdir -p #{deploy_to}/#{shared_dir}/tmp/sockets"
sudo "ln -nfs #{deploy_to}/#{shared_dir}/config/mongrel_cluster.yml /etc/mongrel_cluster/#{application}.yml"
sudo "ln -nfs #{deploy_to}/#{shared_dir}/system/#{application}.conf /etc/rails/#{application}.conf"
sudo "ln -nfs #{deploy_to}/#{shared_dir}/system/#{application}.common /etc/rails/#{application}.common"
end
# =============================================================================
# TASKS (MISC?)
# =============================================================================
desc "Tasks to execute after code update"
task :after_update_code, :roles => [:app, :db] do
# relink shared deployment database configuration
run "ln -nfs #{deploy_to}/#{shared_dir}/config/database.yml #{release_path}/config/database.yml"
# relink shared tmp dir (for session and cache data)
run "if [ -d #{release_path}/tmp ]; then mv #{release_path}/tmp #{release_path}/tmp.snv; fi"
run "ln -nfs #{deploy_to}/#{shared_dir}/tmp #{release_path}/tmp"
# relink shared mongrel configuration
sudo "ln -s #{deploy_to}/#{shared_dir}/config/mongrel_cluster.yml #{release_path}/config/mongrel_cluster.yml"
end
desc "Tasks to execute after a deploy"
task :after_deploy, :roles => [:app, :db] do
# some examples could be:
# restart rails_cron, backgrounDRb
# relink shared user/file dirs
# relink shared ferret_index
end
desc "Restart the web server"
task :restart_web, :roles => [:web] do
sudo "/etc/init.d/#{web_server} restart"
end
