前提条件
手順の流れ
- cookbook 作成
- attributes 作成
- templates 作成
- chef 実行
- 実行結果確認
手順詳細
基本的にCentOS の場合と同じです。
ただし、事前インストールするライブラリの種類が異なるので、attributes/default.rb のpackages 内容には注意。
1. cookbook 作成
sudo knife cookbook create rbenv -o /var/chef/cookbooks
# # Cookbook Name:: rbenv # Recipe:: default # # Copyright 2016, YOUR_COMPANY_NAME # # All rights reserved - Do Not Redistribute # node['rbenv']['packages'].each do |package_name| package "#{package_name}" do :install end end git "/home/#{node['rbenv']['user']}/.rbenv" do repository node['rbenv']['rbenv_uri'] reference 'master' action :sync group node['rbenv']['group'] user node['rbenv']['user'] end template '.bash_profile' do source 'bash_profile.erb' path "/home/#{node['rbenv']['user']}/.bash_profile" mode '0644' owner node['rbenv']['user'] group node['rbenv']['group'] not_if "grep rbenv /home/#{node['rbenv']['user']}/.bash_profile" end directory "/home/#{node['rbenv']['user']}/.rbenv/plugins" do owner node['rbenv']['user'] group node['rbenv']['group'] mode '0755' action :create end %w{ruby-build rbenv-default-gems rbenv-gem-rehash}.each do |plugin| git "/home/#{node['rbenv']['user']}/.rbenv/plugins/#{plugin}" do repository node['rbenv']["#{plugin}_uri"] reference 'master' action :sync group node['rbenv']['group'] user node['rbenv']['user'] end end template 'default-gems' do source 'default-gems.erb' path "/home/#{node['rbenv']['user']}/.rbenv/default-gems" mode '0755' owner node['rbenv']['user'] group node['rbenv']['group'] not_if { File.exists?("/home/#{node['rbenv']['user']}/.rbenv/default-gems") } end execute "rbenv install #{node['rbenv']['version']}" do command "/home/#{node['rbenv']['user']}/.rbenv/bin/rbenv install #{node['rbenv']['version']}" environment 'HOME' => "/home/#{node['rbenv']['user']}" user node['rbenv']['user'] group node['rbenv']['group'] not_if { File.exists?("/home/#{node['rbenv']['user']}/.rbenv/versions/#{node['rbenv']['version']}") } end execute "rbenv global #{node['rbenv']['version']}" do command "/home/#{node['rbenv']['user']}/.rbenv/bin/rbenv global #{node['rbenv']['version']}" environment 'HOME' => "/home/#{node['rbenv']['user']}" user node['rbenv']['user'] group node['rbenv']['group'] not_if "grep #{node['rbenv']['version']} /home/#{node['rbenv']['user']}/.rbenv/version" end
2. templates 作成
vi /var/chef/cookbooks/rbenv/templates/default/bash_profile.erb
# .bash_profile # Get the alias 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 -)"
sudo vi /var/chef/cookbooks/rbenv/templates/default/default-gems.erb
bundler pry
3. attributes 作成
sudo vi /var/chef/cookbooks/rbenv/attributes/default.rb
ユーザー名(下記では’hoge’)はrbenv のインストール先ユーザーなので、任意に変更のこと。
default['rbenv']['user'] = 'hoge' default['rbenv']['group'] = 'hoge' default['rbenv']['version'] = '2.2.4' default['rbenv']['rbenv_uri'] = 'https://github.com/sstephenson/rbenv.git' default['rbenv']['ruby-build_uri'] = 'https://github.com/sstephenson/ruby-build.git' default['rbenv']['rbenv-default-gems_uri'] = 'https://github.com/sstephenson/rbenv-default-gems.git' default['rbenv']['rbenv-gem-rehash_uri'] = 'https://github.com/sstephenson/rbenv-gem-rehash.git' default['rbenv']['packages'] = %w{git build-essential libssl-dev libreadline-dev}
4. chef 実行
cd /var/chef/cookbooks sudo chef-solo -o rbenv
5. 実行結果確認
su hoge source ~/.bash_profile rbenv -v ruby -v