IT井戸端会議

IT井戸端会議

インフラ、ネットワーク、アプリケーション開発、IT界隈の話等々を東京都千代田区界隈から発信します。

Amazon Linux に Rails 環境を構築する

使用環境

Amazon Linux AMI release 2015.03 (x86_64)

手順

rbenv インストール

ruby のバージョン切り替え可能なツールであるrbenvをインストールします。

まず、git をインストールします。

$ sudo yum install git

rbenv をgit cloneで取得し、パスを通します。

$ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

初期化スクリプトを追加します。

$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

設定を読み込み、rbenvの動作を確認します。

$ source ~/.bash_profile 
$ rbenv -v

ruby-build インストール

ruby をインストールするために、ruby-buildをインストールします。

$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

Ruby インストール

まずは、インストール可能な Ruby のバージョンを確認します。

$ ruby -l
…
  2.2.0-dev
  2.2.0-preview1
  2.2.0-preview2
  2.2.0-rc1
  2.2.0
  2.2.1
  2.2.2
  2.3.0-dev
…

rbenv installgccopensslが必要となるため、インストールします。

$ sudo yum install gcc openssl-devel

今回は、「2.2.2」をインストールします。

$ rbenv install 2.2.2

$ rbenv versions
* system (set by /home/ec2-user/.rbenv/version)
  2.2.2

バージョンを2.2.2に切り替えます。

$ rbenv global 2.2.2
$ rbenv rehash

$ rbenv versions
  system
* 2.2.2 (set by /home/ec2-user/.rbenv/version)

Ruby on Rails インストール

まず、必要となるパッケージをインストールします。

$ sudo yum install ruby-devel install libxml2 libxslt libxml2-devel libxslt-devel
$ gem install nokogiri -- --use-system-libraries

gemを使用してRubyライブラリから、Railsパッケージをインストールします。

$ gem install rails

インストールされたことを確認します。

$ rails -v
Rails 4.2.3

サンプルアプリの実行

サンプルアプリ作成の際、必要となるものをもろもろインストール

$ sudo yum install sqlite-devel
$ sudo yum install nodejs npm --enablerepo=epel

ホームディレクトリ配下にサンプルアプリを作成します。

$ cd ~/
$ rails new sample

Gemfile に追記し、bundle installする。

$ cd ~/sample

$ vi Gemfile
@@ -23,6 +23,10 @@
 # bundle exec rake doc:rails generates the API under doc/api.
 gem 'sdoc', '~> 0.4.0', group: :doc
 
+gem 'sqlite3'
+gem 'io-console'
+gem 'rb-readline'
+
 # Use ActiveModel has_secure_password
 # gem 'bcrypt', '~> 3.1.7'

$ bundle install

サーバを起動します。

$ rails s -b 0.0.0.0

Webブラウザで「http://IPアドレス:3000」にアクセスし、表示されることを確認します。 f:id:candapc:20160118021204p:plain

参考サイト

【Rails初心者の方へ】CentOSへRuby on Railsをインストールしてみました | KEN10.COM

capistranoをほんのちょっと動かしてみる⑧(chef-soloのセットアップ)

capistranoとシェルを使ってchef-soloと前提条件にあるrubyをインストールする。

capistranoとしての流れとしては下記の通り。

①タスク実行 ②変数設定 ③rubyソースをダウンロード ※run_locallyに記載した内容はcapistranoを実行したサーバにて実行される ④roleがclient(./config/deploy/develop.rbに定義)に対して実行 ⑤ダウンロードしたrubyソースを各サーバにアップロード ⑥chef(rubyを含む)のインストールシェルを各サーバにアップロード ⑦各サーバにてそれぞれシェルを実行

前提条件

chef-soloのインストール先サーバにssh接続が出来ること。

使用したファイルとディレクトリは下記の通り。

|-- Capfile
|-- config
|   |-- deploy
|   |   |-- chef.rb
|   |   `-- develop.rb
|   `-- deploy.rb
|-- install_chef.sh
`-- lib
    `-- capistrano
        `-- tasks

[cap install]から変更しているファイルは下記の通り。 1.[./config/deploy/production.rb]は削除 2.[./config/deploy/staging.rb]は削除 3.[./config/deploy/chef.rb]は新規作成 4.[./config/deploy/develop.rb]は新規作成 5.[./config/install_chef.sh]は新規作成(capistranoから実行するシェル)

3.[./config/deploy/chef.rb]は下記の通り。

load './config/deploy/develop.rb'

namespace :chef do
        task :install do
                src_dir = "/root/captochef/chef-init"
                src_fil = "#{src_dir}/ruby-2.2.2.tar.gz"
                dst_dir = "/root"
                shl_dir = "/root/captochef/chef-init"
                shl_fil = "#{shl_dir}/install_chef.sh"
                dst_fil = "#{dst_dir}/install_chef.sh"
                url_ruby = "http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.2.tar.gz"
                run_locally do
                        execute "wget #{url_ruby}"
                end
                on roles(:client) do
                        upload! src_fil, dst_dir, :recursive => true
                        upload! shl_fil, dst_dir, :recursive => true
                        execute "sh #{dst_fil}"
                end
        end
end

4.[./config/deploy/develop.rb]は下記の通り。 ※今回は2台に対し実行し、事前に鍵を登録しているためssh_optionsにはユーザ名のみ指定

role :client, %w{chefsolo01 chefsolo02}

set :ssh_options, {
        user: 'root',
}

5.[./config/install_chef.sh]は下記の通り。 ※とりあえず動けばいいのでシェル内容は適当です。

#!/bin/sh

RET=0           # リターンコード
SRC_DIR="/root"
SRC_FIL=
COM_DIR=

YUM="wget zlib-devel gcc openssl-devel"

for i in ${YUM}; do
        rpm -qa | grep ^${i} > /dev/null 2>&1
        RET=$?
        if [ ${RET} -ne 0 ]; then
                CMD="yum -y install ${i}"
                echo "exec ${CMD}"
                ${CMD}
        fi
done


# rubyファイル設定
if [ ! -f ${SRC_DIR}/ruby*gz ]; then
        echo "ruby file not found"
        exit 8
else
        SRC_FIL=`ls -1 ${SRC_DIR}/ruby*gz`
        COM_DIR=`echo ${SRC_FIL} | sed -e "s/.\{7\}$//"`
fi

tar xf ${SRC_FIL}
cd ${COM_DIR}
./configure --disable-doc
make
make install

gem contents chef > /dev/null 2>&1
RET=$?
if [ ${RET} -eq 1 ]; then
        gem install chef
fi

exit 0

実行してみる。

$ cap chef chef:install
INFO [4db9abad] Running /usr/bin/env wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.2.tar.gz as root@localhost
DEBUG [4db9abad] Command: wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.2.tar.gz
DEBUG [4db9abad]        --2015-07-09 06:18:40--  http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.2.tar.gz
DEBUG [4db9abad]        Resolving cache.ruby-lang.org... 103.245.222.68
DEBUG [4db9abad]        Connecting to cache.ruby-lang.org|103.245.222.68|:80... connected.
DEBUG [4db9abad]        HTTP request sent, awaiting response... 200 OK
DEBUG [4db9abad]        Length: 16613636 (16M) [application/octet-stream]
DEBUG [4db9abad]        Saving to: “ruby-2.2.2.tar.gz”
~~~~~~ 省略 ~~~~~~~
 ffi-yajl, uuidtools, hashie, mixlib-log, chef-zero, erubis, highline, net-ssh-gateway, net-ssh-multi, ffi, wmi-lite, ipaddress, mixlib-shellout, mixlib-config, mixlib-cli, systemu, mime-types, ohai, mixlib-authentication, chef-config, chef after 68 seconds
DEBUG [05623702]        45 gems installed
INFO [05623702] Finished in 520.648 seconds with exit status 0 (successful).

エラー無く完了したが、ログが大量で確認しにくい!

capistranoをほんのちょっと動かしてみる⑦(タスクからタスクを実行)

実行中のタスクから別のタスクを実行する。 実施することはchefuserというユーザを作成しPasswordというパスワードを設定する。 そのあとに鍵を追加する。

使用したファイルとディレクトリは下記の通り。

$ tree --charset=C
|-- Capfile
|-- config
|   |-- deploy
|   |   |-- develop.rb
|   |   `-- os.rb
|   `-- deploy.rb
`-- lib
    `-- capistrano
        `-- tasks

[cap install]から変更しているファイルは下記の通り。 1.[./config/deploy/production.rb]は削除 2.[./config/deploy/staging.rb]は削除 3.[./config/deploy/deploy.rb]は新規作成 4.[./config/deploy/os.rb]は新規作成

3.[./config/deploy/deploy.rb]の中身。

role :client, %w{capistrano}

set :ssh_options, {
        user: 'root',
        password: '****'
}

4.[./config/deploy/os.rb]の中身。 ※[invoke "os:key_set"]にて別タスクを実行。

load './config/deploy/develop.rb'

namespace :os do
        # ユーザ名指定
        user = 'chefuser'

        # パスワード指定
        password = 'password'

        desc "chef install"
        task :useradd do
                # 乱数作成
                salt = rand( 36**8 ).to_s( 36 )

                # 乱数を元に暗号化
                shadow_hash = password.crypt( salt )
                on roles( :client ) do

                        # ユーザ追加
                        execute "useradd -p '#{shadow_hash}' #{user}"

                        # namespaceが[os]のtaskが[key_set]を実行
                        invoke "os:key_set"
                end
        end

        task :key_set do
                src = '/opt/capistrano/manage/files/root/.ssh/authorized_keys'
                dir = '/root/.ssh'
                dst = '/root/.ssh/tmp_keys'
                on roles(:client) do
                        execute "test -d #{dir} && mkdir -p #{dir}"
                        upload! src, dst, :recursive => true
                        execute "cat #{dst} >> #{dir}/authorized_keys"
                        execute "rm -f #{dst}"
                end
        end
end

実行してみる。

$ cap os os:useradd
INFO [14617d6a] Running /usr/bin/env useradd -p 'tcA7gikJhAx6U' chefuser on capistrano
DEBUG [14617d6a] Command: useradd -p 'tcA7gikJhAx6U' chefuser
INFO [14617d6a] Finished in 0.601 seconds with exit status 0 (successful).
INFO [9d403af2] Running /usr/bin/env test -d /root/.ssh && mkdir -p /root/.ssh on capistrano
DEBUG [9d403af2] Command: test -d /root/.ssh && mkdir -p /root/.ssh
INFO [9d403af2] Finished in 0.020 seconds with exit status 0 (successful).
DEBUG Uploading /opt/capistrano/manage/files/root/.ssh/authorized_keys 0.0%
INFO Uploading /opt/capistrano/manage/files/root/.ssh/authorized_keys 100.0%
INFO [9a24584d] Running /usr/bin/env cat /root/.ssh/tmp_keys >> /root/.ssh/authorized_keys on capistrano
DEBUG [9a24584d] Command: cat /root/.ssh/tmp_keys >> /root/.ssh/authorized_keys
INFO [9a24584d] Finished in 0.018 seconds with exit status 0 (successful).
INFO [0136eab2] Running /usr/bin/env rm -f /root/.ssh/tmp_keys on capistrano
DEBUG [0136eab2] Command: rm -f /root/.ssh/tmp_keys
INFO [0136eab2] Finished in 0.015 seconds with exit status 0 (successful).

4行目でuseraddのタスクが終了し5行目からkey_setのタスク内容が実行されていることを確認できればOK。

capistranoをほんのちょっと動かしてみる⑤(ssh鍵接続設定)

sshの鍵を作成して鍵を転送してみる。 ※のちのちパスワード認証から鍵認証に変更するためのテストも兼ねて

事前作業

鍵をrsaで作成

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:
5e:2c:65:f3:30:42:da:f5:86:38:ae:2e:9e:76:2f:b5 root@capistrano.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
+-----------------+

鍵を適当な場所にコピー

$ mkdir -p /opt/capistrano/manage/files/root/.ssh
$ cat /root/.ssh/id_rsa.pub > /opt/capistrano/manage/files/root/.ssh/authorized_keys
$ ls -l /opt/capistrano/manage/files/root/.ssh/authorized_keys
-rw-r--r--. 1 root root 409 Jul  7 05:58 /opt/capistrano/manage/files/root/.ssh/authorized_keys

新規するファイルは下記。 ./config/develop/ssh.rb

# ./config/deploy/manage.rbと同様に./config/servers.rbを読み込む
load './config/servers.rb'
        task :up_key do

                # アップロードディレクトリ等を設定
                src =  '/opt/capistrano/manage/files/root/.ssh/authorized_keys'
                dir =  '/root/.ssh'
                dst =  '/root/.ssh/test_keys'

                on roles( :client ) do

                        # ディレクトリが存在しなかった場合、作成
                        execute "test -d #{dir} && mkdir -p #{dir}"

                        # アップロード(recursiveは再帰的に処理をするためだが今回は無くてもOK)
                        upload! src, dst, :recursive => true

                        # 一時ファイルからauthorized_keysに追記
                        execute "cat #{dst} >> #{dir}/authorized_keys"

                        #一時ファイルの削除
                        execute "rm -f #{dst}"
                end
        end
end

とりあえず1台だけ(openvz)実行したいので下記を修正。 ※これ以外は(4)のファイルそのまま ./config/servers.rb

SSHKit::Backend::Netssh.configure do |ssh|
  ssh.ssh_options = {
    :user => 'root',
#    :passphrase => 'passphrase',
    :password => 'test',
  }
end

role :client, %w{openvz}

実行してみる。

$ cap ssh exec:up_key
INFO [4990c760] Running /usr/bin/env test -d /root/.ssh && mkdir -p /root/.ssh on openvz
DEBUG [4990c760] Command: test -d /root/.ssh && mkdir -p /root/.ssh
INFO [4990c760] Finished in 0.135 seconds with exit status 0 (successful).
DEBUG Uploading /opt/capistrano/manage/files/root/.ssh/authorized_keys 0.0%
INFO Uploading /opt/capistrano/manage/files/root/.ssh/authorized_keys 100.0%
INFO [745f0512] Running /usr/bin/env cat /root/.ssh/test_keys >> /root/.ssh/authorized_keys on openvz
DEBUG [745f0512] Command: cat /root/.ssh/test_keys >> /root/.ssh/authorized_keys
INFO [745f0512] Finished in 0.019 seconds with exit status 0 (successful).
INFO [f8328a0c] Running /usr/bin/env rm -f /root/.ssh/test_keys on openvz
DEBUG [f8328a0c] Command: rm -f /root/.ssh/test_keys
INFO [f8328a0c] Finished in 0.019 seconds with exit status 0 (successful).

できたっぽいのでsshで接続する。

$ ssh root@openvz
Enter passphrase for key '/root/.ssh/id_rsa':
Last login: Tue Jul  7 07:56:34 2015 from 172.31.13.92
$ hostname
openvz

パスワードではなくパスフレーズになったので出来た気がする。

capistranoをほんのちょっと動かしてみる④(namespace)

(3)ではひとつのタスクに複数の処理を記載した。 今回はnamespaceを使って処理を分割する。 namespaceはタスクが増えたときに固めて定義できる空間名みたい。

変更するファイルは下記。 ./config/develop/manage.rb

load './config/servers.rb'

namespace :exec do
        dir = "/root/test"

        desc "test"
        task :mkdir do

                on roles( :client ) do
                        execute "mkdir -p #{dir}"
                end
        end

        task :ls do
                on roles( :client ) do
                        execute "ls -ld #{dir}"
                end
        end
end

mkdirのタスクを実行してみる。(cap 環境 空間名:タスク名)

$ cap manage exec:mkdir
INFO [70b07f74] Running /usr/bin/env mkdir -p /root/test on capistrano
DEBUG [70b07f74] Command: mkdir -p /root/test
INFO [3cde3965] Running /usr/bin/env mkdir -p /root/test on openvz
DEBUG [3cde3965] Command: mkdir -p /root/test
INFO [e8e2ccd0] Running /usr/bin/env mkdir -p /root/test on chef
DEBUG [e8e2ccd0] Command: mkdir -p /root/test
INFO [3cde3965] Finished in 0.150 seconds with exit status 0 (successful).
INFO [e8e2ccd0] Finished in 0.335 seconds with exit status 0 (successful).
INFO [70b07f74] Finished in 0.386 seconds with exit status 0 (successful).

lsのタスクを実行してみる。(cap 環境 空間名:タスク名)

$ cap manage exec:ls
INFO [36d6b1ec] Running /usr/bin/env ls -ld /root/test on capistrano
INFO [d3719a31] Running /usr/bin/env ls -ld /root/test on openvz
DEBUG [d3719a31] Command: ls -ld /root/test
INFO [72a2e639] Running /usr/bin/env ls -ld /root/test on chef
DEBUG [72a2e639] Command: ls -ld /root/test
DEBUG [36d6b1ec] Command: ls -ld /root/test
DEBUG [d3719a31]        drwxr-xr-x 2 root root 4096 Jul  7 02:15 /root/test
INFO [d3719a31] Finished in 0.160 seconds with exit status 0 (successful).
DEBUG [36d6b1ec]        drwxr-xr-x. 2 root root 4096 Jul  7 02:39 /root/test
INFO [36d6b1ec] Finished in 0.384 seconds with exit status 0 (successful).
DEBUG [72a2e639]        drwxr-xr-x. 2 root root 4096 Jul  7 01:36 /root/test
INFO [72a2e639] Finished in 0.390 seconds with exit status 0 (successful).

capistranoをほんのちょっと動かしてみる③の補足

(3)で同時実行数の制限やインターバルを設定したがその検証を少し行う。

テストパターンは下記のとおり。

on roles( :client ), in: :groups, limit: 3, wait: 0 do
on roles( :client ), in: :groups, limit: 2, wait: 0 do
on roles( :client ), in: :groups, limit: 1, wait: 0 do
on roles( :client ), in: :groups, limit: 3, wait: 5 do
on roles( :client ), in: :groups, limit: 2, wait: 5 do
on roles( :client ), in: :groups, limit: 1, wait: 5 do

1.on roles( :client ), in: :groups, limit: 3, wait: 0 do

$ time cap manage mkdir
INFO [6633a717] Running /usr/bin/env mkdir -p /root/test on capistrano
DEBUG [6633a717] Command: mkdir -p /root/test
INFO [456fad8a] Running /usr/bin/env mkdir -p /root/test on openvz
DEBUG [456fad8a] Command: mkdir -p /root/test
INFO [32c08bf7] Running /usr/bin/env mkdir -p /root/test on chef
DEBUG [32c08bf7] Command: mkdir -p /root/test
INFO [456fad8a] Finished in 0.164 seconds with exit status 0 (successful).
INFO [6a273110] Running /usr/bin/env ls -ld /root/test on openvz
DEBUG [6a273110] Command: ls -ld /root/test
DEBUG [6a273110]        drwxr-xr-x 2 root root 4096 Jul  7 02:15 /root/test
INFO [6a273110] Finished in 0.022 seconds with exit status 0 (successful).
INFO [32c08bf7] Finished in 0.357 seconds with exit status 0 (successful).
INFO [134c9f95] Running /usr/bin/env ls -ld /root/test on chef
DEBUG [134c9f95] Command: ls -ld /root/test
DEBUG [134c9f95]        drwxr-xr-x. 2 root root 4096 Jul  7 01:36 /root/test
INFO [134c9f95] Finished in 0.037 seconds with exit status 0 (successful).
INFO [6633a717] Finished in 0.421 seconds with exit status 0 (successful).
INFO [6c913a3c] Running /usr/bin/env ls -ld /root/test on capistrano
DEBUG [6c913a3c] Command: ls -ld /root/test
DEBUG [6c913a3c]        drwxr-xr-x. 2 root root 4096 Jul  7 02:39 /root/test
INFO [6c913a3c] Finished in 0.029 seconds with exit status 0 (successful).

real    0m0.889s

2.on roles( :client ), in: :groups, limit: 2, wait: 0 do

$ time cap manage mkdir
INFO [1643bc54] Running /usr/bin/env mkdir -p /root/test on openvz
DEBUG [1643bc54] Command: mkdir -p /root/test
INFO [21c20ee0] Running /usr/bin/env mkdir -p /root/test on chef
DEBUG [21c20ee0] Command: mkdir -p /root/test
INFO [1643bc54] Finished in 0.139 seconds with exit status 0 (successful).
INFO [6d42baf4] Running /usr/bin/env ls -ld /root/test on openvz
DEBUG [6d42baf4] Command: ls -ld /root/test
DEBUG [6d42baf4]        drwxr-xr-x 2 root root 4096 Jul  7 02:15 /root/test
INFO [6d42baf4] Finished in 0.021 seconds with exit status 0 (successful).
INFO [21c20ee0] Finished in 0.339 seconds with exit status 0 (successful).
INFO [819368aa] Running /usr/bin/env ls -ld /root/test on chef
DEBUG [819368aa] Command: ls -ld /root/test
DEBUG [819368aa]        drwxr-xr-x. 2 root root 4096 Jul  7 01:36 /root/test
INFO [819368aa] Finished in 0.031 seconds with exit status 0 (successful).
INFO [48d17524] Running /usr/bin/env mkdir -p /root/test on capistrano
DEBUG [48d17524] Command: mkdir -p /root/test
INFO [48d17524] Finished in 0.363 seconds with exit status 0 (successful).
INFO [09829b1c] Running /usr/bin/env ls -ld /root/test on capistrano
DEBUG [09829b1c] Command: ls -ld /root/test
DEBUG [09829b1c]        drwxr-xr-x. 2 root root 4096 Jul  7 02:39 /root/test
INFO [09829b1c] Finished in 0.029 seconds with exit status 0 (successful).

real    0m1.220s

3.on roles( :client ), in: :groups, limit: 1, wait: 0 do

$ time cap manage mkdir
INFO [4581eae2] Running /usr/bin/env mkdir -p /root/test on chef
DEBUG [4581eae2] Command: mkdir -p /root/test
INFO [4581eae2] Finished in 0.338 seconds with exit status 0 (successful).
INFO [382747e8] Running /usr/bin/env ls -ld /root/test on chef
DEBUG [382747e8] Command: ls -ld /root/test
DEBUG [382747e8]        drwxr-xr-x. 2 root root 4096 Jul  7 01:36 /root/test
INFO [382747e8] Finished in 0.032 seconds with exit status 0 (successful).
INFO [b6ad8fcf] Running /usr/bin/env mkdir -p /root/test on openvz
DEBUG [b6ad8fcf] Command: mkdir -p /root/test
INFO [b6ad8fcf] Finished in 0.120 seconds with exit status 0 (successful).
INFO [1e56a4ed] Running /usr/bin/env ls -ld /root/test on openvz
DEBUG [1e56a4ed] Command: ls -ld /root/test
DEBUG [1e56a4ed]        drwxr-xr-x 2 root root 4096 Jul  7 02:15 /root/test
INFO [1e56a4ed] Finished in 0.020 seconds with exit status 0 (successful).
INFO [2d16a360] Running /usr/bin/env mkdir -p /root/test on capistrano
DEBUG [2d16a360] Command: mkdir -p /root/test
INFO [2d16a360] Finished in 0.373 seconds with exit status 0 (successful).
INFO [ef492cb0] Running /usr/bin/env ls -ld /root/test on capistrano
DEBUG [ef492cb0] Command: ls -ld /root/test
DEBUG [ef492cb0]        drwxr-xr-x. 2 root root 4096 Jul  7 02:39 /root/test
INFO [ef492cb0] Finished in 0.030 seconds with exit status 0 (successful).

real    0m1.389s

4.on roles( :client ), in: :groups, limit: 3, wait: 5 do

$ time cap manage mkdir
INFO [73bcebca] Running /usr/bin/env mkdir -p /root/test on openvz
DEBUG [73bcebca] Command: mkdir -p /root/test
INFO [d98f1ab5] Running /usr/bin/env mkdir -p /root/test on chef
DEBUG [d98f1ab5] Command: mkdir -p /root/test
INFO [d72f8be1] Running /usr/bin/env mkdir -p /root/test on capistrano
DEBUG [d72f8be1] Command: mkdir -p /root/test
INFO [73bcebca] Finished in 0.171 seconds with exit status 0 (successful).
INFO [568f8e8e] Running /usr/bin/env ls -ld /root/test on openvz
DEBUG [568f8e8e] Command: ls -ld /root/test
DEBUG [568f8e8e]        drwxr-xr-x 2 root root 4096 Jul  7 02:15 /root/test
INFO [568f8e8e] Finished in 0.030 seconds with exit status 0 (successful).
INFO [d98f1ab5] Finished in 0.383 seconds with exit status 0 (successful).
INFO [17e14a69] Running /usr/bin/env ls -ld /root/test on chef
DEBUG [17e14a69] Command: ls -ld /root/test
INFO [d72f8be1] Finished in 0.402 seconds with exit status 0 (successful).
INFO [7ba48d5f] Running /usr/bin/env ls -ld /root/test on capistrano
DEBUG [7ba48d5f] Command: ls -ld /root/test
DEBUG [17e14a69]        drwxr-xr-x. 2 root root 4096 Jul  7 01:36 /root/test
INFO [17e14a69] Finished in 0.034 seconds with exit status 0 (successful).
DEBUG [7ba48d5f]        drwxr-xr-x. 2 root root 4096 Jul  7 02:39 /root/test
INFO [7ba48d5f] Finished in 0.038 seconds with exit status 0 (successful).

real    0m5.913s

5.on roles( :client ), in: :groups, limit: 2, wait: 5 do

$ time cap manage mkdir
INFO [75daad98] Running /usr/bin/env mkdir -p /root/test on openvz
INFO [55e107dd] Running /usr/bin/env mkdir -p /root/test on chef
DEBUG [55e107dd] Command: mkdir -p /root/test
DEBUG [75daad98] Command: mkdir -p /root/test
INFO [75daad98] Finished in 0.136 seconds with exit status 0 (successful).
INFO [26c6b155] Running /usr/bin/env ls -ld /root/test on openvz
DEBUG [26c6b155] Command: ls -ld /root/test
DEBUG [26c6b155]        drwxr-xr-x 2 root root 4096 Jul  7 02:15 /root/test
INFO [26c6b155] Finished in 0.020 seconds with exit status 0 (successful).
INFO [55e107dd] Finished in 0.380 seconds with exit status 0 (successful).
INFO [d10d6bdc] Running /usr/bin/env ls -ld /root/test on chef
DEBUG [d10d6bdc] Command: ls -ld /root/test
DEBUG [d10d6bdc]        drwxr-xr-x. 2 root root 4096 Jul  7 01:36 /root/test
INFO [d10d6bdc] Finished in 0.036 seconds with exit status 0 (successful).
INFO [291bdb27] Running /usr/bin/env mkdir -p /root/test on capistrano
DEBUG [291bdb27] Command: mkdir -p /root/test
INFO [291bdb27] Finished in 0.358 seconds with exit status 0 (successful).
INFO [11d1580f] Running /usr/bin/env ls -ld /root/test on capistrano
DEBUG [11d1580f] Command: ls -ld /root/test
DEBUG [11d1580f]        drwxr-xr-x. 2 root root 4096 Jul  7 02:39 /root/test
INFO [11d1580f] Finished in 0.031 seconds with exit status 0 (successful).

real    0m11.259s

6.on roles( :client ), in: :groups, limit: 1, wait: 0 do

$ time cap manage mkdir
INFO [4b80f071] Running /usr/bin/env mkdir -p /root/test on chef
DEBUG [4b80f071] Command: mkdir -p /root/test
INFO [4b80f071] Finished in 0.337 seconds with exit status 0 (successful).
INFO [ecb0ac53] Running /usr/bin/env ls -ld /root/test on chef
DEBUG [ecb0ac53] Command: ls -ld /root/test
DEBUG [ecb0ac53]        drwxr-xr-x. 2 root root 4096 Jul  7 01:36 /root/test
INFO [ecb0ac53] Finished in 0.032 seconds with exit status 0 (successful).
INFO [1dd44c9b] Running /usr/bin/env mkdir -p /root/test on openvz
DEBUG [1dd44c9b] Command: mkdir -p /root/test
INFO [1dd44c9b] Finished in 0.116 seconds with exit status 0 (successful).
INFO [4082cebe] Running /usr/bin/env ls -ld /root/test on openvz
DEBUG [4082cebe] Command: ls -ld /root/test
DEBUG [4082cebe]        drwxr-xr-x 2 root root 4096 Jul  7 02:15 /root/test
INFO [4082cebe] Finished in 0.020 seconds with exit status 0 (successful).
INFO [159e9397] Running /usr/bin/env mkdir -p /root/test on capistrano
DEBUG [159e9397] Command: mkdir -p /root/test
INFO [159e9397] Finished in 0.345 seconds with exit status 0 (successful).
INFO [86776b77] Running /usr/bin/env ls -ld /root/test on capistrano
DEBUG [86776b77] Command: ls -ld /root/test
DEBUG [86776b77]        drwxr-xr-x. 2 root root 4096 Jul  7 02:39 /root/test
INFO [86776b77] Finished in 0.029 seconds with exit status 0 (successful).

real    0m16.333s

結論としては3台程度では検証できない。 waitを0にすると順実行され、 waitは処理終了後に必ず実行される。

capistranoをほんのちょっと動かしてみる③(並列制御)

(2)では複数のサーバに対し並列で実行した。 今回は並列実行数を制限して行う。

(2)から変更するファイルは下記の2ファイル。 ./config/deploy/manage.rb ./config/servers.rb    

./config/deploy/manage.rbの中身。

load './config/servers.rb'

task :mkdir do
  dir = '/root/test'

  # 同時実行数を2サーバとし、インターバルを3秒とする
  on roles( :client ), in: :groups, limit: 2, wait: 3 do
    execute "mkdir -p #{dir}"
    execute "ls -ld #{dir}"
  end
end

./config/servers.rbの中身 ※roleに定義しているサーバを1個追加しただけ。

SSHKit::Backend::Netssh.configure do |ssh|
  ssh.ssh_options = {
    :user => 'root',
    :password => '****',
  }
end
# ホスト名がcapistranoのサーバを追加
role :client, %w{chef openvz capistrano}

実行してみる。 ※timeコマンドは実行時間計測コマンド

$ time cap manage mkdir
INFO [0c298885] Running /usr/bin/env mkdir -p /root/test on openvz
INFO [64711761] Running /usr/bin/env mkdir -p /root/test on chef
DEBUG [64711761] Command: mkdir -p /root/test
DEBUG [0c298885] Command: mkdir -p /root/test
INFO [0c298885] Finished in 0.156 seconds with exit status 0 (successful).
INFO [844bf243] Running /usr/bin/env ls -ld /root/test on openvz
DEBUG [844bf243] Command: ls -ld /root/test
DEBUG [844bf243]        drwxr-xr-x 2 root root 4096 Jul  7 02:15 /root/test
INFO [844bf243] Finished in 0.030 seconds with exit status 0 (successful).
INFO [64711761] Finished in 0.372 seconds with exit status 0 (successful).
INFO [cf1ad737] Running /usr/bin/env ls -ld /root/test on chef
DEBUG [cf1ad737] Command: ls -ld /root/test
DEBUG [cf1ad737]        drwxr-xr-x. 2 root root 4096 Jul  7 01:36 /root/test
INFO [cf1ad737] Finished in 0.032 seconds with exit status 0 (successful).
INFO [1d2d7b9d] Running /usr/bin/env mkdir -p /root/test on capistrano
DEBUG [1d2d7b9d] Command: mkdir -p /root/test
INFO [1d2d7b9d] Finished in 0.383 seconds with exit status 0 (successful).
INFO [c4032d51] Running /usr/bin/env ls -ld /root/test on capistrano
DEBUG [c4032d51] Command: ls -ld /root/test
DEBUG [c4032d51]        drwxr-xr-x. 2 root root 4096 Jul  7 02:39 /root/test
INFO [c4032d51] Finished in 0.033 seconds with exit status 0 (successful).

real    0m7.288s
user    0m0.424s
sys     0m0.132s

実行時間は約7.3秒。 waitが実行されたのは下記の2回(のように感じた) ・2サーバ実行後 ・残りの1サーバ実行後