Creating my first Gem

A while back I started using the RightScale API to automate some tasks related to servers that we have in the cloud. Initially, all the work was done using scripts in various languages, but with Curl calls to the API. Switching over to writing all my tools in Ruby, I realized that the API was lacking when it came to authentication and ease of use.

Being that I am lazy, I did what any good coder would do, and created a class to handle the connections.

Normally, things would end there. But, being that I have started working with Ruby and Gems, I decided that I was going to create a Gem. This was mainly so that as I write new tools, I would not have to add this library manually to all of my apps. Ruby Gems provide a nice mechanism to add code/libraries to a machine for use by various programs.

All this leads to my learning process of how to create a Gem, and add it to the open RubyGems collection. I am going to try and recount all the steps that are needed. Hopefully in an order that will make more sense than the one that I went through to get my first gem added to the public domain.

RightScale API 1.0 with Ruby — Authentication

RightScale provides an API for automating tasks. However, getting started with them can be a bit confusing. Lets start with the various versions of the API. As of writing this, they are as follows:

  • API 1.0 — This API only works with Amazon AWS Cloud
  • API 1.5 — Works with most other providers and is NOT compatible with Amazon AWS Cloud
  • API 2.0 — This is supposed to be the grand unified API that will work with all supported clouds.

My focus is on how to connect to the RightScale API using Ruby. There are some differences in the API’s and how they make some calls, so they current focus is going to be on version 1.0 with Amazon AWS. While there are rest-client gems you could download, I wanted manual control so I do not include any of these.

Authentication

One of the trickiest parts of using the API is getting the formatting and flags correct. For this example you only need to require two classes based on http. All this was figured out from the documentation.

require 'net/http'
require 'net/https'

Ruby 1.9.3 on Mac OS X version 10.7 (10.7.3 to be precise)

I already know a number of programming languages, but as we are moving to a couple of tools that utilize Ruby, it became clear that I needed to break down and learn ruby. Working on MS Windows and Linux getting various versions installed has not been an issue. It is only when I went to use version 1.9.x on my MacBook that I realized this was not as simple as I hoped.

There are two main branches when it comes to Ruby: 1.8.x and 1.9.x. While both work fine, there are a number of improvements in the 1.9 stream, and as such I wanted to learn on this one. On my linux machines I was using RVM to manage different installations of Ruby without issue. You can go over to RVM for instructions on how to install RVM. Note: Close your Terminal and reopen so that the environment is properly sourced.

The problem is when you go to install 1.9.x or anything with RVM. I was getting the following errors:

mymachine:~ myusername$ rvm install 1.9.3
Compiling yaml in /Users/myusername/.rvm/src/yaml-0.1.4.
Error running 'make ', please read /Users/myusername/.rvm/log/ruby-1.9.3-p125/yaml/make.log

Database file /Users/myusername/.rvm/config/packages does not exist.