Cloud

Abstract Cloud – Fog: Set up

In this series I’m going to show you how to use the Fog Cloud Abstraction Library to manage cloud services in code (or on the command line). Fog is a ruby library that allows you to interact with multiple cloud providers (AWS, Azure, Oracle etc) through code. It abstracts away the complexities of each providers APIs and authentication mechanisms and attempts to make it easier to deal with networks, servers etc.

In this post I’m going to take you through setting up Fog and getting a listing of servers in an Oracle Cloud account (the process for Azure, AWS etc is very similar).

Setup

It’s actually pretty easy to install Fog. First identify which Fog gem you need for your cloud provider (you can find a list here). I’m using the Oracle Cloud Platform, so I’m going to the using the fog-oraclecloud gem (shameless plug – I wrote this!).

There’s a variety of ways to use, but for the time being, let’s just install it locally so we can use it in our code:

gem install fog-oraclecloud

Now let’s create a file and require the fog gem:

require 'fog/oraclecloud'

Before you can call the Oracle Cloud we need to give Fog our credentials. You can pass these through when you start a connection, but the easier way is to add the following to ~/.fog:

default:
  oracle_username: USERNAME
  oracle_password: PASSWORD
  oracle_domain: IDENTITY DOMAIN
  oracle_region: REGION (us or emea)
  oracle_compute_api: COMPUTE_URL
  oracle_storage_api: STORAGE_URL

Usage

With that out of the way, we can move on to more useful stuff. First let’s get a list of all our Java Cloud Services. Add the following code to our ruby file:

require 'pp'

instances = Fog::OracleCloud[:java].instances
pp instances

The ‘pp’ library just allows us to dump ruby objects out to the command line. The magic happens on line 3. We use the OracleCloud plugin in Fog and utilise the java module to get all instances. That’s all we need to do! Behind the scenes this is authenticating with the Oracle Cloud (with the details we provided above), accessing the correct API to get the Java instances and converting it into a ruby object we can do work with.

For instance we can find out whether the instance is running with:

puts "#{instances.first.service_name} - #{instances.first.status}"

Or get all the access rules (rules that define which ports are open) with:

pp instances.first.access_rules

The cool part is that once you learn how to use one service, the others are pretty similar. For instance to get the status of your Database instances, just run:

instances = Fog::OracleCloud[:database].instances

Creating

So we’ve seen how to list instances in some of our Cloud Platforms, but what about creating a new instance? Well, that’s pretty easy to (don’t run this unless you want to actually spin it up, it will cost you money!):

instance = Fog::OracleCloud[:soa].instances.create(
  :service_name => 'TestSOA',
  :admin_password => 'Welcome1$',
  :admin_username => 'weblogic',
  :shape => 'oc3m',
  :version => '12.2.1',
  :ssh_key => 'ssh-rsa AAAAB3NzaC...',
  :topology => 'osb'
)

This will create an OSB instance called ‘TestSOA’ in the SOA Suite cloud service. What you don’t see is that will also automatically create a storage container and database instance for you. If you did this action through the API you’d have to do all these steps manually, but Fog helps you out and does it for you (isn’t it nice?).

Mocking

Now, before you go run the above, remember that creating anything in your cloud will cost money so wouldn’t it be better to test the code above to make sure it will work with your other code before actually building your new services? Enter Fog.mock. When you set this flag, all other interactions with the Fog API from that point forward will be done in-memory and not actually call the cloud. To use mock, just add the following before you call Fog.

Fog.mock!

Now run the create code above and you should get back a running instance. Even better, this returns in a few seconds, rather than hour or so it would take to actually provision a cloud instance (you can adjust this time if you needed to closer simulate the real world).

Next

In the next post we will go through using this to construct IaaS instances (and the associated storage and networking). Stay tuned!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s