Saturday, 26 February 2011

Play! Framework deploy to cloudbees.com

I've been trying the Play framework in the last few weeks and I've to admit its a real pleasure to code in that environment.
Now, I want to share my experience with deploying a Play! application to cloudbees.com
These guys offer the platform as a service, you can build, test and deploy Java web applications in the cloud. I thought it was brilliant.
Register, for free (DEV tools seems to be free only till the end of Feb 2011) and you get a nice control panel from where you can create git/svn repositories, Jenkins (hudson) jobs, deploy you application create databases etc.

Here are some of the steps you need in order to get your Play! application running there.

I'm using play 1.1.1

  • Edit your play application.conf

    # The production deployment information
    %production.db=java:/comp/env/jdbc/${project_name}
    
    %production.db.url=jdbc:cloudbees://${db_name}
    %production.db.driver=com.cloudbees.jdbc.Driver
    %production.db.user=${db_username}
    %production.db.pass=${db_password}
    %production.jpa.dialect=org.hibernate.dialect.MySQLDialect
    

    What I didn't find on the Play! documentation was the first line to specify the JNDI entry
  • Create the database
    from the website you can easily create a database



  • Create the cloudbees resource file
    Create a folder war/WEB-INF in your Play! project ( if you don't have it ) and create a file called cloudbees-web.xml that looks like this
    <br /><?xml version="1.0"?><br /><br /><cloudbees-web-app xmlns="http://www.cloudbees.com/xml/webapp/1"><br /> <resource name="jdbc/${project_name}" auth="Container" type="javax.sql.DataSource"><br />  <param name="username" value="${db_username}" /><br />  <param name="password" value="${db_password}" /><br />  <param name="url" value="jdbc:cloudbees://${db_name}" /><br /> </resource><br /></cloudbees-web-app><br /><br />


  • Generate your database DDL
    Add the module db to your application
    module.db=${play.path}/modules/db-1.1.1


    and then run the command

    play db:export --%production


    You might get an javax.naming.NoInitialContextException, if you do comment out the line

    #%production.db=java:/comp/env/jdbc/${project_name}

    in the application.conf

    You might also get some sort of command not found error. Make sure you install db-1.1.1. I think if you don't specify the version it'll get version 1.0.1

  • Create your remote db
    This took me some time to figure out. I thought there was some sort of phpMyAdmin but instead you need to use your own MySql client. So on the cloudbees website if you look under
    RUN@Cloud > Databases >Manage >${your_db} in the Overview section you can find server name, port, schema and username.
    Open your client ( I've used MySql workbench ) and copy in there the sql generated at the previous step

  • Build your app
    play war ${path to your play project} -o ${war name}  --%production --zip

    This will generate the for file for production

  • Deploy your app
    I used the cloudbees sdk and works very well.
    Just download it and the first time you use it, it will ask you for some keys that you can find on cloudbees under Account > Security Keys. Put them in and you are ready to go.
    To deploy your application run the command

    bees app:deploy -a login/yourProject ${war file}


    The cool thing is that the bees deploy command will calculate the difference between the last war and the one you are uploading and will only upload that delta!

6 comments:

sixblackbeans said...

Andrea:

I found that if I run the command:

play war ${path to your play project} -o ${war name} --%production --zip

from the project root, I get a 'file name too long' error. Seems it creates nested directory structure that goes forever. When I run it outside of project, it works fine.

jdtangney said...

Cool! And Thanks!

Any idea how I can deploy a Play! app from DEV@cloud directly to my RUN@cloud instance?

mericano1 said...

If you are using maven you can use the cloudbees maven plugin detailed at https://cloudbees.zendesk.com/entries/421755-automated-continuous-deployment-from-dev-cloud-to-run-cloud.
If your not using maven afaik the cloudbees guys are working on a Jenkins plugin to deploy artifacts to run@cloud which will be available very soon.
Hope this helps

Prashant said...

I have been trying to deploy my play application but I always get following error. Did you get the same anytime you worked on it?
-----------
@66a2f4ko5
Can't start in PROD mode with errors

Database error
A database error occured : Cannot connected to the database, Name jdbc is not bound in this Context

mericano1 said...

Hi Prashant,
I had the same problem and as far as I remember there was something wrong in the cloudbees-web.xml.

I would suggest you using the bees SDK to run your application locally so you can figure out where the issue is without deploying all the time to the clouldbees servers.

juettner said...

Thanks for the great write-up, it really helped me get past some mental roadblocks.

For creating the remote db you can also use Play's evolutions feature (described here).