Linux集成git+maven拉包部署脚本

1、拉包

#!/bin/bash
# Desc   : Git control
# Author : shiyu.zha
# Modify : 2019.07.15 by shiyu.zha

APP_NAME=nsdapi
GIT_DIR=/home/finance/App/$APP_NAME.msxf.lotest/git/$APP_NAME
TARGET_DIR=/home/finance/App/$APP_NAME.msxf.lotest/git/$APP_NAME/$APP_NAME-server/$APP_NAME-server-prov/target/
RELEASE_DIR=/home/finance/App/$APP_NAME.msxf.lotest/release

cd $GIT_DIR
echo "拉取最新代码....."
git pull

echo "开始打包....."
mvn clean package -Dmaven.test.skip=true
echo "打包完成....."

if [ ! -d $RELEASE_DIR ]; then
  echo "目录 $RELEASE_DIR 不存在"
else
  echo "备份..."
  rm -f $RELEASE_DIR/$APP_NAME.jar_
  mv $RELEASE_DIR/$APP_NAME.jar $RELEASE_DIR/$APP_NAME.jar_
fi

echo "开始拷贝..."
cp $TARGET_DIR/*.jar $RELEASE_DIR/$APP_NAME.jar
echo "拷贝完成..."

2、部署

#!/bin/bash
# Desc   : Application service control
# Author : shiyu.zha

if [[ "$#" < 2 || "$#" > 3 ]]; then
  echo "This scripts argument invalid !"
  echo "Usage: $0 {start|stop|restart|status|stack|heap} app_name [app_port]"  
  exit 1
fi

## 应用设置
APP_USER=finance
APP_NAME=$2
APP_PORT=$3

JAVA=/opt/jdk1.8.0_112/bin/java
SPRING_PROFILES_ACTIVE=test
APP_JMX="-Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=18100 "
APP_JDWP="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=28100  "

##当前用户
CURRENT_USER=`whoami| cut -d' ' -f1 | sort | uniq`

echo "current user is $CURRENT_USER"

## 设置应用域名
APP_DOMAIN="$APP_NAME".msxf.lotest # 需要根据环境修改

## 应用目录
APP_HOME=/home/finance/App/"$APP_DOMAIN"
[ -d $APP_HOME ] && app_exists=$?
if [[ "$app_exists" != 0 ]]; then
  echo "$APP_HOME not exists."
  exit 1
fi

## 日志目录
APP_LOGS=/home/finance/Logs/"$APP_DOMAIN"

## 服务目录
APP_SERVERS="$APP_HOME"/servers

## 需要指定端口指令判断
if [[ "$1" != "status" ]]; then
  ## 端口参数
  if [[ $APP_PORT == "" ]]; then
    for server_port in `ls -a $APP_SERVERS`
    do
      if [ x"$server_port" != x"." -a x"$server_port" != x".." ]; then
        if [ -d "$APP_SERVERS"/"$server_port" ]; then
          if [[ $APP_PORT == "" ]]; then
            APP_PORT=$server_port
          else
            echo "This scripts argument invalid !"
            echo "Usage: $0 {start|stop|restart} app_name app_port"
            exit 1
          fi
        fi
      fi
    done
  fi

  ## 需要指定端口指令,判断是否有端口 
  [ -d $APP_SERVERS/$APP_PORT ] && port_exists=$?
  if [[ "$port_exists" != 0 ]]; then
    echo "$APP_SERVERS/$APP_PORT not exists."
    exit 1
  fi

  ## 内嵌tomcat工作目录
  TMP_PATH=$APP_SERVERS/$APP_PORT

  ## 设置应用停止URI
  APP_SHUTDOWN_URI=localhost:"$APP_PORT"/shutdown

  ## 端口实例进程ID
  APP_PID="$APP_SERVERS"/"$APP_PORT"/pid
fi

## 程序JAR
APP_JAR="$APP_HOME"/release/"$APP_NAME".jar
[ -f $APP_JAR ] && jar_exists=$?

## 当前时间
TIME_NOW=`date +%Y%m%d%H%M%S`

checkStart() {
  if [[ "$jar_exists" != 0 ]]; then
    echo "Application: $APP_JAR not exists."
    exit 1
  fi
 if [ -f "$APP_PID" ]; then
    if [ -s "$APP_PID" ]; then
      echo "Existing PID:$APP_PID file found during start."
      if [ -r "$APP_PID" ]; then
        PID=`cat "$APP_PID"`
        ps -p $PID >/dev/null 2>&1
        if [ $? -eq 0 ] ; then
          echo "Application $APP_NAME:$APP_PORT to still be running with PID $PID. Start aborted."
          exit 1
        else
          echo "Removing/clearing stale PID:$APP_PID file."
          rm -f "$APP_PID" >/dev/null 2>&1
          if [ $? != 0 ]; then
            if [ -w "$APP_PID" ]; then
              cat /dev/null > "$APP_PID"
            else
              echo "Unable to remove or clear stale PID:$APP_PID file. Start aborted."
              exit 1
            fi
          fi
        fi
      else
        echo "Unable to read PID:$APP_PID file. Start aborted."
        exit 1
      fi
    else
      rm -f "$APP_PID" >/dev/null 2>&1
      if [ $? != 0 ]; then
        if [ ! -w "$APP_PID" ]; then
          echo "Unable to remove or write to empty PID:$APP_PID file. Start aborted."
          exit 1
        fi
      fi
    fi
  fi
}

checkPid() {
  if [ -f "$APP_PID" ]; then
    if [ -s "$APP_PID" ]; then
      kill -0 `cat "$APP_PID"` >/dev/null 2>&1
      if [ $? -gt 0 ]; then
        echo "PID:$APP_PID file found but no matching process was found."
        exit 1
      fi
    else
      echo "PID:$APP_PID file is empty and has been ignored."
    fi
  else
    echo "$APP_PID file does not exist. Is running?"
    exit 1
  fi
}

start() {
  checkStart

  echo "Starting $APP_NAME:$APP_PORT"

  ## 设置全局变量
  CUR_PATH=$(cd "$(dirname "$0")"; pwd)
  if [ -f "$CUR_PATH"/setenv.sh ]; then
    if [ -r "$CUR_PATH"/setenv.sh ]; then
      . "$CUR_PATH"/setenv.sh
    fi
  fi

  ## 设置应用变量
  if [ -f "$APP_HOME"/setenv.sh ]; then
    if [ -r "$APP_HOME"/setenv.sh ]; then
      . "$APP_HOME"/setenv.sh
    fi
  fi
  ## 设置变量
  [ -z "$APP_OPTS" ] && APP_OPTS=
  APP_OPTS="$APP_OPTS -Dspring.profiles.active=$SPRING_PROFILES_ACTIVE -Dtomcat.port=$APP_PORT -Djava.io.tmpdir=$TMP_PATH $PINPOINT $APP_JMX $APP_JDWP"

  ## 服务启动
  if [ $CURRENT_USER == "root" ]; then
    #su $APP_USER -c "nohup $JAVA -jar $APP_OPTS $APP_JAR >/dev/null 2>&1 & echo \$! > $APP_PID"
         su $APP_USER -c "nohup $JAVA -jar $APP_OPTS $APP_JAR & echo \$! > $APP_PID"
  else
    `nohup $JAVA -jar $APP_OPTS $APP_JAR >/dev/null 2>&1 & echo \$! > $APP_PID`
  fi

  echo "Application $APP_NAME:$APP_PORT started."
}

stop() {
  echo "Stopping $APP_NAME:$APP_PORT"

  checkPid

  ## 服务停止 
  http_code=`curl -X POST -I -m 10 -o /dev/null -s -w %{http_code} $APP_SHUTDOWN_URI`

  if [[ $http_code != "200" ]]; then
    echo "The stop command failed. Attempting to signal the process to stop through OS signal."
    kill -15 `cat "$APP_PID"` >/dev/null 2>&1
  fi

  SLEEP=10
  if [ -f "$APP_PID" ]; then
    while [ $SLEEP -ge 0 ];do
      kill -0 `cat "$APP_PID"` >/dev/null 2>&1
      if [ $? -gt 0 ]; then
        rm -f "$APP_PID" >/dev/null 2>&1
        if [ $? != 0 ]; then
          if [ -w "$APP_PID" ]; then
            cat /dev/null > "$APP_PID"
          else
            echo "The PID file could not be removed or cleared."
          fi
        fi
        echo "Application $APP_NAME:$APP_PORT stopped."
        break
      fi
      if [ $SLEEP -gt 0 ]; then
        sleep 1
      fi
      SLEEP=`expr $SLEEP - 1 `
    done
  fi
  KILL_SLEEP=5
  if [  -f "$APP_PID" ]; then
    if [ -s "$APP_PID" ]; then
      PID=`cat "$APP_PID"`
      echo "Killing application $APP_NAME:$APP_PORT with the PID: $PID"
      kill -9 $PID
      while [ $KILL_SLEEP -ge 0 ]; do
        kill -0 `cat "$APP_PID"` >/dev/null 2>&1
        if [ $? -gt 0 ]; then
            rm -f "$APP_PID" >/dev/null 2>&1
            if [ $? != 0 ]; then
                if [ -w "$APP_PID" ]; then
                    cat /dev/null > "$APP_PID"
                else
                    echo "The PID file could not be removed."
                fi
            fi
            # Set this to zero else a warning will be issued about the process still running
            KILL_SLEEP=0
            echo "The application $APP_NAME:$APP_PORT process has been killed."
            break
        fi
        if [ $KILL_SLEEP -gt 0 ]; then
            sleep 1
        fi
        KILL_SLEEP=`expr $KILL_SLEEP - 1 `
      done

      if [ $KILL_SLEEP -gt 0 ]; then
          echo "Application $APP_NAME:$APP_PORT has not been killed completely yet. The process might be waiting on some system call or might be UNINTERRUPTIBLE."
      fi
    fi
  fi
}

stack() {
  checkPid
  PID=`cat "$APP_PID"`
  jstack_file=$APP_LOGS/jstack-$APP_PORT-$PID-$TIME_NOW.dump

  echo "Dump thread stack $APP_NAME:$APP_PORT, pid:$PID, file: $jstack_file"

  if [ $CURRENT_USER == "root" ]; then
    su $APP_USER -c "jstack -F -l $PID > $jstack_file 2>&1"
  else
    jstack -F -l $PID > $jstack_file 2>&1
  fi

}


heap() {
  checkPid
  PID=`cat "$APP_PID"`
  heap_file=heap-$APP_PORT-$PID-$TIME_NOW.dump

  echo "Dump jvm head $APP_NAME:$APP_PORT, pid:$PID, file: $heap_file"

  if [ $CURRENT_USER == "root" ]; then
    su $APP_USER -c "jmap -F -dump:format=b,file=$heap_file $PID 2>&1"
  else
    jmap -F -dump:format=b,file=$heap_file $PID 2>&1
  fi

}

status() {
  servers=0
  for server_port in `ls -a $APP_SERVERS`
  do
    if [ x"$server_port" != x"." -a x"$server_port" != x".." ]; then
      if [ -d "$APP_SERVERS"/"$server_port" ]; then
        servers=$((servers+1))
        server_status="stopped"
        server_pid="$APP_SERVERS"/"$server_port"/pid
        if [ -f "$server_pid" ]; then
          if [ -s "$server_pid" ]; then
            kill -0 `cat "$server_pid"` >/dev/null 2>&1
            if [ $? -eq 0 ]; then
              server_status="running"
            fi
          fi
        fi
        echo "$servers. $server_port:$server_status"
      fi
    fi
  done
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    echo "Sleep 10 seconds for eureka check"
    sleep 10
    start
    ;;
  status)
    status
    ;;
  stack)
    stack
    ;;
  heap)
    heap
    ;;
  *)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值