Chronicling the trials and tribulations of developing for the modern web.



Deploy any project using Capistrano 2

Posted by Pat Nakajima on June 28, 2007 in Development.

Feel left out of the Capistrano party because you don’t use Rails? Take a trip to fun.patnakajima.com/recipe and follow the directions. What you’ll find there is an almost completely redone recipe for deploying pretty much almost any project you have.

Requirements

  • You use some sort of source code management (Subversion, CVS, Perforce, etc.)
  • You have SSH access to the remote server, and administrative privileges.
  • You want to start deploying your project like the cool kids.
Here’s a look at what the recipe looks like:

load 'deploy' if respond_to?(:namespace) # cap2 differentiator

require 'rubygems'
require 'bells/recipes/apache' # This one requires the Bells gem

set :application, "your project's name" 
set :domain, "your.project.domain" 
role :app, domain
role :web, domain
role :db,  domain, :primary => true

# Set the Unix user and group that will actually perform each task
# on the remote server. The defaults are set to 'deploy'
set :user, "deploy" 
set :group, "deploy" 

# Deployment Settings
set :repository,  "set your repository path here" 
set :deploy_to, "/var/www/sites/#{application}" # This is where your project will be deployed.

# Uncomment this if you want to deploy your project by copying
# the files from your local computer.
# set :deploy_via, :copy

# =============================================================
# Apache Settings
# =============================================================
set :apache_server_name, nil
set :apache_conf_path, "/usr/local/apache2/conf/sites" 
set :apache_conf_file, "#{application}.conf" 
set :apache_ctl, "/usr/local/apache2/bin/apachectl" 
set :apache_server_aliases, []
# set :apache_ssl_enabled, false
# set :apache_ssl_ip, nil
# set :apache_ssl_forward_all, false
# set :apache_ssl_chainfile, false

# In the event you have the urge to develop your own vhost erb template, you can modify
# this variable. I wouldn't do it unless you're strongly confident in what you're doing.
set :apache_conf_template, 'http://svn.nakadev.com/tools/recipe/templates/vhost.conf'

# ================================================================================================
# DON'T MODIFY ANYTHING BELOW THIS POINT UNLESS YOU KNOW WHAT YOU ARE DOING
# ================================================================================================

# Making sure things will go smoothly.
depend :remote, :directory, apache_conf_path
depend :remote, :directory, deploy_to
depend :local, :gem, 'bells'

namespace :deploy do
  namespace :apache do
    desc "Setup vhost conf on Apache web server." 
    task :setup do
      sudo "mkdir -p #{apache_conf_path}" 
      sudo "chown -R #{user}:#{group} #{apache_conf_path} && chmod -R 775 #{apache_conf_path}" 
      logger.info "generating .conf file" 
      conf = Net::HTTP.get URI.parse(apache_conf_template)
      require 'erb'
      result = ERB.new(conf).result(binding)
      logger.info "putting #{application}.conf on #{domain}" 
      put result, "#{application}.conf" 
      run "mv #{application}.conf #{apache_conf_path}/#{apache_conf_file}" 
    end
  end

  # Overwritten to provide flexibility for people who aren't using Rails.
  task :setup, :except => { :no_release => true } do
    dirs = [deploy_to, releases_path, shared_path]
    dirs += %w(system).map { |d| File.join(shared_path, d) }
    run "umask 02 && mkdir -p #{dirs.join(' ')}" 
  end

  # Also overwritten to remove Rails-specific code.
  task :finalize_update, :except => { :no_release => true } do
    run "chmod -R g+w #{release_path}" if fetch(:group_writable, true)
  end

  # Each of the following tasks are Rails specific. They're removed.
  task :migrate do
  end

  task :migrations do
  end

  task :cold do
  end

  task :start do
  end

  task :stop do
  end

  # Do nothing (To restart apache, run 'cap deploy:apache:restart')
  task :restart do
  end
end

Note: For the most recent version of this recipe, visit fun.patnakajima.com/recipe.

As always, if you have any comments about the recipe, leave them here on this post.

commentsareclosed

Sorry, but comments are closed on this blog after 30 days.