Capistranoのセットアップ

最初人がしゃべっていたままを覚えていて、キャピストラーノと思ってたのですが
カピストラーノみたいですね。読み方。

## Capistranoインストール、簡単なtask作成

vagrantを使用して`node01``node02``node03`を立ち上げて`node01`で作業。
あらかじめ`node01`から各サーバーに`ssh node02`等でsudoが使えるユーザーでssh接続できるように設定済み
パスワード無しの`sudo`が実行できる設定は以下、vagrantユーザーがパスワード無しのsudo実行出来る。

    # visudo

```
vagrant   ALL=(ALL)  NOPASSWD: ALL

Defaults env_keep += "PATH"
```

    $ sudo gem install capistrano capistrano_colors
    $ mkdir capis-recipes
    $ cd capis-recipes
    $ vi Capfile

```ruby:Capfile
role :node, "node02", "node03"

task :ls do
  run "ls"
end
```

    $ cap ls

これで`$HOME`ディレクトリ直下のls情報が出力される。すげー簡単。

## deployタスクの作成、実行

`ls`じゃなくってもう少し複雑な事をさせるためのテンプレートの作成。deployタスクの設定ファイル作成。

    $ capify .
    [add] writing './Capfile'
    [add] making directory './config'
    [add] writing './config/deploy.rb'
    [done] capified!
    $ tree
    .
    ├── Capfile
    └── config
        └── deploy.rb

    1 directory, 2 files

ちょっとしたファイルが作成される。
例として自分の[dotfiles](https://github.com/soramugi/dotfiles)をデプロイする設定を書く

    $ vi config/deploy.rb

```deploy.rb
set :application, "dotfiles"
set :repository,  "git://github.com/soramugi/dotfiles.git"

set :scm, :git

role :node, "node02", "node03"
```

    $ cap deploy:setup

作業ディレクトリの作成

    $ cap deploy:check

ちゃんとデプロイ出来るかの確認
と、ここでエラーが出る。どうやら書き込み権限が無いよ、というのと`git`コマンドが無いというもの。
各nodeサーバーに入り以下のディレクトリに書き込み権限を付与、gitコマンドインストール。

    $ sudo chown -R vagrant.  /u/
    $ sudo yum install git

標準のデプロイディレクトリは`/u/`ディレクトリを作成してそこにデプロイを行う。
デプロイするディレクトリを指定する時は以下。あらかじめ指定して権限を付与していた方が良いだろう

```deploy.rb
set :deploy_to, "/tmp/"
```

そしてデプロイ実行

    $ cap deploy

無事デプロイ出来ていれば完了

## tail -f logタスクの作成

複数台サーバーで同じコマンドを流せるようになったので
ログの閲覧が出来るように設定を行う。

`cap -T`コマンドで作成したタスクを閲覧してカンニング出来るようにする。
標準のタスク閲覧

    $ cap -T
    cap deploy                # Deploys your project.
    cap deploy:check          # Test deployment dependencies.
    cap deploy:cleanup        # Clean up old releases.
    cap deploy:cold           # Deploys and starts a `cold' application.
    cap deploy:create_symlink # Updates the symlink to the most recently deployed version.
    cap deploy:migrate        # Run the migrate rake task.
    cap deploy:migrations     # Deploy and run pending migrations.
    cap deploy:pending        # Displays the commits since your last deploy.
    cap deploy:pending:diff   # Displays the `diff' since your last deploy.
    cap deploy:restart        # Blank task exists as a hook into which to install your own environment specific behaviour.
    cap deploy:rollback       # Rolls back to a previous version and restarts.
    cap deploy:rollback:code  # Rolls back to the previously deployed version.
    cap deploy:setup          # Prepares one or more servers for deployment.
    cap deploy:start          # Blank task exists as a hook into which to install your own environment specific behaviour.
    cap deploy:stop           # Blank task exists as a hook into which to install your own environment specific behaviour.
    cap deploy:symlink        # Deprecated API.
    cap deploy:update         # Copies your project and updates the symlink.
    cap deploy:update_code    # Copies your project to the remote servers.
    cap deploy:upload         # Copy files to the currently deployed version.
    cap invoke                # Invoke a single command on the remote servers.
    cap shell                 # Begin an interactive Capistrano session.

    Some tasks were not listed, either because they have no description,
    or because they are only used internally by other tasks. To see all
    tasks, type `cap -vT'.

    Extended help may be available for these tasks.
    Type `cap -e taskname' to view it.

一回だけ複数サーバーのログを閲覧するだけなら`cap shell`を使えば良いと思う。
割と頻繁に見るapacheのアクセスログやphpログの`tail -f`出力タスクの作成

    $ sudo gem install capistrano-colorized-stream
    $ vi config/log.rb

```config/log.rb
require 'capistrano/colorized_stream'

namespace :log do
  desc 'tail -f php.log'
  task :php do
    stream "tail -f /var/log/php/php.log ; true"
  end

  desc 'tail -f httpd access_log'
  task :access do
    stream 'tail -f /var/log/httpd/access_log ; true'
  end
end
```

以下追加

    $ vi Capfile

```Capfile
load 'config/log'
```


    $ cap -T
    cap deploy                # Deploys your project.
    cap deploy:check          # Test deployment dependencies.
    cap deploy:cleanup        # Clean up old releases.
    cap deploy:cold           # Deploys and starts a `cold' application.
    cap deploy:create_symlink # Updates the symlink to the most recently deployed version.
    cap deploy:migrate        # Run the migrate rake task.
    cap deploy:migrations     # Deploy and run pending migrations.
    cap deploy:pending        # Displays the commits since your last deploy.
    cap deploy:pending:diff   # Displays the `diff' since your last deploy.
    cap deploy:restart        # Blank task exists as a hook into which to install your own environment specific behaviour.
    cap deploy:rollback       # Rolls back to a previous version and restarts.
    cap deploy:rollback:code  # Rolls back to the previously deployed version.
    cap deploy:setup          # Prepares one or more servers for deployment.
    cap deploy:start          # Blank task exists as a hook into which to install your own environment specific behaviour.
    cap deploy:stop           # Blank task exists as a hook into which to install your own environment specific behaviour.
    cap deploy:symlink        # Deprecated API.
    cap deploy:update         # Copies your project and updates the symlink.
    cap deploy:update_code    # Copies your project to the remote servers.
    cap deploy:upload         # Copy files to the currently deployed version.
    cap invoke                # Invoke a single command on the remote servers.
    cap log:access            # tail -f httpd access_log
    cap log:php               # tail -f php.log
    cap shell                 # Begin an interactive Capistrano session.

    Some tasks were not listed, either because they have no description,
    or because they are only used internally by other tasks. To see all
    tasks, type `cap -vT'.

    Extended help may be available for these tasks.
    Type `cap -e taskname' to view it.

以下が追加されているのが解る

```
cap log:access            # tail -f httpd access_log
cap log:php               # tail -f php.log
```

    $ cap log:access

実行でログが流れる幸せ！


参考リンク

[エンジニアよ、大志を抱け: Capistranoメモ](http://engineers-be-ambitious.blogspot.jp/2008/03/capistrano.html)
[Streaming Log with Capistrano - ni-blog] (http://orihubon.com/blog/2012/02/09/streaming-log-with-capistrano/)
[はじめてのCapistrano #Rails #capistrano - Qiita \[キータ\]] (http://qiita.com/items/1a4cfd01cb538f2d8f0e)
[Capistranoでデプロイする | kenkennote] (http://kenkennote.com/blog/2012/06/06/deploy-with-capistrano/)
[つくるぶガイドブログ: capistrano で本番環境にデプロイ] (http://www.tkrb.jp/guide/2008/02/capistrano_rails.html)
[Rals3.2.9で始める Git連携Capistranoによる自動デプロイ - 酒と泪とRubyとRailsと] (http://morizyun.github.io/blog/capistrano-git-rails-deploy/)