Supporting Play Framework on OpenShift with the DIY Application Type

 

When lots of choice is not enough

In this article we’ll show you what you can do with the recently announced “do-it-yourself” application type on OpenShift. As an example, we’ll take a look at how to implement native support for a Play framework application.

 

When lots of choice is not enough

In this article we’ll show you what you can do with the recently announced “do-it-yourself” application type on OpenShift. As an example, we’ll take a look at how to implement native support for a Play framework application.

What’s Play? Play is a framework that makes it easier to build web applications with Java and Scala, allowing Java developers to achieve productivity levels only known to people working with frameworks like rails or django. Play is also one of those frameworks that abandoned the servlet container mantra and instead decided to take a different path. Play implements its own, lightweight and highly optimized, asynchronous http server based on Netty, a network framework which happens to be part of JBoss.

 

 

If you are familar with OpenShift you know it gives you lots of choices for developing and deploying applications on the cloud. You can pick among PHP, Ruby, Perl, Python, Node.js or Java. On the database side, you get to choose between MySQL, PostgreSQL and MongoDB.

In spite all these choices, web development is evolving so rapidly these days, that no platform can provide every possible option. Everyday it’s becoming more and more common to work with custom-made http servers built for enhanced responsiveness and greater scalability. On the Java side, what used to be the omnipresent servlet container, is no longer the only option available.

So, what can we do to support all these heterogeneous and constantly evolving technologies? Well, Red Hat engineers came out with a simple, yet powerful, solution.

(If you want to check out more Play framework goodness on OpenShift, then check out this webinar next week with Jorge Aliss, Mark Atwood and myself.)

Enter the DIY application type

These are the application types are currently supported by openshift:  jbossas-7, python-2.6, jenkins-1.4, ruby-1.8, diy-0.1, php-5.3, and perl-5.10 They are all pretty self-explanatory, except one of them that looks rather suspicious: diy-0.1

A “DIY” application is just a barebones app, with no server preloaded, ready to be tailored to your needs. With this new app type, OpenShift is begining to blur the line between an IaaS and a PaaS, providing you with a controlled and scalable environment, and at the same time giving you the freedom to implement the technology that best suits your needs. [We should note that as of this writing, the DIY application type is an experimental playground, so please set your expections accordingly.]

 

Let’s enable Play framework support

To show you the power that this new application type puts in your hands, we’ll implement one of the most voted feature request for Openshift: native support for Play framework.

Until today, if you wanted to deploy a Play application on OpenShift, your only option was to package it in a war file and deploy it to any servlet container (and indeed it’s very easy, have a look at this article), but you would be wasting resources and missing advanced features, like asynchronous request handling.

To take a sneak peek at what we are trying to achieve, you can have a look at the Github quickstart project we prepared for this post. You just have to create a new DIY application, pull the quickstart sources from the github repository, and then push the app to your OpenShift repo and that’s it! Here’s what that looks like:

rhc app create play diy-0.1 

cd play

git remote add upstream -m master <a href="https://github.com/opensas/play-example.git">https://github.com/opensas/play-example.git</a> 

git pull -s recursive -X theirs upstream master git push&nbsp;

After that your app will be ready at http://play-yournamespace.rhcloud.com

There are also quickstarts for Play 2 framework.

Now, if you want to take a deep dive to see how we developed these quickstarts and get the basis for tweaking OpenShift to fit your needs – read on!

Look ma, no servlets!

Before attempting to create a DIY application on OpenShift, you have to familiarize yourself with the technology you are about to use. You should have a clear understanding of the steps needed to set it all up on your workstation and then reproduce it on OpenShift.

For a Play application we won’t need JBoss, or any application sever, no Tomcat, and not even a servlet container at all. We just have to install the framework and start Play’s own http server.

Doing this on your own workstation is as easy as downloading Play, unzipping it, and then running:

play new demo

cd demo

play start

To stop the application we issue

play stop

Now we'll have to do the same in our server at OpenShift.

 

Getting to know our own cloud

Let's create a new application named 'raw'

rhc app create raw diy-0.1

Now let's see what we created:

rhc app show -a raw
 
Application Info
================
raw
    Framework: diy-0.1
     Creation: 2012-03-19T01:18:31-04:00
         UUID: youruuid
      Git URL: ssh://youruuid@raw-yourdomain.rhcloud.com/~/git/raw.git/
   Public URL: http://raw-yourdomain.rhcloud.com/

You can browse to http://raw-yourdomain.rhcloud.com/ to see the following page.

It's just the same static page you can find at raw/index.html

Now let's see what we have in our repo:

cd raw
ls -a
 
.git                      # our local git repo
misc                      # empty dir, you can just deleted it, no one will miss it
.openshift/action_hooks   # this is where our hook scripts are located
raw                       # it holds the static page
README                    # some info

Pretty barebone app, but there's a folder that's quite interesting for us:

ls .openshift/actions_hooks
build  deploy  post_deploy  pre_build  start  stop

These are the scripts that OpenShift uses for building, deploying, starting and stopping our app. These scripts are executed on the remote OpenShift server, so now we'll have a look at it to better know our environment.

Take the output from rhc app show -a raw and issue the following

ssh youruuid@raw-yourdomain.rhcloud.com

You'll be at your HOME directory on your OpenShift server. You have the following structure:

git
    .env            # Contains the definition of your environment variables
 
    &lt;your app&gt;.git  # That&#39;s your git repo at ssh://youruuid@raw-yourdomain.rhcloud.com/~/git/raw.git/
 
&lt;your app&gt;
 
    repo            # $OPENSHIFT_REPO_DIR - Here you&#39;ll find the content of you app folder.
                    # everytime you push changes the data is saved here.
 
    data            # $OPENSHIFT_DATA_DIR - This is a persitent directory, 
                    # the info in there will survive the server restarts. 
 
    logs            # $OPENSHIFT_DIY_LOG_DIR - This is where you should save the logs of your app. 
                    # It&#39;s the folder that&#39;s scanned by the &#39;rhc app tail&#39; command
 
/tmp/             # $OPENSHIFT_TMP_DIR - Temporary folder, you have write permissions in there

These are basically the folders that we are interested in.

 

Ok, show me the code

When we issued the rhc app create command with the DIY application type, we created a raw application, so before doing anything else we'll have to put a Play application in there. I'll take the Zencontacts sample app as an example. So, starting in your raw folder, copy a Play app like this:

cp -r &lt;play installation folder&gt;/samples-and-tests/zencontact/* .

Now we'll take a simplified version of the Play 1.x quickstart project to see how to do it. We added links to the real script for you to have a look at therm.

First we'll have to prepare the .openshift/action_hooks/pre_build script to check if the framework is installed, and if it's not you should download it and install it.

You could issue something as simple as this:

.openshift/action_hooks/pre_build (script at github)

if ! [[ -d ${OPENSHIFT_DATA_DIR}play-1.2.4 ]]; then
  curl -o ${OPENSHIFT_DATA_DIR}play-1.2.4.zip http://download.playframework.org/releases/play-1.2.4.zip
  unzip ${OPENSHIFT_DATA_DIR}play-1.2.4.zip
  rm ${OPENSHIFT_DATA_DIR}play-1.2.4.zip
fi


Then to start the application

.openshift/action_hooks/start (script at github)

cd ${OPENSHIFT_REPO_DIR}

.openshift/action_hooks/stop

#tell play to save log files in openshift log dir
export PLAY_LOG_PATH=${OPENSHIFT_DIY_LOG_DIR}

#run with openshift id
${OPENSHIFT_DATA_DIR}play-1.2.4/play start --%openshift

Don't forget to configure your server to listen to port ${OPENSHIFT_INTERNAL_PORT} on address ${OPENSHIFT_INTERNAL_IP}

In our case we just add the following lines to our application.conf file:

%openshift.http.port=${OPENSHIFT_INTERNAL_PORT}
%openshift.http.address=${OPENSHIFT_INTERNAL_IP}

And now all we have to do is the stop script

.openshift/action_hooks/stop (script at github)

cd ${OPENSHIFT_REPO_DIR}

if [[ -f "server.pid" ]]; then
  ${OPENSHIFT_DATA_DIR}play-1.2.4/play stop
fi

And that's it!

Now we just have to commit our changes and push them to openshift.

git add .

git commit -m &quot;zentask sample app running on openshift&quot;

git push

You will then see our little beast in action, downloading and installing play 1.2.4, and finally running our application. Go on, have a look at you app at: http://raw-yourdomain.rhcloud.com/

Remember that this is a simplified version, with no error checks and no logging. For a more complete example have a look at the real quickstart scripts in OpenShift

 

Play on playaz!

From its very beginning, OpenShift has provided support for a wide range of standard technologies. With this new option, it also gives you the tools to completely customize it to your needs. You just need to get familiar with the OpenShift environment and start hacking away some simple bash scripts. I'm really looking forward to see what the community will create with the new Do-It-Yourself app type on OpenShift.

Categories
Java, OpenShift Online
Tags
Comments are closed.