The Cloud Foundry Blog

Cloud Foundry Now Supports the Rails Console

Ruby and Rails developers can now remotely access the popular rails console using the Cloud Foundry command line tool (VMC). This new feature enables inspection of the Cloud Foundry Runtime App environment, troubleshooting application issues in runtime, and even the ability to modify data “on the fly” for one-off admin tasks.  Using the new vmc rails-console command, developers can target any Cloud Foundry instance, including the upcoming release of Micro Cloud Foundry.

Getting Started

First, install or update your Cloud Foundry command line tool (‘VMC’) to the latest preview version using the following command:

gem install vmc --pre

You can verify that you got the right version using:

vmc -v

which should show the version to be 0.3.16.beta.3 or higher.

Next, push or update a Rails app using vmc push.

To access the console, run:

vmc rails-console appname

That’s all there is to it!  Read on for a more detailed example…

Using the Rails Console

Let’s get started by deploying a Rails application to Cloud Foundry. We’ll use Enki, an open source blog engine.   I’ll use the cloudfoundry-samples fork, where I’ve added the mysql2 gem to enki’s Gemfile.  Now I’m ready to push to Cloud Foundry:

mycomp:enki$ vmc push mynewblog
Would you like to deploy from the current directory? [Yn]:
Application Deployed URL [mynewblog.cloudfoundry.com]:
Detected a Rails Application, is this correct? [Yn]:
Memory Reservation ("64M", "128M", "256M", "512M", "1G") ["256M"]:
Creating Application: OK
Create services to bind to 'mynewblog'? [yN]: y
1: mongodb
2: mysql
3: postgresql
4: rabbitmq
5: redis
What kind of service?: 2
Specify the name of the service [mysql-8f1d2]:
Creating Application: OK
Creating Service [mysql-8f1d2]: OK
Binding Service [mysql-8f1d2]: OK
Uploading Application:
  Checking for available resources: OK
  Processing resources: OK
  Packing application: OK
  Uploading (20M): OK
Push Status: OK
Staging Application: OK
Starting Application: OK

Looks like the app deployed successfully.  I saved the manifest generated from this vmc output, so you can do just a simple “vmc push” with the application name and URL after cloning the sample.

I’ll make my first blog post about this new Rails Console support and wait for the comments to start rolling in…

Unfortunately, it looks like I have a nasty comment from Joe that needs deleting.  Let’s fire up the Rails Console through vmc to easily get rid of that comment.  The vmc rails-console command can be run from any directory.

mycomp:enki$ vmc rails-console mynewblog
Deploying tunnel application 'caldecott'.
Uploading Application:
  Checking for available resources: OK
  Packing application: OK
  Uploading (1K): OK
Push Status: OK
Staging Application 'caldecott': OK
Starting Application 'caldecott': OK
Connecting to 'mynewblog' console: OK

irb():001:0>

Since this is the first time I’ve run Rails Console, vmc will deploy the caldecott application for me, which helps tunnel communication to my remote Rails application.  After that, it fires up the console and waits for input.

irb():001:0> @comment = Comment.find_by_author("Joe")
#<Comment id: 7, post_id: 1, author: "Joe", body: "I have something real...">
irb():002:0> @comment.delete
#<Comment id: 7, post_id: 1, author: "Joe", body: "I have something real...">
irb():003:0> @comment = Comment.find_by_author("Joe")
nil
irb():004:0> exit

In this console session, I use Comment.find_by_author to find Joe’s comment, delete the comment, and then check to ensure that Joe’s comment has been removed.   Let’s refresh the web page and see that it’s gone:

This is just one small example of what can be done with Rails Console.  I could use it for all sorts of diagnostics as well, such as perusing the database, running ruby commands to inspect state, or interacting with controller methods to check responsiveness.  For more information, check out the Rails Console documentation or this handy screencast from RailsCasts.

Enabling the Rails Console

The console will automatically be available when you push any Rails application.  If you already have a Rails application deployed to Cloud Foundry, you will need to update or redeploy your app to stage the console support.

What are you waiting for? Go try it out!

Ready to start interacting with your Rails app on Cloud Foundry? Make sure to update to the latest vmc gem, and you should be ready to give it a whirl.  Please feel free to direct any suggestions or feedback to the support forums!

- Jennifer Hickey
The Cloud Foundry Team

Don’t have a Cloud Foundry account yet?  Sign up for free today

 

This entry was posted in Announcement, Cloud Foundry, Media. Bookmark the permalink.

7 Responses to Cloud Foundry Now Supports the Rails Console

  1. Pingback: Cloud Foundry Further Improves Support for Ruby Applications | Blog

  2. Pingback: 7 Cool Things Learned in the London Cloud Foundry Open Tour. | Tomás Lin’s Programming Brain Dump

  3. Brad Midgley says:

    Could you update this to show how to work with console-vmc-plugin and vmc 0.4.x?

  4. Aurelien says:

    I would also like to know how the rails console work with vmc 0.4.x
    otherwise it gives me Unknown command ‘rails-console’…

    thank you :-)

    • Jennifer Hickey says:

      You will need to do a “gem install console-vmc-plugin” prior to pushing your Rails app (this will ensure that console is enabled when the app is created). You should then be able to run “vmc console ” to access the rails console.

  5. Henry Chen says:

    Could we support run ‘vmc console rake db:seed’ from the command line, just as heroku did.

    I log in ‘vmc console’ from China, but it always fails due to time-out issue. I would like ‘vmc console’ can support async usage, so I don’t want to wait for the irb console shows up sooooo slowly.

    $vmc console XXXXXXXXXX
    Opening console on port 10000… OK
    RuntimeError: Login attempt timed out.
    For more information, see ~/.vmc/crash

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>