Get Started with CloudCaptain & Grails

 

This tutorial will get you started with CloudCaptain and Grails. It should take you about 5-10 minutes to complete.

Prerequisites

Before you begin, ensure you have successfully:

  1. created a CloudCaptain Account (simply log in with your GitHub account, it's free)
  2. downloaded and installed the latest CloudCaptain Client
  3. downloaded and installed the latest version of VirtualBox
  4. downloaded and installed the latest JDK with JAVA_HOME set up correctly
  5. downloaded and installed the latest version of Grails with GRAILS_HOME set up correctly

Creating the Grails application

Creating a new Grails app is incredibly easy. All you need is:

> grails create-app getstarted-grails

Once that's done, you will have the following structure:

 getstarted-grails
   gradle
   grails-app
     assets
     conf
       spring
       application.yml
       logback.groovy
     controllers
     domain
     i18n
     init
     services
     taglib
     utils
     views
   src
     main
       groovy
       webapp
     test
       groovy
     integration-test
       groovy
   .gitignore
   build.gradle
   gradle.properties
   gradlew
   gradlew.bat

Your application is now fully set up. As your skeleton also includes Spring Boot's actuator (Spring Boot's turn-key production readiness features), you now have two urls available:

  • / : your controller
  • /health : Spring Boot actuator's health check page

Go ahead and build it:

getstarted-grails> grails war

Great. Your Grails application is now available under build/libs/getstarted-grails-0.1.jar.

Fusing a CloudCaptain image and running it locally on VirtualBox

Now it's time to fuse your application into a CloudCaptain image and launch an instance of it on VirtualBox:

getstarted-grails> boxfuse run

Fusing Image for getstarted-grails-0.1.jar ...
Image fused in 00:10.089s (97828 K) -> myuser/getstarted-grails:0.1
Launching Instance of myuser/getstarted-grails:0.1 on VirtualBox ...
Forwarding http port localhost:8080 -> vb-9feb5d7c:8080
Instance launched in 00:04.688s -> vb-9feb5d7c
Waiting for Payload to start on Instance vb-9feb5d7c ...
Payload started in 00:06.029s -> https://127.0.0.1:8080

CloudCaptain has automatically detected the Spring Boot Actuator and used the /health endpoint to check whether the instance came up correctly.

Open a browser at this address to see your new application up and running within the VirtualBox VM by simply executing:

getstarted-grails> boxfuse open

You can also see your newly created image:

getstarted-grails> boxfuse ls

Images available locally:
+------------------------------+---------------------------+-------+---------+-----------+--------------+---------+---------------------+
| Image                        |          Payload          | Debug |  Java   | AppServer |    Ports     |  Size   |    Generated at     |
+------------------------------+---------------------------+-------+---------+-----------+--------------+---------+---------------------+
| myuser/getstarted-grails:0.1 | getstarted-grails-0.1.jar | false | 8.60.22 | Grails    | http -> 8080 | 97828 K | 2015-12-21 17:47:50 |
+------------------------------+---------------------------+-------+---------+-----------+--------------+---------+---------------------+
Total: 1

As well as the instance that is running:

getstarted-grails> boxfuse ps

Running Instances on VirtualBox in the dev environment :
+-------------+------------------------------+---------------------+-----------------------+---------------------+
|  Instance   |            Image             |        Type         |          URL          |     Launched at     |
+-------------+------------------------------+---------------------+-----------------------+---------------------+
| vb-9feb5d7c | myuser/getstarted-grails:0.1 | 4 CPU / 1024 MB RAM | https://127.0.0.1:8080 | 2015-12-21 17:47:56 |
+-------------+------------------------------+---------------------+-----------------------+---------------------+
Total: 1

Deploying your application to AWS

Now let's deploy the image to AWS. As CloudCaptain works with your AWS account, it first needs the necessary permissions to do so. So if you haven't already done it, go to the CloudCaptain Console and connect your AWS account now.

Every new CloudCaptain account comes with 3 environments: dev, test and prod. dev is your local VirtualBox environment and test and prod are on AWS.

So let's deploy our application to the prod environment on AWS:

getstarted-grails> boxfuse run -env=prod

Creating myuser/getstarted-grails ... Pushing myuser/getstarted-grails:0.1 ... Verifying myuser/getstarted-grails:0.1 ... Waiting for AWS to create an AMI for myuser/getstarted-grails:0.1 in eu-central-1 (this may take up to 50 seconds) ... AMI created in 00:19.095s in eu-central-1 -> ami-fd5b4491 Creating Elastic IP ... Mapping getstartedgrails-myuser.boxfuse.io to 52.29.77.147 ... Creating security group boxsg-myuser-prod-getstarted-grails-0.1 ... Launching t2.micro instance of myuser/getstarted-grails:0.1 (ami-fd5b4491) in prod (eu-central-1) ... Instance launched in 00:50.707s -> i-2c1daf90 Waiting for AWS to boot Instance i-2c1daf90 and Payload to start at https://52.59.247.126:8080/health ... Payload started in 00:55.567s -> https://52.59.247.126:8080/health Remapping Elastic IP 52.29.77.147 to i-2c1daf90 ... Waiting 15s for AWS to complete Elastic IP Zero Downtime transition ... Deployment completed successfully. myuser/getstarted-grails:0.1 is up and running at https://getstartedgrails-myuser.boxfuse.io:8080/

Notice that CloudCaptain has reused your image unchanged instead fusing a new one.

With that one command CloudCaptain has automatically pushed your image to the CloudCaptain Vault as well as provisioned, configured and secured all necessary AWS resources. There is no manual work necessary on your behalf.

All you need to do is simply navigate to your new domain to see your Spring Boot application in action on AWS:

Bonus: update your application with zero downtime

Now let's take things one step further and deploy an update of your application with zero downtime using blue/green deployments.

Start by modifying grails-app/views/index.gsp and change the title:

<h1>Welcome to Grails on CloudCaptain!</h1>

then bump the version in build.gradle:

version "0.2"

and rebuild the jar:

getstarted-grails> grails clean
getstarted-grails> grails war

Finally deploy the new version of your application to AWS:

getstarted-grails> boxfuse run -env=prod

Fusing Image for getstarted-grails-0.2.jar ... Image fused in 00:10.048s (97827 K) -> myuser/getstarted-grails:0.2 Pushing myuser/getstarted-grails:0.2 ... Verifying myuser/getstarted-grails:0.2 ... Waiting for AWS to create an AMI for myuser/getstarted-grails:0.2 in eu-central-1 (this may take up to 50 seconds) ... AMI created in 00:18.013s in eu-central-1 -> ami-7b5a4517 Creating security group boxsg-myuser-prod-getstarted-grails-0.2 ... Launching t2.micro instance of myuser/getstarted-grails:0.2 (ami-7b5a4517) in prod (eu-central-1) ... Instance launched in 00:41.174s -> i-b502b009 Waiting for AWS to boot Instance i-b502b009 and Payload to start at https://52.59.254.138:8080/health ... Payload started in 01:27.645s -> https://52.59.254.138:8080/health Remapping Elastic IP 52.29.77.147 to i-b502b009 ... Waiting 15s for AWS to complete Elastic IP Zero Downtime transition ... Terminating instance i-2c1daf90 ... Destroying Security Group sg-5981cb30 ... Deployment completed successfully. myuser/getstarted-grails:0.2 is up and running at https://getstartedgrails-myuser.boxfuse.io:8080/

And there it is:

Summary

In this brief guide we have seen how to:

  • create a Grails application
  • fuse it into a CloudCaptain image
  • deploy the image locally on VirtualBox
  • deploy the image unchanged to AWS
  • update the application with zero downtime

Now it's your turn. Take your favorite Grails application and deploy it with ease and pleasure.

And don't forget, CloudCaptain also comes with a Gradle plugin to seamlessly integrate with your CI/CD workflow.

CloudCaptain Grails Documentation