Skip to content

Instantly share code, notes, and snippets.

@rkmathi
Created December 5, 2012 15:24
Show Gist options
  • Save rkmathi/4216508 to your computer and use it in GitHub Desktop.
Save rkmathi/4216508 to your computer and use it in GitHub Desktop.
http://blog.has-key.org/208 - 4. デプロイの準備
require "capistrano_colors"
require "bundler/capistrano"
# RVM settings
# $:.unshift(File.expand_path("./lib", ENV["rvm_path"]))
require "rvm/capistrano"
set :rvm_type, :user
set :rvm_ruby_string, "1.9.3"
# Repository settings
default_run_options[:pty] = true
set :application, "{アプリケーション名}"
set :scm, "git"
set :scm_verbose, false
set :repository, "{gitリポジトリパス}"
set :branch, "{ブランチ名}"
set :deploy_via, :remote_cache
set :keep_releases, 3
# SSH settings
ssh_options[:forward_agent] = true
ssh_options[:paranoid] = true
ssh_options[:port] = {ポート番号}
ssh_options[:keys] = "{SSH秘密鍵のパス}"
set :user, "{ユーザ名}"
set :runner, "{ユーザ名}"
set :use_sudo, false
set :deploy_to, "/var/www/#{application}"
set :rails_env, "production"
# Role settings
set :domain, "{デプロイサーバのIPアドレス}"
role :web, domain
role :app, domain
role :db, domain, :primary => true
# Assets settings
set :normalize_asset_timestamps, false
load "deploy/assets"
# Bundle settings
set :bundle_flags, "--quiet"
# Capistrano task settings
namespace :deploy do
task :start, :roles => :app do
run "cd #{current_path};\
bundle exec unicorn_rails -c config/unicorn.rb -E #{rails_env} -D"
end
task :stop, :roles => :app do
run "kill -s QUIT `cat /tmp/unicorn_#{application}.pid`"
end
task :restart, :roles => :app do
stop
start
end
task :drop, :roles => :app do
run "cd #{current_path};\
bundle exec rake RAILS_ENV=production db:drop"
end
task :seed, :roles => :app do
run "cd #{current_path};\
bundle exec rake RAILS_ENV=production db:seed"
end
task :remigrate, :roles => :app do
stop
drop
migrate
start
end
end
namespace :assets do
task :precompile, :roles => :web do
run "cd #{current_path} &&\
RAILS_ENV=#{rails_env} bundle exec rake assets:precompile"
end
task :cleanup, :roles => :web do
run "cd #{current_path} &&\
RAILS_ENV=#{rails_env} bundle exec rake assets:clean"
end
end
application = '{アプリケーション名}'
listen "/tmp/unicorn_#{application}.sock"
pid "/tmp/unicorn_#{application}.pid"
working_directory "/var/www/#{application}/current"
worker_processes 4
timeout 30
if ENV['RAILS_ENV'] == 'production'
shared_path = "/var/www/#{application}/shared"
stderr_path "#{shared_path}/log/unicorn.stderr.log"
stdout_path "#{shared_path}/log/unicorn.stdout.log"
end
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true
before_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
old_pid = "/tmp/unicorn_#{application}.pid.oldbin"
if File.exists?(old_pid) && old_pid != server.pid
begin
Process.kill(:QUIT, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
sleep 1
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment