Thursday, March 24, 2011

development on Windows deploying to Linux with cruise and bundler?

In my previous post, I set-up bundler with rails-2.2.2 application. Soon after that, I found myself in another trouble.

When I set-up bundler, I had to create Gemfile for the gems required by my rails application. Also, when I run bundle install command, it creates Gemfile.lock file, this file holds various information:
  • Gem repositories (sources)
  • Which version of gem the application is using (gem specification)
  • If there is any dependency of any gem, then what they are
  • Platform details
In my current project, I use Windows 7 as development machine, there is a cruise server on linux machine, I am committing my code to a subversion repository and once the build passes on cruise it deploys to a linux server. Every time I run bundle install, it creates a Gemfile.lock with windows version of gems and platform details for windows. If I commit Gemfile and Gemfile.lock, it fails the build and I have to manually login to cruise box and remove those files and re-run bundle install command, so that it generates the Gemfile and Gemfile.lock for linux environment, so that on integration and production servers, the Gemfile and Gemfile.lock will remain with linux version and there is no failures there. This was a pain.

I wrote a ruby script to do this job for me. With this script, I removed everything windows specific in Gemfile and Gemfile.lock I can think of. Here is what I did (look at: https://github.com/gouravtiwari/windows_linux_fix_for_bundler for source code)
  • I extended cruise:init rake task(created fix.rake in lib), to include my ruby script.
  • In the script, I am removing windows traces, updating the platform details, running bundle command and then committing the latest Gemfile and Gemgfile.lock toSVN.
This way, if by mistake I am committing Gemfile and Gemfile.lock to SVN, I don't need to go to cruise and re-generate Gemfile and Gemfile.lock.

Friday, March 4, 2011

Bundler with rails-2.2.2

I am working on a rails application, which is on rails-2.2.2. I have used bundler on rails-3 application, but never tried bundler for rails-2.2.2. The benefit of bundler is huge, it is gem manager for rails application. So, now no pain of vendoring/localizing gems in rails application.

gembundler.com gives step-by-step process on how to install/configure bundler for rails-2.3.x and rails-3.x applications. So, I wanted to find out how does it work with rails-2.2.2 application.

So, here is how I did.
  • First of all I have installed bundler
    gem install bundler // installed bundler 1.0.10
  • Updated rubygems
    gem update --system // rubygems > 1.3.6
  • Created config/preinitializer.rb file with below content:
    begin
      require "rubygems"
      require "bundler"
    rescue LoadError
      raise "Could not load the bundler gem. Install it with `gem install bundler`."
    end

    if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new("0.9.24")
      raise RuntimeError, "Your bundler version is too old for Rails 2.3." +
       "Run `gem install bundler` to upgrade."
    end

    begin
      # Set up load paths for all bundled gems
      ENV["BUNDLE_GEMFILE"] = File.expand_path("../../Gemfile", __FILE__)
      Bundler.setup
    rescue Bundler::GemNotFound
      raise RuntimeError, "Bundler couldn't find some gems." +
        "Did you run `bundle install`?"
    end
     
  • Created Gemfile with something similar:
    require 'rubygems'
    source 'http://rubygems.org'
    source 'http://gemcutter.org'
    source 'http://gems.github.com'

    gem 'rails', '2.2.2'
    gem 'hpricot', '0.6.0'
    gem "composite_primary_keys", '1.0.8'
    gem 'javan-whenever', '0.3.7'
    gem 'mysql', '2.8.1'
    gem 'activerecord-oracle_enhanced-adapter', '1.1.9'
    gem 'ruby-oci8', '1.0.3'

    group :test do
      gem 'mocha', '0.9.5'
      gem 'rspec',             '1.3.1', :require => 'spec'
      gem 'rspec-rails',       '1.3.3'
      gem 'database_cleaner',  '0.5.0'
      gem 'capybara'
      gem 'ruby-debug'
      gem 'factory_girl'
    end
  • From the application root, I ran:
    bundle install
That is it and I have my application up and running with bundler. Once I tested my application, I removed gems folder from /vendor (no need for localized version of gems now.)