From 58a439ada67e1cb3e123445daabfa380712ef3d6 Mon Sep 17 00:00:00 2001 From: zhoney Date: Wed, 3 Apr 2019 14:08:36 +0800 Subject: [PATCH] add HugeGraphServer as system service (#170) implement: #169 Change-Id: Ic9ee18696499e7d9b12e47fb956bdeaa575bd220 --- .../src/assembly/static/bin/hugegraph | 127 ++++++++++++++++++ .../src/assembly/static/bin/hugegraph.service | 10 ++ .../src/assembly/static/bin/install.sh | 74 ++++++++++ 3 files changed, 211 insertions(+) create mode 100644 hugegraph-dist/src/assembly/static/bin/hugegraph create mode 100644 hugegraph-dist/src/assembly/static/bin/hugegraph.service create mode 100755 hugegraph-dist/src/assembly/static/bin/install.sh diff --git a/hugegraph-dist/src/assembly/static/bin/hugegraph b/hugegraph-dist/src/assembly/static/bin/hugegraph new file mode 100644 index 0000000000..09b3381316 --- /dev/null +++ b/hugegraph-dist/src/assembly/static/bin/hugegraph @@ -0,0 +1,127 @@ +#!/bin/bash +# +# hugegraph This shell script takes care of starting and stopping +# HugeGraphServer. +# +# chkconfig: - 58 74 +# description: HugeGraphServer is graph database server. It provides graph \ +# service by RESTful API consisted with graph, schema, gremlin and other APIs. + +### BEGIN INIT INFO +# Provides: HugeGraphServer +# Required-Start: $java +# Required-Stop: $java +# Should-Start: - +# Should-Stop: - +# Short-Description: start and stop HugeGraphServer +# Description: HugeGraphServer is graph database server. It provides graph +# service by RESTful API consisted with graph, schema, gremlin +# and other APIs. +### END INIT INFO + +# Variables +INSTALL_DIR= +SERVER_PORT= + +BIN_DIR=$INSTALL_DIR/bin +SERVER_URL="http://localhost:${SERVER_PORT}" +DETECT_URL="$SERVER_URL/versions" + +# Start the HugeGraphServer +start() { + echo "Starting HugeGraphServer..." + # Verify if the service is running + get_status + if [ $? -eq 0 ]; then + echo "The service is already running" + exit 0 + else + # Run the service + $BIN_DIR/start-hugegraph.sh + + # sleep time before the service verification + #sleep 10 + + # Verify if the service is running + get_status + if [ $? -eq 0 ]; then + echo "Service was successfully started" + exit 0 + else + echo "Failed to start service" + exit 1 + fi + fi +} + +# Stop the MATH +stop() { + echo "Stopping HugeGraphServer..." + # Verify if the service is running + get_status + if [ $? -eq 0 ]; then + # Stop the service + $BIN_DIR/stop-hugegraph.sh + + # Sleep time before the service verification + #sleep 10 + + # Verify if the service is running + get_status + if [ $? -eq 0 ]; then + echo "Failed to stop service" + exit 1 + else + echo "Service was successfully stopped" + exit 0 + fi + else + echo "The service is already stopped" + exit 0 + fi +} + +# Verify the status of HugeGraphServer +status() { + echo "Checking status of HugeGraphServer..." + # Verify if the HugeGraphServer is running + get_status + if [ $? -eq 0 ]; then + echo "Service is running" + exit 0 + else + echo "Service is stopped" + exit 1 + fi +} + +# Get status of HugeGraphServer to ensure it is alive +get_status() { + HTPP_CODE=`curl -I -s -w "%{http_code}" -o /dev/null $DETECT_URL` + if [ $HTPP_CODE = 200 ]; then + return 0 + else + return 1 + fi +} + +# Main logic +case "$1" in + start) + start + ;; + stop) + stop + ;; + status) + status + ;; + restart|reload) + stop + start + ;; + *) + echo $"Usage: $0 {start|stop|status|restart|reload}" + exit 1 +esac +exit 0 diff --git a/hugegraph-dist/src/assembly/static/bin/hugegraph.service b/hugegraph-dist/src/assembly/static/bin/hugegraph.service new file mode 100644 index 0000000000..803f6115b7 --- /dev/null +++ b/hugegraph-dist/src/assembly/static/bin/hugegraph.service @@ -0,0 +1,10 @@ +[Unit] +Description=HugeGraph RESTful service + +[Service] +Type=forking +ExecStart= +ExecStop= + +[Install] +WantedBy=multi-user.target diff --git a/hugegraph-dist/src/assembly/static/bin/install.sh b/hugegraph-dist/src/assembly/static/bin/install.sh new file mode 100755 index 0000000000..3c7557b833 --- /dev/null +++ b/hugegraph-dist/src/assembly/static/bin/install.sh @@ -0,0 +1,74 @@ +#!/bin/bash + +# This script is used to install hugegraph as a system service +# Usage: install.sh port + +function abs_path() { + SOURCE="${BASH_SOURCE[0]}" + while [ -h "$SOURCE" ]; do + DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" + done + echo "$( cd -P "$( dirname "$SOURCE" )" && pwd )" +} + +# Variables +BIN=`abs_path` +TOP="$(cd $BIN/../ && pwd)" + +# Command "service" related +SCRIPT_NAME=hugegraph +SRC_SCRIPT=$BIN/$SCRIPT_NAME +INIT_DIR=/etc/init.d + +# Command "systemctl" related +SERVICE=hugegraph.service +SERVICE_FILE=$BIN/$SERVICE +START_SCRIPT=start-hugegraph.sh +STOP_SCRIPT=stop-hugegraph.sh +SYSTEMD_DIR=/etc/systemd/system + +install_to_service() { + # Set HugeGraphServer port if provided + read -t 30 -p "Please input HugeGraphServer port:" SERVER_PORT + sed -i "s/SERVER_PORT=/SERVER_PORT=${SERVER_PORT}/g" $SRC_SCRIPT + + # Set INSTALL PATH + sed -i "s?INSTALL_DIR=?INSTALL_DIR=${TOP}?g" $SRC_SCRIPT + + # Install + sudo cp -fp $SRC_SCRIPT $INIT_DIR + sudo chmod +x $INIT_DIR/$SCRIPT_NAME +} + +install_to_systemd() { + # Set working directory + sed -i "s?ExecStart=?ExecStart=${BIN}/${START_SCRIPT}?g" $SERVICE_FILE + sed -i "s?ExecStop=?ExecStop=${BIN}/${STOP_SCRIPT}?g" $SERVICE_FILE + # Install + sudo cp -fp $SERVICE_FILE $SYSTEMD_DIR + sudo chmod +x $SYSTEMD_DIR/$SERVICE + # Make update work + sudo systemctl daemon-reload + # Start on boot + sudo systemctl enable $SERVICE +} + +test_command() { + command -v $1 >/dev/null 2>&1 + return $? +} + +# Install HugeGraph as service +if test_command systemctl; then + install_to_systemd +elif test_command service; then + install_to_service +else + echo "System must support either systemctl or service, but none is supported now." + exit 1 +fi + +# Init store +$BIN/init-store.sh