PageRenderTime 153ms CodeModel.GetById 121ms app.highlight 8ms RepoModel.GetById 12ms app.codeStats 10ms

/rel/files/ucengine

http://github.com/AF83/ucengine
#! | 156 lines | 132 code | 24 blank | 0 comment | 0 complexity | b77d95aca89a58bcfa5e0064e93915ae MD5 | raw file
  1#!/bin/bash
  2# -*- tab-width:4;indent-tabs-mode:nil -*-
  3# ex: ts=4 sw=4 et
  4
  5RUNNER_SCRIPT_DIR=$(cd ${0%/*} && pwd)
  6
  7RUNNER_BASE_DIR=${RUNNER_SCRIPT_DIR%/*}
  8RUNNER_ETC_DIR=$RUNNER_BASE_DIR/etc
  9RUNNER_LOG_DIR=$RUNNER_BASE_DIR/log
 10PIPE_DIR=/tmp/$RUNNER_BASE_DIR/
 11RUNNER_USER=
 12
 13# Make sure this script is running as the appropriate user
 14if [ ! -z "$RUNNER_USER" ] && [ `whoami` != "$RUNNER_USER" ]; then
 15    exec sudo -u $RUNNER_USER -i $0 $@
 16fi
 17
 18# Make sure CWD is set to runner base dir
 19cd $RUNNER_BASE_DIR
 20
 21# Make sure log directory exists
 22mkdir -p $RUNNER_LOG_DIR
 23
 24# Extract the target node name from node.args
 25NAME_ARG=`grep -e '-[s]*name' $RUNNER_ETC_DIR/vm.args`
 26if [ -z "$NAME_ARG" ]; then
 27    echo "vm.args needs to have either -name or -sname parameter."
 28    exit 1
 29fi
 30
 31# Extract the target cookie
 32COOKIE_ARG=`grep -e '-setcookie' $RUNNER_ETC_DIR/vm.args`
 33if [ -z "$COOKIE_ARG" ]; then
 34    echo "vm.args needs to have a -setcookie parameter."
 35    exit 1
 36fi
 37
 38# Identify the script name
 39SCRIPT=`basename $0`
 40
 41# Parse out release and erts info
 42START_ERL=`cat $RUNNER_BASE_DIR/releases/start_erl.data`
 43ERTS_VSN=${START_ERL% *}
 44APP_VSN=${START_ERL#* }
 45
 46# Add ERTS bin dir to our path
 47ERTS_PATH=$RUNNER_BASE_DIR/erts-$ERTS_VSN/bin
 48
 49# Setup command to control the node
 50NODETOOL="$ERTS_PATH/escript $ERTS_PATH/nodetool $NAME_ARG $COOKIE_ARG"
 51
 52# Check the first argument for instructions
 53case "$1" in
 54    start)
 55        # Make sure there is not already a node running
 56        RES=`$NODETOOL ping`
 57        if [ "$RES" = "pong" ]; then
 58            echo "Node is already running!"
 59            exit 1
 60        fi
 61        HEART_COMMAND="$RUNNER_BASE_DIR/bin/$SCRIPT start"
 62        export HEART_COMMAND
 63        mkdir -p $PIPE_DIR
 64        # Note the trailing slash on $PIPE_DIR/
 65        $ERTS_PATH/run_erl -daemon $PIPE_DIR/ $RUNNER_LOG_DIR "exec $RUNNER_BASE_DIR/bin/$SCRIPT console" 2>&1
 66        echo "Node is started. Check status with $0 ping"
 67        ;;
 68
 69    stop)
 70        # Wait for the node to completely stop...
 71        case `uname -s` in
 72            Linux|Darwin|FreeBSD|DragonFly|NetBSD|OpenBSD)
 73                # PID COMMAND
 74                PID=`ps ax -o pid= -o command=|\
 75                    grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'`
 76                ;;
 77            SunOS)
 78                # PID COMMAND
 79                PID=`ps -ef -o pid= -o args=|\
 80                    grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'`
 81                ;;
 82            CYGWIN*)
 83                # UID PID PPID TTY STIME COMMAND
 84                PID=`ps -efW|grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $2}'`
 85                ;;
 86        esac
 87        $NODETOOL stop
 88        while `kill -0 $PID 2>/dev/null`;
 89        do
 90            sleep 1
 91        done
 92        ;;
 93
 94    restart)
 95        ## Restart the VM without exiting the process
 96        $NODETOOL restart
 97        ;;
 98
 99    reboot)
100        ## Restart the VM completely (uses heart to restart it)
101        $NODETOOL reboot
102        ;;
103
104    ping)
105        ## See if the VM is alive
106        $NODETOOL ping
107        ;;
108
109    attach)
110        # Make sure a node IS running
111        RES=`$NODETOOL ping`
112        if [ "$RES" != "pong" ]; then
113            echo "Node is not running!"
114            exit 1
115        fi
116
117        shift
118        $ERTS_PATH/to_erl $PIPE_DIR
119        ;;
120
121    console|console_clean)
122        # .boot file typically just $SCRIPT (ie, the app name)
123        # however, for debugging, sometimes start_clean.boot is useful:
124        case "$1" in
125            console)        BOOTFILE=$SCRIPT ;;
126            console_clean)  BOOTFILE=start_clean ;;
127        esac
128        # Setup beam-required vars
129        ROOTDIR=$RUNNER_BASE_DIR
130        BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
131        EMU=beam
132        PROGNAME=`echo $0 | sed 's/.*\\///'`
133        CMD="$BINDIR/erlexec -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -embedded -config $RUNNER_ETC_DIR/app.config -args_file $RUNNER_ETC_DIR/vm.args -- ${1+"$@"}"
134        export EMU
135        export ROOTDIR
136        export BINDIR
137        export PROGNAME
138
139        # Dump environment info for logging purposes
140        echo "Exec: $CMD"
141        echo "Root: $ROOTDIR"
142
143        # Log the startup
144        logger -t "$SCRIPT[$$]" "Starting up"
145
146        # Start the VM
147        exec $CMD
148        ;;
149
150    *)
151        echo "Usage: $SCRIPT {start|stop|restart|reboot|ping|console|attach}"
152        exit 1
153        ;;
154esac
155
156exit 0