RUN@cloud » Play Framework

Play Framework

Last modified by Michael Neale on 2013/04/30 08:26

CloudBees includes first-class support for running Play! applications in the Cloud.

  • Easy deployment of Play! apps to RUN@cloud
  • Cloud-based databases (RDBMS, MongoDB, CouchDB etc)
  • Continuous Integration/deployment for Play! with Jenkins
  • GIT source code repositories for team collaboration

To get "git push" style deployment, run the clickstart that sets up the above in one click. This will set up a play 2.1.1 starter app (we try to keep it up to date - easy to update) websocket ready, with a play/sbt build/test.

For those who want to deploy via other means, or want to know more, please do read on. CloudBees runs play2 dists "natively".

Creating a Play2.x application

$ play new myPlayApp
       _            _
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/
            
play! http://www.playframework.org

The new application will be created in /Users/spike/webapps/myPlayApp

What is the application name?
> myPlayApp

Which template do you want to use for this new application?

  1 - Create a simple Scala application
  2 - Create a simple Java application
  3 - Create an empty project

> 1

OK, application myPlayApp is created.

Once the application has been created you can use the play command again to enter the Play 2.0 console .  You can then use the start command to launch your application for local development.

$ cd myPlayApp
$ play start
Play server process ID is 72021
[info] play - Application started (Prod)
[info] play - Listening for HTTP on port 9000...

Deploying a Play2 application

Using the Cloudbees SDK

The best way to deploy your Play2 application to CloudBees is to export the application as a Play Distribution file and then deploy the app with our Play2 ClickStack. This is what is used by the Play2 Clickstart currently for you: 

$ play dist
$ bees app:deploy -a APP_ID -t play2  dist/APP_NAME-VERSION.zip

Alternatively, you may also stage, zip your project directory and then deploy, it's up to you and the ClickStack will sniff the contents of the zip file to know how to handle your application.

$ play stage
$ zip ../playapp.zip .
$ bees app:deploy -a APP_ID -t play2 ../playapp.zip

SBT plugin

The SBT plugin is an easy way to deploy Play2 apps onto CloudBees from within the play console. It is also very simple to configure.

Add the following to project/plugins.sbt

resolvers += "Sonatype OSS Snasphots" at "https://oss.sonatype.org/content/repositories/snapshots"

addSbtPlugin("com.cloudbees.deploy.play" % "sbt-cloudbees-play-plugin" % "0.3-SNAPSHOT")

Then project Build.scala
import cloudbees.Plugin._
//...
//...
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA)
    .settings(cloudBeesSettings :_*)
    .settings(
  CloudBees.applicationId := Some("<applicationname>")
)

We need the api key/secret for the CloudBees platform. If you have used the CloudBees SDK before, this will be populated for you already. If not, then create ~/.bees/bees.config and add the following (You can get the secret and key from Grandcentral)

bees.api.secret=XXXXXXXXXXXXXX=
bees.api.url=https\://api.cloudbees.com/api
bees.api.key=XXXXXXXXXXXXXXX
bees.project.app.domain=<accountname>

Once you have this, run play cloudbees-deploy from your app folder.

play cloudbees-deploy
[info] Loading global plugins from /Users/ivan/.sbt/plugins
[info] Loading project definition from /Users/ivan/CloudBees/sbt-cb-app/project
[info] Set current project to sbt-cb-app (in build file:/Users/ivan/CloudBees/sbt-cb-app/)

Your application is ready in /Users/ivan/CloudBees/sbt-cb-app/dist/sbt-cb-app-1.0.zip

[info] Deploying application '<accountname>/<appname>' to Run@Cloud
Uploading delta archive: /Users/ivan/CloudBees/sbt-cb-app/dist/DELTA-sbt-cb-app-1.0.zip
........................uploaded 25%
........................uploaded 50%
........................uploaded 75%
........................upload completed
deploying application to server(s)...
[info] Application avalible at http://<appname>.<accountname>.cloudbees.net
[success] Total time: 37 s, completed Oct 1, 2012 9:45:05 PM

If you want to deploy your play app with a different configuration file, you can use the following command:

play "cloudbees-deploy-config CONFIG_NAME"

Where CONFIG_NAME is the base name (without .conf) of the file in your conf directory.

Using the Play2 ClickStart

The Play2 clickstart currently uses our Play2 ClickStack, which is handling Play2 apps natively - see below for how to customise it after you have launched it.

Websockets

With Play2 - you will probably want to use websockets - you can with cloudbees. 

To enable you will need the CloudBees SDK: 

bees app:proxy:update -a yourAppId httpVersion=1.1

You only need to do this once.

CloudBees Databases

The CloudBees database service can be used from your play application, by doing the following:

  • Create a database (you can also do this from the UI if you like)
    bees db:create YOUR_DB_NAME -u YOUR_DB_USERNAME -p YOUR_DB_PASSWORD
  • Edit your apps database configuration. Tip: use "bees config:set" command to store credentials to anything sensitive - you can then refer to them from your application.conf file as ${config_name} - no secrets in source code!

Your configuration should look like this: 

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://HOST/DBNAME"
db.default.user=${DB_USER}
db.default.pass=${PASSWORD}
db.default.extendedProfile=scala.slick.driver.MySQLDriver
db.default.partitionCount=1
db.default.maxConnectionAge=1 minute
db.default.connectionTimeout=10000
db.default.maxConnectionsPerPartition=2
db.default.minConnectionsPerPartition=2

The MySQL driver will also be needed, add this to your project Build.scala config:

"com.typesafe.slick" %% "slick" % "1.0.0",
    "mysql" % "mysql-connector-java" % "5.1.21",

You then set your database name, password etc via: bees config:set DB_USER=user here  - so you don't have to store passwords in your config.

Note you can also bind databases to an application (and other things, like MongoDB) via bindings which will provide environment variables you can refer to in your config, as an alternative approach.

[Learn more about using CloudBees Databases]

MongoDB

CloudBees offer MongoDB via the mongohq hosted service (along with many other services). 

Firstly - pick a plan and subscribe. The free one is fine. 

Then create a database (either from the UI, or with the command below): 

 bees mongohq:create -a michaelnealeclickstart4 mydb1 plan=sandbox

You can then use this database in your application.

Configuration Variables and Play

You can use the CloudBees SDK and the "bees config:set" commands to set name value pairs that will be provided to your application at runtime (they are stored encrypted). To access these - you can use ${var_name} in your application.conf file - just like environment variables.

Loading an Alternative Play Configuration File

Play allows you to load an alternative configuration using a -Dconfig.resource=CONF_FILENAME system property.  You app will then look for the alternative configuration file in the application classpath (you usually provide these alternative configuration files into your application conf/ directory before packaging).

To customize this value on CloudBees, you can use the Bees SDK:

bees config:set -a APPID config.resource=myalt.conf

Note: you'll need to restart your app for this config param to be applied

Private mode

If you want to have your application run in a "private mode" where only those with the password can access it (ie, during development) - you can use the Global interceptor here - simply copy that into your controllers directory and follow the instructions.

Loading config files in production

Play applications run on CloudBees in production mode - using the "dist" packaged version of the Play application. If you have files in your project, you need to know to load them from the classpath not from the "getFile" methods - those getFile methods may work in development time, but in production play apps, the files will not be there. 

For example, if you have a file called "mydata.json" in the conf directory, you access that as follows: 

Play.application.resourceAsStream("mydata.json")

Note it stores the file in the root of the classpath (no "conf" item). 

Continuous Deployment with DEV@cloud Jenkins

If you want this done quickly with one click - launch the clickstart - see the button to the left ! Click it ! it will set up all of the below for you, but if you like to set things up by hand, read on:

Click on the Builds link in the toolbar to open your Jenkins instance, create a free style job and add your source repository.

For the job you need to add an "Execute script" build step, with the following command.

java -XX:MaxPermSize=256m -Xmx512M -jar /opt/sbt/sbt-launch-0.11.3-2.jar -Dsbt.log.noformat=true clean test

This will download the version of play you use, and run your tests.

CloudBees expects that logs will be printed to STDOUT it is a good idea to add the following to conf/logger.xml

<configuration>

 <conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel" />

 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
   <encoder>
     <pattern>%date - [%level] - from %logger in %thread %n%message%n%xException%n</pattern>
   </encoder>
 </appender>

 <logger name="play" level="INFO" />
 <logger name="application" level="INFO" />

 <!-- Off these ones as they are annoying, and anyway we manage configuration ourself -->
 <logger name="com.avaje.ebean.config.PropertyMapLoader" level="OFF" />
 <logger name="com.avaje.ebeaninternal.server.core.XmlConfigLoader" level="OFF" />
 <logger name="com.avaje.ebeaninternal.server.lib.BackgroundThread" level="OFF" />

 <root level="ERROR">
   <appender-ref ref="STDOUT" />
 </root>
</configuration>

You can use Jenkins Cloudbees Deployer plugin to deploy continuously your play2 application. You'll the have to configure sbt build step to call the "dist" task, and setup the deployer with "first match" web application pattern set to dist/*.zip.

Play! version 1

If you are after version 1 support, see here

Created by Ivan Meredith on 2011/06/17 23:16