diff --git a/.gitignore b/.gitignore index 91b7119c137bdb228a79a5d5f9dcf45bc9fe60b0..9947615c22f44b75a4c41c13954086fd1bbf2e00 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ /build /dist /database/views +/service diff --git a/package.json b/package.json index 1c2f5763a601787a7bd710b2a9c354c8ba5d46c0..e9cd60a701ff886bddf6a7ee872a7b77d17cb23c 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "test": "ts-node node_modules/istanbul/lib/cli.js cover -x \"**/*.spec.ts\" -e .ts _mocha", "show-coverage": "xdg-open coverage/lcov-report/index.html", "doc-api": "raml2html -i specs/blendb-api-v1.raml -o doc/api-v1-reference.html", - "database": "ts-node database/config.ts database" + "database": "ts-node database/config.ts database", + "service": "./scripts/service.sh" }, "repository": { "type": "git", diff --git a/scripts/service.sh b/scripts/service.sh new file mode 100755 index 0000000000000000000000000000000000000000..9ce087698c6ce7322fd38a941878791921448dcf --- /dev/null +++ b/scripts/service.sh @@ -0,0 +1,67 @@ +#! /bin/bash + +#HUGE work around to make this script allways execute at his location in dir struct +basePath=$(echo $BASH_SOURCE | rev | cut -c 11- | rev) +cd $basePath/.. +mkdir -p service +WORKSPACE=$(pwd) +REAL_USER=$USER +BLENDB_PORT=3001 + + +if [[ $1 != "" ]]; then + BLENDB_PORT=$1 +fi + +if [[ $2 != "" ]]; then + REAL_USER=$2 +fi + + +cd service +echo "[Unit]" > blendb.service +echo "Description=BlenDB API" >> blendb.service +echo "After=network.target" >> blendb.service +echo "" > blendb.service +echo "[Service]" >> blendb.service +echo "User=$REAL_USER" >> blendb.service +echo "WorkingDirectory=$WORKSPACE" >> blendb.service +echo "ExecStart=$WORKSPACE/service/service.sh" >> blendb.service +echo "" >> blendb.service +echo "[Install]" >> blendb.service +echo "WantedBy=multi-user.target" >> blendb.service + +echo "#! /bin/bash" > service.sh +USING_NVM="false" +VERSION=$(node --version) +error=$(source $NVM_DIR/nvm.sh) + +if [[ $? -eq 0 ]]; then + source $NVM_DIR/nvm.sh + echo "source $NVM_DIR/nvm.sh" >> service.sh + echo "nvm use $VERSION" >> service.sh + USING_NVM="true" +else + echo "nvm not found, using native node version" + echo "" +fi + +echo "PORT=$BLENDB_PORT npm start" >> service.sh +chmod +x service.sh + +echo "The service files were created with this parameters" +echo "User that runs the service: $REAL_USER" +echo "PORT: $BLENDB_PORT" +echo "Blendb home dir (WORKSPACE): $WORKSPACE" +echo "Node Version: $VERSION" +echo "Using NVM: $USING_NVM" +echo "" +echo "Node version is get using 'node --version, use nvm to change it" +echo "To set different user and port use npm run service -- <port> [<user>]" + +echo "Run this commands, as root (or sudo) to finish the process and start blendb" +SYSTEMD_PATH=/etc/systemd/system/blendb.service +echo -n "rm -f $SYSTEMD_PATH && " +echo -n "ln -s $WORKSPACE/service/blendb.service $SYSTEMD_PATH && " +echo "systemctl daemon-reload && systemctl restart blendb.service" +