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).
エラー無く完了したが、ログが大量で確認しにくい!