Skip to content

Commit

Permalink
* fixed _devops scripts
Browse files Browse the repository at this point in the history
* updated README.md
* version pump
  • Loading branch information
Martin Hecher committed Feb 22, 2015
1 parent 6b8d52a commit 216f060
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 51 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

This is the documentation for 'microservice-base' source. Most likely you are reading this because you are using the [generator-duraark](https://github.com/duraark/generator-duraark) scaffolding tool. If not I recommend to check out the project before returning to this page. [generator-duraark](https://github.com/duraark/generator-duraark) allows you to scaffold microservices development and deploy environments via a CLI tool.

The microservice-base gives you a sane starting point for developing [NodeJS](http://www.nodejs.org) based microservices.
The microservice-base gives you a sane starting point for developing [NodeJS](http://www.nodejs.org) based microservices with SailsJS.

The repository includes:
* [SailsJS](http://sailsjs.org) for API-logic
* [SailsJS](http://sailsjs.org) for API-layer
* Mocha test suite integration for SailsJS (based on http://stackoverflow.com/questions/21798840/how-use-mocha-test-framework-with-node-js-and-sails-js)
* Full Docker support for deployment (see the 'scripts' folder)
* Setup for local testing with [PM2](https://github.com/Unitech/pm2)
* Full Docker support for deployment (see the '_devops_' folder)
* Setup for local testing with and production with [PM2](https://github.com/Unitech/pm2) and [nodemon](http://nodemon.io/).

## Installation

Expand All @@ -19,22 +19,22 @@ After that you can add you own API logic via the SailsJS CLI (if you are not fam

## Local development

For local development [PM2](https://github.com/Unitech/pm2) is used as process manager. To start developing locally start the script './src/scripts/serve-dev.sh'. It starts the SailsJS server on port 1337. The '--watching' option of PM2 is not enabled at the moment, due to an issue where the service is constantly restarted. You can simply exchange PM2 with other process managers, like nodemon, forever, etc. to get automatic restarts on code changes.
For local development [nodemon](http://nodemon.io/) is used as process manager. To start developing locally change into the '_devops' folder and run the script 'serve-dev.sh' there (it is important to change into the _devops directory before, as the scripts are working relativ to this folder!). The SailsJS server is started on port 1337. We are currently not using the more sophisticated PM2 as process manager due to an issue where the service is constantly restarted when using PM2's '--watch' option.

## Docker deployment

The 'deployment' folder contains helper scripts for deploying your service via [Docker](http://www.docker.com). Two steps are necessary to configure the deployment:
The '_devops_' folder also contains helper scripts for deploying your service via [Docker](http://www.docker.com). Two steps are necessary to configure the deployment:

1. Change the name of your service in ./deployment/service-info.txt to your liking
1. Change the name of your service in ./_devops_/service-info.txt to your liking
2. Have a look at the ./Dockerfile. Per default it installs NodeJS and SailsJS and starts the SailsJS server on port 1337.

To deploy the service do the following:

1. Build the docker container via './deployment/docker-build.sh'. The docker image will be available as 'local/your-service-name' afterwards.
2. Start the docker container via './deployment/docker-start-service.sh $PORT'.
1. Build the docker container via './_devops_/docker-build-image.sh'. The docker image will be available as 'local/your-service-name' afterwards.
2. Start the docker container via './_devops_/docker-start-container.sh $PORT'.

When the container is started successfully the service is available on $PORT. If no $PORT is given as parameter it is started on SailsJS's default port 1337.
When the container is started successfully the service is available on $PORT. If no $PORT is given as parameter SailsJS's default port 1337 is assumed.

## Debugging the Docker container

The script './deployment/docker-run.sh' starts the built docker container and gives you an interactive shell for debugging.
The script './_devops_/docker-run-container.sh' starts the built docker container and gives you an interactive shell for debugging. You can also give the script a custom command as parameter. In this case the container will execute this command when the container starts (the default is '/bin/bash').
7 changes: 4 additions & 3 deletions _devops/docker-build-image.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/bin/sh

# TODO: use proper config file!
SERVICEFILE=./service-info.txt

NAMESPACE=$(sed '1q;d' $SERVICEFILE)
IMAGENAME=$NAMESPACE/$(sed '2q;d' $SERVICEFILE)
DOCKERFILE=$(sed '3q;d' $SERVICEFILE)
NAMESPACE=$(sed '2q;d' $SERVICEFILE)
IMAGENAME=$NAMESPACE/$(sed '4q;d' $SERVICEFILE)
DOCKERFILE=$(sed '6q;d' $SERVICEFILE)

echo "Building docker image '$IMAGENAME' from Dockerfile located at '$DOCKERFILE':"
echo "docker build -t $IMAGENAME $DOCKERFILE"
Expand Down
7 changes: 4 additions & 3 deletions _devops/docker-run-container.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/bin/sh

# TODO: use proper config file!
SERVICEFILE=./service-info.txt

NAMESPACE=$(sed '1q;d' $SERVICEFILE)
IMAGENAME=$NAMESPACE/$(sed '2q;d' $SERVICEFILE)
FOLDER=$(pwd)/$(sed '4q;d' $SERVICEFILE)
NAMESPACE=$(sed '2q;d' $SERVICEFILE)
IMAGENAME=$NAMESPACE/$(sed '4q;d' $SERVICEFILE)
FOLDER=$(sed '8q;d' $SERVICEFILE)

COMMAND=$1

Expand Down
14 changes: 9 additions & 5 deletions _devops/docker-start-container.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/bin/sh

NAMESPACE="local"
CONTAINERNAME=$(cat ./service-info.txt)
IMAGENAME=$NAMESPACE/$CONTAINERNAME
DEFAULTPORT=1337
# TODO: use proper config file!
SERVICEFILE=./service-info.txt

NAMESPACE=$(sed '2q;d' $SERVICEFILE)
IMAGENAME=$NAMESPACE/$(sed '4q;d' $SERVICEFILE)
CONTAINERNAME=$(sed '4q;d' $SERVICEFILE)

DEFAULTPORT=1337
HOSTPORT=$1

if [ -z "$1" ]
Expand All @@ -16,6 +19,7 @@ fi

echo "Removing "
docker rm -f $CONTAINERNAME
echo "\n(NOTE: If the above command yields an error don't worry, that's fine. We tried to remove the old container before starting, but there was none started.)"

echo "\nStarted as "
docker run -d -p $HOSTPORT:1337 --name $CONTAINERNAME --volumes-from microservice-files --link microservice-files:files --link microservice-ifcmetadata:ifcmetadata --link microservice-e57metadata:e57metadata $IMAGENAME
docker run -d -p $HOSTPORT:1337 --name $CONTAINERNAME $IMAGENAME
20 changes: 14 additions & 6 deletions _devops/serve-dev.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#!/bin/sh

# TODO: read all necessary data from file!
# TODO: use proper config file!
SERVICEFILE=./service-info.txt

SERVICENAME=$(cat ./service-info.txt)
INDEXFILE="app.js"
FOLDER="../"
SERVICENAME=$(sed '2q;d' $SERVICEFILE)
INDEXFILE=$(sed '10q;d' $SERVICEFILE)
APPFOLDER=$(dirname "${INDEXFILE}")

#(cd $FOLDER; pm2 delete $SERVICENAME; pm2 start $INDEXFILE -x --name $SERVICENAME)
# FIXXME: pm2 with --watching enabled currently restarts the service permanently.
# We are using nodemon to circumvent this problem but are working on a solution
# to make it work with pm2

# Using 'pm2' as process manager:
#pm2 delete $SERVICENAME; pm2 start $INDEXFILE -x --name $SERVICENAME --watch
#pm2 logs
(cd $FOLDER; nodemon -w api -w config)

# Using 'nodemon' as process manager:
(cd $APPFOLDER; nodemon -w api -w config)
18 changes: 11 additions & 7 deletions _devops/serve-prod.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#!/bin/sh

# TODO: read all necessary data from file!
# TODO: use proper config file!
SERVICEFILE=./service-info.txt

SERVICENAME=$(cat ./service-info.txt)
INDEXFILE="app.js"
FOLDER="../"
SERVICENAME=$(sed '2q;d' $SERVICEFILE)
INDEXFILE=$(sed '10q;d' $SERVICEFILE)
APPFOLDER=$(dirname "${INDEXFILE}")

#(cd $FOLDER; pm2 delete $SERVICENAME; pm2 start $INDEXFILE -x --name $SERVICENAME -- --prod)
#pm2 logs
(cd $FOLDER; nodemon)
# Using 'pm2' application manager:
pm2 delete $SERVICENAME; pm2 start $INDEXFILE -x --name $SERVICENAME -- --prod
pm2 logs

# Using 'nodemon' for application control:
(cd $APPFOLDER; nodemon -w api -w config)
9 changes: 7 additions & 2 deletions _devops/service-info.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# namespace:
local
# imagename:
microservice-base
# dockerfile location:
../

../
# volume folder (can be empty, but has to be absolute if present)
/tmp
# indexfile:
../app.js
10 changes: 6 additions & 4 deletions _devops/setup.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/bin/sh

# TODO: use proper config file!
SERVICEFILE=./service-info.txt

SRC=$(sed '5q;d' $SERVICEFILE)
INDEXFILE=$(sed '10q;d' $SERVICEFILE)
APPFOLDER=$(dirname "${INDEXFILE}")

echo "Setting up project:"
echo " * installing dependencies: (cd $SRC && npm install && bower install)"
(cd $SRC && npm install && bower install)
echo "\nSetting up project:"
echo "\n * installing dependencies: (cd $APPFOLDER && npm install && bower install)\n"
(cd $APPFOLDER && npm install && bower install)
16 changes: 9 additions & 7 deletions _devops/test.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/bin/sh

# TODO: use proper config file!
SERVICEFILE=./service-info.txt

SRC=$(sed '5q;d' $SERVICEFILE)
INDEXFILE=$(sed '10q;d' $SERVICEFILE)
APPFOLDER=$(dirname "${INDEXFILE}")

echo "Starting test suite:"
echo " * installing global dependencies: npm install -g sails\n\n"
echo "\nStarting test suite:"
echo "\n * installing global dependencies: npm install -g sails (NOTE: Assumes you are root!\n"
npm install -g sails

echo "\n\n * installing local dependencies: (cd $SRC && npm -d install)\n\n"
(cd $SRC && npm -d install)
echo "\n\n * installing local dependencies: (cd $APPFOLDER && npm -d install)\n\n"
(cd $APPFOLDER && npm -d install)

echo "\n\n * execute tests: (cd $SRC && npm test)"
(cd $SRC && npm test)
echo "\n\n * execute tests: (cd $APPFOLDER && npm test)"
(cd $APPFOLDER && npm test)
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "duraark-microservice-base",
"private": false,
"version": "0.2.4",
"description": "Base docker image and initial Sails.js setup for a DURAARK microservice.",
"keywords": ["duraark", "microservice", "archiving"],
"version": "0.2.5",
"description": "Base docker image and initial Sails.js setup a REST microservice.",
"keywords": ["duraark", "microservice", "SailsJS", "NodeJS", "API"],
"dependencies": {
"sails": "~0.10.2",
"sails-disk": "~0.10.0",
Expand Down

0 comments on commit 216f060

Please sign in to comment.