diff --git a/package.json b/package.json index 5ccda0e5c2a6c068104cfd4e093443aa95fb7d49..f073b3173b5861b5046ac380b8fce857e87e4490 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "multimeter": "0.1.1", "naclb": "0.2.2", "nat-upnp": "https://github.com/c-geek/node-nat-upnp", + "pm2": "0.15.10", "q": "1.1.2", "q-io": "1.13.1", "request": "2.31.0", diff --git a/ucoin.sh b/ucoin.sh index b18e9588e98e0ab4a53b5a8adb85937fe9bf6362..4cb142e5247494637bfa819883a61eb8b40db999 100755 --- a/ucoin.sh +++ b/ucoin.sh @@ -8,7 +8,11 @@ ucoind() { - NODE=node + local UCOIN_DATABASE + local UCOIN_LOG_FILE + local UCOIN_DATA_HOME + local NODE + local PM2 if [[ -d $UCOIN_DIR/node ]]; then NODE=$UCOIN_DIR/node/bin/node @@ -17,8 +21,71 @@ ucoind() { VERSION=`$NODE -v` if [[ $VERSION != v0.12* ]]; then - echo "Node.js v0.12 is not available"; + echo "$NODE v0.12 is required"; else - $NODE --harmony $UCOIN_DIR/bin/ucoind $* + + # OK, execute command + PM2=$UCOIN_DIR/node_modules/pm2/bin/pm2 + UCOIN_DATA_HOME=$HOME/.config/ucoin + + case "$1" in + + #--------------------------------- + # UCOIN DAEMON MANAGEMENT: START + #--------------------------------- + + start) + local test + local UCOIN_LOG_FILE + local UCOIN_ERR_FILE + UCOIN_LOG_FILE=$UCOIN_DATA_HOME/$UCOIN_DATABASE/ucoin.log + UCOIN_ERR_FILE=$UCOIN_DATA_HOME/$UCOIN_DATABASE/ucoin.err.log + if [ -z $UCOIN_DATABASE ]; then + UCOIN_DATABASE="ucoin_default" + fi + test=`$PM2 list | grep "$UCOIN_DATABASE.*online"` + if [ -z "$test" ]; then + echo $UCOIN_LOG_FILE + $PM2 start -f "$UCOIN_DIR/bin/ucoind" --name "$UCOIN_DATABASE" --interpreter="$NODE" --node-args="--harmony" --log $UCOIN_LOG_FILE --error $UCOIN_ERR_FILE --merge-logs -- start --mdb "$UCOIN_DATABASE" --httplogs 2>/dev/null + echo "uCoin with DB '$UCOIN_DATABASE' started. Use 'ucoind logs' to see interactive logs." + else + echo 1>&2 "uCoin '$UCOIN_DATABASE' already started." + fi + ;; + + + #--------------------------------- + # UCOIN DAEMON MANAGEMENT: STOP & OTHERS + #--------------------------------- + + list|info|logs|stop|restart|monit|delete) + UCOIN_DATABASE=$2 + if [ -z $UCOIN_DATABASE ]; then + UCOIN_DATABASE="$UCOIN_DB" + fi + if [ -z $UCOIN_DATABASE ]; then + UCOIN_DATABASE="ucoin_default" + fi + $PM2 $1 $UCOIN_DATABASE + ;; + + delete-all) + $PM2 delete all + ;; + + #--------------------------------- + # UCOIN NORMAL COMMANDS + #--------------------------------- + + *) + $NODE --harmony "$UCOIN_DIR/bin/ucoind" --mdb "$UCOIN_DATABASE" $* + ;; + + esac fi; } + +# If the script was launched with parameters, try to launch the uCoin command +if [ ! -z $1 ]; then + ucoind $* +fi