service.sh 1.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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"