ChefでCentOS 6.7 + rbenv + ruby 2.3.0の環境を構築する。


## 関連記事

* [Chefを使ってより効率的にサーバを管理しよう。まずは用語から](http://qiita.com/park-jh/items/9e2e358d2e82a7736cf7)
* [Berkshelfを使って外部クックブックをノードに適用する。](http://qiita.com/park-jh/items/751464a4cc6d8abcd6fd)
* [クックブックを作成してノードに適用する。- その１](http://qiita.com/park-jh/items/8d641809850b5930d39c)
* [クックブックを作成してノードに適用する。- その２](http://qiita.com/park-jh/items/88237342cfd202e6893f)
* ChefでCentOS 6.7 + rbenv + ruby 2.3.0の環境を構築する。
* [ChefでCentOS 6.7 + Vim 7.4 + luaを設置する。](http://qiita.com/park-jh/items/371d179d5e71618a1f14)
* [ChefでVimプラグインマネージャーNeoBundleを設置する。](http://qiita.com/park-jh/items/ec09e40f9b4aaf9fedfd)
* [Chefのレシピをデバッグしてみよう。](http://qiita.com/park-jh/items/96a076e167327a6a5a39)
* [ChefでCentOS 6.7 + nginxを設置する。- 外部クックブック](http://qiita.com/park-jh/items/3013c49494e524fced31)
* [ChefでCentOS 6.7 + nginxを設置する。- カスタムクックブック](http://qiita.com/park-jh/items/43451cea21ee1724c24b)
* [ChefでCentOS 6.7 + nodejs + npmを設置する。](http://qiita.com/park-jh/items/96089e7e1c97b0fe3a6c)
* [ChefでVimプラグインマネージャーvim-plugを設置する。](http://qiita.com/park-jh/items/ba27ad412ce9926fd94d)
* [ohaiのカスタムプラグインを作ってみよう。](http://qiita.com/park-jh/items/34be55abb7c63b36cddb)
* [Chef SoloからChef Zeroへ](http://qiita.com/park-jh/items/44c249c9de768c53b423)
* [Chefでnginx + php(php-fpm) + mariadb + wordpressを設置する。](http://qiita.com/park-jh/items/7515e47dddc38257a454)

## rbenvをインストール

### クックブックの生成

* 雛形生成

```sh
  % bin/kinfe cookbook create ruby-env -o site-cookbooks/
```

### レシピを修正

* site-cookbooks/ruby-env/recipes/default.rb

```ruby
  %w(gcc git openssl-devel).each do |pkg|
    package pkg do
      action :install
    end
  end

  git "/home/#{node['ruby-env']['user']}/.rbenv" do
    repository node['ruby-env']['rbenv_url']
    action :sync
    user node['ruby-env']['user']
    group node['ruby-env']['group']
  end

  template ".bash_profile" do
    source '.bash_profile.erb'
    path "/home/#{node['ruby-env']['user']}/.bash_profile"
    mode '0644'
    owner node['ruby-env']['user']
    group node['ruby-env']['group']
    not_if "grep rbenv ~/.bash_profile", :environment => {:'HOME' => "/home/#{node['ruby-env']['user']}"}
  end
```

### 属性値追加

* site-cookbooks/ruby-env/attributes/default.rb

```ruby
  default['ruby-env']['user'] = 'vagrant'
  default['ruby-env']['group'] = 'vagrant'
  default['ruby-env']['version'] = '2.3.0'
  default['ruby-env']['rbenv_url'] = 'https://github.com/rbenv/rbenv.git'
  default['ruby-env']['ruby-build_url'] = 'https://github.com/rbenv/ruby-build.git'
```

### テンプレート生成

* site-cookbooks/ruby-env/templates/default/.bash_profile.erb

```sh
  # .bash_profile

  # Get the aliases and functions
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi

  # User specific environment and startup programs
  PATH=$PATH:$HOME/bin

  export PATH="$HOME/.rbenv/bin:$PATH"
  eval "$(rbenv init -)"
```

### 実行一覧にレシピを追加

```sh
  % bin/knife node run_list add dev 'recipe[ruby-env]' -z

  dev:
    run_list:
      recipe[yum-epel]
      recipe[yum-update]
      recipe[mariadb]
      recipe[ruby-env]
```

### ノードに適用

```sh
  % bin/knife solo cook dev
```

### 実行結果

```sh
  % bin/knife solo cook dev

  Running Chef on dev...
  Checking Chef version...
  Installing Berkshelf cookbooks to 'cookbooks'...
  Resolving cookbook dependencies...
  Using yum (3.10.0)
  Using yum-epel (0.6.5)
  Vendoring yum (3.10.0) to cookbooks/yum
  Vendoring yum-epel (0.6.5) to cookbooks/yum-epel
  Uploading the kitchen...
  Generating solo config...
  Running Chef: sudo chef-solo -c ~/chef-solo/solo.rb -j ~/chef-solo/dna.json
  Starting Chef Client, version 12.7.2
  Compiling Cookbooks...
  Converging 11 resources
  Recipe: yum-epel::default
    * yum_repository[epel] action create
      * template[/etc/yum.repos.d/epel.repo] action create (up to date)
      * execute[yum clean metadata epel] action nothing (skipped due to action :nothing)
      * execute[yum-makecache-epel] action nothing (skipped due to action :nothing)
      * ruby_block[yum-cache-reload-epel] action nothing (skipped due to action :nothing)
       (up to date)
  Recipe: yum-update::default
    * execute[yum-update] action run
      - execute yum -y update
  Recipe: mariadb::default
    * cookbook_file[/etc/yum.repos.d/MariaDB.repo] action create (up to date)
    * yum_package[MariaDB-server] action install (up to date)
    * yum_package[MariaDB-client] action install (up to date)
    * yum_package[MariaDB-devel] action install (up to date)
  Recipe: ruby-env::default
    * yum_package[gcc] action install
      - install version 4.4.7-16.el6 of package gcc
    * yum_package[git] action install
      - install version 1.7.1-3.el6_4.1 of package git
    * yum_package[openssl-devel] action install
      - install version 1.0.1e-42.el6_7.2 of package openssl-devel
    * git[/home/vagrant/.rbenv] action sync
      - clone from https://github.com/rbenv/rbenv.git into /home/vagrant/.rbenv
      - checkout ref 29b4da77370af0a3413dce00a72c7631302a2b25 branch HEAD
    * template[.bash_profile] action create
      - update content in file /home/vagrant/.bash_profile from 173704 to cc39f6
      --- /home/vagrant/.bash_profile	2015-07-23 18:55:47.000000000 +0000
      +++ /home/vagrant/..bash_profile20160223-17719-yfseb1	2016-02-23 07:58:51.632306139 +0000
      @@ -2,12 +2,12 @@

       # Get the aliases and functions
       if [ -f ~/.bashrc ]; then
      -	. ~/.bashrc
      +  . ~/.bashrc
       fi

       # User specific environment and startup programs
      -
       PATH=$PATH:$HOME/bin

      -export PATH
      +export PATH="$HOME/.rbenv/bin:$PATH"
      +eval "$(rbenv init -)"
      - restore selinux security context

  Running handlers:
  Running handlers complete
  Chef Client finished, 6/15 resources updated in 21 seconds
```

## ruby 2.3.0をインストール

### レシピを修正

```diff
% git diff
diff --git a/site-cookbooks/ruby-env/recipes/default.rb b/site-cookbooks/ruby-env/recipes/default.rb
index 203539a..bbc8749 100644
--- a/site-cookbooks/ruby-env/recipes/default.rb
+++ b/site-cookbooks/ruby-env/recipes/default.rb
@@ -7,7 +7,7 @@
 # All rights reserved - Do Not Redistribute
 #

-%w(gcc git openssl-devel).each do |pkg|
+%w(gcc gcc-c++ git openssl-devel readline-devel).each do |pkg|
   package pkg do
     action :install
   end
@@ -28,3 +28,25 @@ template ".bash_profile" do
   group node['ruby-env']['group']
   not_if "grep rbenv ~/.bash_profile", :environment => {:'HOME' => "/home/#{node['ruby-env']['user']}"}
 end
+
+directory "/home/#{node['ruby-env']['user']}/.rbenv/plugins" do
+  owner node['ruby-env']['user']
+  group node['ruby-env']['group']
+  mode '0755'
+  action :create
+end
+
+git "/home/#{node['ruby-env']['user']}/.rbenv/plugins/ruby-build" do
+  repository node['ruby-env']['ruby-build_url']
+  action :sync
+  user node['ruby-env']['user']
+  group node['ruby-env']['group']
+end
+
+execute "rbenv install #{node['ruby-env']['version']}" do
+  command "/home/#{node['ruby-env']['user']}/.rbenv/bin/rbenv install #{node['ruby-env']['version']}"
+  user node['ruby-env']['user']
+  group node['ruby-env']['group']
+  environment 'HOME' => "/home/#{node['ruby-env']['user']}"
+  not_if { File.exists?("/home/#{node['ruby-env']['user']}/.rbenv/versions/#{node['ruby-env']['version']}") }
+end

```

### ノードに適用

```sh
  % bin/knife solo cook dev
```

### 実行結果

```sh
  % bin/knife solo cook dev

  Running Chef on dev...
  Checking Chef version...
  Installing Berkshelf cookbooks to 'cookbooks'...
  Resolving cookbook dependencies...
  Using yum (3.10.0)
  Using yum-epel (0.6.5)
  Vendoring yum (3.10.0) to cookbooks/yum
  Vendoring yum-epel (0.6.5) to cookbooks/yum-epel
  Uploading the kitchen...
  Generating solo config...
  Running Chef: sudo chef-solo -c ~/chef-solo/solo.rb -j ~/chef-solo/dna.json
  Starting Chef Client, version 12.7.2
  Compiling Cookbooks...
  Converging 15 resources
  Recipe: yum-epel::default
    * yum_repository[epel] action create
      * template[/etc/yum.repos.d/epel.repo] action create (up to date)
      * execute[yum clean metadata epel] action nothing (skipped due to action :nothing)
      * execute[yum-makecache-epel] action nothing (skipped due to action :nothing)
      * ruby_block[yum-cache-reload-epel] action nothing (skipped due to action :nothing)
       (up to date)
  Recipe: yum-update::default
    * execute[yum-update] action run
      - execute yum -y update
  Recipe: mariadb::default
    * cookbook_file[/etc/yum.repos.d/MariaDB.repo] action create (up to date)
    * yum_package[MariaDB-server] action install (up to date)
    * yum_package[MariaDB-client] action install (up to date)
    * yum_package[MariaDB-devel] action install (up to date)
  Recipe: ruby-env::default
    * yum_package[gcc] action install (up to date)
    * yum_package[git] action install (up to date)
    * yum_package[openssl-devel] action install (up to date)
    * yum_package[readline-devel] action install
      - install version 6.0-4.el6 of package readline-devel
    * git[/home/vagrant/.rbenv] action sync (up to date)
    * template[.bash_profile] action create (skipped due to not_if)
    * directory[/home/vagrant/.rbenv/plugins] action create (up to date)
    * git[/home/vagrant/.rbenv/plugins/ruby-build] action sync (up to date)
    * execute[rbenv install 2.3.0] action run
      - execute /home/vagrant/.rbenv/bin/rbenv install 2.3.0

  Running handlers:
  Running handlers complete
  Chef Client finished, 3/19 resources updated in 04 minutes 13 seconds
```

## 補足

* いくつが設定が足りなかったため、リソースを追加した。

```ruby
execute "rbenv global #{node['ruby-env']['version']}" do
  command "/home/#{node['user']}/.rbenv/bin/rbenv global #{node['ruby-env']['version']}"
  user node['user']
  group node['group']
  environment 'HOME' => "/home/#{node['user']}"
end

cookbook_file "/home/#{node['user']}/.gemrc" do
  owner node['user']
  group node['group']
  mode '0644'
end

%w(rbenv-rehash bundler).each do |gem|
  execute "gem install #{gem}" do
    command "/home/#{node['user']}/.rbenv/shims/gem install #{gem}"
    user node['user']
    group node['group']
    environment 'HOME' => "/home/#{node['user']}"
    not_if "/home/#{node['user']}/.rbenv/shims/gem list | grep #{gem}"
  end
end
```

## 参考

* https://docs.chef.io/
* Chef実践入門