le 57ième déploiement capistrano sur Dreamhost
18 mars 2007 par Marc Charlet
Le but de cet article n'est pas de vous donner un article de plus (de trop?) sur comment déployer une application rails sur dreamhost. Pour cela, il vous suffit de consulter l'un des nombreux articles disponibles sur le web1.
A titre d'exemple voici trois des articles qui me sont venus en aide dans la réalisation de mon premier déploiement:
- How To Deploy your first rails app to Dreamhost using Capistrano in Windows
- Déploiement d’une application Rails avec Capistrano
- Shovel—Rails Deployment with Capistrano (for Dreamhost)
Dans cet article, je me propose de vous donner un aperçu pratique de la méthode que j'utilise et surtout de vous communiquer les sources de mes fichiers de travail, tout particulièrement mon deploy.rb
La méthode que j'utilse est simple:
- je prépare dans mon environnement de développement une série de fichiers avec l'extension prd qui seront en fait les fichiers de production
- mon fichier deploy.rb est ensuite chargé de recopier les fichiers de production prd sur les fichiers de développement qui ont été déployés par capistrano.
Les fichiers de production que je prépare sont:
- /config/environment.rb.pr
- /public/.htaccess.prd
- /public/dispatch.rb.prd
- /public/dispatch.fcgi.prd
- /public/dispatch.cgi.prd
mon deploy.rb
-
# the name of your subversion application
-
# donner un nom à votre application
-
set :application, "monapplic"
-
-
# use exactly what you see on the Dreamhost subversion control panel
-
# reprendre le libellé exatct tel qu'il apparaît dans les écrans de configuration de dreamhost
-
set :repository, "http://toutenruby.com/monSVN/monapplic"
-
-
# These three should be set to your subdomain name you set up
-
# on Dreamhost. The explanation of all of these is beyond
-
# the scope of this tutorial.
-
# Donner le nom exact de votre (sous-) domaine tel qu'il a été défini sur dreamhost
-
role :web, "monapplic.toutenruby.com"
-
role :app, "monapplic.toutenruby.com"
-
role :db, "monapplic.toutenruby.com", :primary => true
-
-
# This is the directory you created when you setup
-
# your subdomain on Dreamhost. Don't include the
-
# public/current part...
-
# Donner le chemin vers le répertoire de (sous-) domaine tel que défini sur dreamhost
-
# Ne pas inclure la partie /public/current...
-
set :deploy_to, "/home/mon_utilisateur/monapplic.toutenruby.com"
-
-
# Dreamhost doesn't allow you to use sudo.
-
# Dreamhost ne permet pas l'utilisation de la commande sudo
-
set :use_sudo, false
-
-
# Do this so subversion doesn't create a .svn in your
-
# world-accessible directory.
-
# Empêche subversion de créer un répertoire .svn dans la partie publique du déploiement
-
set :checkout, "export"
-
-
# Make sure to set this to your DreamHost user name that
-
# you use when you connect to ssh
-
# Entrer les paires nom d'utilisateur/mot de passe
-
# de votre accès ssh sur dreamhost
-
# de votre accès au dépôt (repository) de subversion
-
set :user, "mon_utilisateur_ssh"
-
set :password, "mot_de_passe_ssh"
-
set :svn_username, "mon_utilisateur_svn"
-
set :svn_password, "mot_de_passe_svn"
-
-
#
-
# TASKS
-
#
-
-
desc "Tasks to execute after symlink"
-
task :after_symlink, :roles => [:web, :app] do
-
-
#replace dev files with production *.prd files
-
# Remplacer les fichiers dev par les fichiers de production (*.prd)
-
-
# environment.rb
-
run "cp #{current_path}/config/environment.rb.prd #{current_path}/config/environment.rb"
-
# .htaccess
-
run "cp #{current_path}/public/.htaccess.prd #{current_path}/public/.htaccess"
-
#dispatch.rb
-
run "cp #{current_path}/public/dispatch.rb.prd #{current_path}/public/dispatch.rb"
-
#dispatch.cgi
-
run "cp #{current_path}/public/dispatch.cgi.prd #{current_path}/public/dispatch.cgi"
-
#dispatch.fcgi
-
run "cp #{current_path}/public/dispatch.fcgi.prd #{current_path}/public/dispatch.fcgi"
-
-
end
-
-
desc "Tasks to execute after code update"
-
task :after_update_code, :roles => [:app, :db, :web] do
-
-
# fix permissions
-
# redéfinir les droits d'accès
-
run "chmod +x #{release_path}/script/process/reaper"
-
run "chmod +x #{release_path}/script/process/spawner"
-
run "chmod 755 #{release_path}/public/dispatch.*"
-
end
-
-
desc "Restarting after deployment"
-
task :after_deploy, :roles => [:app, :db, :web] do
-
run "touch #{current_path}/public/dispatch.fcgi"
-
end
-
-
desc "Restarting after rollback"
-
task :after_rollback, :roles => [:app, :db, :web] do
-
run "touch #{current_path}/public/dispatch.fcgi"
-
end
-
-
desc "Restart the FCGI processes on the app server as a regular user."
-
task :restart, :roles => :app do
-
run "ruby #{current_path}/script/process/reaper -a graceful --dispatcher=dispatch.fcgi"
-
end
environment.rb.prd
la modification apportée en production est l'insertion des variables environnement
- RAILS_ENV
- GEM_HOME
- GEM_PATH
-
# Be sure to restart your web server when you modify this file.
-
-
# Uncomment below to force Rails into production mode when
-
# you don't control web/app server and can't set it the proper way
-
ENV['RAILS_ENV'] ||= 'production'
-
-
ENV['GEM_PATH']='/home/mon_utilisateur/extra/lib/ruby/gems/1.8'
-
ENV['GEM_HOME']='/home/mon_utilisateur/extra/lib/ruby/gems/1.8'
-
-
-
# Specifies gem version of Rails to use when vendor/rails is not present
-
RAILS_GEM_VERSION = '1.2.2' unless defined? RAILS_GEM_VERSION
.htaccess.prd
-
RewriteRule ^$ index.html [QSA]
-
RewriteRule ^([^.]+)$ $1.html [QSA]
-
RewriteCond %{REQUEST_FILENAME} !-f
-
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
dispatcher.rb.prd
ne pas oublier ici de modifier le chemin vers ruby pour votre environnement de production
-
#!/home/mon_utilisateur/extra/bin/ruby
-
-
require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
-
-
# If you're using RubyGems and mod_ruby, this require should be changed to an absolute path one, like:
-
# "/usr/local/lib/ruby/gems/1.8/gems/rails-0.8.0/lib/dispatcher" -- otherwise performance is severely impaired
-
-
require "dispatcher"
-
-
ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(dir) } if defined?(Apache::RubyRun)
-
Dispatcher.dispatch
dispatcher.cgi.prd
Ne sera pas (à priori) utilisé en production.
-
#!/home/mon_utilisateur/extra/bin/ruby
-
-
require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
-
-
# If you're using RubyGems and mod_ruby, this require should be changed to an absolute path one, like:
-
# "/usr/local/lib/ruby/gems/1.8/gems/rails-0.8.0/lib/dispatcher" -- otherwise performance is severely impaired
-
require "dispatcher"
-
-
ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(dir) } if defined?(Apache::RubyRun)
-
Dispatcher.dispatch
dispatcher.fcgi.prd
- modification du chemin vers ruby pour votre environnement de production
- apporter le traîtement approprié pour la situation des processus fcgi par dreamhost2
-
#!/home/mon_utilisateur/extra/bin/ruby
-
#
-
# You may specify the path to the FastCGI crash log (a log of unhandled
-
# exceptions which forced the FastCGI instance to exit, great for debugging)
-
# and the number of requests to process before running garbage collection.
-
#
-
# By default, the FastCGI crash log is RAILS_ROOT/log/fastcgi.crash.log
-
# and the GC period is nil (turned off). A reasonable number of requests
-
# could range from 10-100 depending on the memory footprint of your app.
-
#
-
# Example:
-
# # Default log path, normal GC behavior.
-
# RailsFCGIHandler.process!
-
#
-
# # Default log path, 50 requests between GC.
-
# RailsFCGIHandler.process! nil, 50
-
#
-
# # Custom log path, normal GC behavior.
-
# RailsFCGIHandler.process! '/var/log/myapp_fcgi_crash.log'
-
#
-
require File.dirname(__FILE__) + "/../config/environment"
-
require 'fcgi_handler'
-
-
ENV['RAILS_ENV'] = 'production'
-
-
ENV['GEM_PATH'] = '/home/mon_utilisateur/extra/lib/ruby/gems/1.8'
-
ENV['GEM_HOME'] = '/home/mon_utilisateur/extra/lib/ruby/gems/1.8'
-
-
# see http://gabrito.com/post/keeping-rails-running-at-dreamhost-part-2
-
#
-
-
class RailsFCGIHandler
-
private
-
def busy_exit_handler(signal)
-
dispatcher_log :info, "busy: asked to terminate during request signal #{signal}, deferring!"
-
@when_ready = :exit
-
end
-
-
# Dreamhost sends the term signal and if were handling a request defer it
-
def term_process_request(cgi)
-
install_signal_handler('TERM',method(:busy_exit_handler).to_proc)
-
Dispatcher.dispatch(cgi)
-
rescue Exception => e # errors from CGI dispatch
-
raise if SignalException === e
-
dispatcher_error(e)
-
ensure
-
install_signal_handler('TERM', method(:exit_now_handler).to_proc)
-
end
-
alias_method :process_request, :term_process_request
-
end
-
-
# class RailsFCGIHandler
-
# private
-
# def frao_handler(signal)
-
# dispatcher_log :info, "asked to terminate immediately"
-
# dispatcher_log :info, "frao handler working its magic!"
-
# restart_handler(signal)
-
# end
-
# alias_method :exit_now_handler, :frao_handler
-
# end
-
-
RailsFCGIHandler.process!
- une bonne petite recherche google et vous n'aurez que l'embarras du choix [retour]
- voir nottament le site http://gabrito.com/post/keeping-rails-running-at-dreamhost-part-2 [retour]
Tags: capistrano, deploiement, deploy.rb, Dreamhost, fcgi, gem, guide-débutant, rails, RoR, Ruby on Rails
Une réponse à “le 57ième déploiement capistrano sur Dreamhost”

Merci pour le lien vers le site blog.fbollon.net. J’espère que tu y as trouvé des choses intéressantes pour écrire ton article.
Bonne continuation sur RAILS!