capistrano3使ってみた

3へのだいたいの変更点

setメソッドで定義した値を読むには、fetchメソッドを使う
capistrano-extがデフォでサポート
自作タスクの構文がかわった

今までdeploy:migeationsと実行していたmigrationタスクは、
cap production deployで一緒に実行されるようになってるみたい。

自作タスク定義の構文がかわった

2.x
namespace :deploy do
  desc "update database config file(database.yml) from database.release"
  task :update_databaseconfig, :roles => :app, :except => { :no_release => true } do
    run "cp #{release_path}/config/#{database_yml} #{release_path}/config/database.yml "
  end
  before 'deploy:finalize_update', 'deploy:update_databaseconfig'

  task :restart, :roles => :app, :except => { :no_release => true } do
    run "sudo service #{unicorn_service_name} upgrade"
  end
end
3.x
namespace :deploy do
  task :updated_database_config do
    on roles(:app) do
      within release_path do
        execute :cp, "config/#{fetch(:database_yml)}",  "config/database.yml "
      end
    end
  end
  before 'assets:precompile', 'updated_database_config'

  task :restart do
    on roles(:app), in: :sequence, wait: 5  do
      execute "sudo service #{fetch(:unicorn_service_name)} upgrade"
    end
  end
end

withinはディレクトリがあればブロックを実行するようです。すごく非直感的、、。
inオプション後で調べる。
set :linked_filesが便利らしい....

ハマたったところ

今まで使っていた/etc/init.d/unicorn_init.shで upgrade start stopができない問題に直面。

/var/www/antena/shared/bundle/ruby/2.0.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:193:in `pid=': Already running on PID:10475 (or pid=/var/www/antena/shared/pids/unicorn.pid is stale) (ArgumentError)

2.xだとunicornのpidsファイルは次のパスに置かれていたのだけど、3から置かれなくてなってた。
unicorn_init.shはcurrent/tmp/pidsを見ていたのでエラーになってたみたい。

/var/www/antena/current/tmp/pids

unicorn_init.shに定義しているpidsファイルを場所をshared配下に変更して解決。

ソース

Capfile
# Load DSL and Setup Up Stages
require 'capistrano/setup'

# Includes default deployment tasks
require 'capistrano/deploy'

require 'capistrano/rvm'

require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
config/deploy.rb
set :user, 'jiikko'
set :use_sudo, false
set :application, 'antena'
set :scm, :git
set :repo_url, 'git@bitbucket.org:jiikko/antena.git'
set :branch, "master" # どのブランチをデプロイするか
set :git_shallow_clone, 1 # 1つ前のコミットまでとる
set :deploy_via, :export  # .git/のできるかできないか
set :app_name, 'ぺろぺろあんてな'
set :unicorn_service_name, 'antena_unicorn'

namespace :deploy do
  task :updated_database_config do
    on roles(:app) do
      within release_path do
        execute :cp, "config/#{fetch(:database_yml)}",  "config/database.yml "
      end
    end
  end
  before 'assets:precompile', 'updated_database_config'

  task :restart do
    on roles(:app) do
      execute "sudo service #{fetch(:unicorn_service_name)} upgrade"
    end
  end
end

after :deploy, 'deploy:cleanup'
config/deploy/production.rb
# -*- coding: utf-8 -*-
set :deploy_to, "/var/www/antena"
set(:app_uri, 'http://prpr-antena.net/')
set(:env, '本番')
set :branch, "master"
set :rails_env, "production"
set :migration_role, 'db'

set :database_yml, 'database.yml.production'

server "133.242.187.194", user: 'jiikko', roles: %w{web app db}

実行時間

2.x

27.73 real 0.79 user 0.19 sys

3.x

36.58 real 1.40 user 0.33 sys
時間かかってる、、、。

移行の感想

自作タスクの書き換えが気持ち大変だった感。
実行中のログはわかりやすくなってる。
2.xだとdeploy.rbがカオスだった気がするけど3はだいぶ整理された気がする。


ということで ぺろぺろあんてな はcapistrano3でデプロイしています!