bitbucket+Capistrano(+multistage)でデプロイ設定

前回ブログに書いたbitBucketを本格的に使おうと思い
bitBucket+Capistranoでデプロイできるように設定しました。
もともとsvnでは使用していたもののgitになると勝手が違う。。

設定を行うついでに、capistrano-extをインストールして、
multistageというコマンド実行時にデプロイ先を選択できるように作りました。
これでいちいちdeploy.rbを書き換えること無くProduction,Staging,Develop
それぞれのサーバーに配置できるように!

こういう感じの構成↓

ということで以下手順。

GITのインストール

前書いた記事↓
gitをさくらサーバーへインストールする方法
http://d.hatena.ne.jp/oggata/20100201/1266908009

rubyのインストール

cd /usr/local/src
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p290.tar.gz
tar xvzf ruby-1.9.2-p180.tar.gz
cd ruby-1.9.2-p290.tar.gz
./configure --prefix=/usr
make
make install
ruby -v

gemのインストール

wget http://rubyforge.org/frs/download.php/74619/rubygems-1.7.2.tgz
tar xvzf rubygems-1.7.2.tgz
cd rubygems-1.7.2
ruby setup.rb
gem -v

zlib-develのインストール

yum install zlib-devel
cd ruby-1.9.1-p429/ext/zlib
ruby extconf.rb --with-zlib-include=/usr/include -with-zlib-lib=/usr/lib
make
make install

railsのインストール

gem install rails --include-dependencies
※3.1の場合
gem install rails --pre
rails -v

capistoranoインストール

gem install capistrano -y
cap -v

capistranoのライブラリを入れる

gem install capistrano-ext
gem install capistrano_colors

公開鍵と秘密鍵の生成->デフォルトでは/root/.ssh配下に id_rsa.pubと id_rsaが作成される

以前ここにも書いた↓
http://d.hatena.ne.jp/oggata/20110724/1311513458

#ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 

Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:

公開鍵(id_rsa.pub)->bitBucketに登録する
                  ->デプロイ先の/home/[デプロイ用のユーザー名]/.ssh/ 配下にauthorized_keysという名前で設置する

秘密鍵->下記から作成するCapistranoのdeploy.rbに設置場所と秘密鍵で指定したpassphraseを記述する

Capistranoの設定

①設定ファイルの自動作成
#mkdir cap_github2
#cd cap_github2
#capify .
>ここで基本ファイルが作成される
>[add] writing './Capfile'
>[add] making directory './config'
>[add] writing './config/deploy.rb'

②設定ファイルの記述

#mkdir ./config/deploy
#vim ./config/deploy/honban.rb
---------------------------------------------
role :app, "xxx.xxx.xxx.xxx"
#role :app, "xxx.xxx.xxx.xxx"
#role :web, "xxx.xxx.xxx.xxx"
#role :web, "xxx.xxx.xxx.xxx"
#role :db,  "xxx.xxx.xxx.xxx", :primary => true
---------------------------------------------
#vim ./config/deploy.rb
---------------------------------------------
require "capistrano_colors"
require "capistrano/ext/multistage"
set:application,"project"
set:user,"xxxxx" 
set:password,"xxxxx" 
default_run_options[:pty] = true
set:scm,:gitset:repository,"git@bitbucket.org:xxxxxx/public_test_repo.git"
set:deploy_to,"/var/www/#{application}"
---------------------------------------------

③Capコマンドを使って実行する

3-1.セットアップ(初回に実行)
#cap honban deploy:setup
3-2.デプロイを実行
#cap honban deploy:cold
3-3.以前の状態に戻す
#cap honban deploy:rollback
3-4.以前の状態のソースコードを削除
#cap honban deploy:cleanup

(参考)
http://www.geekzshu.com/rails/1200

======================================

いろいろなCapDeploy時のエラーのまとめ

======================================

Q.Permission denied (publickey).

fatal: The remote end hung up unexpectedly

A公開鍵の設定がうまくいってない。bitbucketに正しく公開鍵が設定されているか確認しましょう。or set :default_run_options, :pty => trueをdeploy.rbに追記する

Q.no tty present and no askpass program specified

servers: ["xxx.xxx.xxx.xxx"]
[xxx.xxx.xxx.xxx] executing command
[err :: xxx.xxx.xxx.xxx] sudo: no tty present and no askpass program specified
command finished in 54ms

A.capistranoのログインuserはwheelにいれておきましょう

かならずcapistranoのログインuserはwheelにいれておきましょう。sudoが必須です。
usermod -G wheel user_name

Q.No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)

[err :: xxx.xxx.xxx.xxx] rake aborted!
[err :: xxx.xxx.xxx.xxx]
[err :: xxx.xxx.xxx.xxx] No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)
[err :: xxx.xxx.xxx.xxx]
[err :: xxx.xxx.xxx.xxx] /usr/local/lib/ruby/1.9.1/rake.rb:2367:in `raw_load_rakefile'

A.rake実行が失敗してます

deploy用サーバーにはrakeが実行できないとダメ。
そのためにruby,gem,railsが必須です。上記の手順を呼んでインストールしましょう。
#rails -v
Rails 3.2.4.rc1
# ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553)
# gem -v
1.7.2

Q.sorry, you must have a tty to run sudo

[err :: xxx.xxx.xxx.xxx] sudo
[err :: xxx.xxx.xxx.xxx] :
[err :: xxx.xxx.xxx.xxx] sorry, you must have a tty to run sudo
[err :: xxx.xxx.xxx.xxx]

A./etc/sudoersの設定を見直しましょう

デプロイ先の
# vim /etc/sudoers
を開いて
Defaults requirettyをコメントアウトする

Q.tar: Child returned status 1

[err :: localhost] gzip:
[err :: localhost] stdin: unexpected end of file
[err :: localhost] tar: Child returned status 1
[err :: localhost] tar: 処理中にエラーが起きましたが、最後まで処理してからエラー終了させました

A.GZIPがない or デプロイサーバーがlocalhost or TARがおかしい

deploy用サーバーに
GZIPがないのでインストールする
#yum install bzip2*
#yum install zlib-devel

または、デプロイサーバーがlocalhostではないですか?
デプロイサーバーとcapistranoサーバーは分けた方が〇

またはtarが失敗しているので、下記が実行できるか確認
#圧縮
tar cvzf ./kk.tar.gz ./kk.txt
#解凍
tar -xzf ./kk.tar.gz

QPermission denied

servers: ["xxx.xxx.xxx.xxx"]
[xxx.xxx.xxx.xxx] executing command
[xxx.xxx.xxx.xxx :: err] fatal: could not create work tree dir '/var/www/project/releases/20120531071013'.: Permission denied
command finished in 57ms
[deploy:update_code] rolling back
executing "rm -rf /var/www/project/releases/20120531071013; true"
servers: ["xxx.xxx.xxx.xxx"]
[xxx.xxx.xxx.xxx] executing command

A.デプロイ先のディレクトリのアクセス権限を設定しましょう

chmod 777 -R project/
[xxx.xxx.xxx.xxx] executing command
[xxx.xxx.xxx.xxx :: err] Host key verification failed.
[xxx.xxx.xxx.xxx :: err] fatal: The remote end hung up unexpectedly
command finished in 1065ms

Q.Host key verification failed.

servers: ["xxx.xxx.xxx.xxx"]
[xxx.xxx.xxx.xxx] executing command
[xxx.xxx.xxx.xxx :: err] Host key verification failed.
[xxx.xxx.xxx.xxx :: err] fatal: The remote end hung up unexpectedly
command finished in 1156ms

Aデプロイ元からデプロイ先にログインできる? or デプロイ先とbitbucketが接続できる?

公開鍵の場合
# ssh deploy_user@'xxx.xxx.xxx.xxx' -i /root/.ssh/id_rsa -p 22
ID/PASSの場合
# ssh -l apple 'xxx.xxx.xxx.xxx'

自分の場合は、bitbucketにデプロイ用サーバーの公開鍵しか載せておらず、
デプロイ先の公開鍵もbitbucketに載せる必要があった。