Fils RSS pour les
Articles
Commentaires

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:

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

 

  1. # the name of your subversion application
  2. # donner un nom à votre application
  3. set :application, "monapplic"
  4.                
  5. # use exactly what you see on the Dreamhost subversion control panel
  6. # reprendre le libellé exatct tel qu'il apparaît dans les écrans de configuration de dreamhost
  7. set :repository, "http://toutenruby.com/monSVN/monapplic"
  8.          
  9. # These three should be set to your subdomain name you set up
  10. # on Dreamhost.  The explanation of all of these is beyond
  11. # the scope of this tutorial.
  12. # Donner le nom exact de votre (sous-) domaine tel qu'il a été défini sur dreamhost
  13. role :web, "monapplic.toutenruby.com"
  14. role :app, "monapplic.toutenruby.com"
  15. role :db, "monapplic.toutenruby.com", :primary => true           
  16.                
  17. # This is the directory you created when you setup
  18. # your subdomain on Dreamhost.  Don't include the
  19. # public/current part...
  20. # Donner le chemin vers le répertoire de (sous-) domaine tel que défini sur dreamhost
  21. # Ne pas inclure la partie /public/current...
  22. set :deploy_to, "/home/mon_utilisateur/monapplic.toutenruby.com"
  23.            
  24. # Dreamhost doesn't allow you to use sudo.
  25. # Dreamhost ne permet pas l'utilisation de la commande sudo
  26. set :use_sudo, false
  27.            
  28. # Do this so subversion doesn't create a .svn in your
  29. # world-accessible directory.
  30. # Empêche subversion de créer un répertoire .svn dans la partie publique du déploiement
  31. set :checkout, "export"
  32.            
  33. # Make sure to set this to your DreamHost user name that
  34. # you use when you connect to ssh
  35. # Entrer les paires nom d'utilisateur/mot de passe
  36. #     de votre accès ssh sur dreamhost
  37. #     de votre accès au dépôt (repository) de subversion
  38. set :user, "mon_utilisateur_ssh"
  39. set :password, "mot_de_passe_ssh"
  40. set :svn_username, "mon_utilisateur_svn"
  41. set :svn_password, "mot_de_passe_svn"
  42.            
  43. #     
  44. # TASKS
  45. #
  46.  
  47. desc "Tasks to execute after symlink"
  48. task :after_symlink, :roles => [:web, :app] do
  49.                
  50. #replace dev files with production *.prd files
  51. # Remplacer les fichiers dev par les fichiers de production (*.prd)
  52.                
  53. # environment.rb
  54. run "cp #{current_path}/config/environment.rb.prd #{current_path}/config/environment.rb"
  55. # .htaccess
  56. run "cp #{current_path}/public/.htaccess.prd #{current_path}/public/.htaccess"
  57. #dispatch.rb
  58. run "cp #{current_path}/public/dispatch.rb.prd #{current_path}/public/dispatch.rb"
  59. #dispatch.cgi
  60. run "cp #{current_path}/public/dispatch.cgi.prd #{current_path}/public/dispatch.cgi"
  61. #dispatch.fcgi
  62. run "cp #{current_path}/public/dispatch.fcgi.prd #{current_path}/public/dispatch.fcgi"
  63.  
  64. end
  65.                
  66. desc "Tasks to execute after code update"
  67. task :after_update_code, :roles => [:app, :db, :web] do
  68.  
  69. # fix permissions
  70. # redéfinir les droits d'accès
  71. run "chmod +x #{release_path}/script/process/reaper"
  72. run "chmod +x #{release_path}/script/process/spawner"
  73. run "chmod 755 #{release_path}/public/dispatch.*"
  74. end
  75.            
  76. desc "Restarting after deployment"
  77. task :after_deploy, :roles => [:app, :db, :web] do
  78. run "touch #{current_path}/public/dispatch.fcgi"
  79. end
  80.            
  81. desc "Restarting after rollback"
  82. task :after_rollback, :roles => [:app, :db, :web] do
  83. run "touch #{current_path}/public/dispatch.fcgi"
  84. end
  85.                
  86. desc "Restart the FCGI processes on the app server as a regular user."
  87. task :restart, :roles => :app do
  88. run "ruby #{current_path}/script/process/reaper -a graceful  --dispatcher=dispatch.fcgi"
  89. end

environment.rb.prd

la modification apportée en production est l'insertion des variables environnement

  • RAILS_ENV
  • GEM_HOME
  • GEM_PATH
  1. # Be sure to restart your web server when you modify this file.
  2.  
  3. # Uncomment below to force Rails into production mode when
  4. # you don't control web/app server and can't set it the proper way
  5. ENV['RAILS_ENV'] ||= 'production'
  6.  
  7. ENV['GEM_PATH']='/home/mon_utilisateur/extra/lib/ruby/gems/1.8'
  8. ENV['GEM_HOME']='/home/mon_utilisateur/extra/lib/ruby/gems/1.8'
  9.  
  10.  
  11. # Specifies gem version of Rails to use when vendor/rails is not present
  12. RAILS_GEM_VERSION = '1.2.2' unless defined? RAILS_GEM_VERSION

.htaccess.prd

 

  1. RewriteRule ^$ index.html [QSA]
  2. RewriteRule ^([^.]+)$ $1.html [QSA]
  3. RewriteCond %{REQUEST_FILENAME} !-f
  4. RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

dispatcher.rb.prd

ne pas oublier ici de modifier le chemin vers ruby pour votre environnement de production

  1. #!/home/mon_utilisateur/extra/bin/ruby
  2.  
  3. require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
  4.  
  5. # If you're using RubyGems and mod_ruby, this require should be changed to an absolute path one, like:
  6. # "/usr/local/lib/ruby/gems/1.8/gems/rails-0.8.0/lib/dispatcher" -- otherwise performance is severely impaired
  7.  
  8. require "dispatcher"
  9.  
  10. ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(dir) } if defined?(Apache::RubyRun)
  11. Dispatcher.dispatch

dispatcher.cgi.prd

Ne sera pas (à priori) utilisé en production.

  1. #!/home/mon_utilisateur/extra/bin/ruby
  2.  
  3. require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
  4.  
  5. # If you're using RubyGems and mod_ruby, this require should be changed to an absolute path one, like:
  6. # "/usr/local/lib/ruby/gems/1.8/gems/rails-0.8.0/lib/dispatcher" -- otherwise performance is severely impaired
  7. require "dispatcher"
  8.  
  9. ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(dir) } if defined?(Apache::RubyRun)
  10. 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
  1. #!/home/mon_utilisateur/extra/bin/ruby
  2. #
  3. # You may specify the path to the FastCGI crash log (a log of unhandled
  4. # exceptions which forced the FastCGI instance to exit, great for debugging)
  5. # and the number of requests to process before running garbage collection.
  6. #
  7. # By default, the FastCGI crash log is RAILS_ROOT/log/fastcgi.crash.log
  8. # and the GC period is nil (turned off).  A reasonable number of requests
  9. # could range from 10-100 depending on the memory footprint of your app.
  10. #
  11. # Example:
  12. #   # Default log path, normal GC behavior.
  13. #   RailsFCGIHandler.process!
  14. #
  15. #   # Default log path, 50 requests between GC.
  16. #   RailsFCGIHandler.process! nil, 50
  17. #
  18. #   # Custom log path, normal GC behavior.
  19. #   RailsFCGIHandler.process! '/var/log/myapp_fcgi_crash.log'
  20. #
  21. require File.dirname(__FILE__) + "/../config/environment"
  22. require 'fcgi_handler'
  23.  
  24. ENV['RAILS_ENV'] = 'production'
  25.  
  26. ENV['GEM_PATH'] = '/home/mon_utilisateur/extra/lib/ruby/gems/1.8'
  27. ENV['GEM_HOME'] = '/home/mon_utilisateur/extra/lib/ruby/gems/1.8'
  28.  
  29. # see http://gabrito.com/post/keeping-rails-running-at-dreamhost-part-2
  30. #
  31.  
  32. class RailsFCGIHandler
  33.  private
  34.    def busy_exit_handler(signal)
  35.      dispatcher_log :info, "busy: asked to terminate during request signal #{signal}, deferring!"
  36.      @when_ready = :exit
  37.    end
  38.  
  39.    # Dreamhost sends the term signal and if were handling a request defer it
  40.    def term_process_request(cgi)
  41.      install_signal_handler('TERM',method(:busy_exit_handler).to_proc)
  42.      Dispatcher.dispatch(cgi)
  43.    rescue Exception => e  # errors from CGI dispatch
  44.      raise if SignalException === e
  45.      dispatcher_error(e)
  46.    ensure
  47.      install_signal_handler('TERM', method(:exit_now_handler).to_proc)
  48.    end
  49.    alias_method :process_request, :term_process_request
  50. end
  51.  
  52. # class RailsFCGIHandler
  53. # private
  54. #   def frao_handler(signal)
  55. #     dispatcher_log :info, "asked to terminate immediately"
  56. #     dispatcher_log :info, "frao handler working its magic!"
  57. #     restart_handler(signal)
  58. #   end
  59. #   alias_method :exit_now_handler, :frao_handler
  60. # end
  61.  
  62. RailsFCGIHandler.process!

 


  1. une bonne petite recherche google et vous n'aurez que l'embarras du choix [retour]
  2. voir nottament le site http://gabrito.com/post/keeping-rails-running-at-dreamhost-part-2 [retour]

Tags: , , , , , , , , ,

Une réponse à “le 57ième déploiement capistrano sur Dreamhost”

  1. le 21 mar 2007 à 16:53 euphrate_ylb

    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!