Write an OpenShift app using Play !

April 14, 2012

First create a simple play application, for that run the following commands from cmdline

play new simpleapp

where simpleapp is the name of the application, this will create a folder named simpleapp containing play auto generated stuffs.

Now edit the simpleapp/app/views/Application/index.html, replace the contents of index.html with

<h1>simple play application</h1>

Running this app locally is very simple, just run the command below [sourcecode language=”bash”] play run simpleapp [/sourcecode]

You can view your application at localhost:9000 in some web browser.

Now lets go to Redhat OpenShift PaaS, and create an account at https://openshift.redhat.com. Once account is created and OpenShift client tools are setup, we can go for creating an application in OpenShift.

Basically app URL will be like [sourcecode language=”html”] http://<application name>-<domain-name>.rhcloud.com [/sourcecode], so create a domain and application. Domain creation is done by running

[sourcecode language=”bash”] rhc-create-domain -n cloudfundoo -l mail@service.com -p password [/sourcecode]

After that create the application

[sourcecode language=”bash”] rhc-create-app -l mail@service.com -p password -t jbossas-7 -a simpleapp [/sourcecode]

On application creation it will create a directory with same application name, contents of directory will be

[sourcecode language=”bash”] #ls simpleapp deployments pom.xml README src [/sourcecode]

we will remove both pom.xml and src directory, which is not needed for our play app, remove from git tree also.

[sourcecode language=”bash”] #rm -rf pom.xml #rm -rf src #git add -A #git commit -m “Remove the default code from git” #git push origin [/sourcecode]

Lets create a war for our simpleapp play framework application. Run this

play war <directory of play simpleapp> 
-o <directory of OpenShift simpleapp directory>/deployments/simpleapp.war

Now create a simpleapp.war.dodeploy file to tell jboss to go ahead and deploy our app.

#touch simpleapp/deployments/simpleapp.dodeploy

Finally we need to push our play app’s war to OpenShift app git repository. For that we need to run these.

#cd simpleapp/deployments
#git add -A
#git commit -m "Checking in new play app war"
#git push origin

Now time to see your live cloud app at http://simpleapp-cloudfundoo.rhcloud.com/simpleapp/