用Shell写的JBoss自动部署脚本

由于开发要求开发环境需要在每天晚上自动部署,因此花了一天时间写了个基于jboss的自动部署脚本。

#!/bin/bash
#deploy jboos script
#
Usage()
{
  echo "Usage: `basename $0` -D earDir -J JBOSS_HOME -L LOGDIR"
  if [ "A$1" != "A" ]; then
    echo $1
  fi
  exit 1
}
func_checkInstall() {
  earName=$1
  checkEar=`$deployCMD --connect controller=$ipAddress --command=deploy`
  echo $checkEar | grep $earName 2>&1 >> /dev/null
  return $?
}
func_checkIsStart() {
  isStart=""
  $deployCMD --connect controller=$ipAddress --command=ls 2>&1 >> /dev/null
  isStart=$?
}
func_start() {
  echo "`date +"%Y-%m-%d %H:%M:%S"` startting jboss service... " | tee -a $logfile
  $startJbossCMD -c=standalone-full.xml 2>&1 >> $startlogfile &
  sleep 2m
  func_checkIsStart
  if [ $isStart -ne 0 ]
  then
    echo "`date +"%Y-%m-%d %H:%M:%S"` failed to start Jboss" | tee -a $logfile
        echo "Error: failed to start Jboss" | tee -a $logfile
    exit 1
  else
    echo "`date +"%Y-%m-%d %H:%M:%S"` starting Jboss successfully." | tee -a $logfile
        echo "`date +"%Y-%m-%d %H:%M:%S"` Jboss started." | tee -a $logfile
  fi
}
func_ear() {
  earAll=`ls $earHome/*.ear | awk -F / {'print $NF'}`
  if [ "X$earAll" == "X" ]; then
    echo "no ear files in $earALL" | tee -a $logfile
    exit 1
  fi
  for i in FSC.ear CDA.ear DMS.ear RMS.ear CLS.ear POI.ear WAS.ear
  do
    earName=`echo $earAll | grep $i`
    if [ $? -eq 0 ]
    then
      func_deploy $i
    fi
  done
}
func_deploy() {
  earName=$1
  echo "`date +"%Y-%m-%d %H:%M:%S"` Uninstalling application: $earName" | tee -a $logfile
  $deployCMD --connect controller=$ipAddress --commands="undeploy ${earName}"
  func_checkInstall $earName
  if [ $? -ne 0 ]; then
    echo "`date +"%Y-%m-%d %H:%M:%S"` Application $earName uninstalled successfully." | tee -a $logfile
    echo "`date +"%Y-%m-%d %H:%M:%S"` Application uninstalled: $earName" | tee -a $logfile
  else
    echo "`date +"%Y-%m-%d %H:%M:%S"` Application $earName failed to uninstall." | tee -a $logfile
    echo "`date +"%Y-%m-%d %H:%M:%S"` faild to uninstall: $earName" | tee -a $logfile
  fi
  echo "`date +"%Y-%m-%d %H:%M:%S"` Installing ear: $earHome/$earName" | tee -a $logfile
  $deployCMD --connect controller=$ipAddress --commands="deploy ${earHome}/${earName}"
  func_checkInstall $earName
  if [ $? -eq 0 ]; then
    earPrefix=`echo $earName | awk -F. {'print $1'}`
    echo "`date +"%Y-%m-%d %H:%M:%S"` Application $earName installed successfully." | tee -a $logfile
    echo "`date +"%Y-%m-%d %H:%M:%S"` Ear file installed: $earHome/$earName" | tee -a $logfile
    echo "`date +"%Y-%m-%d %H:%M:%S"` $earPrefix installation completed." | tee -a $logfile
  else
    echo "`date +"%Y-%m-%d %H:%M:%S"` Application $earName failed to install." | tee -a $logfile
    echo "`date +"%Y-%m-%d %H:%M:%S"` Ear file installation failed: $earHome/$earName" | tee -a $logfile
    echo "`date +"%Y-%m-%d %H:%M:%S"` $earPrefix installation failed." | tee -a $logfile
  fi
}
scriptname=`basename "$0"`
scriptdir=`pwd "$0"`
earHome=""
JbossHome=""
ipAddress="127.0.0.1"
while getopts D:d:J:j:L:l:H:h: OPTIONS
do
  case $OPTIONS in
    D)  earHome=$OPTARG;;
    d)  earHome=$OPTARG;;
    J)  JbossHome=$OPTARG;;
    j)  JbossHome=$OPTARG;;
    L)  logdir=$OPTARG;;
    l)  logdir=$OPTARG;;
    H)  Usage;;
    h)  Usage;;
    ?)  Usage;;
  esac
done
if [ "A$earHome" == "A" ]; then
  Usage "Error: earDir is empty."
fi
if [ "A$JbossHome" == "A" ]; then
  Usage "Error: JBOSS_HOME is empty."
fi
if [ -d $JbossHome ]; then
  startJbossCMD=$JbossHome/bin/standalone.sh
  deployCMD=$JbossHome/bin/jboss-cli.sh
else
  Usage "-bash: $JbossHome: No sush directory "
fi
if [ "X$logdir" == "X" ]
then
  logdir=/tmp
else
  if [ -d $logdir ]
  then
    logdir=$logdir
  else
    mkdir $logdir
    logdir=$logdir
  fi
fi
logfile=$logdir/${scriptname}_`date +%Y-%m-%d_%H_%M_%S`.log
startlogfile=$logdir/startlog_`date +%Y-%m-%d_%H_%M_%S`.log
echo "=================================" | tee -a $logfile
echo ""
echo "setup jboss environment" | tee -a $logfile
echo "IP address: $ipAddress" | tee -a $logfile
echo "JBOSS_HOME: $JbossHome" | tee -a $logfile
echo "earDir: $earHome" | tee -a $logfile
echo "logfile: $logfile"
echo ""
echo "=================================" | tee -a $logfile
if [ -d $earHome ]; then
  earV=`ls $earHome`
else
  Usage "-bash: $earHome: No sush directory."
fi
func_checkIsStart
if [ $isStart -eq 0 ]
then
  func_ear
else
  echo "`date +"%Y-%m-%d %H:%M:%S"` Can not connect to jboss" | tee -a $logfile
  exit
fi
echo ""
echo "`date +"%Y-%m-%d %H:%M:%S"` Application JBoss Installation finished " | tee -a $logfile
echo "`date +"%Y-%m-%d %H:%M:%S"` Restart the jboss service now" | tee -a $logfile
echo "`date +"%Y-%m-%d %H:%M:%S"` shutting down jboss service..."
$deployCMD --connect controller=$ipAddress --command=:shutdown
sleep 1m
ps -ef | grep standalone.sh | grep -v "grep standalone.sh"
if [ $? -eq 1 ]
then
  echo "`date +"%Y-%m-%d %H:%M:%S"` server stopped." | tee -a $logfile
  TempDir=$JbossHome/standalone/tmp
  if [ -d $TempDir ]; then
    echo "removeing old files..." | tee -a $logfile
    rm -rf $TempDir/*
  else
    echo "Error: $TempDir no such directory" | tee -a $logfile
  fi
  func_start
else
  echo "`date +"%Y-%m-%d %H:%M:%S"` faild to stop the server" | tee -a $logfile
  echo "check whether there has any unusual on jboss node service or not" | tee -a $logfile
  exit 1
fi
echo "" | tee -a $logfile
echo "Check log file: $logfile" | tee -a $logfile
echo "End script at `date +"%Y-%m-%d %H:%M:%S"`" | tee -a $logfile

相关推荐