Capistrano3を利用してBitbucketプライベートリポジトリにあるRailsアプリをデプロイしてみた

スクリーンショット 2016-01-09 20.02.08
Ruby on Rails

Railsアプリを配置する際、毎回手作業で頑張って配置してきたが、そろそろ自動デプロイを・・・ ということでCapistrano3を勉強し始めました。

今回は、あまり凝ったことはせず、単純にソースをBitbucketから取得して、サーバー上にソースを置くというところまで試してみます。

 

やりたいこと

  • ローカルからサーバにRailsアプリを配置したい
  • RailsアプリのソースはBitbucketプライベートリポジトリから取得したい
  • サーバ上に配置するユーザはrootやvagrantユーザではなく新たに作成したユーザを使いたい

 

前提

  • サーバーはUbuntu14.04 LTS (vagrantでローカルに構築)
  • ruby2.2.0
  • rails4
  • サーバ上にはあらかじめgit,rvmを入れている
  • サーバー名はcap-test
  • サーバーはvagrantを利用して構築

 

サーバ確認

ログインできることを確認

[r_nishio@rnmac] $ ssh cap-test
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Last login: Sat Jan  9 08:19:39 2016 from 10.0.2.2
vagrant@vagrant-ubuntu-trusty:~$

 

デプロイ用ユーザを作成 SSHでログインできるようにする

まずはローカルでSSH公開鍵を作成する

[r_nishio@rnmac] $ pwd                                                                                                                               [~/.ssh]
/Users/r_nishio/.ssh
[r_nishio@rnmac] $ mkdir cap-deploy-test                                                                                                             [~/.ssh]
[r_nishio@rnmac] $ ssh-keygen -t rsa                                                                                                                 [~/.ssh]
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/r_nishio/.ssh/id_rsa): /Users/r_nishio/.ssh/cap-deploy-test/id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/r_nishio/.ssh/cap-deploy-test/id_rsa.
Your public key has been saved in /Users/r_nishio/.ssh/cap-deploy-test/id_rsa.pub.
The key fingerprint is:
SHA256:YZRoGJu0kr7vdH8HjdBJSQhqHeNsR5U9+FsJebxfv4g r_nishio@rnmac.local
The key's randomart image is:
+---[RSA 2048]----+
|    oo+oo=o= o   |
|   o.Oo=o = = o  |
|  o *.= +o o + o |
| . o . o..o . + .|
|  .     S. o o .o|
|   .      o o   o|
|  . . .    .. . .|
|   o . .  .E.. . |
|   .o   .. .     |
+----[SHA256]-----+
[r_nishio@rnmac] $ ls -la cap-deploy-test/                                                                                                           [~/.ssh]
total 16
drwxr-xr-x   4 r_nishio  staff   136  1  9 18:52 ./
drwx------  13 r_nishio  staff   442  1  9 18:52 ../
-rw-------   1 r_nishio  staff  1675  1  9 18:52 id_rsa
-rw-r--r--   1 r_nishio  staff   402  1  9 18:52 id_rsa.pub
[r_nishio@rnmac] $  

パスフレーズは何も入力せず enter を押す。

次にサーバー側でユーザ作成と公開鍵の配置を行う。

まずはサーバ上での作業

root@vagrant-ubuntu-trusty:/home# ll
total 16
drwxr-xr-x  4 root    root    4096  1月  9 09:58 ./
drwxr-xr-x 23 root    root    4096  1月  9 08:14 ../
drwxr-xr-x  3 chato   chato   4096  1月  9 08:39 chato/
drwxr-xr-x  6 vagrant vagrant 4096  1月  9 08:39 vagrant/
root@vagrant-ubuntu-trusty:/home# mkdir deploy
root@vagrant-ubuntu-trusty:/home# useradd deploy -d /home/deploy
root@vagrant-ubuntu-trusty:/home# passwd deploy
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@vagrant-ubuntu-trusty:/home# chown deploy:deploy /home/deploy
root@vagrant-ubuntu-trusty:/home# ll /home/
total 20
drwxr-xr-x  5 root    root    4096  1月  9 09:58 ./
drwxr-xr-x 23 root    root    4096  1月  9 08:14 ../
drwxr-xr-x  3 chato   chato   4096  1月  9 08:39 chato/
drwxr-xr-x  2 deploy  deploy  4096  1月  9 09:58 deploy/
drwxr-xr-x  6 vagrant vagrant 4096  1月  9 08:39 vagrant/

ユーザ確認と.sshフォルダ作成


root@vagrant-ubuntu-trusty:/home# su deploy
deploy@vagrant-ubuntu-trusty:/home$ cd
deploy@vagrant-ubuntu-trusty:~$ ls -ltar
total 8
drwxr-xr-x 5 root   root   4096  1月  9 09:58 ..
drwxr-xr-x 2 deploy deploy 4096  1月  9 09:58 .
deploy@vagrant-ubuntu-trusty:~$ pwd
/home/deploy
deploy@vagrant-ubuntu-trusty:~$
deploy@vagrant-ubuntu-trusty:~$ mkdir .ssh
deploy@vagrant-ubuntu-trusty:~$ ls -ltar
total 12
drwxr-xr-x 5 root   root   4096  1月  9 09:58 ..
drwxrwxr-x 2 deploy deploy 4096  1月  9 10:04 .ssh
drwxr-xr-x 3 deploy deploy 4096  1月  9 10:04 .
deploy@vagrant-ubuntu-trusty:~$
deploy@vagrant-ubuntu-trusty:~$ mkdir share
deploy@vagrant-ubuntu-trusty:~$ chmod 777 share/
deploy@vagrant-ubuntu-trusty:~$ ls -ltar
total 16
drwxr-xr-x 5 root   root   4096  1月  9 09:58 ..
drwxrwxr-x 2 deploy deploy 4096  1月  9 10:04 .ssh
drwxrwxrwx 2 deploy deploy 4096  1月  9 10:06 share
drwxr-xr-x 4 deploy deploy 4096  1月  9 10:06 .

ローカルに戻って公開鍵の転送

[r_nishio@rnmac] $ pwd                                                                                                               [~/.ssh/cap-deploy-test]
/Users/r_nishio/.ssh/cap-deploy-test
[r_nishio@rnmac] $ ll                                                                                                                [~/.ssh/cap-deploy-test]
total 16
-rw-------  1 r_nishio  staff  1675  1  9 18:52 id_rsa
-rw-r--r--  1 r_nishio  staff   402  1  9 18:52 id_rsa.pub
[r_nishio@rnmac] $ scp id_rsa.pub cap-test://home/deploy/share                                                                       [~/.ssh/cap-deploy-test]
id_rsa.pub                                                                                                                  100%  402     0.4KB/s   00:00
[r_nishio@rnmac] $  

サーバ上に再び戻って公開鍵の配置

deploy@vagrant-ubuntu-trusty:~$ pwd
/home/deploy
deploy@vagrant-ubuntu-trusty:~$ cat share/id_rsa.pub >> .ssh/authorized_keys

配置したのでローカルから接続できるように ローカルの.ssh/config を修正する。
なお、IdentityFileに先ほど作成した秘密鍵を設定する。

Host cap-test-deploy
  HostName 127.0.0.1
  User deploy
  Port 2200
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile ~/.ssh/cap-deploy-test/id_rsa
  IdentitiesOnly yes
  LogLevel FATAL

これでサーバにdeployユーザーでログインできるので確認する。


[r_nishio@rnmac] $ ssh cap-test-deploy                                                                                               [~/.ssh/cap-deploy-test]
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

$ pwd
/home/deploy

Railsプロジェクト準備


[r_nishio@rnmac] $ rails new cap-deploy-test
      create
      create  README.rdoc
      create  Rakefile
      create  config.ru
      以下省略

Bitbucketプライベートリポジトリにpushする

[r_nishio@rnmac] $ git init                                                                                                       [~/000_work/cap-deploy-test]
Initialized empty Git repository in /Users/r_nishio/000_work/cap-deploy-test/.git/
[r_nishio@rnmac] $ git add .                                                                                               [master ~/000_work/cap-deploy-test]
[r_nishio@rnmac] $ git commit -m "first"                                                                                   [master ~/000_work/cap-deploy-test]
[r_nishio@rnmac] $ git remote add origin git@bitbucket.org:r_nishio/cap-deploy-test.git                                    [master ~/000_work/cap-deploy-test]
[r_nishio@rnmac] $ git push -u origin --all                                                                                [master ~/000_work/cap-deploy-test]
Counting objects: 64, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (53/53), done.
Writing objects: 100% (64/64), 16.54 KiB | 0 bytes/s, done.
Total 64 (delta 2), reused 0 (delta 0)
To git@bitbucket.org:r_nishio/cap-deploy-test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
[r_nishio@rnmac] $  

Capistrano準備

Gemファイルの編集
rubyはrvmを利用して導入しているので次のようにしている。rbenv使う場合は別の書き方が必要。

group :development do
  gem 'spring'
  gem 'capistrano', '~> 3.2.0', require: false
  gem 'capistrano3-unicorn', require: false
  gem 'capistrano-rails', require: false
  gem 'rvm1-capistrano3', require: false
  gem 'capistrano-bundler', require: false
  gem 'capistrano-maintenance', github: 'capistrano/maintenance', require: false
end

Capistarnoの設定ファイルを作成する


[r_nishio@rnmac] $ cap install                                                                                             [master ~/000_work/cap-deploy-test]
mkdir -p config/deploy
create config/deploy.rb
create config/deploy/staging.rb
create config/deploy/production.rb
mkdir -p lib/capistrano/tasks
Capified
[r_nishio@rnmac] $  

ちなみに Capify . というやり方はCapistrano3ではできないみたい。

[r_nishio@rnmac] $ capify .                                                                                                [master ~/000_work/cap-deploy-test]
--------------------------------------------------------------------------------
Capistrano 3.x is incompatible with Capistrano 2.x.

This command has become `cap install` in Capistrano 3.x

For more information see http://www.capistranorb.com/
--------------------------------------------------------------------------------

Capfileを次のように編集

require 'capistrano/setup'
require 'capistrano/deploy'

require 'rvm1/capistrano3'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'

Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

config/deploy.rb をまずは次のように編集

# config valid only for Capistrano 3.1
require 'rvm1/capistrano3'

lock '3.2.1'

set :application, 'cap-deploy-test'
set :repo_url, 'git@bitbucket.org:r_nishio/cap-deploy-test.git'

set :deploy_to, '/var/www/cap-deploy-test'

# Default value for :scm is :git
set :scm, :git

# Default value for :format is :pretty
set :format, :pretty

# Default value for :log_level is :debug
set :log_level, :debug

# Default value for :pty is false
set :pty, false

# RVM
set :rvm_type, :system
set :rvm1_ruby_version, '2.2.0'

set :keep_releases, 5

特記すべき点は

set :repo_url, 'git@bitbucket.org:r_nishio/cap-deploy-test.git'

のところである。
bitbucketのトップページにあるurlをそのまま貼っている。

スクリーンショット 2016-01-09 18.37.59

結果的にこの方法ではパブリックリポジトリならこれでうまくいくのだが、プライベートリポジトリではうまくいかない。
まずはこのまま進めてみることにする。

次にconfig/deploy/staging.rb を次のように修正する。
今回はroleはappだけで実験。

set :branch, 'master'
set :rails_env, 'production'

role :app, %w{deploy@cap-test-deploy}
#role :web, %w{deploy@cap-test-deploy}
#role :db,  %w{deploy@cap-test-deploy}

set :ssh_options, {
  keys: [File.expand_path('~/.ssh/cap-deploy-test/id_rsa')],
  forward_agent: true
}

一旦上記設定で試してみる

結果的には上記のままだとエラーになってしまうが、どんなエラーになるか試してみる

[r_nishio@rnmac] $ cap staging deploy:check                                                                                [master ~/000_work/cap-deploy-test]
/Users/r_nishio/.rvm/gems/ruby-2.2.1@global/gems/bundler-1.11.2/lib/bundler.rb:289: warning: Insecure world writable dir /Users/r_nishio/.rvm/gems/ruby-2.2.1/bin in PATH, mode 040777
INFO [52ad41e0] Running /usr/bin/env mkdir -p /var/www/cap-deploy-test/rvm1scripts/ as deploy@cap-test-deploy
DEBUG [52ad41e0] Command: /usr/bin/env mkdir -p /var/www/cap-deploy-test/rvm1scripts/
INFO [52ad41e0] Finished in 0.114 seconds with exit status 0 (successful).
DEBUG Uploading /Users/r_nishio/.rvm/gems/ruby-2.2.1/gems/rvm1-capistrano3-1.4.0/script/rvm-auto.sh 0.0%
INFO Uploading /Users/r_nishio/.rvm/gems/ruby-2.2.1/gems/rvm1-capistrano3-1.4.0/script/rvm-auto.sh 100.0%
INFO [71dbb8e1] Running /usr/bin/env chmod +x /var/www/cap-deploy-test/rvm1scripts/rvm-auto.sh as deploy@cap-test-deploy
DEBUG [71dbb8e1] Command: /usr/bin/env chmod +x /var/www/cap-deploy-test/rvm1scripts/rvm-auto.sh
INFO [71dbb8e1] Finished in 0.005 seconds with exit status 0 (successful).
INFO [88a612c1] Running /usr/bin/env mkdir -p /tmp/cap-deploy-test/ as deploy@cap-test-deploy
DEBUG [88a612c1] Command: /usr/bin/env mkdir -p /tmp/cap-deploy-test/
INFO [88a612c1] Finished in 0.005 seconds with exit status 0 (successful).
DEBUG Uploading /tmp/cap-deploy-test/git-ssh.sh 0.0%
INFO Uploading /tmp/cap-deploy-test/git-ssh.sh 100.0%
INFO [e6a61a2d] Running /usr/bin/env chmod +x /tmp/cap-deploy-test/git-ssh.sh as deploy@cap-test-deploy
DEBUG [e6a61a2d] Command: /usr/bin/env chmod +x /tmp/cap-deploy-test/git-ssh.sh
INFO [e6a61a2d] Finished in 0.005 seconds with exit status 0 (successful).
DEBUG [0d699626] Running /usr/bin/env git ls-remote -h git@bitbucket.org:r_nishio/cap-deploy-test.git as deploy@cap-test-deploy
DEBUG [0d699626] Command: ( export GIT_ASKPASS="/bin/echo" GIT_SSH="/tmp/cap-deploy-test/git-ssh.sh" ; /usr/bin/env git ls-remote -h git@bitbucket.org:r_nishio/cap-deploy-test.git )
DEBUG [0d699626]        Warning: Permanently added the RSA host key for IP address '104.192.143.3' to the list of known hosts.
DEBUG [0d699626]        conq: repository access denied. deployment key is not associated with the requested repository.
DEBUG [0d699626]        fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
DEBUG [0d699626] Finished in 3.363 seconds with exit status 128 (failed).
[r_nishio@rnmac] $  

deployユーザでサーバー上で /var/www/cap-deploy-test フォルダを作るところまでは成功しましたがbitbucketからソースを落とすところで失敗しました。

これはdeployを行うユーザ(user名:deploy) がbitbucket側でソースを落とすことを許可されていないため。なので、deployユーザでもソースを落とせるようにbitbucket側に公開鍵を設定する。

スクリーンショット 2016-01-09 20.02.08

先に作成した公開鍵を貼り付ける
スクリーンショット 2016-01-09 20.03.13

スクリーンショット 2016-01-09 20.03.31

次にローカルの.ssh/configに下記を記載する

Host bitbucket-cap-deploy-test
	User git
	port 22
	HostName bitbucket.org
	Identityfile ~/.ssh/cap-deploy-test/id_rsa
	TCPKeepAlive yes
	IdentitiesOnly yes

ローカル側でつなげるか確認する

[r_nishio@rnmac] $ ssh -T git@bitbucket-cap-deploy-test                                                                    [master ~/000_work/cap-deploy-test]
authenticated via a deploy key.


You can use git or hg to connect to Bitbucket. Shell access is disabled.

This deploy key has read access to the following repositories:

r_nishio/cap-deploy-test: deploy -- r_nishio@rnmac.local
[r_nishio@rnmac] $  

OKですね。

次に、サーバー側がbitbucket-cap-deploy-test の名前解決ができるように サーバー側のdeployユーザの.ssh/configに下記を記載する

deploy@vagrant-ubuntu-trusty:~/.ssh$ cat config
Host bitbucket-cap-deploy-test
        HostName bitbucket.org
deploy@vagrant-ubuntu-trusty:~/.ssh$

config/deploy.rb の記載を修正する

#set :repo_url, 'git@bitbucket.org:r_nishio/cap-deploy-test.git'
set :repo_url, 'git@bitbucket-cap-deploy-test:r_nishio/cap-deploy-test.git'

最後にローカルで次のコマンドを実行する
これをやらないとうまく鍵を使いまわしてくれない

[r_nishio@rnmac] $ ssh-add ~/.ssh/cap-deploy-test/id_rsa                                                                                         [~/008_test]
Identity added: /Users/r_nishio/.ssh/cap-deploy-test/id_rsa (/Users/r_nishio/.ssh/cap-deploy-test/id_rsa)
[r_nishio@rnmac] $  

なお、何回か別の鍵でテストをやっていたら、前のキーが残っていてうまくいかなかったので

ssh-add -D

で一度全部消してから実行した。

これで再度試してみる

[r_nishio@rnmac] $ cap staging deploy:check                                                                                [master ~/000_work/cap-deploy-test]
/Users/r_nishio/.rvm/gems/ruby-2.2.1@global/gems/bundler-1.11.2/lib/bundler.rb:289: warning: Insecure world writable dir /Users/r_nishio/.rvm/gems/ruby-2.2.1/bin in PATH, mode 040777
INFO [d812e9f5] Running /usr/bin/env mkdir -p /var/www/cap-deploy-test/rvm1scripts/ as deploy@cap-test-deploy
DEBUG [d812e9f5] Command: /usr/bin/env mkdir -p /var/www/cap-deploy-test/rvm1scripts/
INFO [d812e9f5] Finished in 0.091 seconds with exit status 0 (successful).
DEBUG Uploading /Users/r_nishio/.rvm/gems/ruby-2.2.1/gems/rvm1-capistrano3-1.4.0/script/rvm-auto.sh 0.0%
INFO Uploading /Users/r_nishio/.rvm/gems/ruby-2.2.1/gems/rvm1-capistrano3-1.4.0/script/rvm-auto.sh 100.0%
INFO [69edb105] Running /usr/bin/env chmod +x /var/www/cap-deploy-test/rvm1scripts/rvm-auto.sh as deploy@cap-test-deploy
DEBUG [69edb105] Command: /usr/bin/env chmod +x /var/www/cap-deploy-test/rvm1scripts/rvm-auto.sh
INFO [69edb105] Finished in 0.005 seconds with exit status 0 (successful).
INFO [d82c5789] Running /usr/bin/env mkdir -p /tmp/cap-deploy-test/ as deploy@cap-test-deploy
DEBUG [d82c5789] Command: /usr/bin/env mkdir -p /tmp/cap-deploy-test/
INFO [d82c5789] Finished in 0.005 seconds with exit status 0 (successful).
DEBUG Uploading /tmp/cap-deploy-test/git-ssh.sh 0.0%
INFO Uploading /tmp/cap-deploy-test/git-ssh.sh 100.0%
INFO [a5c2648c] Running /usr/bin/env chmod +x /tmp/cap-deploy-test/git-ssh.sh as deploy@cap-test-deploy
DEBUG [a5c2648c] Command: /usr/bin/env chmod +x /tmp/cap-deploy-test/git-ssh.sh
INFO [a5c2648c] Finished in 0.005 seconds with exit status 0 (successful).
DEBUG [51aad930] Running /usr/bin/env git ls-remote -h git@bitbucket-cap-deploy-test:r_nishio/cap-deploy-test.git as deploy@cap-test-deploy
DEBUG [51aad930] Command: ( export GIT_ASKPASS="/bin/echo" GIT_SSH="/tmp/cap-deploy-test/git-ssh.sh" ; /usr/bin/env git ls-remote -h git@bitbucket-cap-deploy-test:r_nishio/cap-deploy-test.git )
DEBUG [51aad930]        34bafe6810bf4630f16d5db94d7a5dad27a76c8f        refs/heads/master
DEBUG [51aad930] Finished in 5.273 seconds with exit status 0 (successful).
INFO [7790c089] Running /usr/bin/env mkdir -pv /var/www/cap-deploy-test/shared /var/www/cap-deploy-test/releases as deploy@cap-test-deploy
DEBUG [7790c089] Command: /usr/bin/env mkdir -pv /var/www/cap-deploy-test/shared /var/www/cap-deploy-test/releases
DEBUG [7790c089]        mkdir: created directory ‘/var/www/cap-deploy-test/shared’
mkdir: created directory ‘/var/www/cap-deploy-test/releases’
INFO [7790c089] Finished in 0.011 seconds with exit status 0 (successful).
INFO [70fa553d] Running /usr/bin/env mkdir -pv /var/www/cap-deploy-test/shared/public/assets as deploy@cap-test-deploy
DEBUG [70fa553d] Command: /usr/bin/env mkdir -pv /var/www/cap-deploy-test/shared/public/assets
DEBUG [70fa553d]        mkdir: created directory ‘/var/www/cap-deploy-test/shared/public’
mkdir: created directory ‘/var/www/cap-deploy-test/shared/public/assets’
INFO [70fa553d] Finished in 0.006 seconds with exit status 0 (successful).
[r_nishio@rnmac] $  

チェックはうまくいったのでいざ配置

[r_nishio@rnmac] $ cap staging deploy                                                                                      [master ~/000_work/cap-deploy-test]
/Users/r_nishio/.rvm/gems/ruby-2.2.1@global/gems/bundler-1.11.2/lib/bundler.rb:289: warning: Insecure world writable dir /Users/r_nishio/.rvm/gems/ruby-2.2.1/bin in PATH, mode 040777
INFO [cda02ff2] Running /usr/bin/env mkdir -p /var/www/cap-deploy-test/rvm1scripts/ as deploy@cap-test-deploy
DEBUG [cda02ff2] Command: /usr/bin/env mkdir -p /var/www/cap-deploy-test/rvm1scripts/
INFO [cda02ff2] Finished in 0.089 seconds with exit status 0 (successful).
DEBUG Uploading /Users/r_nishio/.rvm/gems/ruby-2.2.1/gems/rvm1-capistrano3-1.4.0/script/rvm-auto.sh 0.0%
INFO Uploading /Users/r_nishio/.rvm/gems/ruby-2.2.1/gems/rvm1-capistrano3-1.4.0/script/rvm-auto.sh 100.0%
INFO [0de1cd2c] Running /usr/bin/env chmod +x /var/www/cap-deploy-test/rvm1scripts/rvm-auto.sh as deploy@cap-test-deploy
DEBUG [0de1cd2c] Command: /usr/bin/env chmod +x /var/www/cap-deploy-test/rvm1scripts/rvm-auto.sh
INFO [0de1cd2c] Finished in 0.005 seconds with exit status 0 (successful).
INFO [eff96fd5] Running /usr/bin/env mkdir -p /tmp/cap-deploy-test/ as deploy@cap-test-deploy
DEBUG [eff96fd5] Command: /usr/bin/env mkdir -p /tmp/cap-deploy-test/
INFO [eff96fd5] Finished in 0.006 seconds with exit status 0 (successful).
DEBUG Uploading /tmp/cap-deploy-test/git-ssh.sh 0.0%
INFO Uploading /tmp/cap-deploy-test/git-ssh.sh 100.0%
INFO [c415422f] Running /usr/bin/env chmod +x /tmp/cap-deploy-test/git-ssh.sh as deploy@cap-test-deploy
DEBUG [c415422f] Command: /usr/bin/env chmod +x /tmp/cap-deploy-test/git-ssh.sh
INFO [c415422f] Finished in 0.005 seconds with exit status 0 (successful).
DEBUG [321a013a] Running /usr/bin/env git ls-remote -h git@bitbucket-cap-deploy-test:r_nishio/cap-deploy-test.git as deploy@cap-test-deploy
DEBUG [321a013a] Command: ( export GIT_ASKPASS="/bin/echo" GIT_SSH="/tmp/cap-deploy-test/git-ssh.sh" ; /usr/bin/env git ls-remote -h git@bitbucket-cap-deploy-test:r_nishio/cap-deploy-test.git )
DEBUG [321a013a]        34bafe6810bf4630f16d5db94d7a5dad27a76c8f        refs/heads/master
DEBUG [321a013a] Finished in 4.649 seconds with exit status 0 (successful).
INFO [4f8f78b5] Running /usr/bin/env mkdir -pv /var/www/cap-deploy-test/shared /var/www/cap-deploy-test/releases as deploy@cap-test-deploy
DEBUG [4f8f78b5] Command: /usr/bin/env mkdir -pv /var/www/cap-deploy-test/shared /var/www/cap-deploy-test/releases
INFO [4f8f78b5] Finished in 0.009 seconds with exit status 0 (successful).
INFO [fde2f4e7] Running /usr/bin/env mkdir -pv /var/www/cap-deploy-test/shared/public/assets as deploy@cap-test-deploy
DEBUG [fde2f4e7] Command: /usr/bin/env mkdir -pv /var/www/cap-deploy-test/shared/public/assets
INFO [fde2f4e7] Finished in 0.009 seconds with exit status 0 (successful).
DEBUG [a9539fe1] Running /usr/bin/env [ -f /var/www/cap-deploy-test/current/REVISION ] as deploy@cap-test-deploy
DEBUG [a9539fe1] Command: [ -f /var/www/cap-deploy-test/current/REVISION ]
DEBUG [a9539fe1] Finished in 0.006 seconds with exit status 1 (failed).
DEBUG [3978cc6d] Running /usr/bin/env [ -f /var/www/cap-deploy-test/repo/HEAD ] as deploy@cap-test-deploy
DEBUG [3978cc6d] Command: [ -f /var/www/cap-deploy-test/repo/HEAD ]
DEBUG [3978cc6d] Finished in 0.006 seconds with exit status 0 (successful).
INFO The repository mirror is at /var/www/cap-deploy-test/repo
DEBUG [e83edcef] Running /usr/bin/env if test ! -d /var/www/cap-deploy-test/repo; then echo "Directory does not exist '/var/www/cap-deploy-test/repo'" 1>&2; false; fi as deploy@cap-test-deploy
DEBUG [e83edcef] Command: if test ! -d /var/www/cap-deploy-test/repo; then echo "Directory does not exist '/var/www/cap-deploy-test/repo'" 1>&2; false; fi
DEBUG [e83edcef] Finished in 0.005 seconds with exit status 0 (successful).
INFO [8abfe998] Running /usr/bin/env git remote update as deploy@cap-test-deploy
DEBUG [8abfe998] Command: cd /var/www/cap-deploy-test/repo && ( export GIT_ASKPASS="/bin/echo" GIT_SSH="/tmp/cap-deploy-test/git-ssh.sh" ; /usr/bin/env git remote update )
DEBUG [8abfe998]        Fetching origin
INFO [8abfe998] Finished in 5.433 seconds with exit status 0 (successful).
DEBUG [ab2bbf1f] Running /usr/bin/env if test ! -d /var/www/cap-deploy-test/repo; then echo "Directory does not exist '/var/www/cap-deploy-test/repo'" 1>&2; false; fi as deploy@cap-test-deploy
DEBUG [ab2bbf1f] Command: if test ! -d /var/www/cap-deploy-test/repo; then echo "Directory does not exist '/var/www/cap-deploy-test/repo'" 1>&2; false; fi
DEBUG [ab2bbf1f] Finished in 0.004 seconds with exit status 0 (successful).
INFO [a138bef5] Running /usr/bin/env mkdir -p /var/www/cap-deploy-test/releases/20160109131938 as deploy@cap-test-deploy
DEBUG [a138bef5] Command: cd /var/www/cap-deploy-test/repo && ( export GIT_ASKPASS="/bin/echo" GIT_SSH="/tmp/cap-deploy-test/git-ssh.sh" ; /usr/bin/env mkdir -p /var/www/cap-deploy-test/releases/20160109131938 )
INFO [a138bef5] Finished in 0.005 seconds with exit status 0 (successful).
INFO [ecf8306e] Running /usr/bin/env git archive master | tar -x -C /var/www/cap-deploy-test/releases/20160109131938 as deploy@cap-test-deploy
DEBUG [ecf8306e] Command: cd /var/www/cap-deploy-test/repo && ( export GIT_ASKPASS="/bin/echo" GIT_SSH="/tmp/cap-deploy-test/git-ssh.sh" ; /usr/bin/env git archive master | tar -x -C /var/www/cap-deploy-test/releases/20160109131938 )
INFO [ecf8306e] Finished in 0.010 seconds with exit status 0 (successful).
DEBUG [d465a1b0] Running /usr/bin/env if test ! -d /var/www/cap-deploy-test/repo; then echo "Directory does not exist '/var/www/cap-deploy-test/repo'" 1>&2; false; fi as deploy@cap-test-deploy
DEBUG [d465a1b0] Command: if test ! -d /var/www/cap-deploy-test/repo; then echo "Directory does not exist '/var/www/cap-deploy-test/repo'" 1>&2; false; fi
DEBUG [d465a1b0] Finished in 0.004 seconds with exit status 0 (successful).
DEBUG [6f35af2c] Running /usr/bin/env git rev-parse --short master as deploy@cap-test-deploy
DEBUG [6f35af2c] Command: cd /var/www/cap-deploy-test/repo && ( export GIT_ASKPASS="/bin/echo" GIT_SSH="/tmp/cap-deploy-test/git-ssh.sh" ; /usr/bin/env git rev-parse --short master )
DEBUG [6f35af2c]        34bafe6
DEBUG [6f35af2c] Finished in 0.006 seconds with exit status 0 (successful).
DEBUG [e00a6951] Running /usr/bin/env if test ! -d /var/www/cap-deploy-test/releases/20160109131938; then echo "Directory does not exist '/var/www/cap-deploy-test/releases/20160109131938'" 1>&2; false; fi as deploy@cap-test-deploy
DEBUG [e00a6951] Command: if test ! -d /var/www/cap-deploy-test/releases/20160109131938; then echo "Directory does not exist '/var/www/cap-deploy-test/releases/20160109131938'" 1>&2; false; fi
DEBUG [e00a6951] Finished in 0.004 seconds with exit status 0 (successful).
INFO [0f700b1b] Running /usr/bin/env echo "34bafe6" >> REVISION as deploy@cap-test-deploy
DEBUG [0f700b1b] Command: cd /var/www/cap-deploy-test/releases/20160109131938 && /usr/bin/env echo "34bafe6" >> REVISION
INFO [0f700b1b] Finished in 0.006 seconds with exit status 0 (successful).
INFO [774fb269] Running /usr/bin/env mkdir -pv /var/www/cap-deploy-test/releases/20160109131938/public as deploy@cap-test-deploy
DEBUG [774fb269] Command: /usr/bin/env mkdir -pv /var/www/cap-deploy-test/releases/20160109131938/public
INFO [774fb269] Finished in 0.007 seconds with exit status 0 (successful).
DEBUG [890c51fe] Running /usr/bin/env [ -L /var/www/cap-deploy-test/releases/20160109131938/public/assets ] as deploy@cap-test-deploy
DEBUG [890c51fe] Command: [ -L /var/www/cap-deploy-test/releases/20160109131938/public/assets ]
DEBUG [890c51fe] Finished in 0.010 seconds with exit status 1 (failed).
DEBUG [f56e012a] Running /usr/bin/env [ -d /var/www/cap-deploy-test/releases/20160109131938/public/assets ] as deploy@cap-test-deploy
DEBUG [f56e012a] Command: [ -d /var/www/cap-deploy-test/releases/20160109131938/public/assets ]
DEBUG [f56e012a] Finished in 0.005 seconds with exit status 1 (failed).
INFO [f117ab8e] Running /usr/bin/env ln -s /var/www/cap-deploy-test/shared/public/assets /var/www/cap-deploy-test/releases/20160109131938/public/assets as deploy@cap-test-deploy
DEBUG [f117ab8e] Command: /usr/bin/env ln -s /var/www/cap-deploy-test/shared/public/assets /var/www/cap-deploy-test/releases/20160109131938/public/assets
INFO [f117ab8e] Finished in 0.005 seconds with exit status 0 (successful).
DEBUG [a2b2f1da] Running /usr/bin/env if test ! -d /var/www/cap-deploy-test/releases/20160109131938; then echo "Directory does not exist '/var/www/cap-deploy-test/releases/20160109131938'" 1>&2; false; fi as deploy@cap-test-deploy
DEBUG [a2b2f1da] Command: if test ! -d /var/www/cap-deploy-test/releases/20160109131938; then echo "Directory does not exist '/var/www/cap-deploy-test/releases/20160109131938'" 1>&2; false; fi
DEBUG [a2b2f1da] Finished in 0.004 seconds with exit status 0 (successful).
INFO [1b974ccf] Running /var/www/cap-deploy-test/rvm1scripts/rvm-auto.sh 2.2.0 bundle install --path /var/www/cap-deploy-test/shared/bundle --without development test --deployment --quiet as deploy@cap-test-deploy
DEBUG [1b974ccf] Command: cd /var/www/cap-deploy-test/releases/20160109131938 && /var/www/cap-deploy-test/rvm1scripts/rvm-auto.sh 2.2.0 bundle install --path /var/www/cap-deploy-test/shared/bundle --without development test --deployment --quiet
DEBUG [1b974ccf]        /var/www/cap-deploy-test/rvm1scripts/rvm-auto.sh: 66: exec:
DEBUG [1b974ccf]        bundle: not found
DEBUG [1b974ccf]
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deploy@cap-test-deploy: bundle exit status: 127
bundle stdout: Nothing written
bundle stderr: /var/www/cap-deploy-test/rvm1scripts/rvm-auto.sh: 66: exec: bundle: not found

SSHKit::Command::Failed: bundle exit status: 127
bundle stdout: Nothing written
bundle stderr: /var/www/cap-deploy-test/rvm1scripts/rvm-auto.sh: 66: exec: bundle: not found
/Users/r_nishio/.rvm/gems/ruby-2.2.1@global/gems/capistrano-bundler-1.1.4/lib/capistrano/tasks/bundler.cap:35:in `block (5 levels) in <top (required)>'
/Users/r_nishio/.rvm/gems/ruby-2.2.1@global/gems/capistrano-bundler-1.1.4/lib/capistrano/tasks/bundler.cap:26:in `block (4 levels) in <top (required)>'
/Users/r_nishio/.rvm/gems/ruby-2.2.1@global/gems/capistrano-bundler-1.1.4/lib/capistrano/tasks/bundler.cap:25:in `block (3 levels) in <top (required)>'
Tasks: TOP => deploy:updated => bundler:install
(See full trace by running task with --trace)
The deploy has failed with an error: #<SSHKit::Runner::ExecuteError: Exception while executing as deploy@cap-test-deploy: bundle exit status: 127
bundle stdout: Nothing written
bundle stderr: /var/www/cap-deploy-test/rvm1scripts/rvm-auto.sh: 66: exec: bundle: not found
>
[r_nishio@rnmac] $  

今度は配置まで行ったがbundle installで失敗してます。

そもそも追加したユーザ(deploy)でrvmコマンドやbundle コマンドが失敗するため、使えるようにする

/etc/passwd で最後の/bin/bashを追加

deploy:x:1002:1003::/home/deploy:/bin/bash

/home/deploy/.bashrc を作成し、以下を記載

PATH=$PATH:/usr/local/rvm/bin # Add RVM to PATH for scripting
source /usr/local/rvm/scripts/rvm

これでdeployユーザでログインした際にrvmコマンドが使えるようになりました。

deploy@vagrant-ubuntu-trusty:~$ rvm list

rvm rubies

=* ruby-2.2.0 [ x86_64 ]
   ruby-2.2.1 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

deploy@vagrant-ubuntu-trusty:~$

rootユーザでbundlerをインストール

gem install bundler

さてこれで再度デプロイをやってみたところ

(中略)
INFO [14d4f381] Running /var/www/cap-deploy-test/rvm1scripts/rvm-auto.sh 2.2.0 bundle install --path /var/www/cap-deploy-test/shared/bundle --without development test --deployment --quiet as deploy@cap-test-deploy
DEBUG [14d4f381] Command: cd /var/www/cap-deploy-test/releases/20160110075210 && /var/www/cap-deploy-test/rvm1scripts/rvm-auto.sh 2.2.0 bundle install --path /var/www/cap-deploy-test/shared/bundle --without development test --deployment --quiet
DEBUG [14d4f381]
DEBUG [14d4f381]        Warning: PATH set to RVM ruby but GEM_HOME and/or GEM_PATH not set, see:
DEBUG [14d4f381]            https://github.com/wayneeseguin/rvm/issues/3212
DEBUG [14d4f381]
DEBUG [14d4f381]        Warning! PATH is not properly set up, $GEM_HOME is not set,
DEBUG [14d4f381]                 usually this is caused by shell initialization files - check them for 'PATH=...' entries,
DEBUG [14d4f381]                 it might also help to re-add RVM to your dotfiles: 'rvm get stable --auto-dotfiles',
DEBUG [14d4f381]                 to fix temporarily in this shell session run: 'rvm use ruby-2.2.0'.
INFO [14d4f381] Finished in 1.163 seconds with exit status 0 (successful).
INFO [1f3c917b] Running /usr/bin/env rm -rf /var/www/cap-deploy-test/current as deploy@cap-test-deploy
DEBUG [1f3c917b] Command: /usr/bin/env rm -rf /var/www/cap-deploy-test/current
INFO [1f3c917b] Finished in 0.107 seconds with exit status 0 (successful).
INFO [a8523231] Running /usr/bin/env ln -s /var/www/cap-deploy-test/releases/20160110075210 /var/www/cap-deploy-test/current as deploy@cap-test-deploy
DEBUG [a8523231] Command: /usr/bin/env ln -s /var/www/cap-deploy-test/releases/20160110075210 /var/www/cap-deploy-test/current
INFO [a8523231] Finished in 0.106 seconds with exit status 0 (successful).
DEBUG [0ffa2074] Running /usr/bin/env ls -x /var/www/cap-deploy-test/releases as deploy@cap-test-deploy
DEBUG [0ffa2074] Command: /usr/bin/env ls -x /var/www/cap-deploy-test/releases
DEBUG [0ffa2074]        20160109131551  20160109131938  20160109133730  20160109134137  20160109134448
20160109134617  20160109140913  20160109140944  20160109141054  20160110065833
20160110071328  20160110073411  20160110073959  20160110074617  20160110074753
20160110074855  20160110075210
DEBUG [0ffa2074] Finished in 0.104 seconds with exit status 0 (successful).
INFO Keeping 5 of 17 deployed releases on cap-test-deploy
INFO [a96f37c9] Running /usr/bin/env rm -rf /var/www/cap-deploy-test/releases/20160109131551 /var/www/cap-deploy-test/releases/20160109131938 /var/www/cap-deploy-test/releases/20160109133730 /var/www/cap-deploy-test/releases/20160109134137 /var/www/cap-deploy-test/releases/20160109134448 /var/www/cap-deploy-test/releases/20160109134617 /var/www/cap-deploy-test/releases/20160109140913 /var/www/cap-deploy-test/releases/20160109140944 /var/www/cap-deploy-test/releases/20160109141054 /var/www/cap-deploy-test/releases/20160110065833 /var/www/cap-deploy-test/releases/20160110071328 /var/www/cap-deploy-test/releases/20160110073411 as deploy@cap-test-deploy
DEBUG [a96f37c9] Command: /usr/bin/env rm -rf /var/www/cap-deploy-test/releases/20160109131551 /var/www/cap-deploy-test/releases/20160109131938 /var/www/cap-deploy-test/releases/20160109133730 /var/www/cap-deploy-test/releases/20160109134137 /var/www/cap-deploy-test/releases/20160109134448 /var/www/cap-deploy-test/releases/20160109134617 /var/www/cap-deploy-test/releases/20160109140913 /var/www/cap-deploy-test/releases/20160109140944 /var/www/cap-deploy-test/releases/20160109141054 /var/www/cap-deploy-test/releases/20160110065833 /var/www/cap-deploy-test/releases/20160110071328 /var/www/cap-deploy-test/releases/20160110073411
INFO [a96f37c9] Finished in 0.149 seconds with exit status 0 (successful).
DEBUG [655dd4a4] Running /usr/bin/env if test ! -d /var/www/cap-deploy-test/releases; then echo "Directory does not exist '/var/www/cap-deploy-test/releases'" 1>&2; false; fi as deploy@cap-test-deploy
DEBUG [655dd4a4] Command: if test ! -d /var/www/cap-deploy-test/releases; then echo "Directory does not exist '/var/www/cap-deploy-test/releases'" 1>&2; false; fi
DEBUG [655dd4a4] Finished in 0.103 seconds with exit status 0 (successful).
INFO [994ac6ff] Running /usr/bin/env echo "Branch master (at 90e6d0b) deployed as release 20160110075210 by r_nishio" >> /var/www/cap-deploy-test/revisions.log as deploy@cap-test-deploy
DEBUG [994ac6ff] Command: echo "Branch master (at 90e6d0b) deployed as release 20160110075210 by r_nishio" >> /var/www/cap-deploy-test/revisions.log
INFO [994ac6ff] Finished in 0.110 seconds with exit status 0 (successful).
[r_nishio@rnmac] $  

途中エラーやWARNが出ているところがありますがとりあえず配置できました。

サーバー側もちゃんと currentリンクが作成されています。


deploy@vagrant-ubuntu-trusty:/var/www/cap-deploy-test$ pwd
/var/www/cap-deploy-test
deploy@vagrant-ubuntu-trusty:/var/www/cap-deploy-test$ ls -la
total 28
drwxrwxr-x 6 deploy deploy 4096  1月  9 16:04 .
drwxrwxrwx 3 root   root   4096  1月  9 10:44 ..
lrwxrwxrwx 1 deploy deploy   48  1月  9 16:04 current -> /var/www/cap-deploy-test/releases/20160110075210
drwxrwxr-x 7 deploy deploy 4096  1月  9 16:04 releases
drwxrwxr-x 7 deploy deploy 4096  1月  9 13:15 repo
-rw-rw-r-- 1 deploy deploy   74  1月  9 16:04 revisions.log
drwxrwxr-x 2 deploy deploy 4096  1月  9 15:28 rvm1scripts
drwxrwxr-x 4 deploy deploy 4096  1月  9 15:46 shared
deploy@vagrant-ubuntu-trusty:/var/www/cap-deploy-test$

試しに bundle exec rails server をサーバー側で実行。


deploy@vagrant-ubuntu-trusty:/var/www/cap-deploy-test/current$ bundle exec rails server
/usr/local/rvm/gems/ruby-2.2.0/gems/bundler-1.11.2/lib/bundler/shared_helpers.rb:78: warning: Insecure world writable dir /var/www in PATH, mode 040777
/usr/local/rvm/gems/ruby-2.2.0/gems/bundler-1.11.2/lib/bundler/shared_helpers.rb:78: warning: Insecure world writable dir /var/www in PATH, mode 040777
=> Booting WEBrick
=> Rails 4.2.4 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-01-09 16:10:25] INFO  WEBrick 1.3.1
[2016-01-09 16:10:25] INFO  ruby 2.2.0 (2014-12-25) [x86_64-linux]
[2016-01-09 16:10:25] INFO  WEBrick::HTTPServer#start: pid=23682 port=3000

エラーなく動作しました。

今回は、単純に配置までを実施しましたが、途中何度もエラーに遭遇しました。
また、途中配置はできてるが、途中 失敗?(failedと表示されている部分)している部分やWARNINGが表示されている部分もあるため、その部分も後々調べていこうと思います。

参考

Capistrano 3系でRails4.1のデプロイ[rbenv][rvm][ruby2.1] | 酒と泪とRubyとRailsと

Ruby on Rails環境構築ガイド | 黒田 努 (著)

コメントはまだありません

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

AWS
Docker
ECS+ALBの動的ポートマッピングでダウンタイムのないデプロイを試してみた

はじめに 少し前に個人で作成しているWebサービスのインフラにDockerを使い始めました。 複数台 …

スクリーンショット 2016-08-16 0.43.10
AngularJS
クライアントAngularJS サーバーサイドRails5 におけるOmniauth 認証を試してみる

去年にEOPESを公開してから1年半。 初めての外部公開サービスだったが、ソースは結構ごり押し部分も …

MarketTreeViewのイメージ
Ruby on Rails
[Eve Online 3rd party app] Market Tree Viewの実装 [EOPES制作記]

Eve Online 3rd party app のEOPES を作り始めてから約3ヶ月が経ちました …