Jboss automatic deployment script written in shell

Source: Internet
Author: User

Because development requires that the development environment be automatically deployed every night, it takes one day to write a jboss-based Automatic deployment script.

#!/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


This script is useful for reference in getopts usage, and then some logic judgment before running the deployment.

This article is from the "Step by Step" blog and will not be reposted!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.