TechTalkThursday: RVM & Vagrant

Posted on by Nina Talley

Last Thursday our Technical Director, Scott Sampson, had a Tech Talk focused on RVM and Vagrant.

This gave the guys a great review of some of our standard practices as well as a chance to ask some "Why?" questions that had maybe been kicking around their heads.

Being that Cloudspace is a company that works on multiple client projects at once, we have consistently run into a problem when making our VM box files. Different clients, rotating engineer pairs and different stages of client applications means many different Ruby versions are being used everyday. With RVM, that problem is solved. Quite commonly we found that our RVM install on our base boxes wouldn't work off the bat.  We found ourselves having to uninstall, then reinstall just to get RVM working. This is obviously very annoying!

 

What we've done to correct this problem is setup an install process using Vagrant paired with chef-solo to install RVM. This cuts out the constant uninstall/reinstall, and saves us a few headaches.

Have no clue what I'm talking about? Here is a quick synopsis of each topic before we jump into Scott's lovingly prepared "Installing RVM the Cloudspace Way".

What is RVM?

- RVM is a version manager for Ruby. It keeps your Ruby environment, including your RubyGems, partitioned allowing you to install multiple versions of Ruby on the same system.  It also allows you to keep different sets of gems for each project.  A must have for any Ruby development shop.

What is Vagrant?

- (from wikipedia) Vagrant is open-source software for creating and configuring virtual development environments. It can be considered a wrapper around VirtualBox and configuration management software such as Chef, Salt and Puppet.

- Basically allows you to quickly bring up development boxes that mimic production environments, to reduce issues like "well it's working on my dev box but not on production".

What is chef-solo?

-chef-solo is an open source, limited-functionality version of Chef. It allows the use of cookbooks with nodes that do not have access to a Chef Server, which is incredibly useful.

 

Installing RVM the Cloudspace Way

In order to use RVM with chef-solo we need the RVM recipe.  We used the one from Fletcher Nichol on github (https://github.com/fnichol/chef-rvm).  Clone or add as a submodule to your current chef-solo cookbooks directory.

Next open your Vagrantfile and add the following two recipes to the the chef run list.

chef.add_recipe "rvm::vagrant" chef.add_recipe "rvm::system"

Next add the following hash to your chef json data:

chef.json = {
  :rvm => {
    :rubies => [
      "1.9.2"
    ],
    :default_ruby => "ruby-1.9.2-p320",
    :user_default_ruby => "ruby-1.9.2-p320",
    'vagrant' => {
      'system_chef_solo' => '/usr/local/ruby/bin/chef-solo'
    }
  }
}
 

Obviously you may need to change versions of Ruby and paths based on what you need but  after this all you need to do is Vagrant up.

Gotchas:

Notice the system_chef_solo key in the json data above.  If you do not add that it will not allow you re-up the box after the first time and you will have to destroy it to make it work again.

Extras:

If you have you a custom application recipe that you like to use to run bundler and migrations you will need to then wrap the calls in rvm_shell resource.  This allows you to run commands in the RVM environment of your choice.  Here is an example below:

rvm_shell "bundle-install" do
  ruby_string "ruby-1.9.2-p320@test-client"
  user "vagrant"
  cwd "/srv/test_client"
  code %{bundle install}
end
 

And that about wraps it up! Have any questions regarding RVM, Vagrant or chef-solo? Throw them our way! Scott's big brain has the answers you need!

 
comments powered by Disqus