Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker #14

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
.vagrant
ansible
24 changes: 20 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
sudo: required

language: scala
scala:
- 2.11.8
jdk:
- oraclejdk8

services:
- docker

env:
DOCKER_VERSION: 1.11.1-0~trusty
DOCKER_COMPOSE_VERSION: 1.7.1

before_install:
- sudo apt-get -o Dpkg::Options::="--force-confnew" install -y docker-engine=${DOCKER_VERSION}

- sudo rm /usr/local/bin/docker-compose
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin

script:
- docker-compose up --build tests
36 changes: 36 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Pull base image
FROM java:8

ENV SCALA_VERSION 2.11.8
ENV SBT_VERSION 0.13.8

# Install Scala
## Piping curl directly in tar
RUN \
curl -fsL http://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C /root/ && \
echo >> /root/.bashrc && \
echo 'export PATH=~/scala-$SCALA_VERSION/bin:$PATH' >> /root/.bashrc

# Install sbt
RUN \
curl -L -o sbt-$SBT_VERSION.deb http://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \
dpkg -i sbt-$SBT_VERSION.deb && \
rm sbt-$SBT_VERSION.deb && \
apt-get update && \
apt-get install sbt && \
sbt sbtVersion

# Add sources
ADD . /source

# Build project
RUN cd /source && sbt clean stage

# Set current directory
WORKDIR /source

# Run application
CMD ["/source/run.sh"]

EXPOSE 8080

31 changes: 31 additions & 0 deletions Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Pull base image
FROM java:8

ENV SCALA_VERSION 2.11.8
ENV SBT_VERSION 0.13.8

# Install Scala
## Piping curl directly in tar
RUN \
curl -fsL http://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C /root/ && \
echo >> /root/.bashrc && \
echo 'export PATH=~/scala-$SCALA_VERSION/bin:$PATH' >> /root/.bashrc

# Install sbt
RUN \
curl -L -o sbt-$SBT_VERSION.deb http://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \
dpkg -i sbt-$SBT_VERSION.deb && \
rm sbt-$SBT_VERSION.deb && \
apt-get update && \
apt-get install sbt && \
sbt sbtVersion

# Add sources
ADD . /source

# Set current directory
WORKDIR /source

# Run application
CMD ["/source/run_tests.sh"]

46 changes: 42 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Development

To bootstrap development process, there is a prepared environment setup.
It is based on Vagrant Ubuntu image with all the required tools in place, such as Java, Scala, mongodb and etc., set up by Ansible.
Please make sure you have Vagrant [installed](https://www.vagrantup.com/downloads.html) on your computer.

To initialize the environment run:
```bash
Expand Down Expand Up @@ -43,6 +44,8 @@ For more information check [Vagrant](https://www.vagrantup.com/docs/) and [Ansib
Build and Deployment
--------------------

### SBT

Application is built using `sbt` and `sbt-native-packager` plugin.

To build it run:
Expand All @@ -51,13 +54,48 @@ sbt stage
```
The resulting application will be placed in *target/universal/stage/* directory.

To test it run:
When testing or running the application, you need to make sure that all the required dependencies (mongo and etc.) are in place, up and running.

To run the application execute:
```bash
sbt run
```

By default it will start on port *8080*, which can be overridden by defining appropriate `$PORT` environment variable.

While testing, in order to distinguish between tests that don't require third-party dependencies (unit tests) and tests that do (integration test), there is a convention to name them `*Spec` and `*Integ` respectively.
The first ones can be run using sbt only and are usually much faster.

To test the application you can run any of the following commands:
```bash
sbt test
sbt "testOnly *Spec" # to run only unit tests
sbt "testOnly *Integ" # to run only integration tests
sbt test # to run all the tests
```


### Docker

In order to simplify deployment and integration processes, Docker containers are used.
Two separate containers, one for deployment and one for testing, are defined. Container management is done using Docker Compose.
*(Please note that depending on your environment setup some of the examples below might have to be run as `sudo`)*

To run the application execute:
```bash
sbt run
docker-compose up --build -d app
```
Here, `--build` means that the container image needs to be rebuilt before run. Flag `-d` means that it should run in *detached* mode.

To test the application you can run any of the following commands:
```bash
docker-compose up --build unitTests # to run only unit tests
docker-compose up --build integTests # to run only integration tests
docker-compose up --build tests # to run all the tests
```
By default it will start on port *8080*, which can be overridden by defining appropriate `$PORT` environment variable.

To stop all the running containers you can simply execute:
```bash
docker-compose down
```

For more information on how to work with Docker please see [Docker Docs](https://docs.docker.com).
2 changes: 1 addition & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.synced_folder ".", "/vagrant"
config.vm.provider "virtualbox" do |v|
v.memory = 2048
v.memory = 3072
end
config.vm.define :dev do |dev|
dev.vm.network :forwarded_port, host: 8080, guest: 8080
Expand Down
46 changes: 46 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
version: '2'

services:
app:
image: lyrics-engine
build:
context: .
dockerfile: Dockerfile
ports:
- 8080:8080
links:
- db

db:
image: mongo
ports:
- 27017:27017

testsBase:
image: lyrics-engine-tests
build:
context: .
dockerfile: Dockerfile.test

unitTests:
extends: testsBase
environment:
- TEST_TYPE=unit

integTests:
extends: testsBase
links:
- db
environment:
- TEST_TYPE=integ
- DB_HOST=db
- DB_PORT=27017

tests:
extends: testsBase
links:
- db
environment:
- TEST_TYPE=all
- DB_HOST=db
- DB_PORT=27017
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scalalab3.lyricsengine.parser.DataSetParser
/**
* @author Vlad Fefelov
*/
class DataSetParser$Test extends FlatSpec with Matchers {
class DataSetParserSpec extends FlatSpec with Matchers {

"A DataSetParser" should "parse data" in {

Expand Down
3 changes: 3 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

target/universal/stage/bin/lyrics-engine
14 changes: 14 additions & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -e

if [ "$TEST_TYPE" = "unit" ]
then
sbt "testOnly *Spec"
elif [ "$TEST_TYPE" = "integ" ]
then
sbt "testOnly *Integ"
elif [ "$TEST_TYPE" = "all" ]
then
sbt test
fi