Capistrano Script for Ruby on Rails

Here is an excellent capistrano deploy script for Ruby on Rails applications. It changes the permissions and on the files and also pushes the mongrel cluster file. I changed my information so make sure you look at the script from beginning to end.

#Capistrano Recipe 2.0 for pushing the files. Developed by Arthur Gressick

set :domain, "subdomain.your_site.com"
set :user, "root"
set :application, "your_site_root"
#make sure and don't run as super user
#set :use_sudo, false

#SVN Repository information
#default_run_options[:pty] = false

set :scm, :subversion
set :repository_addy,  "svn://svn.your_repository.com/ruby_project/trunk"
set :deploy_via, :export
set :deploy_to, "/home/user/vhosts/#{application}"


set :scm_user, "username"
set :scm_password, Proc.new { Capistrano::CLI.password_prompt("SVN password for #{scm_user}, please: ") }
set :repository, Proc.new { "--username #{scm_user} --password #{scm_password} --no-auth-cache #{repository_addy}"; }

#set :scm_username, "username"
#set :scm_prefer_prompt, true
#set :scm_auth_cache, true

#DEFINE the servers that need to be updated
role :app, "ip_address"
role :web, "ip_address"
role :db,  "ip_address", :primary => true

#SET BACKUPS
set :keep_releases, 3 # keep only 3 versions of the site on the server

#DEPLOY:SETUP Script to set the permissions
desc "Setup the permissions for the project"
task :setup_permissions, :roles => :app do
 as = fetch(:runner, "root")
 
 run "chown -Rh username:username /home/username/vhosts/#{application}"
 run "chown -Rh www-data:www-data /home/username/vhosts/#{application}/shared"
end
after "deploy:setup", :setup_permissions

#DEPLOY:COLD Scripts for moving the mongrel cluster file to folder.
desc "Setup a link to the mongrel cluster folder"
task :setup_mongrel_cluster, :roles => :app do
 as = fetch(:runner, "root")
 
 run "chown -Rh username:username /home/username/vhosts/#{application}/releases/"
 run "ln -s /home/username/vhosts/#{application}/current/config/mongrel_cluster.yml /etc/mongrel-cluster/sites-enabled/#{application}.yml"
end
after "deploy:cold", :setup_mongrel_cluster

#---------------------------------------------------------------------------------------------------
#MONGREL management
namespace :deploy do

 desc "Start the server"
 task :start, :roles => :app do
  as = fetch(:runner, "root")
  
  invoke_command "/etc/init.d/mongrel-cluster start"
 end
 
 desc "Stop the server"
 task :stop, :roles => :app do
  as = fetch(:runner, "root")
  
  invoke_command "/etc/init.d/mongrel-cluster stop"
 end
 
 desc "Restart the server"
 task :restart, :roles => :app do
  as = fetch(:runner, "root")
  
  #copy out the log files.
  run "cp /home/username/vhosts/#{application}/shared/log/production.log /home/username/vhosts/#{application}/shared/log/production-bak.log"
  run "rm -rf /home/username/vhosts/#{application}/shared/log/production.log"
  
  run "chown -Rh username:username /home/username/vhosts/#{application}/releases/"
  
  run "chmod -R 777 /home/username/vhosts/#{application}/current/tmp"
  
  invoke_command "/etc/init.d/mongrel-cluster restart"
 end
end

#Run all of the clean up scripts.
after "deploy:update", "deploy:cleanup" #Clean up the directory so that there are only x working copies set the number above.

desc "After Symlink shared files path"
task :after_symlink, :roles => :app do
  #FileUtils.mkdir_p("#{shared_path}/files") unless File.exists?("#{shared_path}/files")
  run "ln -nfs #{shared_path}/files #{release_path}/public/files"
  #run "ln -nfs #{shared_path}/tmp #{release_path}/public/tmp"
end

Comments

Popular posts from this blog

Console over SSH (ONTAP)

Cacti Monitor Server Step by Step Ubuntu

Brocade Zone Setup