LoginSignup
23
21

More than 5 years have passed since last update.

ItamaeでCentOSにRuby環境構築

Posted at

以前書いた、
Chef Solo+Vagrantで、CentOSにrbenvでRuby環境構築
と同じことを、Chef SoloではなくItamaeを利用して行った。

ryotarai/itamae

Chefの記法とほとんど同じように書けるし、よく分からないディレクトリがいっぱい作成されて混乱することもない。
Chefにそこまで慣れていない自分としては、シンプルに管理できそうな分こっちの方が良いんじゃないかという印象でした。

Vagrantで仮想環境作成

CentOS 6.6の環境をVagrantを使って起動する。

$ vagrant box add opscode-centos-6.6 http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-6.6_chef-provisionerless.box
$ vagrant init opscode-centos-6.6

$ vagrant up

Itamaeのレシピ作成

インストール

Itamaeをbundle install

Gemfile
gem 'itamae'
$ gem list itamae
*** LOCAL GEMS ***

itamae (1.0.13)

レシピ作成

レシピを書いていく。
Chefだとリモートサーバ(今回のケースだとCentOSの仮想環境)側にもインストールが必要だったが、一手間減った:smiley:

Chefと違って、初期化時に色んなディレクトリが自動で作成されるわけではないので、
適当にファイルを作っていく。
今回は、Rubyインストール用のレシピ用のファイルを1つ、変数格納用のJSONファイルを1つ作成している。

以下、その内容。

/path/to/itamae/recipes/ruby-env.rb
%w(gcc openssl-devel readline-devel git).each do |pkg|
  package pkg do
    action :install
  end
end

git "/home/#{node["ruby-env"]["user"]}/.rbenv" do
  repository "https://github.com/#{node["ruby-env"]["rbenv_url"]}"
  user node["ruby-env"]["user"]
end

execute "Add rbenv to bash_profile" do
  user node["ruby-env"]["user"]
  command "echo 'export PATH=\"#{node["ruby-env"]["rbenv_path"]}:$PATH\"' >> ~/.bash_profile;" \
    "echo 'eval \"$(rbenv init -)\"' >> ~/.bash_profile"
  not_if "grep 'rbenv init' ~/.bash_profile"
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

%w(ruby-build rbenv-default-gems rbenv-gem-rehash).each do |plgin|
  git "/home/#{node["ruby-env"]["user"]}/.rbenv/plugins/#{plgin}" do
    repository "https://github.com/#{node["ruby-env"]["#{plgin}_url"]}"
    user node["ruby-env"]["user"]
  end
end

%w(bundler pry).each do |gem_name|
  execute "Add default gems when installing ruby: #{gem_name}" do
    user node["ruby-env"]["user"]
    command "echo '#{gem_name}' >> ~/.rbenv/default-gems"
    not_if "grep '#{gem_name}' ~/.rbenv/default-gems"
  end
end

node["ruby-env"]["versions"].each do |version|
  execute "Install Ruby v#{version}" do
    user node["ruby-env"]["user"]
    command "#{node["ruby-env"]["rbenv_path"]}/rbenv install #{version}"
    not_if "#{node["ruby-env"]["rbenv_path"]}/rbenv versions | grep #{version}"
  end
end

execute "Set global ruby version #{node["ruby-env"]["global_version"]}" do
  user node["ruby-env"]["user"]
  command "#{node["ruby-env"]["rbenv_path"]}/rbenv global #{node["ruby-env"]["global_version"]}"
end
/path/to/itamae/node.json
{
  "ruby-env": {
    "user": "vagrant",
    "group": "vagrant",
    "rbenv_path": "$HOME/.rbenv/bin",
    "rbenv_url": "sstephenson/rbenv.git",
    "ruby-build_url": "sstephenson/ruby-build.git",
    "rbenv-default-gems_url": "sstephenson/rbenv-default-gems.git",
    "rbenv-gem-rehash_url": "sstephenson/rbenv-gem-rehash.git",
    "versions": ["2.1.5"],
    "global_version": "2.1.5"
  }
}

あとは実行するのみ。
READMEに書いてあるとおり、itamae ssh -h vagrant_vm_name --vagrant recipe.rbで実行。また、--node-jsonオプションでJSONファイル指定。

$ itamae ssh -h default --vagrant --node-json node.json recipes/ruby-env.rb
 INFO : Starting Itamae...
 INFO : Loading node data from /Users/Aono/work/itamae_test/node.json...
 INFO : Recipe: /Users/Aono/work/itamae_test/recipes/ruby-env.rb
 INFO :    package[gcc]
 INFO :       action: install
 INFO :          installed will change from 'false' to 'true'
 INFO :    package[openssl-devel]
 INFO :       action: install
 INFO :          installed will change from 'false' to 'true'
 INFO :    package[readline-devel]
 INFO :       action: install
 INFO :          installed will change from 'false' to 'true'
 INFO :    package[git]
 INFO :       action: install
 INFO :          installed will change from 'false' to 'true'
 INFO :    git[/home/vagrant/.rbenv]
 INFO :       action: sync
 INFO :          exist will change from 'false' to 'true'
 INFO :    execute[Add rbenv to bash_profile]
 INFO :       action: run
 INFO :    directory[/home/vagrant/.rbenv/plugins]
 INFO :       action: create
 INFO :          exist will change from 'false' to 'true'
 INFO :          mode will be '0755'
 INFO :          owner will be 'vagrant'
 INFO :          group will be 'vagrant'
 INFO :    git[/home/vagrant/.rbenv/plugins/ruby-build]
 INFO :       action: sync
 INFO :          exist will change from 'false' to 'true'
 INFO :    git[/home/vagrant/.rbenv/plugins/rbenv-default-gems]
 INFO :       action: sync
 INFO :          exist will change from 'false' to 'true'
 INFO :    git[/home/vagrant/.rbenv/plugins/rbenv-gem-rehash]
 INFO :       action: sync
 INFO :          exist will change from 'false' to 'true'
 INFO :    execute[Add default gems when installing ruby: bundler]
 INFO :       action: run
 INFO :    execute[Add default gems when installing ruby: pry]
 INFO :       action: run
 INFO :    execute[Install Ruby v2.1.5]
 INFO :       action: run
 INFO :    execute[Set global ruby version 2.1.5]
 INFO :       action: run

リモートサーバ側でもいちおう目視確認。(ほんとはServerspecでテストした方が良いのでしょうが、今回は端折ります)

$ vagrant ssh

vagrant@localhost > ruby -v
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux]

vagrant@localhost > gem list
bigdecimal (1.2.4)
bundler (1.7.9)
coderay (1.1.0)
io-console (0.4.2)
json (1.8.1)
method_source (0.8.2)
minitest (4.7.5)
pry (0.10.1)
psych (2.0.5)
rake (10.1.0)
rdoc (4.1.0)
slop (3.6.0)
test-unit (2.1.5.0)

おわり

という感じで、ItamaeはChefと比較してシンプルな構成で始めることができる。
Chefに慣れてない人や、そもそもプロビジョニングツール触ったことない人は試してみる価値がありそうです。
良い感じなので業務で使いたい。もうすこし触ってみます

23
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
21