2008年3月12日水曜日

Capistranoメモ

今まで面倒で使っていなかったが、今回のプロジェクトはかなり大規模構成になる(ことが予想される)ので、早めに自動化できるところはしておこうと。

Capistrano

Javaでいうところのantみたいなもの。プロジェクトのSubversionからのチェックアウト〜デプロイまでやってくれると。ネットで出回っている(日本語の)情報が古くなっている部分があるのでメモしておく。

このあたりを参考に。
http://www.capify.org/getting-started/rails

0. railsのアプリ作っとけと
# rails moe


1. capistranoのインストール
# sudo gem install capistrano


2. railsアプリをcapistrano対応にする
# capify /path-to/moe


3. config/deploy.rbを編集
+ set :deploy_to, "/path-to-deploy/"


4. taskのチェック
# 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: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 # Restarts your application.
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 # Start the application servers.
cap deploy:stop # Stop the application servers.
cap deploy:symlink # Updates the symlink to the most recently deployed ...
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 deploy:web:disable # Present a maintenance page to visitors.
cap deploy:web:enable # Makes the application web-accessible again.
cap invoke # Invoke a single command on the remote servers.
cap shell # Begin an interactive Capistrano session.

OK。

5. Mongrelを走らせるためのスクリプトファイル作成
# vim script/spin
+ script/process/spawner -p 11000 -i 3 -P /your-prefix

6. セットアップ
# cap deploy:setup

* executing `deploy:setup'
* executing "umask 02 && mkdir -p /path-to-deploy/ /path-to-deploy/releases /path-to-deploy/shared /path-to-deploy/shared/system /path-to-deploy/shared/log /path-to-deploy/shared/pids"
servers: ["hoge.net"]
[hoge.net] executing command
command finished

最初、/path-to-home/.ssh/id_rsa.pubがないと怒られて終了。
クライアント側に/path-to-home/.ssh/id_rsa.pubを用意して無事正常終了。

7. 依存性(gemとかですかね)のチェック
# cap -q deploy:check
You appear to have all necessary dependencies installed

OK。

7. cap deploy:cold
Permission deniedとか出てきた。公開鍵認証使ってるせいか・・・。
以下の記述をdeploy.rbに追加して解決。ついでにsudoを使わないようにもしておく。

+ set :use_sudo, false
+ ssh_options[:keys] = %w(/path-to-home/.ssh/id_dsa)
+ ssh_options[:port] = 22
+ ssh_options[:forward_agent] = true

8. cap deploy
Apache側でVirtualHost + mod_proxy_balancerの設定をしてから実行。
→うはwww動いたwwww

これで

 a) ローカルからリモートのsubversion鯖にcommit
 b) cap deploy

するだけでサーバーのデプロイ&アップデートが完了するようになりました。めでたしめでたし。

0 コメント: