capistrano3を軽くいじったメモ

### インストール

#### capistranoをインストールする。

```bash
cat Gemfile
source 'http://rubygems.org'

gem 'capistrano'
```

#### bundle install

pathは好みでいれてください。

```bash
bundle install --path vendor/bundle
```

#### capの初期インストール

```bash
bundle exec cap install

mkdir -p config/deploy
create config/deploy.rb
create config/deploy/staging.rb
create config/deploy/production.rb
mkdir -p lib/capistrano/tasks
Capified
```
出来た


#### タスク内容を見る

```bash
cap -vT
```


| コマンド| 説明|
|:-----------|:------------|
|cap deploy                         |# Deploy a new release|
|cap deploy:check                   |# Check required files and directories exist|
|cap deploy:check:directories       |# Check shared and release directories exist|
|cap deploy:check:linked_dirs       |# Check directories to be linked exist in shared|
|cap deploy:check:linked_files      |# Check files to be linked exist in shared|
|cap deploy:check:make_linked_dirs  |# Check directories of files to be linked exist in shared|
|cap deploy:cleanup                 |# Clean up old releases|
|cap deploy:cleanup_rollback        |# Remove and archive rolled-back release|
|cap deploy:finished                |# Finished|
|cap deploy:finishing               |# Finish the deployment, clean up server(s)|
|cap deploy:finishing_rollback      |# Finish the rollback, clean up server(s)|
|cap deploy:log_revision            |# Log details of the deploy|
|cap deploy:published               |# Published|
|cap deploy:publishing              |# Publish the release|
|cap deploy:revert_release          |# Revert to previous release timestamp|
|cap deploy:reverted                |# Reverted|
|cap deploy:reverting               |# Revert server(s) to previous release|
|cap deploy:rollback                |# Rollback to previous release|
|cap deploy:started                 |# Started|
|cap deploy:starting                |# Start a deployment, make sure server(s) ready|
|cap deploy:symlink:linked_dirs     |# Symlink linked directories|
|cap deploy:symlink:linked_files    |# Symlink linked files|
|cap deploy:symlink:release         |# Symlink release to current|
|cap deploy:symlink:shared          |# Symlink files and directories from shared to release|
|cap deploy:updated                 |# Updated|
|cap deploy:updating                |# Update server(s) by setting up a new release|
|cap install                        |# Install Capistrano, cap install STAGES=staging,production|

#### タスクの追加

タスクを追加したい場合のオペレーション。
今回はhostnameコマンドを実行するタスクを追加。

```bash
cat lib/capistrano/tasks/hostname.cap
namespace :hostname do

  desc "Get Servername"
  task :get => :environment do
    on roles(:app) do
      execute :hostname, "-s"
    end
  end

end
```

#### タスク登録の確認

hostnameのnamespaceがあることを確認します。

```bash
cap -vT|fgrep hostname
cap hostname:get                   # Get Servername
```


#### ステージの追加

以下が実行出来るようにします。

* production
* staging

上記がデフォルトで設定されているので、developmentを追加します。

```bash
bundle exec cap development deploy --dry-run

** Invoke ensure_stage (first_time)
** Execute (dry run) ensure_stage
cap aborted!
Don't know how to build task 'development'
/home/user/hoge/cap/bundle/ruby/1.9.1/gems/capistrano-3.1.0/lib/capistrano/application.rb:15:in `run'
/home/user/hoge/cap/bundle/ruby/1.9.1/gems/capistrano-3.1.0/bin/cap:3:in `<top (required)>'
/home/user/hoge/cap/bundle/ruby/1.9.1/bin/cap:23:in `load'
/home/user/hoge/cap/bundle/ruby/1.9.1/bin/cap:23:in `<main>'
(See full trace by running task with --trace)
```

環境が無いことを確認。

#### development環境用のファイルを追加

```bash
cp config/deploy/staging.rb config/deploy/development.rb
```

#### development環境で実行する。

```bash
bundle exec cap development deploy --dry-run
** Invoke development (first_time)
** Execute (dry run) development
** Invoke deploy (first_time)
** Execute (dry run) deploy
```