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:
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.
$ 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!!
config/mongo.ymlconfiguration file for us.
$ script/rails generate mongo_mapper:configThe file looks like this:
defaults: &defaults host: 127.0.0.1 port: 27017 development: <<: *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'] %>We 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, the
production section of your config/mongo.ymlshould 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.
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: OK redeye:myapp jsr$Now you can point your browser to http://mongodb-on-cf-demo.cloudfoundry.com/ and you should see the list of messages!
— Jared Rosoff