Gradle Plugin for Cloud Foundry PaaS

Image
Monsieur Cool

Cédric Champeau invented this really useful plugin for the gradle eco-system. It allows build.gradle scripts to use the full power of the gradle eco-system to manage and upload full-blown sites to a PaaS provider like Pivotal.

scott frederick
J.R.Ewing, Dallas

More recently my colleague Scott Frederick ( Twitter handle @scottyfred ) has added further improvements into this wonderful plugin. With it, we can now have gradle scripts programatically talk to a target cloud foundry PaaS provider. His post about this really useful piece of tooling can be found here.

Intro

Hero

caelyfAs you may or may not know, Cloud Foundry (http://www.cloudfoundry.com/) is an open source platform for cloud computing vendors.

It offers a standard set of API attachment points that an applications can use to work with the cloud foundry feature set. For this post, we’ve used samples from deployment of our own toolkit called Caelyf. It’s written in the groovy language by Guillaume Laforge of groovy fame. Caelyf is a small toolkit of features and templates for simple web services. It’s targeted specifically at the PaaS Cloud Foundry platform. ( Contact details at bottom of page ).

PaaS

A number of vendors have decided to offer the cloud foundry platform as a cloud computing service for private clients and enterprises, hence we get the abbreviation PaaS – platform-as-a-service. Because it’s a standard package, our applications can benefit from free (limited) or paid deployments on someone else’s hardware – hey, good-Oh! They keep it ticking, do the backups, install and manage software, url assignments and also the network connections for us.

Remote System Management

To manage a remote application, application developers need a tool. For us, there is a ruby gem named ‘CF‘ – short for cloud foundry. The CF tool is a command line app for your terminal or command line sessions. Some terse documentation can be found here: http://rubydoc.info/gems/cf/5.4.4/frames. CF has a variety of commands. Typically we would log in to the target system with a command like ‘cf login‘ and then provide credentials. When approved, a ‘cf push‘ command would send our app to the vendor’s system. With the CF tool installed, we do this :

Our Tasks

With an active internet connection, all we do, as app developers, is :

1. sign up with a vendor to get access credentials. ( Try Pivotal for a free trial at https://login.run.pivotal.io/login )
2. write an app in an acceptable programming language like java or groovy, ( or others depending on vendor )
3. compile the app
4. create a ‘war’ file for the target system containing the app
5. download the CF ruby gem as a command line tool from here: http://rubygems.org/gems/cf
6. cd into the folder of your app’s ‘war’ file
7. cf login and cf push <app name>
8. pick any data persistence services the app needs
9. identify the vendor target api upload address – usually “https://api.run.pivotal.io” for Pivotal
10. declare the uri name the app is to have on the internet, our example below is “caelyftemplate.cfapps.io” – yes, we know the GPars links are not working yet  they’re on the buglist :}
11. wait for the upload to complete
12. open a browser and point it to the address from step 10.
13. enjoy your app – live – in all it’s glory for all the world to see !

Automated Deployment

Ok, the cf tool is necessary for manual steps to keep an app running in the cloud, but i like the idea of making PaaS deployment a part of the build cycle, not a bunch of manual steps that might go wrong. Fortunately, as noted above, there is a plugin for the gradle build tool we can leverage to get the job done. Cédric Champeau did the origin version of this plug for cloud foundry version one and Scott has updated it for version two.

Plugin Overview

Scott’s provided an overview of the gradle plugin for cloud foundry on the github repository here : https://github.com/cloudfoundry/cf-java-client/tree/master/cloudfoundry-gradle-plugin. This plugin offers similar services and features as provided by the ruby gem CF tool. With this gradle plugin, we can programatically do many of the tasks needed to deploy and manage applications on a cloud foundry platform.

Sample Gradle Script

Here is a sample gradle build script we used to upload our own caelyf open source web framework. You’ll need to change some of the properties before you use it. A complete list of plugin properties is at the bottom of this post.

The ‘version‘ and ‘group‘ should be those of your app, your credentials need to replace the ‘username‘ and ‘password‘ fields, the ‘application‘ name should be that of your app, the ‘file‘ declaration for your war file should point to your war file – NOT to a directory! It MUST be packaged as a war file! The ‘uri‘ reflects your app’s name and is what users key into their browser address bar to access your app. The ‘env‘ variable is just for demo purposes, you can drop it, and ‘serviceInfos‘ can be adjusted to the persistence type your app needs. See the full list of config options at the bottom of this post.

deployCF.gradle


// caelyf template project: use gradle plugin to upload to cloud foundry PaaS
import org.apache.ivy.plugins.resolver.URLResolver
apply plugin: 'cloudfoundry'

// current Caelyf version
version = '1.1.1'
group = 'org.caelyf'

buildscript {
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/springsource-milestones" }
}
dependencies {
classpath group: 'org.cloudfoundry', name: 'cf-gradle-plugin', version: '1.0.1'
}
}

// plugin details here
cloudfoundry {
username="men@work.com"
password="dudesRus"
target = "https://api.run.pivotal.io"
space = "development"
application = "caelyftemplate"
file = file("build/libs/template-project-1.1.1.war")
uri = "caelyftemplate.cfapps.io"
memory = 512
instances = 1

env = [
"greeting": "Hello"
]

serviceInfos {
"redis${randomWord}" {
label = "rediscloud"
provider = "Garantiadata"
version = "n/a"
plan = "25mb"
}
}
}

Sample Terminal Session

We used the above gradle script for these sessions. It was named deployCF.gradle and we had previously installed the gradle build tool from http://www.gradle.org/ download point. The gradle ‘bin’ folder was added to our system’s PATH variable.

This attempt to upload to Cloud Foundry failed. We had declared too small a memory size of 256mb for the target application. This caused the PaaS service to ‘flap’, meaning ‘to fail’


redapple:jim /Volumes/PENDRIVE/caelyf-1.1/caelyf/template-project $ gradle -b deployCF.gradle cf-push
:cf-push
Creating service redisc3cf7
Creating application caelyftemplate
Uploading '/Volumes/PENDRIVE/caelyf-1.1/caelyf/template-project/build/libs/template-project-1.1.1.war'
Starting caelyftemplate
-----> Downloaded app package (9.4M)
-----> Downloaded app buildpack cache (38M)
-----> Downloading OpenJDK 1.7.0_45 from http://download.pivotal.io.s3.amazonaws.com/openjdk/lucid/x86_64/openjdk-1.7.0_45.tar.gz (3.0s)
Expanding JRE to .java (1.0s)
-----> Downloading Tomcat 7.0.47 from http://download.pivotal.io.s3.amazonaws.com/tomcat/tomcat-7.0.47.tar.gz (0.0s)
Expanding Tomcat to .tomcat (0.1s)
-----> Downloading Buildpack Tomcat Support 1.1.1 from http://download.pivotal.io.s3.amazonaws.com/tomcat-buildpack-support/tomcat-buildpack-support-1.1.1.jar (0.0s)
-----> Uploading droplet (46M)

Checking status of caelyftemplate
0 of 1 instances running (1 down)
0 of 1 instances running (1 down)
0 of 1 instances running (1 down)
0 of 1 instances running (1 starting)
0 of 1 instances running (1 starting)
0 of 1 instances running (1 starting)
0 of 1 instances running (1 starting)
0 of 1 instances running (1 down)
0 of 1 instances running (1 down)
0 of 1 instances running (1 down)
0 of 1 instances running (1 down)
0 of 1 instances running (1 flapping)
:cf-push FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':cf-push'.
> Application caelyftemplate start unsuccessful

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 4 mins 8.943 secs

// ok, that means we needed to bump memory allocation to 512MB, and as you see below, our successful attempt looked like this:


redapple:jim /Volumes/PENDRIVE/caelyf-1.1/caelyf/template-project $ gradle -b deployCF.gradle cf-push
:cf-push
Creating service redis9zcdk
Creating application caelyftemplate
Uploading '/Volumes/PENDRIVE/caelyf-1.1/caelyf/template-project/build/libs/template-project-1.1.1.war'
Starting caelyftemplate
-----> Downloaded app package (9.4M)
-----> Downloading OpenJDK 1.7.0_45 from http://download.pivotal.io.s3.amazonaws.com/openjdk/lucid/x86_64/openjdk-1.7.0_45.tar.gz (0.8s)
Expanding JRE to .java (1.0s)
-----> Downloading Tomcat 7.0.47 from http://download.pivotal.io.s3.amazonaws.com/tomcat/tomcat-7.0.47.tar.gz (3.3s)
Expanding Tomcat to .tomcat (0.1s)
-----> Downloading Buildpack Tomcat Support 1.1.1 from http://download.pivotal.io.s3.amazonaws.com/tomcat-buildpack-support/tomcat-buildpack-support-1.1.1.jar (0.0s)
-----> Uploading droplet (46M)

Checking status of caelyftemplate
1 of 1 instances running (1 running)
Application caelyftemplate is available at http://caelyftemplate.cfapps.io
BUILD SUCCESSFUL

Total time: 2 mins 11.812 secs

Results

Ok, for a limited time, while our trial period lasts, you can point your browser at the temporary address of  http://caelyftemplate.ng.bluemix.net. Note that the trial time period may expire before you can try this. If so, point your browser at our alternate example at http://caelyftemplate.cfapps.io/ This might take one or two trys as the PaaS server can take a little time to wake up. :}

Caelyf

Millennium_FalconFor more samples of Caelyf, see our website at http://www.caelyf.org and a free template is running here:http://caelyftemplate.ng.bluemix.netDownload the template from our website.

The PaaS menu link from our home page has a list of several PaaS vendors we’ve successfully tried our apps on. Note we have only tried this gradle plugin on the Pivotal cloud foundry target. Hope to verify it will work on those other vendors as well. Please remember that new PaaS vendors are coming online every day, so our list might expand in the future.

Looks like cloudy times are just arouund the corner. Good hunting !


Plugin Configuration Options

  • target – the URL of the target API (defaults to http://api.run.pivotal.io and is different for each vendor)
  • organization – the name of the Cloud Foundry organization to target – optional if only one exists
  • space – the name of the Cloud Foundry space within the organization to target like live,test,development
  • username – your username on the target platform
  • password – your password on the target platform
  • application – the name of your application (defaults to the Gradle project name)
  • file (type: File) – path to the WAR file (not folder) to be deployed
  • memory – amount of memory for an application in megabytes (defaults to 512)
  • instances – number of instances (defaults to 1)
  • uri – a URI to map to the application for your user’s browser address
  • uris (type: List) – a list of URIs to map to the application – advanced configs
  • command – the command to run when the application is started – advanced configs
  • buildpack – the URL of a buildpack to use to stage the application – advanced configs
  • env (type: Map) – environment variables to set for the application – advanced configs
  • startApp (type: boolean) – start the application on push (defaults to true) – advanced configs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s