Fog OpenStack Tutorial, Essex Edition
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Getting started with Fog and OpenStack Essex

Before we start, make sure you have Essex support in Fog.

Essex support has not been merged into Fog upstream so you'll need a custom Fog build to work through the tutorial.

Get the custom gem build from or build fog yourself from

Follow the merge here if you are interested:

It's a good idea to have OpenStack clients installed to explore other commands. You can install them in Ubuntu with the following command:

sudo apt-get install python-novaclient glance-client python-keystoneclient

Let's get started:

require 'fog'

Connect and authenticate

conn ={
  :provider => 'OpenStack',
  :openstack_api_key => "changeme",
  :openstack_username => "",
  :openstack_auth_url => ""

openstack_auth_url is the URL of the Keystone authentication server in this case.

openstack_api_key is the password I use to login to the dashboard (Horizon) and use the API.

Find the server flavor we want.

flavor = conn.flavors.find { |f| == 'm1.tiny' }

m1.tiny has 512 MB of RAM and no additional ephemeral storage. List the flavors available with the command 'nova flavor-list'.

Find the server image/template we want

image = conn.images.find { |i| == 'ubuntu-precise-amd64' }

List the images available with 'nova image-list' or glance index.

Create the server

server = conn.servers.create :name => "fooserver-#{ '%Y%m%d'}",
                             :image_ref =>,
                             :flavor_ref =>,
                             :key_name => 'my-foo-keypair' # optional

This will create the server asynchronously, since waiting for server.ready? is optional. key_name is optional and is used to inject the specified keypair to the instance if cloud-init is present. You can then login via SSH without password, among other things (

List currently available keypairs with 'nova keypair-list'

Wait for the server to be ready (optional, wait for state == 'ACTIVE'):

server.wait_for { ready? }

You can also check the status of the server with 'nova list':

|                  ID                  |       Name      | Status |      Networks     |
| e56b9306-063a-4622-89cb-b5069f805221 | foobar-20120428 | BUILD  | private=   |

List the servers currently running (active or building)

conn.servers.each do |s|
  puts # server name
  puts s.state

Associate a public IP to the server

ip = conn.addresses.find { |ip| ip.instance_id.nil? }

If we find a free IP, not being used by any instance, use that. Otherwise, create it:

if ip.nil?
  puts 'Creating IP...'
  ip = conn.addresses.create

Associate the IP address to the server:

ip.server = server

Cleanup or regret it, @geemus dixit