Skip to content

Instantly share code, notes, and snippets.

@wallace
Created June 2, 2009 21:21
Show Gist options
  • Select an option

  • Save wallace/122602 to your computer and use it in GitHub Desktop.

Select an option

Save wallace/122602 to your computer and use it in GitHub Desktop.
set :application, "appname"
set :repository, "[email protected]:/store/git/appname/.git"
set :deploy_to, "/var/www/rails/#{application}"
# set the default rails_env here, and override it in the tasks
# when necessary
set :rails_env, "development"
# If you aren't deploying to /u/apps/#{application} on the target
# servers (which is the default), you can specify the actual location
# via the :deploy_to variable:
# set :deploy_to, "/var/www/#{application}"
# If you aren't using Subversion to manage your source code, specify
# your SCM below:
set :scm, :git
set :mongrel_conf, "/var/www/rails/appname/current/config/mongrel_cluster.yml"
set :mongrel_environment, "development"
set :user, "defaultuser"
set :use_sudo, false
set :runner, "defaultuser"
# For some reason , this is required to have git checkout
# from athena successfully
default_run_options[:pty] = true
ssh_options[:keys] = [File.join(ENV["HOME"], ".ec2", "appname")]
ssh_options[:forward_agent] = true
ssh_options[:paranoid] = false
set :scm_user, "defaultuser"
# make sure we've added the appname key to ssh-agent
on :start do
`ssh-add #{ssh_options[:keys]}`
end
# set default hoptoad_api_key to production for cold deploy
set :hoptoad_api_key, 'production_hoptoad_key'
task :production do
default_environment['RAILS_ENV']='production'
set :rails_env, 'production'
set :branch, 'production'
set :deploy_via, :remote_cache
role :app, "appname.com"
role :web, "appname.com"
role :db, "appname.com", :primary => true
end
task :staging do
default_environment['RAILS_ENV']='staging'
set :rails_env, 'staging'
set :branch, 'master' if variables[:branch].nil? # allows us to override deployed branch on command line 'cap -S branch=2-3-stable staging deploy'
set :deploy_via, :remote_cache
role :app, "devbox.com"
role :web, "devbox.com"
role :db, "devbox.com", :primary => true
set :hoptoad_api_key, 'staging_hoptoad_key'
end
task :ec2dev do
default_environment['RAILS_ENV']='ec2dev'
set :rails_env, 'ec2dev'
set :branch, 'master'
set :deploy_via, :remote_cache
role :app, "ec2.devbox.com"
role :web, "ec2.devbox.com"
role :db, "ec2.devbox.com", :primary => true
set :hoptoad_api_key, 'another_hoptoad_key'
end
# Dependancies that need to be met for the server to function correctly
depend :remote, :gem, "rubyzip", ">=0.9.1"
depend :remote, :gem, "daemons", ">=1.0.9"
depend :remote, :gem, "pdf-writer", ">=1.1.7"
depend :remote, :gem, "rcov", ">=0.8.1.2.0"
depend :remote, :gem, "tzinfo", ">=0.3.6"
depend :remote, :gem, "mocha", ">=0.5.6" #for testing only
depend :remote, :command, "unzip"
depend :remote, :command, "pdftk"
# desc "This will deploy the app"
# task :deploy do
# run "svn --quiet #{checkout} #{repository} #{release_path}"
# run "ln -nfs #{release_path} #{current_path}"
# run "ln -nfs #{shared_path}/uploads #{current_path}/uploads"
# end
namespace :deploy do
desc <<-DESC
[internal] Touches up the released code. This is called by update_code \
after the basic deploy finishes. It assumes a Rails project was deployed, \
so if you are deploying something else, you may want to override this \
task with your own environment's requirements.
This task will make the release group-writable (if the :group_writable \
variable is set to true, which is the default). It will then set up \
symlinks to the shared directory for the log, system, and tmp/pids \
directories, and will lastly touch all assets in public/images, \
public/stylesheets, and public/javascripts so that the times are \
consistent (so that asset timestamping works).
LOCAL MODIFICATIONS: We create the approot/uploads symlink here.
DESC
task :finalize_update, :except => { :no_release => true } do
run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
# mkdir -p is making sure that the directories are there for some SCM's that don't
# save empty folders
run <<-CMD
rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids &&
mkdir -p #{latest_release}/public &&
mkdir -p #{latest_release}/tmp &&
mkdir -p #{shared_path}/uploads &&
ln -s #{shared_path}/log #{latest_release}/log &&
ln -s #{shared_path}/system #{latest_release}/public/system &&
ln -s #{shared_path}/pids #{latest_release}/tmp/pids &&
ln -s #{latest_release}/config/ec2/database.yml #{latest_release}/config/database.yml &&
ln -s #{latest_release}/config/environments/production.rb #{latest_release}/config/environments/staging.rb &&
ln -s #{shared_path}/uploads #{latest_release}/public/uploads
CMD
stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
asset_paths = %w(images stylesheets javascripts).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
# regenerate config files for apache
deploy.generate_configs
run <<-CMD
echo "HoptoadNotifier.configure do |config| config.api_key = '#{hoptoad_api_key}' end" > #{latest_release}/config/initializers/hoptoad.rb
CMD
end
desc <<-DESC
Prepares one or more servers for deployment. Before you can use any \
of the Capistrano deployment tasks with your project, you will need to \
make sure all of your servers have been prepared with `cap setup'. When \
you add a new server to your cluster, you can easily run the setup task \
on just that server by specifying the HOSTS environment variable:
$ cap HOSTS=new.server.com setup
It is safe to run this task on servers that have already been set up; it \
will not destroy any deployed revisions or data.
LOCAL MODIFICATIONS: We create the shared/uploads directory here.
DESC
task :setup, :except => { :no_release => true } do
dirs = [deploy_to, releases_path, shared_path]
dirs += %w(system log pids uploads).map { |d| File.join(shared_path, d) }
run "umask 02 && mkdir -p #{dirs.join(' ')}"
end
task :restart do
# run "touch #{deploy_to}/current/tmp/restart.txt"
run "([ -e /usr/bin/emerge ] && sudo apache2ctl configtest || sudo apache2ctl -t) && sudo /etc/init.d/apache2 restart"
deploy.god.stop #force god to stop and restart in the correct directory
backgroundrb.stop
deploy.god.start
end
task :restart_rails do
run "touch #{deploy_to}/current/tmp/restart.txt"
end
task :start do
run "sudo /etc/init.d/apache2 start"
backgroundrb.start
end
task :stop do
run "sudo /etc/init.d/apache2 stop"
backgroundrb.stop
end
task :generate_configs do
desc "regenerates the apache config files in config/apache/conf/*.erb"
run "#{latest_release}/script/generate_apache_configs #{rails_env}"
end
namespace :god do
task :start do
desc "Start god if its not running."
#start up god.
run <<-CMD
nohup god -c #{deploy_to}/current/config/god/monitoring.god -P #{deploy_to}/current/tmp/pids/god_monitoring.pid -l #{deploy_to}/current/log/god.log && sleep 4
CMD
end
task :stop do
desc "Stop god."
run "god terminate; exit 0"
end
end
namespace :backgroundrb do
task :restart do
begin stop; rescue; end #this catches the bdrb error where a PID file doesn't exist
start
end
task :stop do
run "cd #{deploy_to}/current && #{deploy_to}/current/script/backgroundrb -e #{rails_env} stop"
end
task :start do
run "cd #{deploy_to}/current && #{deploy_to}/current/script/backgroundrb -e #{rails_env} start"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment