thbkrkr
12/14/2013 - 4:58 PM

start|stop|restart|ps|mail play|redis|mongo|es|all

start|stop|restart|ps|mail play|redis|mongo|es|all

#!/bin/bash

set -e

source ~/.profile
export MONGO_DATA="/data/mongodb"

usage()
{
cat <<EOF

  usage: p PROCESS ACTION [OPTION]

  Control process

  PARAMETERS:
  ===========
     ACTION   start|stop|restart|ps|mail
    PROCESS   play|redis|mongo|es|all

  OPTIONS:
  ========
     -q   Desactive l'envoi de mail
     -h   Affiche ce message

EOF
}

[ -z $LOG_PATH ] && echo "Error: did you defined LOG_PATH in .profile?" && exit 1
[ ! -d $LOG_PATH ] && echo "Error: $LOG_PATH doesn't exist." && exit 1

quit() {
  usage
  exit 1
}

USER=`whoami`
GROUP=$USER
JAVA_OPTS="-Xms64m -Xmx128m -server"

MAILFROM=xxxxx@gmail.com
MAILTO=
MAILSERVER=smtp.gmail.com:587
MAILPWD=xxxxxxxxxxxx

RED="\\033[1;31m"
GREEN="\\033[1;32m"
YELLOW="\\033[1;33m"
BLUE="\\033[1;34m"
MAGENTA="\\033[1;35m"
CYAN="\\033[1;36m"
GRAS="\033[1m"
END="\\033[1;00m"

_ok() {
  echo -e "$GREEN [OK]$END - $1"
}

_ko() {
  echo -e "$RED [KO]$END - $1"
}

_wa() {
  echo -e "$YELLOW [WA]$END - $1"
}

_log() {
  echo -e "$CYAN [`date +"%Y-%m-%d %H:%M"`] --- $1 --- $END"
}

_top() {
  _ok "top -bc -n 1 -p $1"
  top -bc -n 1 -p $1
}

# start

_start_started() {
  _wa "$NAME already started"
  _top $1
}

_start_stopped() {
  _ok "$NAME not started"
  _ok "Starting $NAME ..."
  mkdir -p logs
  
  start_$NAME

  i=50
  while [ "$i" -gt 0 ] 2>/dev/null; do
    for a in \\ \| \/ -;
    do
      echo -n " $i "; sleep 0.1; echo -ne \\r
      i=$(expr $i - 1)
    done
  done
  echo " .. BIM !"
  [ "x$NOMAIL" == "x" ] && _mail
}

_start() {
  _log "start"
  _control _start_started _start_stopped

  if [ $(_ps | grep -c 'not started') == "1" ]; then
    _ko "$NAME not started"
  else
    _ok "$NAME started"
  fi
}

# stop

_stop_started() {
  _ok "kill -SIGTERM $1"
  kill -SIGTERM $1
  sleep 1
  while kill -n 0 $1 2>/dev/null; do
    for a in \\ \| \/ -;
    do
      echo -n $a; sleep 0.1; echo -ne \\r
    done
  done
  if [ $(_ps | grep -c 'not started') == "1" ]; then
    _ok "$NAME not started"
  else
    _ko "$NAME started"
  fi
}

_stop_stopped() {
  _wa "$NAME already stopped"
}

_stop() {
  _log "stop"
  _control _stop_started _stop_stopped
}

# ps

_ps_started() {
  _ok "$NAME started"
  _top $1
}

_ps_stopped() {
  _ok "$NAME not started"
}

_ps() {
  _log "ps"
  _control _ps_started _ps_stopped
}

_restart() {
  _stop
  _start
}

_control() {

  local _pidRunning=$1
  local _noPid=$2

  # file .pid
  if [ -f $NAME.pid ]; then
    PID=`cat $NAME.pid`

    # but pid is blank ?!
    if [ "x$PID" == "x" ]; then

      rm $NAME.pid
      _ko "$NAME not started"
      _wa "Error: $NAME.pid exists but was empty, now deleted"
    else

      # check pid is in ps
      if [ $(ps -p $PID > /dev/null; echo $?) == 0 ]; then
        $_pidRunning $PID
      else
        rm $NAME.pid
        _ko "$NAME not started"
        _ko "$NAME.pid exists but $NAME isn't started, murder in air ?"
        _wa "$NAME.pid exists but was empty, now deleted"
      fi

    fi
  
  # no file .pid
  else

    PID=$(get_pid_$NAME)

    # but pid is blank ?!
    if [ "x$PID" == "x" ]; then

      _ko "$NAME not started"
      $_noPid $PID
    
    else

      # check pid is in ps
      if [ $(ps -p $PID > /dev/null; echo $?) == 0 ]; then
        $_pidRunning $PID
      else
        _ko "Error: something goes wrong !"
      fi

    fi
  fi

}

_sendmail() {
  local title="$1"
  local message="$2"
  _ok "sendemail -f $MAILFROM -t $MAILTO -u \"$title\" ..."
  sendemail -f $MAILFROM -t $MAILTO -u "$title" -m "$message" -s $MAILSERVER -o tls=yes -xu $MAILFROM -xp $MAILPWD
}

_mailstart() {
  local gitlog=$(git log --pretty=format:'%h - %an, %ar : %s' -5)
  local ps=$(top -bc -n 1 -p $PID)
  local message="$gitlog \n\n $ps"
  local title="[OK] $NAME started `date +"%H:%M:%S %d-%m-%y"` $JAVA_OPTS"
  _sendmail "$title" "$message"
}

_mailstop() {
  local gitlog=$(git log --pretty=format:'%h - %an, %ar : %s' -5)
  local jps=$(jps)
  local message="$gitlog \n\n $jps"
  local title="[KO] $NAME not started `date +"%H:%M:%S %d-%m-%y"`"
  _sendmail "$title" "$message"
}

_mail() {
  [ $(which sendemail > /dev/null; echo $?) != "0" ] &&
    _ko "need: sudo apt-get install libio-socket-ssl-perl libnet-ssleay-perl sendemail" && exit 1

  _control _mailstart _mailstop

  title="$title "
}

# need to be in a git branch staged
start_play() {
  nohup java -cp "`dirname $0`/target/staged/*" play.core.server.NettyServer `dirname $0`/ $JAVA_OPTS > $LOG_PATH/playapp.log 2>&1 &
}

start_mongo() {
  mongod --fork --logpath $LOG_PATH/mongodb.log --dbpath $MONGO_DATA
}

stop_mongo() {
  mongod --shutdown --dbpath $MONGO_DATA
}

start_redis() {
  REDISPORT=6379
  cd $REDIS_DATA
  #cp ~/biiimenv/redis_${REDISPORT}.conf .
  nohup /usr/local/bin/redis-server > $LOG_PATH/redis.log 2>&1 & #redis_${REDISPORT}.conf
}

start_es() {
  nohup $ES_HOME/bin/elasticsearch > $LOG_PATH/es.log 2>&1 &
}

get_pid_play() {
  ps -ef | grep play | grep -v grep | cut -c10-15
}

get_pid_redis() {
  ps -ef | grep redis-server | grep -v grep | cut -c10-15
}

get_pid_mongo() {
  ps -ef | grep mongod | grep -v grep | cut -c10-15
}

get_pid_es() {
  ps -ef | grep elasticsearch | grep -v grep | cut -c10-15
}

# MAIN

ACTION=$1
NAME=$2

NOMAIL=$3

[ -z $ACTION ] && usage && exit 1

case "$ACTION" in
    start|stop|restart|ps)
      
      case "$NAME" in
        redis|mongo|es|play)
          _$ACTION
        ;;
        all)
          _$ACTION redis
          _$ACTION mongo
          _$ACTION es
        ;;
        *)
        usage && exit 1
        ;;
      esac

    ;;

    *)
      usage && exit 1
    ;;
esac