How to Host Scala App with Scalatra and Jelastic Cloud

Scalatra is a tiny open-source web micro-framework, written in Scala. It’s most appropriate for construction of RESTful APIs, building high-performance web sites and mobile applications. Being extremely quick and simple, this framework could be considered as one of the best choices for beginners to learn Scala. At the same time, Scalatra is powerful enough to accomplish almost any widely required task thanks to the attachable libraries, which can greatly expand this tool’s basic possibilities.

So, let’s learn how to create and deploy an application into Jelastic Cloud with the help of Scalatra.

Environment Creation

1. To start with, log in to your Jelastic account and click the New environment button in the upper left corner of the dashboard. scalatra 01create

2. In the opened environment wizard, choose the Tomcat 7 application server within the Java tab. Define the required amount of resources using the Vertical scaling section, select the preferred environment region and name your environment (e.g. scalatra). scalatra 02env create1 After all the rest of parameters are configured (if needed), click on the Create button at the bottom right corner of the frame.

3. Wait a minute for your environment to be created. scalatra 03created

Installing the Required Utilities

For the further operations to be performed, you need to access your account via the Jelastic SSH Gateway, select the appropriate environment (scalatra in our case) and enter your Tomcat 7 container.

scalatra 04pre step

Tip: In case you haven’t performed similar operations before, you need to:

1. First of all, after connecting to the shell, add path to the Tomcat /bin folder to the corresponding variable for the comprised scripts to be easily executable:

PATH=$PATH:/opt/tomcat/temp/bin
export PATH
source ~/.bash_profile

scalatra 04second pre step

2. Now let’s install a few necessary utilities:

  • Conscript represents a plugin, required for building and updating Scala code. Execute the following command in order to download and install it:
curl https://raw.githubusercontent.com/n8han/conscript/master/setup.sh | sh

scalatra 05curl

  • giter8 is a tool, intended for generating the required project structure (i.e. the hierarchy of directories and files inside) based on project template, fetched from the appropriate Git repo. Get it downloaded using the previously installed Conscript utility:
cs n8han/giter8

scalatra 06giter8

3. Once these processes are completed, navigate to the bin folder and download the sbt-launch java-archive. It will install the sbt build tool, which represents an interactive shell for the scala-based applications' building:

cd bin
wget https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/sbt-launch/0.13.8/sbt-launch.jar

scalatra 07bin wget typesafe

4. Now create a new sbt file here (using, for example, the vim sbt command) and paste the following lines to it:
#!/bin/bash
SBT_OPTS="-Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M" java $SBT_OPTS -jar `dirname $0`/sbt-launch.jar “$@" scalatra 08sbt lines1 When everything is done, don’t forget to save the changes with typing :wq.

5. The next steps are to set the just added script to be executable by the current user with the chmod command, download and unpack an archive with an example app, and remove its zipped version for not occupying the extra disk space:

chmod u+x sbt
wget https://github.com/scalatra/scalatra-website-examples/archive/master.zip; unzip master.zip; rm master.zip

scalatra 09chmod wget unzip

6. Then, switch to the folder with the Jelastic-dedicated project example and open the comprised build.sbt file (which represents a special Jelastic deployment plugin) for editing:

cd scalatra-website-examples-master/2.4/deployment/scalatra-jelastic
vim build.sbt

scalatra 10cd vim

7. While inside, substitute the JelasticKeys.apiHoster parameter’s value with your hosting service provider’s domain (e.g. app.jelastic.com) and specify your environment’s name within the JelasticKeys.deploy ¬†parameter (scalatra in our case). scalatra 11build sbt Save the changes and quit from the vim editor with the :wq command.

Note: You need to additionally export your Jelastic login and password to the current user’s profile, as these variables will be required during the further scripts' running. So, execute the following lines:

export JELASTIC_USERNAME="{jelastic_login}"
export JELASTIC_PWD="{jelastic_password}"
source ~/.bash_profile

where:

  • {jelastic_login} - an email address you use to access the Jelastic dashboard
  • {jelastic_password} - the corresponding account password

scalatra 12export

Build and Deploy an App

Now, as everything is configured, you can proceed to the actual deployment of your project just from the interactive shell.

1. For that, set sbt file to be executable (by means of the chmod command as shown below) and then call this tool to be started:

chmod +x sbt
./sbt

scalatra chmod sbt new On the first run, all the required dependencies are to be downloaded, so be patient - it may take some time.

2. Now you can make your project to be built and deployed directly to the Jelastic platform through simply typing the next command inside the shell:

deploy

scalatra 14deploy Through the output, you can track how the project and its libraries are uploading to the Jelastic dashboard and deploying to the earlier specified environment. scalatra 15success When everything is done, you’ll be shown the corresponding success message with the process duration specified.

3. Now, enter the Jelastic dashboard (or refresh the page if you have it already opened) and click Open in browser for your environment to see what we’ve got: scalatra 16OiB Great! As a result, you’ve just deployed your first example Scalatra app into the Jelastic platform. scalatra 17browser

It was pretty simple, wasn’t it? So, for now you can have fun and learn Scalatra further to create your own stunning apps and get them hosted directly in the Cloud within a few commands!