Skip to content

install run

Brian Setz edited this page Feb 3, 2017 · 3 revisions

Installing and Running Individual Services

Docker is used as a primary method for installing and running individual services. We use a third-party sbt plugin sbt-native-packager to simplify installation process.

Project configuration

Each project may contain at most one executable service, that is one project -> one service. First of all, executable project should define its entry point by specifying its main class in the project description, for example:

lazy val myProject = defineProject(sprayProject, "myProject") settings (
    mainClass in Compile := Some("rugds.myservicepackage.MyMain"),
)

For standalone java/scala applications you do not need to specify anything else, your project is ready for deployment. However, if you need open ports in your running application, you need to explicitly define them:

import com.typesafe.sbt.packager.Keys._
import com.typesafe.sbt.packager.docker.DockerPlugin.autoImport.Docker

dockerExposedPorts := Seq(80, 8080)

Useful sbt commands to check if everything works as expected

running the project using sbt

To check that you have specified your main class properly, just run your project:

sbt run

or (in case of multiple projects build):

sbt yourproject/run

Please note that the latter approach is recommended, even if you have just one project.

running the project using universal build

Prepare script files (there should be executable scripts for both Windows and Linux generated):

sbt stage

If you look at \target\universal\stage directory, you will see two directories: bin (containing executable scripts) and lib (containing dependencies). If you run script for your platform from bin directory, you should have the same result as if you had run the "sbt run" command. Please note that you should look into the target directory of your project, not the one of root (in case of multiple projects). By default, the root project itself cannot be executed.

Packaging and running your project using Docker

To check that your project generates a proper Dockerfile (it is needed to run your service in a docker container), please use the following sbt command:

sbt docker:stage

It will generate Dockerfile in \target\docker.


Please note that to run the following commands you need to have docker installed (in Ubuntu just run "apt-get install docker.io" before continuing). Everything should be run on the server that will execute your service. At this point, this process is manual, but it is expected to be at least partially automated in the nearest future (checking out -> testing -> publish docker image to remote repository -> running the service).

As the first step, please checkout your project from git repository.

Second, run the following command to generate the docker image and publish it to the local repository:

sbt docker:publishLocal

Once published, it is ready to be executed. The following command will start the service in a docker container (here given an example of a weather service):

docker run -d --name weather-service-0.0.1 -v /etc/rugds:/etc/rugds weather-service:0.0.1

where "-d" runs the service in a daemon mode, "-v" defines mapping between host and container directories (in the specified command it defines mapping for /etc/rugds for authentication purposes). The last part is the name of the image of your service.

Additionally, if the service requires remote access on an open port, one should add the docker port forwarding:

-p SERVICE-PORT1:HOST-PORT1 -p SERVICE-PORT2:HOST-PORT2 ...

Packaging project as zip

Sometimes you just want to package your project in a zip archive, distribute it around, and let people run it. To package your project as a zip, you just run:

sbt universal:packageBin

In \target\universal directory you will find zip file containing both bin (executable scrips) and lib (dependencies) directories. To run your project unzip the archive and run the executable script from the bin directory for the target OS.

Docker based development environment

The sbt-scala-parent comes with built in support for managing your development environment using Docker. Every project can define a dependency on a (or multiple) docker image. The dependency are defined using a directory structure as follows

<project name>/src/main/resources/docker-dependencies/<docker hub name>/<docker hub repository>

Inside this directory there should be a file named env.conf which looks like:

environment {
  version = "1.2.3"
  options = "-e MY_CUSTOM_ENV_VAR"
}

Where options is an optional value. An example of depending on an official Docker image for Cassandra

my-cassandra-using-project/src/main/resources/docker-dependencies/library/cassandra

environment {
  version = "3.0.10"
}

Where library indicates that it is an official Docker Hub image. It can also be a custom image, for example

my-cassandra-using-project/src/main/resources/docker-dependencies/rugdsdev/cassandra

environment {
  version = "3.0.10"
  options = "-e CASSANDRA_JMX_PASSWORD=supers3cr3t"
}

Inside the 'my-cassandra-using-project/src/main/resources/docker-dependencies/rugdsdev/cassandra' directory you can place your own Dockerfile + additional files that your custom image might need. The image can be built using

sbt envDockerBuild

To automatically create, start, stop and/or destroy your development environment; use the following commands. To create:

sbt envCreateDev

To start:

sbt envStartDev

To stop:

sbt envStopDev

To remove:

sbt envRemoveDev

The following state machine is associated with the commands:

    +---------------------------------------------+
    |               +-------------+               |
    |               |             |               |
+---+----+     +----+---+     +---v----+     +----v---+
| Create +-----> Start  +----->  Stop  +-----> Remove |
+---^----+     +----^---+     +---+----+     +----+---+
    |               |             |               |
    |               +-------------+               |
    +---------------------------------------------+

Dependencies

Available since scala parent module version 0.1.0 (add dependency to your plugins.sbt file):

addSbtPlugin("rugds" % "sbt-scala-parent" % "0.1.0")