関連記事
- Chefを使ってより効率的にサーバを管理しよう。まずは用語から
- Berkshelfを使って外部クックブックをノードに適用する。
- クックブックを作成してノードに適用する。- その1
- クックブックを作成してノードに適用する。- その2
- ChefでCentOS 6.7 + rbenv + ruby 2.3.0の環境を構築する。
- ChefでCentOS 6.7 + Vim 7.4 + luaを設置する。
- ChefでVimプラグインマネージャーNeoBundleを設置する。
- Chefのレシピをデバッグしてみよう。
- ChefでCentOS 6.7 + nginxを設置する。- 外部クックブック
- ChefでCentOS 6.7 + nginxを設置する。- カスタムクックブック
- ChefでCentOS 6.7 + nodejs + npmを設置する。
- ChefでVimプラグインマネージャーvim-plugを設置する。
- ohaiのカスタムプラグインを作ってみよう。
- Chef SoloからChef Zeroへ
- Chefでnginx + php(php-fpm) + mariadb + wordpressを設置する。
rbenvをインストール
クックブックの生成
- 雛形生成
% bin/kinfe cookbook create ruby-env -o site-cookbooks/
レシピを修正
- site-cookbooks/ruby-env/recipes/default.rb
%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
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
# .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 -)"
実行一覧にレシピを追加
% 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]
ノードに適用
% bin/knife solo cook dev
実行結果
% 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をインストール
レシピを修正
% 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
ノードに適用
% bin/knife solo cook dev
実行結果
% 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
補足
- いくつが設定が足りなかったため、リソースを追加した。
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実践入門