From 216f06036eea393f711c3faf8961db80d828957c Mon Sep 17 00:00:00 2001 From: Martin Hecher Date: Sun, 22 Feb 2015 13:02:28 +0100 Subject: [PATCH] * fixed _devops scripts * updated README.md * version pump --- README.md | 22 +++++++++++----------- _devops/docker-build-image.sh | 7 ++++--- _devops/docker-run-container.sh | 7 ++++--- _devops/docker-start-container.sh | 14 +++++++++----- _devops/serve-dev.sh | 20 ++++++++++++++------ _devops/serve-prod.sh | 18 +++++++++++------- _devops/service-info.txt | 9 +++++++-- _devops/setup.sh | 10 ++++++---- _devops/test.sh | 16 +++++++++------- package.json | 6 +++--- 10 files changed, 78 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 5727cea..3012018 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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'). diff --git a/_devops/docker-build-image.sh b/_devops/docker-build-image.sh index 79110de..889b28b 100755 --- a/_devops/docker-build-image.sh +++ b/_devops/docker-build-image.sh @@ -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" diff --git a/_devops/docker-run-container.sh b/_devops/docker-run-container.sh index 371141d..ea70398 100755 --- a/_devops/docker-run-container.sh +++ b/_devops/docker-run-container.sh @@ -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 diff --git a/_devops/docker-start-container.sh b/_devops/docker-start-container.sh index 7e791f4..4819123 100755 --- a/_devops/docker-start-container.sh +++ b/_devops/docker-start-container.sh @@ -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" ] @@ -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 diff --git a/_devops/serve-dev.sh b/_devops/serve-dev.sh index 74652a7..e8e1087 100755 --- a/_devops/serve-dev.sh +++ b/_devops/serve-dev.sh @@ -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) diff --git a/_devops/serve-prod.sh b/_devops/serve-prod.sh index 418762e..123e6cc 100755 --- a/_devops/serve-prod.sh +++ b/_devops/serve-prod.sh @@ -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) diff --git a/_devops/service-info.txt b/_devops/service-info.txt index c23f332..64576e0 100644 --- a/_devops/service-info.txt +++ b/_devops/service-info.txt @@ -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 \ No newline at end of file diff --git a/_devops/setup.sh b/_devops/setup.sh index 01f1a1f..29b48f9 100755 --- a/_devops/setup.sh +++ b/_devops/setup.sh @@ -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) diff --git a/_devops/test.sh b/_devops/test.sh index 3f3d821..f65b83b 100755 --- a/_devops/test.sh +++ b/_devops/test.sh @@ -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) diff --git a/package.json b/package.json index 3b8a905..35beb90 100644 --- a/package.json +++ b/package.json @@ -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",