Setting up MySQL on Cloudfoundry

In my previous post, I showed how to create a quick Grails app on Cloudfoundry.
It was a very simple ‘Hello World’ type example app. In this post we are going to dig little deeper into cloudfoundry.

At the time of this writing, Cloudfoundry supports 3 different type of data store.
1. MongoDb – No SQL Database
2. redis – A persistent key value database
3. MySQL – Most popular free database

CloudFoundry calls these “Services”. You can create/instantiate these services for your application.
Let’s continue on with our previous example. We will create a MySQL database service and we will call it ‘hcfmysql-db’ (abbreviation to HelloCloudFoundryMySQL)

1. Creating CloudFoundry Service
You can create an instance by invoking the ‘cf-create-service’ command, Lets create ‘hcfmysql-db’

grails cf-create-service mysql hcfmysql-db

You should see an output like upon successful creation.


Service 'hcfmysql-db' provisioned.

You can verify that instance was created by running the following Grails command

grails cf-services

You should see output like follows:

============== System Services ==============

| Service | Version | Description                   |
| mysql   | 5.1     | MySQL database service        |
| redis   | 2.2     | Redis key-value store service |
| mongodb | 1.8     | MongoDB NoSQL store           |

=========== Provisioned Services ============

| Name          | Service |
| hcfmysql-db   | mysql   |

2. Binding the Database to the application
In Cloudfoundry, you will have to “bind” the services you created to the application you are working with.
In our example above, we will have to bind the hcfmysql-db with our HelloCloudFoundry app .
to do so, you can run “cf-bind-service” command.
For instance, in our example we can do:

grails cf-bind-service hcfmysql-db --appname=hellocloudfoundry

You should see the following output

Creating new service binding to 'hcfmysql-db' for 'hellocloudfoundry'.

Application 'hellocloudfoundry' updated
Service 'hcfmysql-db' added

Application 'hellocloudfoundry' stopped.

Trying to start Application: 'hellocloudfoundry'.
Application 'hellocloudfoundry' started.

3. Setting up the app to use the database
When we do the binding to a cloud foundry application, CF will set up the connection settings to the services at runtime. This makes the settings very transparent and intuitive.
Lets configure our app for the data.
Edit the DataSource.groovy found under /grails-app/config

production {
        dataSource {
			dialect = org.hibernate.dialect.MySQLInnoDBDialect
			driverClassName = "com.mysql.jdbc.Driver"
			username = "n/a"
			password = "n/a"
            url = "n/a"
            dbCreate = "update"

Note that i have set up ‘n/a’, under username, password and url. You can put anything you want here as these will be overridden by cloudfoundry at runtime.

This is it! You are done.

4. Putting it all together
Let’s verify that our database is working. To do this we can create a “Domain” Class and we will do a scaffolding.

Lets create a Domain class called “Person” with id and name as two attributes.

grails create-domain-class Person

We will update the class as follows

package hellocloudfoundry

class Person {
    long id
    String name
    static constraints = {

We will create a controller as follows

grails create-controller Person

Update the controller as follows

package hellocloudfoundry

class PersonController {

    def scaffold = Person

Lets the deploy the app on the cloudfoundry.
Since we had already deployed the app before you can do an update

grails prod cf-update

Now you can navigate to your app in my example I deployed my app as
so i can navigate to


Ubuntu Window Placement Similar to Win7

With Ubuntu 11.04, It came with ‘Grid’ enabled under ‘Window Management’. This means, if you drag a ‘Window’ of an app to the right of the window it will stick to the right and left if you do left, taking half of the screen very similar to how Windows 7 has done. Drag from the mouse works fine however, the key combination is different than the Window. In this post I will talk about how to make it similar to how it works on Windows i.e. “Win + Right” key will stick the Window the right and “Win + Left” will stick the Window to the left.

To do this you need “CompizConfig Settings Manager” installed if you don’t have it already.
If you don’t then you can do so by running following command.

$ sudo apt-get install compizconfig-settings-manager

CompizConfig Settings Manager
Go to CompizConfig Settings Manager by going to “System>Preferences>CompizConfig Settings Manager”
Scroll down to the “Window Management”
Compiz Window

1. Click on the ‘Grid’ to manage the key bindings.
2. For this example lets work on the putting the window to the Left (to put on Right follow the same instructions except for picking the right key combinations)
a. Click on the key binding for action you want to edit, in this case right next to ‘Put Left’, (currently has ‘KP4’)
b. You will be prompted with a modal window, click on ‘Grab key cobination’

c. After you click on that button press the key that you want to bind the action with in our case lets do “Window Key + Left Arrow”
d. It should show ‘<Super>Left’ then click on ‘Ok’ button on the window to close it.
e. Now if you click on ‘Win’ Key + Left arrow then current window will take half of the screen and will be on the left side.

You can follow similar steps to do for pan the window right, maximize, minimize etc.

Mounting .disk file on Ubuntu

As mentioned in my earlier post, I recently upgraded my Ubuntu to 11.04. In my 10.10, I had created a virtual HDD called “home.disk” where I stored all my ‘home’ content. Now its time to recover some of that info.
This is how I am able to mount a ‘.disk’ file as a drive on ubuntu and be able to copy the content.

1. Creating a mount point
First of all you need to create a directory where you will be mounting your .disk file to

  $ sudo mkdir /media/homedisk

2. Mounting the disk file
I have my “home.disk” file in my external HDD which is mounted in my ubuntu under “/media/Elements/home.disk”
So, my command to mount it is as follows, (If you .disk file is elsewhere just point to that)

  $ sudo mount -o loop /media/Elements/home.disk /media/homedisk

There you have it!, now you should see an icon on your desktop called ‘homedisk’ and if you click in there, you should see the contents of the file!

Setting up Groovy and Grails on Ubuntu 11.04

After upgrading my Ubuntu since 8.04 to 10.10 as a regular system upgrade my system started becoming really slow. So, I decided to do a clean install of Ubuntu 11.04.
As I am going through this I wanted to document the process of setting up Ubuntu 11.04 with Groovy and Grails.(Note: I have Ubuntu 64 bit installed, there may be extra steps I may had to do compared to 32 bit.)

1. Installing JDK
As a first steps, we will have to install JDK.
Lets run the following command to install the JDK on the system

$ sudo apt-get install sun-java6-bin sun-java6-jre sun-java6-jdk

2. Installing Eclipse
a. Let’s grab the latest version of eclipse from the website

b. I got the “Eclipse IDE for Java EE Developers” (64- bit version as my Ubuntu is 64 bit)

c. After you obtain the zip, simiply unzip the content to the home folder. This should result in structure like “/home/manijshrestha/eclipse”

3. Groovy Eclipse plug-in
You can follow the instructions in groovy website in

a. go to “Help>Install New Software… ”
b. Put “; and hit enter
c. Install “Groovy-Eclipse”, “Groovy Eclipse SDK” and “Extra Groovy Compilers”

4. Grails Install
For grails, spring source provides with STS plugin for eclipse, Lets install that,
For that, Similar to Groovy Install, lets install the STS plug-in. Lets follow the steps below

a. go to “Help>Install New Software… ”

b. Click on “Add” Button and put following URL
(you can give a name like “Spring STS” etc)

c. Click on “Add” Button again to add URL for dependencies:
(you can give name like “Spring Dependencies”)

d. Back in the “Install New Software” window, choose to work with: ”SpringSource update site for Eclipse 3.6 Release”

e. Select ”SpringSource Tool Suite Grails Support” under the category ”Extensions / STS”

f. Press the Next button and follow the wizard to complete the installation. The installation might seem to be stuck at zero percent for a couple of minutes, just be patient. You will also get a warning that you are trying to install unsigned content which is normal in this case.

g. Re-start eclipse as prompted and you are DONE!
(you can verify the settings by going to File>New>Other – You should see “Groovy” and “Grails” listed…)

These plug-ins are installed inside Eclipse so in the “Terminal” you may not see a result if you do “groovy -version” or “grails -version”.

I will post on how to manually install Groovy and Grails on Ubuntu in my next Post.