houndをcapistranoでデプロイする


# 目的
houndCIを独自に運用する場合、デプロイをさくっと終わらせたい。houndCIはrailsアプリなのでcapistranoに対応できる。capistranoの設定ファイルを書き記す。

# Capfile
```rb
# Load DSL and set up stages
require 'capistrano/setup'

# Include default deployment tasks
require 'capistrano/deploy'

require 'capistrano/rbenv'
require 'capistrano/bundler'
require 'capistrano/console'
require 'capistrano/rails/migrations'
require 'capistrano-resque'

require 'capistrano3/unicorn'

# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#   https://github.com/capistrano/rvm
#   https://github.com/capistrano/rbenv
#   https://github.com/capistrano/chruby
#   https://github.com/capistrano/bundler
#   https://github.com/capistrano/rails
#   https://github.com/capistrano/passenger
#
# require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
# require 'capistrano/bundler'
# require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'
# require 'capistrano/passenger'

# Load custom tasks from `lib/capistrano/tasks` if you have any defined

# dotenvはお好みで
require 'dotenv'

Dotenv.load
```

# Gemfile
```rb
gem 'capistrano-rbenv'
gem 'capistrano-bundler'
gem 'capistrano-rails'
gem 'capistrano3-unicorn'
gem 'capistrano-resque'
```

# config/deploy.rb

```rb
# レポジトリのURL。フォークした場合はそのURLを入れる
set :repo_url, 'git@github.com:thoughtbot/hound.git'
set :branch, -> { `git rev-parse --abbrev-ref HEAD`.chomp }.call
set :deploy_to, '/hogehoge'
set :linked_files, %w(.env)
set :linked_dirs, %w(log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system)
set :keep_releases, 2

set :rbenv_type, :system
set :rbenv_ruby, '2.2.0'

set :unicorn_rack_env, -> { fetch(:rails_env) }

# houndで使うresque worker
set :workers, { "high" => 1, "medium" => 1, "low" => 1 }
```

# config/deploy/development.rb
```rb
set :rails_env, :development

# デプロイ先のサーバー名を入れる
DEV_SERVER = 'hogehoge.com'

server DEV_SERVER
set :branch, "master"

role :app, DEV_SERVER
role :resque_worker, DEV_SERVER
role :resque_scheduler, DEV_SERVER
```

# config/unicorn/development.rb

```rb
app_path = "/hogehoge/development/current"

listen 3000, :tcp_nopush => true
timeout 60
preload_app true
worker_processes 2
pid "#{app_path}/tmp/pids/unicorn.pid"

stdout_path "log/unicorn.log"
stderr_path "log/unicorn_error.log"

before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end
```

# デプロイ方法

## リポジトリを落としてくる
## gemをinstallする

```shell-session
bundle install
```

## デプロイ
```shell-session
bundle exec cap development deploy
```
## サーバー(unicorn,resque)再起動
```shell-session
bundle exec cap development unicorn:stop
bundle exec cap development unicorn:start
bundle exec cap development resque:stop
bundle exec cap development resque:start
```

# TODO
最新のhoundはunicornじゃなくてpumaを使っているがそれの対応してない。