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()
     }

Importing MySQL on WordPress Cloud Instance

Prerequisites: MySql, VMC, AppFog installed

In prevouse post, i deployed my first PHP app on Cloud. The reasoning for doing was, so i don’t have to configure and maintain my local development infrastructure(Apache, PhpMyAdmin,etc). So, I make a change following with push to cload and the resulting instance on cloud is my development solution ready for testing, however. In order to do, we need import data into MySql on the cloud instance to keep it in sync with production environment. This post is exactly how we accomplish it with ColdeCott  – an extension of CloudFoundry that lets run commands on MySql instance residing on cloud feeding our PHP App

Step 1 – Install DevKit and ColdeCott

DevKit is needed, so we are able to install ColdeCott. To install DevKit, go to rubbyinstaller page and download DevKit. Afterwards, extract DevKit and in ‘Command Line with Ruby’ go to the folder where you extracted DevKit and run the following:

In the above screen shot, the last command was run to install ColdeCotte – ‘get install CaldeCotte’

Step 1 For Mac – Install ColdeCott via XCode

For Mac environments, the Coldecott is installed via XCode as follows:

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer   
sudo gem install caldecott

Step 2 – Connect to MySql instance

To connect with the particular MySql instance feeding your app, we use a command ‘tunnel’ from Command Line with Ruby  that establishes the connection. Here is the screen shot:

!!! Before tunneling make sure you are logged in by running ‘af login -u appFogUsername’

The Shell on the left you tunnel(connect to) MySQL instance and afterwards, you are able to run any inquiries you like via mysql command. As you can also see while connecting MySQL, it displayed the current instance username, password and database name that you will need next step when importing data.

Step 3 – Import Data

Once you tunneled in your MySql instance (see shell on the left in the above screen shot), you can use another shell from which you can run mysql command to import data as it is displayed in the shell on the left of the screen shot above

Alternative is to use MySql Workbench with the same credentials given to you in Step 2

!!! Once you close the tunnel(shell), the connection is lost with the MySql instance

!!! The credentials seems to stay the same next time you open connection(tunnel) into the MySql instance

After data imported, we have  our development WordPress app working in sync with production and available for testing without much developers configuring and setting up Apache, PhpMyAdmin,MySql,PHP,etc. Next would be nice to have  Gradle script to do all this for us, so that once me make change, it auto updates our cloud instance but that will be another post….till then, happy coding!

Sources:

‘Manage you AppFog data from workstation’ by Jeremy Voorihs

Documentation Installing WebKit by the RubyInstaller Google Group

 

Deploying WordPress using AppFog PaaS

I started this post thinking that i will be able to deploy PHP app on cloud just like Grails app via CloudFoundry Paas.  As i learnt, cloudfoundry directly does not support php technology, however,  if run AppFog on top of cloudfoundry this mission – deploy PHP app such as WordPress on cloud  can be accomplished. This post demonstrates an example of exactly that – WordPress deployed in cloud

In order to use AppFog, we have to do one additional step to process described in post ‘Starting with CloudFoundry on  Windows‘. That is we install AppFog

Step 0 – Installing AppFog

Open Command line with Ruby as described in ‘Starting with CloudFoundry on Windows’ post. From Command line run the following command:

>gem install af

This installed AppFog, so you can use it to deploy WordPress

Step 1 – Sign up for account on AppFog.com.

Step 2 – Configure MySql Service

In order our WordPress app to communicate with MySql db in cloud, we have to set cloud environment variables (currently auto-configuration is available only for Node.js under cloudfoundry, however, its possible in near future it will work for other frameworks as well)

So edit wp-config.php as following:

Replace:
define('DB_NAME', 'dbName');
define('DB_USER', 'dbUser');
define('DB_PASSWORD', 'dbPass');
define('DB_HOST', 'margotskapacs.com');

With:

$services = getenv("VCAP_SERVICES");
$services_json = json_decode($services,true);
$mysql_config = $services_json["mysql-5.1"][0]["credentials"]
define('DB_NAME', $mysql_config["name"]);
define('DB_USER', $mysql_config["user"]);
define('DB_PASSWORD',$mysql_config["password"]);
define('DB_HOST',$mysql_config["hostname"]);
define('DB_PORT',$mysql_config["port"]);

As you see, ‘getenv(“VCAP_SERVICES”) is way to retrieve cloud instance variables that later is passed to your WordPress app

Step 3 – Deploy on Cloud

Open Command line with Ruby and run following commands:

>af login

This will prompt for username and password you registered Step 1

>af push

Given you are in the root directory of your WordPress app. If not, you specify location by ‘ af push –path somePath’

Afterward, follow the default settings and your application will be published under domain name – yourAppName.cloudDomain that you can also find out by logging into appfog.com

Next, how do we import MySql data into our WordPress PHP app residing on Cloud? That’s in next post – Importing MySQL on WordPress Cloud Intance

 

Starting with CloudFoundry on Windows

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

Currently, I am using CloudFoundry to deploy my Grails applications in production and development on cloud. This post is how to configure CloudFoundry on Windows, so you can deploy application. Please, see another post for how to deploy Grails Apps using CloudFoundry or PHP Applications using AppFog

Step 1 – Installing Ruby Installer

You will need this to intall VMC – command line tool to communicate with CloudFoundry Cloud Controller

Go to Ruby Installer website and run executable to install Ruby Installer

Step 2 – Installing RubyGems

To install RubyGems, open command line with Ruby(Start->Programs->Ruby XXX-> Start Command Prompt with Ruby) and run the following command:

>gem update --system

This will install RubyGems

Step 3 -Install VMC

Open Command Line with Ruby like described avobe. Run the following command:

>gem install vmc

This will install VMC tool and with it you are able to run commands on the CoudFoundry Controller to deploy your apps. For that see the post – ‘Deploy WordPress App WordPress on Cloud using CloudFoundry’

Starting On Mac

For Mac you can skip the ‘Step1 – Installing Ruby Installer’ as mac newest OS comes with the ruby installer. Otherwise, rest is the same from Mac as Windows

Issues

1. Environment variable ‘HOME’

We were getting following error:

non-absolute home
C:/DevTools/Ruby193/lib/ruby/gems/1.9.1/gems/vmc-0.3.21/lib/cli/config.rb:92:in `expand_path'
C:/DevTools/Ruby193/lib/ruby/gems/1.9.1/gems/vmc-0.3.21/lib/cli/config.rb:92:in `aliases'
C:/DevTools/Ruby193/lib/ruby/gems/1.9.1/gems/vmc-0.3.21/lib/cli/runner.rb:427:in `process_aliases!'
C:/DevTools/Ruby193/lib/ruby/gems/1.9.1/gems/vmc-0.3.21/lib/cli/runner.rb:460:in `run'
C:/DevTools/Ruby193/lib/ruby/gems/1.9.1/gems/vmc-0.3.21/lib/cli/runner.rb:14:in `run'
C:/DevTools/Ruby193/lib/ruby/gems/1.9.1/gems/vmc-0.3.21/bin/vmc:5:in `<top (required)>'
C:/DevTools/Ruby193/bin/vmc:23:in `load'
C:/DevTools/Ruby193/bin/vmc:23:in `<main>'

This happens if you set environment variable ‘HOME’ to something. Apparently, Ruby gems is using this variable, so by having it set to something causes the above error