Cloud Zone is brought to you in partnership with:

I am a passionate programmer and cloud enthusiast, currently working for Qburst Technologies(http://www.qburst.com/) at Kerala, India. While not in work, I spends his time coding personal projects, experimenting, learning, and blogging. Other areas of interest include system and database administration, reading etc. You can find my personal blog at http://shameerc.com and on twitter @shameerc. Shameer has posted 8 posts at DZone. You can read more from them at their website. View Full User Profile

Scala in Appfog

12.30.2012
| 3439 views |
  • submit to reddit

In this article we will discuss how to deploy a standalone Scala application in AppFog.

AppFog is a Platform as a Service (PaaS) that supports multiple languages as well as multiple clouds. It was built on top of open source PaaS Cloud Foundry and can support any language supported by Cloud Foundry, such as PHP, Java, Scala, and so on. AppFog lets you use your language of choice as well as the public cloud infrastructure. When you deploy an application, you can also select the cloud provider – Amazon, Microsoft, Rackspace, and more. One major advantage of multi-cloud support is that you can deploy an application in multiple clouds, so if one service goes down, you can easily switch to another provider.

Many sample applications (Jumpstarts) are listed in the AppFog dashboard which makes launching new applications very easy. Unfortunately no Scala applications are listed in here. So, to launch our Scala application we’ll  use af command-line tool.

Using af command-line tool

AppFog provides a command-line tool for creating and managing your application.  It’s a ruby shell application which you can install using gem. So for installing af, you should have Ruby and RubyGems installed in your machine. To install af:

$ gem install af

There are number of commands available with this tool, for creating the application, start/stop the application and so on. We will see them as we go on. To use af you must have an AppFog account, so create one if you don’t already have it. Then you should log in from the command-line before creating the application.

shameerc$ af login
Attempting login to [https://api.appfog.com]
Email: username@domain.com
Password: *********
Successfully logged into [https://api.appfog.com]

Once you have logged in successfully, you can start the application in AppFog. Before creating the application in AppFog, we need to setup it in our local machine.

Setting up the project

For demonstration purpose I’ve forked a sample Scala application from Cloud Foundry that will test palindrome, which is hosted in Github. In this application we are using Unfiltered, a library for servicing HTTP requests in Scala. We are using sbt (Simple Build Tool) with the sbt-package-dist plugin for building and packaging the application. To get started, clone the sample application from Github to your local machine.

shameerc$ git clone https://github.com/shameerc/unfilter-me.git 
scala-unfilter

Step inside scala-unfilter folder after cloning source code. You will see the Scala source files, sbt project folder and build file (build.sbt). scala.sbt contains build definitions such as the application name, version, and dependencies.

import com.twitter.sbt._

seq(StandardProject.newSettings: _*)
 
packageDistZipName := "af-unfiltered-sample.zip"

organization := "com.example"

name := "scala-unfilter"

version := "0.1.0"

libraryDependencies ++= Seq(
  "net.databinder" %% "unfiltered-filter" % "0.6.4",
  "net.databinder" %% "unfiltered-jetty" % "0.6.4",
  "org.clapper" %% "avsl" % "0.3.6",
  "net.databinder" %% "unfiltered-spec" % "0.6.4" % "test"
)

resolvers ++= Seq(
  "java m2" at "http://download.java.net/maven/2"
)

In the first two lines we are including the settings for sbt-package-dist. packageDistZipName is the name of package file that will be created by this plugin. Remember that the blank line between statements in build.sbt is actually required by sbt.

You can test the application locally by running sbt.

scala-unfilter shameerc$ sbt run

It will resolve all dependencies, compile, and automatically launch the application in the browser. Next we need to package the application for deploying in AppFog.

scala-unfilter shameerc$ sbt package-dist

This will package the application into a zipped file named af-unfiltered-sample.zip and save in app-base/dist/scala-unfilter-0.1.0/. This file contains all the jar files and libraries required by our application.

Deploying the application

When we deploy an application, we are basically uploading the package file to AppFog server. af push is the command for creating as well as deploying the application for first time.

scala-unfilter shameerc$ af push --path=dist/scala-unfilter-0.1.0/
af-unfiltered-sample.zip

Application Name: scala-unfilter
Detected a Standalone Application, is this correct? [Yn]:
1: erlangR14B02
2: java
3: node04
4: node06
5: php
6: python2
7: ruby18
8: ruby192
9: ruby193
Select Runtime [java]: 2
Selected java
Start Command: java $JAVA_OPTS -jar scala-unfilter-0.1.0/scala-unfilter_2.9.1-0.1.0.jar
Application Deployed URL [None]: scala-unfilter.${target-base}
Memory reservation (128M, 256M, 512M, 1G, 2G) [512M]:
How many instances? [1]:
Bind existing services to 'scala-unfilter'? [yN]:
Create services to bind to 'scala-unfilter'? [yN]:
Would you like to save this configuration? [yN]: y
Manifest written to manifest.yml.
Creating Application: OK
Uploading Application:
  Checking for available resources: OK
  Processing resources: OK
  Packing application: OK
  Uploading (11M): OK  
Push Status: OK
Staging Application 'scala-unfilter': OK                                       
Starting Application 'scala-unfilter': OK     

The above steps will upload and start our application in AppFog. When the --path parameter is omitted, it will deploy from the current directory. Here we’ve given the path to zip file to be uploaded. Next it will ask for the application name, which we given as scala-unfilter. Then af will detect the type of application we are deploying. For example, if it is a PHP application, it will auto-detect and will ask us to confirm that. In this case, we are deploying a packaged application. So af detected it as a stand alone application and thus we are asked to select the runtime environment. For running Scala applications we need Java runtime environment.

We need to use the Start command to run the application. Once the application is deployed, AppFog will unzip the file and place it in the base folder and will try to run the application using java $JAVA_OPTS -jar scala-unfilter-0.1.0/scala-unfilter_2.9.1-0.1.0.jar command in AppFog. Next you need to choose an AppFog URL, memory reservation, and number of instance for your application. Here we’ve accepted default values for them. Then you can bind any new or existing services your application needs (like databases). We have skipped that step here.

Next you will be asked whether you want to locally save the application configuration. af will write the application’s configuration to a file named manifest.yml and store it in the application root. manifest.yml will have all the necessary information about the application like its name, URL, start command, etc. This will make further deployments easier as af will read values from this configuration file. You can directly write the configuration file for deploying application once you are familiarized with the command-line tool.

Once you have successfully followed the above steps, af will create the application in AppFog. Then it will upload packaged application and try to start using the start command we have given. If there are no other issues, you will have a running Scala application in AppFog, which you can access in the given URL.

Modifying and updating application

You don’t need to follow the above steps when you want to update the application after making modifications. Once you have finished the modifications, test it locally and make sure it is working fine. Then package application using sbt package-dist and issue the command given below from application directory.

Shameers-MacBook-Pro:scala-unfilter shameerc$ af update

Updating application 'scala-unfilter'...
Uploading Application:
  Checking for available resources: OK
  Processing resources: OK
  Packing application: OK
  Uploading (11M): OK   
Push Status: OK
Stopping Application 'scala-unfilter': OK
Staging Application 'scala-unfilter': OK                                        
Starting Application 'scala-unfilter': OK

Cool, we have successfully updated our changes in AppFog. When we update the application AppFog will completely remove old files replace them with new one. So be careful when storing static contents like images in AppFog.

Summary

AppFog is a promising multi-language, multi-cloud PaaS. Built on top of Cloud Foundry, it supports many popular languages like PHP, Java, Ruby, etc. As a Jvm-based language, we can also run Scala applications in AppFog. In this article we’ve familiarized with using af command-line tool and deploying a standalone Scala application in AppFog. This is just the beginning; you may face many issues as you add different features to the application. Feel free to post a comment if you are facing any issues. I will try my level best to answer any questions.





Published at DZone with permission of its author, Shameer Cee. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)