要件
- rbenv を使って任意の ruby バージョンをインストール出来る cookbook を書いてみるぞ
環境
- CentOS 6.3 on Vagrant
とりあえず...
以下のように書いてみた。
install_ruby_version="1.9.3-p392" # git "/usr/local/rbenv" do repository "git://github.com/sstephenson/rbenv.git" reference "master" action :sync not_if "ls /usr/local/rbenv" end execute "source_profile" do cwd "/etc" command <<-EOH echo 'export RBENV_ROOT=/usr/local/rbenv' >> /etc/profile echo 'export PATH="$RBENV_ROOT/bin:$PATH"' >> /etc/profile echo 'eval "$(rbenv init -)"' >> /etc/profile source /etc/profile EOH not_if "grep rbenv /etc/profile" end directory "/usr/local/rbenv/plugins" do mode "00755" action :create not_if "ls /usr/local/rbenv/plugins" #action :nothing end git "/usr/local/rbenv/plugins/ruby-build" do repository "git://github.com/sstephenson/ruby-build.git" reference "master" action :sync #notifies :create,resources( :directory => "/usr/local/rbenv/plugins" ) end bash "install_ruby_build" do cwd "/usr/local/rbenv/plugins/ruby-build" user "root" group "root" code <<-EOH ./install.sh EOH environment 'PREFIX' => "/usr/local" end bash "ruby_install" do cwd "/usr/local/rbenv/bin" code <<-EOH ./rbenv install #install_ruby_version ./rbenv rehash ./rbenv global #install_ruby_version EOH not_if "ls /usr/local/rbenv/shims/ruby" end
まとめ
忘れまい not_if
# hoge/huga の所在を確認する場合 not_if "hoge/huge" # × not_if "ls hoge/huga" # ◯
- not_if の判断は " " で囲まれたコマンドの実行結果を判断する
- これまでは " " で書かれたパスの所在のみを確認するだけだと思っていた...
- その為、ファイルの所在確認を not_if で書いても、単純にパスだけ書いていたのでエラーになってしまっていた...
- こういった判断に関しても ruby で書く方が美しい
action:nothing からの notifies :xxxx
cookbook_file "/etc/httpd/conf.d/common.conf" do source "common.conf" mode 0644 action :nothing end package "httpd" do action :install notifies :create, resources( :cookbook_file => "/etc/httpd/conf.d/common.conf" ) end
- とあるリソースが実行された時のみ実行するのが nothing...notifies
- 上の例では cookbook_file の action は nothing となっているのが肝
- package リソースが実行(httpd インストール)された時のみ cookbook_file リソースが実行(create)されることになる