2016年2月26日金曜日

itamaeでrbenv入れるのにはまった恥ずかしい話

ec2でrbenv入れるのにitamaeで入れようとしてたのですが、
rbenvのインストール手順を普通になぞっていったら地味にはまってしまいました。

最初に書いたレシピはこんな感じ
rbenv/default.rb
%w(
  git
  gcc-c++
  glibc-headers
  openssl-devel
  readline
  libyaml-devel
  readline-devel
  zlib
  zlib-devel
  libffi-devel
  libxml2
  libxslt
  libxml2-devel
  libxslt-devel
  sqlite-devel
  mysql-devel
  patch
).each do |pkg|
  package "#{pkg}" do
    action :install
  end
end

git '/usr/local/rbenv' do
  repository 'https://github.com/sstephenson/rbenv.git'
end

directory "/urs/local/rbenv/plugins" do
  action :create
end

git '/usr/local/rbenv/plugins/ruby-build' do
  repository 'https://github.com/sstephenson/ruby-build.git'
end

remote_file "/etc/profile.d/rbenv.sh" do
  action :create
  source "files/etc/profile.d/rbenv.sh"
  owner 'root'
  group 'root'
  mode '644'
end

execute 'install ruby' do
  command("source /etc/profile.d/rbenv.sh; rbenv install 2.3.0")
  command("source /etc/profile.d/rbenv.sh; rbenv global 2.3.0; rbenv rehash")
end

実行してみるとこんな感じ
$ bundle exec itamae ssh -i pem/test.pem -h 172.31.21.186 roles/test.rb
 INFO : Starting Itamae...
 INFO : Recipe: /home/ec2-user/deploy/xxx/roles/test.rb
 INFO :   Recipe: /home/ec2-user/deploy/xxx/cookbooks/rbenv/default.rb
 INFO :     package[git] installed will change from 'false' to 'true'
 INFO :     package[gcc-c++] installed will change from 'false' to 'true'
 INFO :     package[openssl-devel] installed will change from 'false' to 'true'
 INFO :     package[libyaml-devel] installed will change from 'false' to 'true'
 INFO :     package[readline-devel] installed will change from 'false' to 'true'
 INFO :     package[libffi-devel] installed will change from 'false' to 'true'
 INFO :     package[libxml2-devel] installed will change from 'false' to 'true'
 INFO :     package[libxslt-devel] installed will change from 'false' to 'true'
 INFO :     package[sqlite-devel] installed will change from 'false' to 'true'
 INFO :     package[mysql-devel] installed will change from 'false' to 'true'
 INFO :     package[patch] installed will change from 'false' to 'true'
 INFO :     git[/usr/local/rbenv] exist will change from 'false' to 'true'
 INFO :     directory[/urs/local/rbenv/plugins] exist will change from 'false' to 'true'
 INFO :     git[/usr/local/rbenv/plugins/ruby-build] exist will change from 'false' to 'true'
 INFO :     remote_file[/etc/profile.d/rbenv.sh] exist will change from 'false' to 'true'
 INFO :     remote_file[/etc/profile.d/rbenv.sh] mode will be '0644'
 INFO :     remote_file[/etc/profile.d/rbenv.sh] owner will be 'root'
 INFO :     remote_file[/etc/profile.d/rbenv.sh] group will be 'root'
 INFO :     diff:
 INFO :     --- /dev/null 2016-02-25 02:37:28.211836042 +0000
 INFO :     +++ /tmp/itamae_tmp/1456368153.1309056 2016-02-25 02:42:33.144609603 +0000
 INFO :     @@ -0,0 +1,3 @@
 INFO :     +export RBENV_ROOT=/usr/local/rbenv
 INFO :     +export PATH="$RBENV_ROOT/bin:$PATH"
 INFO :     +eval "$(rbenv init -)"
 INFO :     execute[install ruby] executed will change from 'false' to 'true'

正常に終わったように見えてるが、rubyのバージョンが変わってない。。。
$ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]

レシピのrubyインストール部分を以下のようにコマンドを分割する形に修正
rbenv/default.rb
execute 'install ruby' do
  command("source /etc/profile.d/rbenv.sh; rbenv install 2.3.0")
end

execute 'set ruby global' do
  command("source /etc/profile.d/rbenv.sh; rbenv global 2.3.0; rbenv rehash")
end

再度実行してみる
$ bundle exec itamae ssh -i pem/test.pem -h 172.31.21.186 roles/test.rb
 INFO : Starting Itamae...
 INFO : Recipe: /home/ec2-user/deploy/xxx/roles/test.rb
 INFO :   Recipe: /home/ec2-user/deploy/xxx/cookbooks/rbenv/default.rb
 INFO :     execute[install ruby] executed will change from 'false' to 'true'
 INFO :     execute[set ruby global] executed will change from 'false' to 'true'

ちゃんとrubyのバージョンがrbenvで入れたものに変わりました
$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]


参考URL
http://qiita.com/fukuiretu/items/337e6ae15c1f01e93ec3

0 件のコメント:

コメントを投稿