You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by dk...@apache.org on 2016/11/29 00:24:35 UTC

[04/50] [abbrv] tinkerpop git commit: gremlin-server.sh is also init script. updated docs.

gremlin-server.sh is also init script. updated docs.


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/e4a94ad6
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/e4a94ad6
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/e4a94ad6

Branch: refs/heads/TINKERPOP-1363
Commit: e4a94ad6615967a026ccddf7abd3b70d726b860a
Parents: 417a791
Author: Robert Dale <ro...@gmail.com>
Authored: Fri Sep 30 08:39:39 2016 -0400
Committer: Robert Dale <ro...@gmail.com>
Committed: Thu Nov 17 03:45:36 2016 -0500

----------------------------------------------------------------------
 .../src/reference/gremlin-applications.asciidoc | 114 +++++++-
 docs/src/reference/gremlin-variants.asciidoc    |   4 +-
 .../reference/implementations-neo4j.asciidoc    |   2 +-
 .../gremlin-language-variants/index.asciidoc    |   2 +-
 docs/src/upgrade/release-3.3.x.asciidoc         |  50 +---
 .../src/main/bin/gremlin-server.initd           | 173 ------------
 .../src/main/bin/gremlin-server.service         |  36 ---
 gremlin-server/src/main/bin/gremlin-server.sh   | 275 +++++++++++++++++--
 8 files changed, 354 insertions(+), 302 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e4a94ad6/docs/src/reference/gremlin-applications.asciidoc
----------------------------------------------------------------------
diff --git a/docs/src/reference/gremlin-applications.asciidoc b/docs/src/reference/gremlin-applications.asciidoc
index 5a0e397..29d12b9 100644
--- a/docs/src/reference/gremlin-applications.asciidoc
+++ b/docs/src/reference/gremlin-applications.asciidoc
@@ -680,7 +680,7 @@ which will boost performance and reduce resources required on the server.
 Configuration
 ^^^^^^^^^^^^^
 
-The following table describes the various configuration options for the Gremlin Driver:
+The following table describes the various YAML configuration options for the Gremlin Driver:
 
 [width="100%",cols="3,10,^2",options="header"]
 |=========================================================
@@ -959,29 +959,58 @@ can be cached for faster evaluation on the next submission.
 Configuring
 ~~~~~~~~~~~
 
-As mentioned earlier, Gremlin Server is configured though a YAML file.  By default, Gremlin Server will look for a
-file called `conf/gremlin-server.yaml` to configure itself on startup.  To override this default, supply the file
-to use to `bin/gremlin-server.sh` as in:
-
-[source,text]
-----
-bin/gremlin-server.sh conf/gremlin-server-min.yaml
-----
-
-The `gremlin-server.sh` file also serves a second purpose.  It can be used to "install" dependencies to the Gremlin
+The `gremlin-server.sh` file serves multiple purposes.  It can be used to "install" dependencies to the Gremlin
 Server path.  For example, to be able to configure and use other `Graph` implementations, the dependencies must be
-made available to Gremlin Server.  To do this, use the `-i` switch and supply the Maven coordinates for the dependency
+made available to Gremlin Server.  To do this, use the `install` switch and supply the Maven coordinates for the dependency
 to "install".  For example, to use Neo4j in Gremlin Server:
 
 [source,text]
 ----
-bin/gremlin-server.sh -i org.apache.tinkerpop neo4j-gremlin x.y.z
+bin/gremlin-server.sh install org.apache.tinkerpop neo4j-gremlin x.y.z
 ----
 
 This command will "grab" the appropriate dependencies and copy them to the `ext` directory of Gremlin Server, which
 will then allow them to be "used" the next time the server is started.  To uninstall dependencies, simply delete them
 from the `ext` directory.
 
+`bin/gremlin-server.sh` has several other options.
+
+[width="100%",cols="3,10",options="header"]
+|=========================================================
+|Parameter|Description
+|start|Start the server in the background.
+|stop|Shutdown the server.
+|restart|Shutdown a running server then start it again.
+|status|Check if the server is running.
+|console|Start the server in the foreground. Use ^C to kill it.
+|install <group> <artifact> <version>| Install dependencies into the server. "-i" exists for backwards compatibility but is deprecated.
+|<conf file>| Start the server in the foreground using the provided YAML config file.
+|=========================================================
+
+The `bin/gremlin-server.sh` script can be customized with environment variables in `bin/gremlin-server.conf`.
+
+[width="100%",cols="3,10",options="header"]
+|=========================================================
+|Variable |Description
+|DEBUG| Enable debugging of the startup script
+|GREMLIN_HOME| The Gremlin Server install directory. Use this if the script has trouble finding itself.
+|GREMLIN_YAML| The default server YAML file (conf/gremlin-server.yaml)
+|LOG_DIR| Location of gremlin.log where stdout/stderr are captured (logs/)
+|PID_DIR| Location of gremlin.pid
+|RUNAS| User to run the server as
+|JAVA_HOME| Java install location. Will use $JAVA_HOME/bin/java
+|JAVA_OPTIONS| Options passed to the JVM
+|=========================================================
+
+As mentioned earlier, Gremlin Server is configured though a YAML file.  By default, Gremlin Server will look for a
+file called `conf/gremlin-server.yaml` to configure itself on startup.  To override this default, set GREMLIN_YAML in `bin/gemlin-server.conf` or supply the file
+to use to `bin/gremlin-server.sh` as in:
+
+[source,text]
+----
+bin/gremlin-server.sh conf/gremlin-server-min.yaml
+----
+
 The following table describes the various configuration options that Gremlin Server expects:
 
 [width="100%",cols="3,10,^2",options="header"]
@@ -1482,6 +1511,63 @@ and standard deviation evaluation times, as well as the 75th, 95th, 98th, 99th a
 mean, and standard deviation evaluation times, as well as the 75th, 95th, 98th, 99th and 99.9th percentile evaluation
 times.
 
+As A Service
+^^^^^^^^^^^^
+
+Gremlin server can be configured to run as a service.
+
+Init.d (SysV)
++++++++++++++
+
+Link `bin/gremlin-server.sh` to `init.d`
+Be sure to set RUNAS to the service user in `bin/gremlin-server.conf`
+
+[source,bash]
+----
+# Install
+ln -s /path/to/apache-tinkerpop-gremlin-server-x.y.z/bin/gremlin-server.sh /etc/init.d/gremlin-server
+
+# Systems with chkconfig/service. E.g. Fedora, Red Hat
+chkconfig --add gremlin-server
+
+# Start
+service gremlin-server start
+
+# Or call directly
+/etc/init.d/gremlin-server restart
+
+----
+
+Systemd
++++++++
+
+To install, copy the this service template to /etc/systemd/system/gremlin.service
+and update the paths `/path/to/apache-tinkerpop-gremlin-server` with the actual install path of gremlin server.
+
+[source,bash]
+----
+[Unit]
+Description=Apache Tinkerpop Gremlin server daemon
+Documentation=http://tinkerpop.apache.org/
+After=network.target
+
+[Service]
+Type=forking
+ExecStart=/path/to/apache-tinkerpop-gremlin-server/bin/gremlin-server.sh start
+ExecStop=/path/to/apache-tinkerpop-gremlin-server/bin/gremlin-server.sh stop
+PIDFile=/path/to/apache-tinkerpop-gremlin-server/run/gremlin.pid
+User=gremlin
+
+[Install]
+WantedBy=multi-user.target
+----
+
+
+Enable the service with `systemctl enable gremlin-server`
+
+Start the service with `systemctl start gremlin-server`
+
+
 Best Practices
 ~~~~~~~~~~~~~~
 
@@ -1492,7 +1578,7 @@ Tuning
 
 image:gremlin-handdrawn.png[width=120,float=right] Tuning Gremlin Server for a particular environment may require some simple trial-and-error, but the following represent some basic guidelines that might be useful:
 
-* Gremlin Server defaults to a very modest maximum heap size.  Consider increasing this value for non-trivial uses.  Maximum heap size (`-Xmx`) is defined with the `JAVA_OPTIONS` setting in `gremlin-server.sh`.
+* Gremlin Server defaults to a very modest maximum heap size.  Consider increasing this value for non-trivial uses.  Maximum heap size (`-Xmx`) is defined with the `JAVA_OPTIONS` setting in `gremlin-server.conf`.
 * When configuring the size of `threadPoolWorker` start with the default of `1` and increment by one as needed to a maximum of `2*number of cores`.
 * The "right" size of the `gremlinPool` setting is somewhat dependent on the type of scripts that will be processed
 by Gremlin Server.  As requests arrive to Gremlin Server they are decoded and queued to be processed by threads in

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e4a94ad6/docs/src/reference/gremlin-variants.asciidoc
----------------------------------------------------------------------
diff --git a/docs/src/reference/gremlin-variants.asciidoc b/docs/src/reference/gremlin-variants.asciidoc
index 853b087..1fc2559 100644
--- a/docs/src/reference/gremlin-variants.asciidoc
+++ b/docs/src/reference/gremlin-variants.asciidoc
@@ -125,7 +125,7 @@ IMPORTANT: Gremlin-Python is compatible with GraphSON 2.0 only, so this serializ
 
 [source,bash]
 ----
-$ bin/gremlin-server.sh -i org.apache.tinkerpop gremlin-python x.y.z
+$ bin/gremlin-server.sh install org.apache.tinkerpop gremlin-python x.y.z
 $ bin/gremlin-server.sh conf/gremlin-server-modern-py.yaml
 [INFO] GremlinServer -
        \,,,/
@@ -161,7 +161,7 @@ $ bin/gremlin-server.sh conf/gremlin-server-modern-py.yaml
 
 ----
 
-NOTE: The command to use `-i` need only be executed once to gather `gremlin-python` dependencies into Gremlin Servers'
+NOTE: The command to use `install` need only be executed once to gather `gremlin-python` dependencies into Gremlin Servers'
 path. Future starts of Gremlin Server will not require that command.
 
 Within the CPython console, an empty `Graph` is created and a traversal source is spawned with `RemoteStrategy`.

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e4a94ad6/docs/src/reference/implementations-neo4j.asciidoc
----------------------------------------------------------------------
diff --git a/docs/src/reference/implementations-neo4j.asciidoc b/docs/src/reference/implementations-neo4j.asciidoc
index 902295f..42ab2bf 100644
--- a/docs/src/reference/implementations-neo4j.asciidoc
+++ b/docs/src/reference/implementations-neo4j.asciidoc
@@ -58,7 +58,7 @@ gremlin> graph = Neo4jGraph.open('/tmp/neo4j')
 
 TIP: To host Neo4j in <<gremlin-server,Gremlin Server>>, the dependencies must first be "installed" or otherwise
 copied to the Gremlin Server path. The automated method for doing this would be to execute
-`bin/gremlin-server.sh -i org.apache.tinkerpop neo4j-gremlin x.y.z`.
+`bin/gremlin-server.sh install org.apache.tinkerpop neo4j-gremlin x.y.z`.
 
 Indices
 ~~~~~~~

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e4a94ad6/docs/src/tutorials/gremlin-language-variants/index.asciidoc
----------------------------------------------------------------------
diff --git a/docs/src/tutorials/gremlin-language-variants/index.asciidoc b/docs/src/tutorials/gremlin-language-variants/index.asciidoc
index 533aa04..65fb58f 100644
--- a/docs/src/tutorials/gremlin-language-variants/index.asciidoc
+++ b/docs/src/tutorials/gremlin-language-variants/index.asciidoc
@@ -519,7 +519,7 @@ for serialization to a `RemoteConnection` such as GremlinServer.
 
 [source,bash]
 ----
-$ bin/gremlin-server.sh -i org.apache.tinkerpop gremlin-python x.y.z
+$ bin/gremlin-server.sh install org.apache.tinkerpop gremlin-python x.y.z
 $ bin/gremlin-server.sh conf/gremlin-server-modern-py.yaml
 [INFO] GremlinServer -
        \,,,/

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e4a94ad6/docs/src/upgrade/release-3.3.x.asciidoc
----------------------------------------------------------------------
diff --git a/docs/src/upgrade/release-3.3.x.asciidoc b/docs/src/upgrade/release-3.3.x.asciidoc
index 6bcc8e6..6935eed 100644
--- a/docs/src/upgrade/release-3.3.x.asciidoc
+++ b/docs/src/upgrade/release-3.3.x.asciidoc
@@ -113,53 +113,11 @@ Those graph providers who relied on these tests should simply remove them from t
 
 See: link:https://issues.apache.org/jira/browse/TINKERPOP-1235[TINKERPOP-1235]
 
-SysV and Systemd Init Scripts
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-
-This replaces gremlin-server.sh
-
-*SysV*
-
-Certain parameters can be configured via bin/gremlin-server.conf
-
-[source,bash]
-----
-# Install
-ln -s /path/to/apache-tinkerpop-gremlin-server-x.y.z/bin/gremlin-server.initd /etc/init.d/gremlin-server
-
-# Systems with chkconfig/service. E.g. Fedora, Red Hat
-chkconfig --add gremlin-server
-
-# Start
-service gremlin-server start
-
-# Or call directly
-/etc/init.d/gremlin-server restart
-
-# Can even be used without installing
-./bin/gremlin-server.initd restart
-----
-
-*systemd*
-
-JAVA_HOME and JAVA_OPTIONS can be configured via bin/gremlin-server.conf
-
-[source,bash]
-----
-# Install
-cp /path/to/apache-tinkerpop-gremlin-server-x.y.z/bin/gremlin-server.service /etc/systemd/system/
-
-# Configure systemd template
-# Update WorkingDirectory, EnvironmentFile, ExecStart installation path locations
-# Check User, Group
-edit /etc/systemd/system/gremlin-server.service
-
-# Enable
-systemctl enable gremlin-server
+Gremlin-server.sh and Init Scripts
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-# Start
-systemctl start gremlin-server
+`gremlin-server.sh` is now also an init script and can no longer be started without parameters. To start it in the foreground, please use the `console` parameter. Also, it will still start in the foreground when provided a yaml configuration file.
 
-----
+How to install as a service has been added to the reference docs.
 
 See: link:https://issues.apache.org/jira/browse/TINKERPOP-980[TINKERPOP-980]

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e4a94ad6/gremlin-server/src/main/bin/gremlin-server.initd
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/bin/gremlin-server.initd b/gremlin-server/src/main/bin/gremlin-server.initd
deleted file mode 100755
index 1049806..0000000
--- a/gremlin-server/src/main/bin/gremlin-server.initd
+++ /dev/null
@@ -1,173 +0,0 @@
-#!/bin/bash
-#
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License.  You may obtain a copy of the License at
-#
-#   http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied.  See the License for the
-# specific language governing permissions and limitations
-# under the License.
-#
-
-### BEGIN INIT INFO
-# Provides:          gremlin-server
-# Required-Start:    $remote_fs $syslog $network
-# Required-Stop:     $remote_fs $syslog $network
-# Default-Start:     2 3 4 5
-# Default-Stop:      0 1 6
-# Short-Description: Gremlin Server
-# Description:       Apache Tinkerpop Gremlin Server
-# chkconfig:         2345 98 01
-### END INIT INFO
-
-[[ -n "$DEBUG" ]] && set -x
-
-SOURCE="$0"
-while [ -h "$SOURCE" ]; do
-  cd -P "$( dirname "$SOURCE" )" || exit 1
-  DIR="$(pwd)"
-  SOURCE="$(readlink "$SOURCE")"
-  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
-done
-cd -P "$( dirname "$SOURCE" )" || exit 1
-GREMLIN_BIN="$(pwd)"
-
-GREMLIN_CONF=$GREMLIN_BIN/gremlin-server.conf
-
-[[ -r $GREMLIN_CONF ]] && source $GREMLIN_CONF
-[[ -n "$DEBUG" ]] && set -x
-
-if [ -z "$GREMLIN_HOME" ]; then
-  cd ..
-  GREMLIN_HOME="$(pwd)"
-fi
-
-GREMLIN_CMD=$GREMLIN_HOME/bin/gremlin-server.sh
-
-if [ -z "$LOG_DIR" ] ; then
-  LOG_DIR="$GREMLIN_HOME/logs"
-fi
-
-if [ ! -d "$LOG_DIR" ]; then
-  echo LOG_DIR $LOG_DIR does not exist
-  exit 1
-fi
-
-if [ -z "$LOG_FILE" ]; then
-  LOG_FILE="$LOG_DIR/gremlin.log"
-fi
-
-if [ -z "$PID_DIR" ] ; then
-  PID_DIR="$GREMLIN_HOME/run"
-fi
-
-if [ ! -d "$PID_DIR" ]; then
-  echo PID_DIR $PID_DIR does not exist
-  exit 1
-fi
-
-if [ -z "$PID_FILE" ]; then
-  PID_FILE=$PID_DIR/gremlin.pid
-fi
-
-if [ -z "$GREMLIN_YAML" ]; then
-  GREMLIN_YAML=$GREMLIN_HOME/conf/gremlin-server.yaml
-fi
-
-if [ ! -r "$GREMLIN_YAML" ]; then
-  # try relative to home
-  GREMLIN_YAML="$GREMLIN_HOME/$GREMLIN_YAML"
-  if [ ! -r "$GREMLIN_YAML" ]; then
-    echo WARNING: $GREMLIN_YAML is unreadable
-  fi
-fi
-
-status() {
-  if [ -r "$PID_FILE" ] ; then
-    PID=$(cat "$PID_FILE")
-    ps -p "$PID" &> /dev/null
-    RUNNING=$?
-    if [ $RUNNING -gt 0 ]; then
-      echo Server not running
-    else
-      echo Server running with PID $(cat "$PID_FILE")
-    fi
-  else
-    echo Server not running or PID file not found.
-  fi
-}
-
-stop() {
-  if [ -r "$PID_FILE" ] ; then
-    PID=$(cat "$PID_FILE")
-    ps -p "$PID" &> /dev/null
-    RUNNING=$?
-    if [ $RUNNING -gt 0 ]; then
-      echo Server not running
-    else
-      kill "$PID" &> /dev/null || { echo "Unable to kill server [$PID]"; exit 1; }
-      for i in $(seq 1 60); do
-        ps -p "$PID" &> /dev/null || { echo "Server stopped [$PID]"; rm -f "$PID_FILE"; return 0; }
-        [[ $i -eq 30 ]] && kill "$PID" &> /dev/null
-        sleep 1
-      done
-      echo "Unable to kill server [$PID]";
-      exit 1;
-    fi
-  else
-    echo Server not running or PID file not found.
-  fi
-  rm -f "$PID_FILE"
-}
-
-start() {
-
-  if [ -r "$PID_FILE" ] ; then
-    PID=$(cat "$PID_FILE")
-    ps -p "$PID" &> /dev/null
-    RUNNING=$?
-    if [ $RUNNING -eq 0 ]; then
-      echo Server already running with PID $(cat "$PID_FILE").
-      exit 1
-    fi
-  fi
-
-  if [ -z "$RUNAS" ]; then
-    JAVA_HOME="$JAVA_HOME" JAVA_OPTIONS="$JAVA_OPTIONS" $GREMLIN_CMD "$GREMLIN_YAML" >> "$LOG_FILE" 2>&1 &
-    PID=$!
-    disown $PID
-    echo $PID > "$PID_FILE"
-  else
-    su -c "JAVA_HOME=\"$JAVA_HOME\" JAVA_OPTIONS=\"$JAVA_OPTIONS\" $GREMLIN_CMD \"$GREMLIN_YAML\" >> \"$LOG_FILE\" 2>&1 & echo \$! "  "$RUNAS" > "$PID_FILE"
-    chown "$RUNAS" "$PID_FILE"
-  fi
-
-}
-
-case "$1" in
-  status)
-    status
-    ;;
-  restart)
-    stop
-    start
-    ;;
-  start)
-    start
-    ;;
-  stop)
-    stop
-    ;;
-  *)
-    echo "Usage: $0 {start|stop|restart|status}"; exit 1;
-    ;;
-esac

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e4a94ad6/gremlin-server/src/main/bin/gremlin-server.service
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/bin/gremlin-server.service b/gremlin-server/src/main/bin/gremlin-server.service
deleted file mode 100644
index 2cec63f..0000000
--- a/gremlin-server/src/main/bin/gremlin-server.service
+++ /dev/null
@@ -1,36 +0,0 @@
-#
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License.  You may obtain a copy of the License at
-#
-#   http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied.  See the License for the
-# specific language governing permissions and limitations
-# under the License.
-#
-
-[Unit]
-Description=Apache Tinkerpop Gremlin server daemon
-Documentation=http://tinkerpop.apache.org/
-After=network.target
-
-[Service]
-WorkingDirectory=/opt/apache-tinkerpop-gremlin-server
-EnvironmentFile=/opt/apache-tinkerpop-gremlin-server/bin/gremlin-server.conf
-ExecStart=/opt/apache-tinkerpop-gremlin-server/bin/gremlin-server.sh
-KillMode=process
-Restart=no
-RestartSec=60s
-User=gremlin
-Group=gremlin
-
-[Install]
-WantedBy=multi-user.target

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e4a94ad6/gremlin-server/src/main/bin/gremlin-server.sh
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/bin/gremlin-server.sh b/gremlin-server/src/main/bin/gremlin-server.sh
index fcede2b..2dc3ae2 100755
--- a/gremlin-server/src/main/bin/gremlin-server.sh
+++ b/gremlin-server/src/main/bin/gremlin-server.sh
@@ -1,6 +1,5 @@
 #!/bin/bash
 #
-#
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
@@ -18,45 +17,263 @@
 # specific language governing permissions and limitations
 # under the License.
 #
-case `uname` in
-  CYGWIN*)
-    CP="`dirname $0`"/../conf/
-    CP="$CP":$( echo `dirname $0`/../lib/*.jar . | sed 's/ /;/g')
-    ;;
-  *)
-    CP="`dirname $0`"/../conf/
-    CP="$CP":$( echo `dirname $0`/../lib/*.jar . | sed 's/ /:/g')
-esac
-#echo $CP
 
-SOURCE="${BASH_SOURCE[0]}"
-while [ -h "$SOURCE" ]; do
-  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
+### BEGIN INIT INFO
+# Provides:          gremlin-server
+# Required-Start:    $remote_fs $syslog $network
+# Required-Stop:     $remote_fs $syslog $network
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Gremlin Server
+# Description:       Apache Tinkerpop Gremlin Server
+# chkconfig:         2345 98 01
+### END INIT INFO
+
+[[ -n "$DEBUG" ]] && set -x
+
+SOURCE="$0"
+while [[ -h "$SOURCE" ]]; do
+  cd -P "$( dirname "$SOURCE" )" || exit 1
+  DIR="$(pwd)"
   SOURCE="$(readlink "$SOURCE")"
   [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
 done
-DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
-CP=$CP:$( find -L "$DIR"/../ext -mindepth 1 -maxdepth 1 -type d | \
-          sort | sed 's/$/\/plugin\/*/' | tr '\n' ':' )
+cd -P "$( dirname "$SOURCE" )" || exit 1
+GREMLIN_BIN="$(pwd)"
+
+GREMLIN_CONF=$GREMLIN_BIN/gremlin-server.conf
+
+[[ -r $GREMLIN_CONF ]] && source $GREMLIN_CONF
+[[ -n "$DEBUG" ]] && set -x
 
-export CLASSPATH="${CLASSPATH:-}:$CP"
+if [[ -z "$GREMLIN_HOME" ]]; then
+  cd ..
+  GREMLIN_HOME="$(pwd)"
+fi
+
+if [[ -z "$LOG_DIR" ]] ; then
+  LOG_DIR="$GREMLIN_HOME/logs"
+fi
+
+if [[ -z "$LOG_FILE" ]]; then
+  LOG_FILE="$LOG_DIR/gremlin.log"
+fi
+
+if [[ -z "$PID_DIR" ]] ; then
+  PID_DIR="$GREMLIN_HOME/run"
+fi
+
+if [[ -z "$PID_FILE" ]]; then
+  PID_FILE=$PID_DIR/gremlin.pid
+fi
+
+if [[ -z "$GREMLIN_YAML" ]]; then
+  GREMLIN_YAML=$GREMLIN_HOME/conf/gremlin-server.yaml
+fi
+
+if [[ ! -r "$GREMLIN_YAML" ]]; then
+  # try relative to home
+  GREMLIN_YAML="$GREMLIN_HOME/$GREMLIN_YAML"
+  if [[ ! -r "$GREMLIN_YAML" ]]; then
+    echo WARNING: $GREMLIN_YAML is unreadable
+  fi
+fi
+
+# absolute file path requires 'file:'
+LOG4J_CONF="file:$GREMLIN_HOME/conf/log4j-server.properties"
 
 # Find Java
-if [ "$JAVA_HOME" = "" ] ; then
-    JAVA="java -server"
+if [[ "$JAVA_HOME" = "" ]] ; then
+    JAVA="java"
 else
-    JAVA="$JAVA_HOME/bin/java -server"
+    JAVA="$JAVA_HOME/bin/java"
 fi
 
 # Set Java options
-if [ "$JAVA_OPTIONS" = "" ] ; then
+if [[ "$JAVA_OPTIONS" = "" ]] ; then
     JAVA_OPTIONS="-Xms32m -Xmx512m"
 fi
 
-# Execute the application and return its exit code
-if [ "$1" = "-i" ]; then
-  shift
-  exec $JAVA -Dlog4j.configuration=conf/log4j-server.properties $JAVA_OPTIONS -cp $CP:$CLASSPATH org.apache.tinkerpop.gremlin.server.util.GremlinServerInstall "$@"
-else
-  exec $JAVA -Dlog4j.configuration=conf/log4j-server.properties $JAVA_OPTIONS -cp $CP:$CLASSPATH org.apache.tinkerpop.gremlin.server.GremlinServer "$@"
-fi
\ No newline at end of file
+# Build Java CLASSPATH
+CP="$GREMLIN_HOME/conf/"
+CP="$CP":$( echo $GREMLIN_HOME/lib/*.jar . | sed 's/ /:/g')
+CP="$CP":$( find -L "$GREMLIN_HOME"/ext -mindepth 1 -maxdepth 1 -type d | \
+        sort | sed 's/$/\/plugin\/*/' | tr '\n' ':' )
+
+CLASSPATH="${CLASSPATH:-}:$CP"
+
+GREMLIN_SERVER_CMD=org.apache.tinkerpop.gremlin.server.GremlinServer
+GREMLIN_INSTALL_CMD=org.apache.tinkerpop.gremlin.server.util.GremlinServerInstall
+
+
+isRunning() {
+  if [[ -r "$PID_FILE" ]] ; then
+    PID=$(cat "$PID_FILE")
+    ps -p "$PID" &> /dev/null
+    return $?
+  else
+    return 1
+  fi
+}
+
+status() {
+  isRunning
+  RUNNING=$?
+    if [[ $RUNNING -gt 0 ]]; then
+      echo Server not running
+    else
+      echo Server running with PID $(cat "$PID_FILE")
+    fi
+}
+
+stop() {
+  isRunning
+  RUNNING=$?
+  if [[ $RUNNING -gt 0 ]]; then
+    echo Server not running
+    rm -f "$PID_FILE"
+  else
+    kill "$PID" &> /dev/null || { echo "Unable to kill server [$PID]"; exit 1; }
+    for i in $(seq 1 60); do
+      ps -p "$PID" &> /dev/null || { echo "Server stopped [$PID]"; rm -f "$PID_FILE"; return 0; }
+      [[ $i -eq 30 ]] && kill "$PID" &> /dev/null
+      sleep 1
+    done
+    echo "Unable to kill server [$PID]";
+    exit 1;
+  fi
+}
+
+start() {
+  isRunning
+  RUNNING=$?
+  if [[ $RUNNING -eq 0 ]]; then
+    echo Server already running with PID $(cat "$PID_FILE").
+    exit 1
+  fi
+
+  if [[ -z "$RUNAS" ]]; then
+
+    mkdir -p "$LOG_DIR" &>/dev/null
+    if [[ ! -d "$LOG_DIR" ]]; then
+      echo ERROR: LOG_DIR $LOG_DIR does not exist and could not be created.
+      exit 1
+    fi
+
+    mkdir -p "$PID_DIR" &>/dev/null
+    if [[ ! -d "$PID_DIR" ]]; then
+      echo ERROR: PID_DIR $PID_DIR does not exist and could not be created.
+      exit 1
+    fi
+
+    $JAVA -Dlog4j.configuration=$LOG4J_CONF $JAVA_OPTIONS -cp $CP:$CLASSPATH $GREMLIN_SERVER_CMD "$GREMLIN_YAML" >> "$LOG_FILE" 2>&1 &
+    PID=$!
+    disown $PID
+    echo $PID > "$PID_FILE"
+  else
+
+    su -c "mkdir -p $LOG_DIR &>/dev/null"  "$RUNAS"
+    if [[ ! -d "$LOG_DIR" ]]; then
+      echo ERROR: LOG_DIR $LOG_DIR does not exist and could not be created.
+      exit 1
+    fi
+
+    su -c "mkdir -p $PID_DIR &>/dev/null"  "$RUNAS"
+    if [[ ! -d "$PID_DIR" ]]; then
+      echo ERROR: PID_DIR $PID_DIR does not exist and could not be created.
+      exit 1
+    fi
+
+    su -c "$JAVA -Dlog4j.configuration=$LOG4J_CONF $JAVA_OPTIONS -cp $CP:$CLASSPATH $GREMLIN_SERVER_CMD \"$GREMLIN_YAML\" >> \"$LOG_FILE\" 2>&1 & echo \$! "  "$RUNAS" > "$PID_FILE"
+    chown "$RUNAS" "$PID_FILE"
+  fi
+
+  isRunning
+  RUNNING=$?
+  if [[ $RUNNING -eq 0 ]]; then
+    echo Server started $(cat "$PID_FILE").
+    exit 0
+  else
+    echo Server failed
+    exit 1
+  fi
+
+}
+
+startForeground() {
+  isRunning
+  RUNNING=$?
+  if [[ $RUNNING -eq 0 ]]; then
+    echo Server already running with PID $(cat "$PID_FILE").
+    exit 1
+  fi
+
+  if [[ -z "$RUNAS" ]]; then
+    $JAVA -Dlog4j.configuration=$LOG4J_CONF $JAVA_OPTIONS -cp $CP:$CLASSPATH $GREMLIN_SERVER_CMD "$GREMLIN_YAML"
+    exit 0
+  else
+    echo Starting in foreground not supported with RUNAS
+    exit 1
+  fi
+
+}
+
+install() {
+
+  isRunning
+  RUNNING=$?
+  if [[ $RUNNING -eq 0 ]]; then
+    echo Server must be stopped before installing.
+    exit 1
+  fi
+
+  echo Installing dependency $@
+
+  DEPS="$@"
+  if [[ -z "$RUNAS" ]]; then
+    $JAVA -Dlog4j.configuration=$LOG4J_CONF $JAVA_OPTIONS -cp $CP:$CLASSPATH  $GREMLIN_INSTALL_CMD $DEPS
+  else
+    su -c "$JAVA -Dlog4j.configuration=$LOG4J_CONF $JAVA_OPTIONS -cp $CP:$CLASSPATH $GREMLIN_INSTALL_CMD $DEPS "  "$RUNAS"
+  fi
+
+}
+
+case "$1" in
+  status)
+    status
+    ;;
+  restart)
+    stop
+    start
+    ;;
+  start)
+    start
+    ;;
+  stop)
+    stop
+    ;;
+  -i)
+    shift
+    install "$@"
+    ;;
+  install)
+    shift
+    install "$@"
+    ;;
+  console)
+    startForeground
+    ;;
+  *)
+    if [[ -n "$1" ]] ; then
+      if [[ -r "$1" ]]; then
+        GREMLIN_YAML="$1"
+        startForeground
+      elif [[ -r "$GREMLIN_HOME/$1" ]] ; then
+        GREMLIN_YAML="$GREMLIN_HOME/$1"
+        startForeground
+      fi
+      echo Configuration file not found.
+    fi
+    echo "Usage: $0 {start|stop|restart|status|console|install <group> <artifact> <version>|<conf file>}"; exit 1;
+    ;;
+esac