ボクココ

個人開発に関するテックブログ

初めてのCapistrano

Capistranoを初めて使ってみたので、メモ。これはよさげな雰囲気を出してる。
Capistranoは複数のサーバにデプロイするための自動化の方法の一つで、Rubyで作られたとても有名なツールの中の一つだ。他の言語でも同様に使うことができるので、まずはRailsの自動配置をチャレンジしてみよう。

Gemfileの定義

Gemfileには以下を追加すると良さそう。


37 gem 'capistrano'
38 gem 'capistrano_colors'
39 gem 'capistrano-ext'
bundle install したらCapファイルを自動生成っと。

$ capify .
[add] writing './Capfile'
[add] writing './config/deploy.rb'
[done] capified!
こんなメッセージが出てきた。中身はどうなっているんだろう?

Capfile


1 load 'deploy'
2 # Uncomment if you are using Rails' asset pipeline
3 # load 'deploy/assets'
4 load 'config/deploy' # remove this line to skip loading any of the default tasks
ふむ。アセットパイプラインは使ってるので、コメントは外しますかね。
コメントを読んでみると、どうやらconfig/deploy にはデフォルトのタスクが書かれているらしい。どれどれ・・・
ネットで調べると、ここを修正して自分たちのデプロイ用にレシピを書いていく模様。

config/deploy.rb


1 set :application, "set your application name here"
2 set :repository, "set your repository location here"
3
4 set :scm, :subversion
5 # Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
6
7 role :web, "your web-server here" # Your HTTP server, Apache/etc
8 role :app, "your app-server here" # This may be the same as your `Web` server
9 role :db, "your primary db-server here", :primary => true # This is where Rails migrations will run
10 role :db, "your slave db-server here"
11
12 # if you want to clean up old releases on each deploy uncomment this:
13 # after "deploy:restart", "deploy:cleanup"
14
15 # if you're still using the script/reaper helper you will need
16 # these http://github.com/rails/irs_process_scripts
17
18 # If you are using Passenger mod_rails uncomment this:
19 # namespace :deploy do
20 # task :start do ; end
21 # task :stop do ; end
22 # task :restart, :roles => :app, :except => { :no_release => true } do
23 # run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
24 # end
25 # end

各項目の意味は大体わかるが、サンプルが欲しい所。
まぁとりあえずデプロイのやり方は
1、SSHでリリースサーバにつなげる
2、Gitなどのソース管理から対象ソースを取ってくる
3、それらを適切な部分に配置
4、再起動

ですな。やってみよう!

まずはGithubに公開レポジトリを作成。


cd {rails_root}
git init
git add .
git commit -am "first commit"
git remote add origin git@github.com:honkimi/capi_test.git
git push -u origin master
ここら辺は慣れたもんよ。
デプロイする側にGitのインストールと、GithubからCloneできるように、SSHキーの追加も忘れずにね。

んでconfig/deploy.rbを修正
最初の関門は、デプロイサーバにつなげるところだ。
自分の場合はAmazonEC2っぽいやつでFusion Cloudってのを使ってるので、それを指定してみる。




色々頑張った結果。。

本番サーバにアクセスする際


Enter passphrase for key '/home/homma/.ssh/id_rsa':

これが出てきて入力する。→うまくサーバに入れている模様。
その後Githubからcloneするんだけど、その時に


Permission denied (publickey).
fatal: The remote end hung up unexpectedly
がどうしても出てしまう。。

本番サーバでGit cloneすれば普通に撮ってこれるのだが、この時にも


Enter passphrase for key '/home/f-user/.ssh/id_rsa':
が求められて、パスワードを入れなきゃならない。

でもCapistranoだとその表示が出てこなくて、できていないみたい。
どうすればいいかわからずorz

ネットで調べてssh-agentとかやってみたり、 .ssh/configに設定を書いたりしてみたんだけど、
どうしてもこのキーフレーズを入力する項目がCapistranoに出てしまう。
どなたかわかる方いたら教えてください><