Skip to content

Instantly share code, notes, and snippets.

@benjamintanweihao
Created August 5, 2013 17:05
Show Gist options
  • Save benjamintanweihao/6157572 to your computer and use it in GitHub Desktop.
Save benjamintanweihao/6157572 to your computer and use it in GitHub Desktop.
require 'bundler/capistrano'
require 'capistrano-unicorn'
require 'sidekiq/capistrano'
require 'whenever/capistrano'
load 'config/recipes/base'
load 'config/recipes/cache'
load 'config/recipes/postgresql'
load 'config/recipes/redis'
load 'config/recipes/rails'
load 'config/recipes/seed'
load 'config/recipes/tail'
# Deployment Server
server "xxx.xxx.xxx.xxx", :web, :app, :db, primary: true
default_run_options[:pty] = true
default_run_options[:shell] = '/bin/bash --login'
set :application, "qs"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :default_environment, { 'PATH' => "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH" }
set :scm, "git"
set :branch, "master"
set :bundle_without, [:darwin, :development, :test]
set :bundle_cmd, 'bundle'
set :sidekiq_cmd, "#{bundle_cmd} exec sidekiq"
set :sidekiqctl_cmd, "#{bundle_cmd} exec sidekiqctl"
set :sidekiq_timeout, 10
set :sidekiq_role, :app
set :sidekiq_pid, "#{current_path}/tmp/pids/sidekiq.pid"
set :sidekiq_processes, 1
set :whenever_command, "bundle exec whenever"
set_default(:unicorn_pid) { "#{shared_path}/pids/unicorn.pid" }
set_default(:unicorn_pid) { "#{shared_path}/pids/unicorn.pid" }
set_default(:unicorn_workers, 7)
ssh_options[:forward_agent] = true
after "deploy", "deploy:migrate"
after "deploy", "deploy:cleanup"
namespace :deploy do
%w[start stop restart reload].each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
run "/etc/init.d/unicorn_#{application} #{command}"
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
run "mkdir -p #{shared_path}/config"
put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
puts "Now edit the config files in #{shared_path}."
end
after "deploy:setup", "deploy:setup_config"
task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update", "deploy:symlink_config"
desc "Make sure local git is in sync with remote."
task :check_revision, roles: :web do
unless `git rev-parse HEAD` == `git rev-parse origin/#{branch}`
puts "WARNING: HEAD is not the same as origin/#{branch}"
puts "Run `git push` to sync changes."
exit
end
end
desc 'Load DB schema - CAUTION: rewrites database!'
task :load_schema, :roles => :app do
run "cd #{current_path}; bundle exec rake db:schema:load RAILS_ENV=#{rails_env}"
end
before "deploy", "deploy:check_revision"
end
after "deploy:restart", "unicorn:restart"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment