Getting started with VMware CloudFoundry, MongoDB and Rails
Listen to Jared Rosoff’s June 2 webinar “VMware Cloud Foundry with MongoDB”.
Read about getting started with Cloud Foundry, MongoDB, and Node.js.
Last week, VMware launched Cloud Foundry: an open-source platform as a service. It’s pretty radical in that not only can you run your apps on infrastructure operated by VMware, you can also download Cloud Foundry itself and run it on your own machines!
But what’s most awesome about Cloud Foundry is that it supports MongoDB right out of the box! In today’s post, we’re going to walk through the creation of a Rails application using MongoDB and Cloud Foundry.
Here’s what we’re going to need to do:
- Create a new rails project (skipping Active Record)
- Add the dependencies for MongoMapper
- Build a simple app that interacts with the database
- Set up our credentials so we can talk to Cloud Foundry’s MongoDB
- Push our application to CloudFoundry
First we’ll create our new Rails project. My preference is for MongoMapper, so I’m going to skip the ActiveRecord generators when I initialize my project. <pre > $ rails new my_app –skip-active-record
Next we need to setup our dependencies. Cloud Foundry will automatically detect that we’re a Rails app, and it will process our Gemfile if we’ve got one. So we’re need to add any dependencies to our Gemfile so they’ll get installed when we deploy. This is what our Gemfile looks like:
source "http://rubygems.org" gem "rails", "3.0.5" gem "mongo_mapper" gem "bson_ext"I’m assuming you’re using Rails 3 here, but you can easily adapt these instructions for other versions.
Build a simple app that interacts with the database
Now we’ll write a little code to interact with the DB so we can be sure everything’s working.
$ script/rails generate scaffold messages message:string --orm mongo_mapperI’m also going to set the root of my rails app to be our new messages controller and remove our
public/index.html
. Here’s my routes file:
CloudFoundryRailsTutorial::Application.routes.draw do resources :messages root :to => "messages#index" endBe sure to delete your
public/index.html
!!
Set up our credentials so we can talk to Cloud Foundry’s MongoDB
MongoMapper comes with a generator that will create a basic
config/mongo.yml
configuration file for us.
$ script/rails generate mongo_mapper:configThe file looks like this:
defaults: &defaults host: 127.0.0.1 port: 27017We need to modify this so that it can talk to Cloud Foundry’s infrastructure. When CloudFoundry runs your app, it passes in a bunch of information through an environment variable. We need to pull the host, port, username, and password for MongoDB out of this environment variable. After some modification, thedevelopment: <<: *defaults database: myapp_development
test: <<: *defaults database: myapp_test
set these environment variables on your prod server
production: <<: *defaults database: myapp username: <%= ENV['MONGO_USERNAME'] %> password: <%= ENV['MONGO_PASSWORD'] %>
production
section of your config/mongo.yml
should look something like this:
production: host: <%= JSON.parse( ENV['VCAP_SERVICES'] )['mongodb-1.8'].first['credentials']['hostname'] rescue 'localhost' %> port: <%= JSON.parse( ENV['VCAP_SERVICES'] )['mongodb-1.8'].first['credentials']['port'] rescue 27017 %> database: <%= JSON.parse( ENV['VCAP_SERVICES'] )['mongodb-1.8'].first['credentials']['db'] rescue 'cloud_foundry_mongodb_tutorial' %> username: <%= JSON.parse( ENV['VCAP_SERVICES'] )['mongodb-1.8'].first['credentials']['username'] rescue '' %> password: <%= JSON.parse( ENV['VCAP_SERVICES'] )['mongodb-1.8'].first['credentials']['password'] rescue '' %>
Note: The “rescue” clauses are so that you can run this app locally. If you don’t include this and you try to run this app outside of cloud foundry, you’ll get an exception because there’s no VCAP_SERVICES environment variable passed into your app.
Push our application to CloudFoundry
Now we’re ready to test things out. If you haven’t already, you should go ahead and install the
vmc
command line tool. There’s a getting started with VMC guide here. Here’s what it looked like when I deployed my app:
redeye:myapp jsr$ vmc push --runtime ruby19 Would you like to deploy from the current directory? [Yn]: y Application Name: mongodb-on-cf-demo Application Deployed URL: 'mongodb-on-cf-demo.cloudfoundry.com'? Detected a Rails Application, is this correct? [Yn]: y Memory Reservation [Default:256M] (64M, 128M, 256M or 512M) 256M Creating Application: OK Would you like to bind any services to 'mongodb-on-cf-demo'? [yN]: y Would you like to use an existing provisioned service [yN]? n The following system services are available:: 1. mysql 2. mongodb 3. redis Please select one you wish to provision: 2 Specify the name of the service [mongodb-a8a43]: Creating Service: OK Binding Service: OK Uploading Application: Checking for available resources: OK Processing resources: OK Packing application: OK Uploading (5K): OK Push Status: OK Starting Application: OKNow you can point your browser to http://mongodb-on-cf-demo.cloudfoundry.com/ and you should see the list of messages!redeye:myapp jsr$
Congrats! You’ve got your first Rails app up and running on Cloud Foundry and MongoDB!
The code for this mongodb + rails + Cloud Foundry app is up on github
Happy coding and be sure to tell us about all the awesome apps you build on Cloud Foundry!
– Jared Rosoff