用shell寫的jboss自動部署指令碼

來源:互聯網
上載者:User

由於開發要求開發環境需要在每天晚上自動部署,因此花了一天時間寫了個基於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 AA.ear BB.ear CC.ear DD.ear EE.ear FF.ear GG.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: OPTIONSdo  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;;  esacdoneif [ "A$earHome" == "A" ]; then  Usage "Error: earDir is empty."fiif [ "A$JbossHome" == "A" ]; then  Usage "Error: JBOSS_HOME is empty."fiif [ -d $JbossHome ]; then  startJbossCMD=$JbossHome/bin/standalone.sh  deployCMD=$JbossHome/bin/jboss-cli.shelse  Usage "-bash: $JbossHome: No sush directory "fiif [ "X$logdir" == "X" ]then  logdir=/tmpelse  if [ -d $logdir ]  then    logdir=$logdir  else    mkdir $logdir    logdir=$logdir  fifilogfile=$logdir/${scriptname}_`date +%Y-%m-%d_%H_%M_%S`.logstartlogfile=$logdir/startlog_`date +%Y-%m-%d_%H_%M_%S`.logecho "=================================" | tee -a $logfileecho ""echo "setup jboss environment" | tee -a $logfileecho "IP address: $ipAddress" | tee -a $logfileecho "JBOSS_HOME: $JbossHome" | tee -a $logfileecho "earDir: $earHome" | tee -a $logfileecho "logfile: $logfile"echo ""echo "=================================" | tee -a $logfileif [ -d $earHome ]; then  earV=`ls $earHome`else  Usage "-bash: $earHome: No sush directory."fifunc_checkIsStartif [ $isStart -eq 0 ]then  func_earelse  echo "`date +"%Y-%m-%d %H:%M:%S"` Can not connect to jboss" | tee -a $logfile  exitfiecho ""echo "`date +"%Y-%m-%d %H:%M:%S"` Application JBoss Installation finished " | tee -a $logfileecho "`date +"%Y-%m-%d %H:%M:%S"` Restart the jboss service now" | tee -a $logfileecho "`date +"%Y-%m-%d %H:%M:%S"` shutting down jboss service..."$deployCMD --connect controller=$ipAddress --command=:shutdownsleep 1mps -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_startelse  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 1fiecho "" | tee -a $logfileecho "Check log file: $logfile" | tee -a $logfileecho "End script at `date +"%Y-%m-%d %H:%M:%S"`" | tee -a $logfile


這個指令碼值得參考的地方就是getopts的用法,然後就是運行部署前的一些邏輯判斷。

本文出自 “一步一步” 部落格,謝絕轉載!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.