#!/bin/bash # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Package: MyGrid 3.3.1 # # Description: This shell script takes care of starting and stopping MyGrid. # # Copyright (c) 2002-2006 Universidade Federal de Campina Grande # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # OurGrid Version # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - OGVERSION="3.3.1" # Setting macros # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - OK=0 NOK=1 # Setting error codes (from 2 to 254) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ERR_BASE=64 ERR_STD_MISUSE=$[ERR_BASE + 0] ERR_MGROOT_NOT_DEFINED=$[ERR_BASE + 1] ERR_VARS_NOT_DEFINED=$[ERR_BASE + 2] ERR_JAVAOPTIONS_NOT_DEFINED=$[ERR_BASE + 3] ERR_CLASSPATH_NOT_DEFINED=$[ERR_BASE + 6] ERR_JAVA_NOT_FOUND=$[ERR_BASE + 7] ErrDescr=([$ERR_STD_MISUSE]="Syntax error" [$ERR_MGROOT_NOT_DEFINED]="Variable MGROOT not defined" [$ERR_VARS_NOT_DEFINED]="BIN and LIB vars not defined" [$ERR_JAVAOPTIONS_NOT_DEFINED]="Java options not defined" [$ERR_CLASSPATH_NOT_DEFINED]="CLASSPATH variable not defined" [$ERR_JAVA_NOT_FOUND]="Java not found in your path") # Find MGROOT: # $0 is the executable $MGROOT/bin/mygrid or a link to it # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - findmgroot(){ PRG="$0" while [ -h "$PRG" ]; do ls=`ls -ld "$PRG"` link=`expr "$ls" : '.*-> \(.*\)$'` if expr "$link" : '.*/.*' > /dev/null; then PRG="$link" else PRG=`dirname "$PRG"`/"$link" fi done PRGDIR=`dirname "$PRG"` MGROOT=`cd "$PRGDIR/.." ; pwd` if [ -z "$MGROOT" ]; then echo "ERR_MGROOT_NOT_DEFINED" return $ERR_MGROOT_NOT_DEFINED else return $OK fi } # Set the MGROOT and CLASSPATH including all libraries from $MGROOT/lib # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - setclasspath(){ PWD_=`pwd` mypath=. cd "$LIB" for i in `/bin/ls *.jar`; do myclass="$LIB/$i" mypath=$mypath:$myclass done cd "$PWD_" CLASSPATH="$mypath:$MGROOT/resources/doctorPlugins" if [ -z "$CLASSPATH" ]; then return $ERR_CLASSPATH_NOT_DEFINED else return $OK fi } # Set the JAVAOPTION to invoke MyGrid classes # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - setjavaoptions(){ if [ -z ${OGROOT} ]; then JAVAOPTIONS="-DMGROOT=$MGROOT" else JAVAOPTIONS="-DMGROOT=$MGROOT -DOGROOT=$OGROOT" fi } # Set the $VAR $BIN and $LIB # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - setenvvars(){ BIN=$MGROOT/bin LIB=$MGROOT/lib if [ -z "$BIN" ] || [ -z "$LIB" ] ; then echo "ERR_VARS_NOT_DEFINED" return $ERR_VARS_NOT_DEFINED else return $OK fi } checkenvironment() { java -classpath "$CLASSPATH" "$JAVAOPTIONS" "$commandmanager" "$checkconfenv" } start() { java -classpath "$CLASSPATH" "$JAVAOPTIONS" "$commandmanager" -reg "$checkconfenv" java -Xms64m -Xmx1024m -classpath "$CLASSPATH" "$JAVAOPTIONS" "$commandmanager" "$startcommand" & sleep 3 } # +++++ MyGrid Config config() { java -classpath "$CLASSPATH" "$JAVAOPTIONS" "$commandmanager" -reg "$checkconfenv" --mgproperties } # +++++ Stop MyGrid stop() { java -classpath "$CLASSPATH" "$JAVAOPTIONS" "$commandmanager" "$stopcommand" "$COMMAND_ARGS" } status() { java -Xms64m -Xmx1024m -classpath "$CLASSPATH" "$JAVAOPTIONS" "$commandmanager" "$statuscommand" "$COMMAND_ARGS" "$COMMAND_ARGS2" "$COMMAND_ARGS3" exit $? } printerrdescr() { errcode=$1 echo "${ErrDescr[$errcode]}" } print_commands() { echo "MYGRID commands are:" echo " start Start MyGrid on the local machine (base machine)" echo " stop Stop MyGrid on the local machine" echo " -f Forces MyGrid to stop" echo " config Create/Update mg.properties file" echo " restart Stop MyGrid if it was running and start it" echo " gui Start MyGrid's GUI" echo " status Show current status of MyGrid" echo " -i=jobid Use this flag for information on a certain job" echo " -s=state Use this flag for information jobs with a certain status" echo " -l=label Use this flag for information on jobs with a certain label" echo " addjob Add a job for execution on MyGrid environment" echo " canceljob Cancel a job execution" echo " waitforjob Stop and wait until the job specified by is done" echo " setgrid Set MyGrid's gumps" echo " clean Clean the finished jobs" } usage(){ if [ $# -eq 0 ]; then echo "Usage: mygrid command [command-options-and-arguments]" echo print_commands echo echo "command-options-and-arguments depends on the specific command" echo echo "For MyGrid updates and additional information, see the " echo "OurGrid Project home page at http://www.ourgrid.org/ " else case "$1" in start) echo "Usage: mygrid start" ;; stop) echo "Usage: mygrid stop [-f]" ;; config) echo "Usage: mygrid config" ;; restart) echo "Usage: mygrid restart" ;; status) echo "Usage: mygrid status [-l=label s=RUNNING|FINISHED|CANCELED|FAILED|READY -i=jobid]" ;; addjob) echo "Usage: mygrid addjob " ;; canceljob) echo "Usage: mygrid canceljob " ;; waitforjob) echo "Usage: mygrid waitforjob " ;; setgrid) echo "Usage: mygrid setgrid " ;; clean)echo "Usage: mygrid clean " ;; version) echo "OurGrid $OGVERSION - MyGrid" echo echo "For MyGrid updates and additional information, see the " echo "OurGrid Project home page at http://www.ourgrid.org/ " ;; *) echo "not implemented" esac fi exit $ERR_STD_MISUSE } usage_commands(){ UNK_CMD=$1 echo "mygrid: Unknown command '$UNK_CMD'" echo print_commands exit $ERR_STD_MISUSE } nargs=$# default_command="help" if [ $nargs -eq 0 ]; then COMMAND_NAME=$default_command fi if [ "$1" = "help" ] || [ "$1" = "-h" ]; then shift usage $1 fi if [ "$1" = "version" ] || [ "$1" = "-v" ]; then shift usage version fi if [ "$1" = "start" ] || [ "$1" = "config" ] || [ "$1" = "restart" ] || [ "$1" = "gui" ] || [ "$1" = "gettimes" ]; then if [ $nargs -eq 1 ]; then COMMAND_NAME=$1 COMMAND_ARGS=$2 else usage $1 fi fi if [ "$1" = "stop" ]; then if [ $nargs -eq 2 ]; then COMMAND_NAME=$1 COMMAND_ARGS=$2 else if [ $nargs -eq 1 ]; then COMMAND_NAME=$1 COMMAND_ARGS="" else usage $1 fi fi fi if [ "$1" = "status" ]; then if [ $nargs -gt 4 ]; then usage $1 else COMMAND_NAME=$1 COMMAND_ARGS=$2 COMMAND_ARGS2=$3 COMMAND_ARGS3=$4 fi fi if [ "$1" = "addjob" ] || [ "$1" = "canceljob" ] || [ "$1" = "waitforjob" ] || [ "$1" = "setgrid" ]; then if [ $nargs -eq 2 ]; then COMMAND_NAME=$1 COMMAND_ARGS=$2 else usage $1 fi fi if [ "$1" = "clean" ]; then if [ $nargs -eq 2 ]; then COMMAND_NAME=$1 COMMAND_ARGS=$2 else if [ $nargs -eq 1 ]; then COMMAND_NAME=$1 COMMAND_ARGS="" else usage $1 fi fi fi if [ -z "$COMMAND_NAME" ]; then usage_commands $1 fi # In this point, the user has invoked MyGrid correctly. So, we # have to launch the appropriated behaviour. commandmanager="org.ourgrid.mygrid.ui.command.CommandManager" startcommand="start" stopcommand="stop" statuscommand="status" setgridcommand="setgrid" addjobcommand="addjob" canceljobcommand="canceljob" waitforjobcommand="waitforjob" checkconfenv="config" cleancommand="clean" gui="gui" # Some Checkings findmgroot && setenvvars && setclasspath && setjavaoptions ret=$? if [ $ret -eq $OK ]; then case "$COMMAND_NAME" in start) start ;; stop) stop ;; config) config ;; restart) echo "Restarting MyGrid..." stop sleep 3 start ;; status) status exit $? ;; addjob) java -classpath "$CLASSPATH" "$JAVAOPTIONS" "$commandmanager" "$addjobcommand" "$COMMAND_ARGS" ;; canceljob) java -classpath "$CLASSPATH" "$JAVAOPTIONS" "$commandmanager" "$canceljobcommand" "$COMMAND_ARGS" ;; waitforjob) java -classpath "$CLASSPATH" "$JAVAOPTIONS" "$commandmanager" "$waitforjobcommand" "$COMMAND_ARGS" ;; setgrid) java -classpath "$CLASSPATH" "$JAVAOPTIONS" "$commandmanager" "$setgridcommand" "$COMMAND_ARGS" ;; gui) echo "Starting MyGrid GUI" java -classpath "$CLASSPATH" "$JAVAOPTIONS" "$commandmanager" -reg "$gui" "$COMMAND_ARGS" & ;; clean) if [ $nargs -eq 1 ]; then java -classpath "$CLASSPATH" "$JAVAOPTIONS" "$commandmanager" "$cleancommand" else java -classpath "$CLASSPATH" "$JAVAOPTIONS" "$commandmanager" "$cleancommand" "$COMMAND_ARGS" fi ;; *) usage exit $ret ;; esac else errcode=$ret printerrdescr $errcode exit $errcode fi