Deploying Grails app via CloudFoundry Paas

UPDATE: The new version of CloudFoundry API is ‘cf’ instead of ‘vmc’ and the target url is ‘https://api.run.pivotal.io’

In this post we go over step by step to deploy Grails app on cloud via CloudFoundry

Step 1:  Make sure you have set up CloudFoundry(VMC tool) on your machine. For help on it, please,  see post ‘Starting with CloudFoundry on Windows’

Step 2:  Sign up for an account on CloudFoundry website. You need that in order to deploy on cloud

Step 3:  Create WAR file for your Grails App.  One way to do it is run the following command:

grails war target/appName-hotfix-1.0.1.war

this will create the WAR file and put it in the ‘targets’ folder

Perhaps, you like to test it before deploying by running it from the .war file as follows:
grails prod run-war target/appName-hotfix-1.0.1.war -restart

Step 4: Authenticate with CloudFoundry.  Open command line with ruby and go to the ‘targets’ folder containing the WAR file for your app. In this folder run the following command:
vmc login [username]
This will ask for your password. If you do it for first time, you will receive error like the following:

Errno::ENOENT: No such file or directory - /homeDirectory/.vmc/target

In that case, run the following command:
vmc target https://api.cloudfoundry.com

This command links to cloud instance. In the above example, it is for public cloudfoundry cloud instance. it may as well be your micro cloud instance or other,then use their Url in place of ‘api.cloudfoundry.com’

Step 5. Deploy you app. From the ‘target’ folder after authenticated run command ‘vmc push’ as following:

During the deployment as you can see, it will ask for a unique name of the application, instances to run on, and services like DB connections.

If you have application already running, then you can find name:

vmc apps ;this will list all apps running by name
vmc push appName

Step 6. Check your app on the link provided(green)

Importing Data And Connect Database

Connecting Database to import data is the same way as described in blog ‘Importing MySQL on WordPress Cloud Instance‘. Instead using App Fog command ‘af tunnel’, use ‘vmc tunnel’ and rest is the same. For the first time, it my ask to install tunnel vmc plugin. In that case run the following:
vmc install tunnel-vmc-plugin

Other:

Command menu – to see all different vmc commands, run ‘vmc -h’

Troubleshooting – best way to troubleshoot is run ‘vmc logs appName’ and it will display logs

Saving configurations. If you select to save configurations during Step 5, then next time you like to updated your app run command:

vmc update

With the saved configurations the VMC tool will update your app instead of fresh deploayment

Issues:

1.  You may be receiving ‘Not Authorized’ message when deploying. In that case authenticate again by running:

vmc login

as demonstrated here:

2. The following two issues were encountered:

TypeError: can't convert nil into String
-and-
[...]Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems.rb:230:in `activate':can't activate cfoundry[...]

Solution, it was upgrading Ruby gems and reinstalling VMC as following:

sudo gem install rubygems-update
sudo update_rubygems
sudo gem uninstall vmc 
sudo gem install vmc --pre

The fix is in vmc version 0.5.1.rc5 as described following cloudfoundry issue 65
In order to run vmc version 0.5.1.rc5, its needed to have ruby 1.9 or higher. If you need to upgrade, here is good instructions

3. After pushing and at the startup, there was following error:
CFoundry::AccountNotEnoughMemory: 600: Not enough memory capacity, you're allowed: 2048M
Solution: Allocate less memory for the app. To do so, edit ./manifest.yml file and update the memory to something less:

...
- instances: 1
  memory: 1G
  services: 
    mysql-a1638:
...

4. When pushing, i got following error:

0/1 instances: 1 starting
  0/1 instances: 1 starting
  0/1 instances: 1 down
  0/1 instances: 1 down
  0/1 instances: 1 down
  0/1 instances: 1 starting

This was because mysql database service was containing tables from previos deployment. Alternative solutions:
a) Drop the tables by remotely accessing database on cloud via ‘vmc tunnel’ and then MySQLWorkbench(see following post to get you started)
b) Delete the database service and then creating new one. Don’t forget to bind the service back to the application
Any of the two alternatives solved the problem

5. CFoundry::TargetRefused: target refused connection (getaddrinfo: nodename nor servname provided, or not known)
This error was because we were using the Old CloudFoundry API (i.e ‘vmc’) but it needed the new ‘af’ with a new target url ‘https://api.run.pivotal.io’

6. Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
You need to include the MySQL driver with your application. Previously the Java build pack would include this JAR for you, however it no longer does this. To include MySQL driver, add the following runtime dependency in the BuildConfig.groovy:

 dependencies {
...
        runtime 'mysql:mysql-connector-java:5.1.20'
...
       }
    }

And then make sure that your “repositories” block contains “mavenLocal()” and “mavenCentral()”, so that it will load the JAR from Maven central(in BuildConfig.groovy):

     repositories {
...
         mavenLocal()
         mavenCentral()
     }

Leave a Reply

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