Created
May 29, 2013 16:53
-
-
Save alobato/5671849 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "bundler/capistrano" | |
# -------------------------------------------------- | |
# nginx | |
namespace :nginx do | |
%w[start stop restart].each do |command| | |
desc "#{command} nginx" | |
task "#{command}", roles: :web do | |
sudo "service nginx #{command}" | |
end | |
end | |
after "unicorn:start", "nginx:restart" | |
end | |
# -------------------------------------------------- | |
# unicorn | |
namespace :unicorn do | |
%w[start stop restart].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 | |
desc "tail unicorn log file" | |
task :tail_log, roles: :app do | |
trap("INT") { puts 'Interupted'; exit 0; } | |
run "tail -f #{shared_path}/log/unicorn.log" do |channel, stream, data| | |
puts # for an extra line break before the host name | |
puts "#{channel[:host]}: #{data}" | |
break if stream == :err | |
end | |
end | |
desc "Setup unicorn configuration for this application" | |
task :setup, roles: :app do | |
run "mkdir -p /home/#{user}/.unicorn/#{application}" | |
run "cd /home/#{user}/.unicorn/#{application} && wget -O config.rb https://raw.github.com/gist/5131682/unicorn_conf.rb" | |
run "cd /home/#{user}/.unicorn/#{application} && ruby -pi -e \"gsub(/appname/, '#{application}')\" config.rb" | |
run "cd /home/#{user}/.unicorn/#{application} && wget -O init.sh https://raw.github.com/gist/5130853/unicorn_init.sh" | |
run "cd /home/#{user}/.unicorn/#{application} && ruby -pi -e \"gsub(/appname/, '#{application}')\" init.sh && chmod +x init.sh" | |
sudo "ln -nfs /home/#{user}/.unicorn/#{application}/init.sh /etc/init.d/unicorn_#{application}" | |
end | |
desc "Autostart unicorn" | |
task :autostart, roles: :app do | |
sudo "update-rc.d unicorn_#{application} defaults" | |
end | |
after "deploy:cold", "unicorn:setup" | |
after "unicorn:setup", "unicorn:autostart" | |
after "unicorn:autostart", "unicorn:start" | |
after "deploy", "unicorn:restart" | |
after "deploy:rollback", "unicorn:restart" | |
end | |
# -------------------------------------------------- | |
# mysql | |
def set_default(name, *args, &block) | |
set(name, *args, &block) unless exists?(name) | |
end | |
set_default(:db_name) { Capistrano::CLI.ui.ask "Database name: " } | |
set_default(:mysql_password) { Capistrano::CLI.password_prompt "MySQL Password: " } | |
namespace :mysql do | |
desc "Create mysql db - cap deploy:mysql:create_db" | |
task :create_db, roles: :db do | |
run "mysqladmin -u root -p#{mysql_password} create #{db_name}" | |
end | |
end | |
# -------------------------------------------------- | |
# check | |
namespace :check do | |
desc "Make sure local git is in sync with remote." | |
task :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 | |
before "deploy", "check:revision" | |
before "deploy:migrations", "check:revision" | |
before "deploy:cold", "check:revision" | |
end | |
# -------------------------------------------------- | |
# log | |
desc "tail production log file" | |
task :tail_production_log, roles: :app do | |
trap("INT") { puts 'Interupted'; exit 0; } | |
run "tail -f #{shared_path}/log/production.log" do |channel, stream, data| | |
puts # for an extra line break before the host name | |
puts "#{channel[:host]}: #{data}" | |
break if stream == :err | |
end | |
end | |
# -------------------------------------------------- | |
# sake | |
# $ bundle exec cap sake task="cache:update_cached_tags" | |
# http://jessewolgamott.com/blog/2012/09/10/the-one-where-you-run-rake-commands-with-capistrano/ | |
desc "Run a task on a remote server" | |
task :sake, roles: :app do | |
run("cd #{deploy_to}/current && bundle exec rake #{ENV['task']} RAILS_ENV=#{rails_env}") | |
end | |
# -------------------------------------------------- | |
# namespace :deploy do | |
# desc "Setup config" | |
# task :setup_config, roles: :app do | |
# run "mkdir -p #{shared_path}/config" | |
# put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml" | |
# put File.read("config/config.example.yml"), "#{shared_path}/config/config.yml" | |
# puts "Now edit the config files in #{shared_path}." | |
# end | |
# after "deploy:setup", "deploy:setup_config" | |
# desc "Symlink config" | |
# task :symlink_config, roles: :app do | |
# run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml" | |
# run "ln -nfs #{shared_path}/config/config.yml #{release_path}/config/config.yml" | |
# end | |
# after "deploy:finalize_update", "deploy:symlink_config" | |
# end | |
# -------------------------------------------------- | |
set :application, 'APPNAME' | |
#set :repository, "git@bitbucket.org:USERNAME/#{application}.git" | |
set :repository, "git@github.com:USERNAME/#{application}.git" | |
set :branch, "master" | |
set :user, "deployer" | |
set :deploy_to, "/home/#{user}/apps/#{application}" | |
set :use_sudo, false | |
default_environment["PATH"] = "/home/#{user}/.rbenv/shims:/home/#{user}/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games" | |
server "DOMAIN", :web, :app, :db, primary: true | |
# ssh_options[:port] = 888 | |
after "deploy", "deploy:cleanup" # keep only the last 5 releases |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment