Rbenv, chef and bundler

April 2, 2012

What we aim

We're trying to use rbenv in our infrastructure in order to achieve the following multiple goals:

So far, I've managed to install rbenv system wide on the servers using Riot Games excellent cookbook. A simple:

require_recipe "rbenv"
# Setup the rubies
PICOR_RUBY="ree-1.8.7-2012.02"
CHEF_RUBY="1.9.2-p318"
OPINL_RUBY="1.9.2-p318"

rbenv_ruby PICOR_RUBY
rbenv_ruby CHEF_RUBY

in a chef cookbook, automatically download and install these versions of Ruby in the system wide rbenv. Because I'm not ready to switch, I'm not yet setting up automatic rbenv switching:

# File "/home/apps/.rbenv-version" do
#   owner "apps"
#   group "apps"
#   backup false
#   content PICOR_RUBY
# end

# File "/root/.rbenv-version" do
#   owner "root"
#   group "root"
#   backup false
#   content CHEF_RUBY
# end

But this should go in action as soon as things works ok.

Next step, Gems

Because Bundle has some kind of memory, you should not use it to deploy gems from a given user if you plan to deploy another gemset in another environement with the SAME user later.

At fotopedia, we use GEM_HOME and GEM_PATH to change gemset because it works and it's really simple (for binaries, we use a combination of symlinks in /usr/local/bin and alternative selection provided by our Debian OS). So we actually install several gemset with the same user. The workaround to have bundler work correctly is to change its HOME when running. Ugly, but efficient:

script "Bundle picor gems" do
  interpreter "bash"
  user "root"
  cwd  "/tmp/"
  environment rbenv_as_hash
  code <<-EOS
# Discard previous forced state
rm -rf .bundle
export HOME=$PWD
export RBENV_VERSION=ree-1.8.7-2012.02
rbenv exec gem list bundler | grep 'bundler' || gem install bundler
cd /etc/bundler
rbenv exec bundle install --shebang ruby-local-exec --binstubs /usr/local/picorstubs
EOS
  action :nothing
end

This script is run when the Gemfile changes in /etc/bundler (our current location for the Picor gems). the gems chefs are installed almost the same way:

script "Bundle chef gems" do
  interpreter "bash"
  user "root"
  cwd "/root/"
  environment rbenv_as_hash
  code &lt;&lt;-EOS
export RBENV_VERSION=ree-1.8.7-2012.02
rbenv execgem list bundler | grep 'bundler' || rbenv exec  gem install bundler
# Use this because bundle in path is 1.8 bundle
rbenv exec bundle install --deployment --shebang ruby-local-exec --binstubs /usr/local/chefstubs
EOS
  action :nothing
end

Once this has run, /usr/local/picotstubs contains the picor binary stubs ready to be used and the chefstubs the one for chef.

Next Step. Rewiring daemons to use one gemset according to what I need. Once the stubs are there, this might be straighforward...

Contact? Question?

Comments

Commentaires
I am Pierre Baillet, senior software engineer. I write here mostly on computers and code, games, life and server stuff.