Parcels打包脚本

2019年12月17日 作者 刘剑

同时打测试和生产包

20-functions.sh

#!/bin/bash
echo '=================================================================='
echo '发布脚本的公共函数'
echo '2019-12-9 by LJ'
echo '=================================================================='



# '=================================================================='
# '为测试test-startup.sh设置远程调试端口'
# '2019-12-13 by lj'
# '=================================================================='
function setRemoteDebugInTestStartup()
{

    echo -e "\n\n===================================================== 为测试startup设置远程调试端口,LJ@2019-12-12\n"

    project=$1

    if [[ $project == 'eship-customer-center' ]];then

            sed -i 's/-Dspring.profiles.active=prod/-Dspring.profiles.active=test -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=7022,suspend=n/g' `grep "java -jar" -rl --include=test-startup.sh */`

    elif [[ $project == 'eship-label-ex' ]];then

            sed -i 's/-Dspring.profiles.active=prod/-Dspring.profiles.active=test -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=7028,suspend=n/g' `grep "java -jar" -rl --include=test-startup.sh */`

    elif [[ $project == 'eship-exportData' ]];then

            sed -i 's/-Dspring.profiles.active=prod/-Dspring.profiles.active=test -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=7032,suspend=n/g' `grep "java -jar" -rl --include=test-startup.sh */`
    fi

}



# '=================================================================='
# '获取本机局域网IP'
# '2019-12-9 by Kee'
# '=================================================================='
function getIP()
{

    local_ip=`ip addr |grep inet |grep 255 |grep -v 127.0.0.1|grep -v inet6 | awk '{print $2}' | awk -F "/" '{print $1}'`

    hostIP=""
    for element in $local_ip
    do
        if [[ $element =~ "172.17" ]]
        then
          dockerIP=$element
        else
          hostIP=$element
        fi
    done

    echo $hostIP
}

21-package.sh

#!/bin/bash
source ./20-functions.sh

echo '=================================================================='
echo 'step1: 打包eship-common, eship-export-data, eship-label-ex, customer'
echo 'setp2: 压缩全部jar 到 /parcels/deploy/date/, 不包含三方lib。 默认将压缩包保存到windows D盘 parcels目录 或 linux的 /d/parcels/目录'
echo '2019-12-9 by LJ'
echo '=================================================================='




echo -e "\n\n============================================================更新代码, LJ@2019-12-13\n"
read -p "是否pull, 更新代码?[y/n]?" isPull

if [[ $isPull == 'y' ]];then

    git reset --hard
    git pull

fi




echo -e "\n\n============================================================编译, LJ@2019-12-9\n"
read -p "是否再编译[y/n]?" isCompile

if [ $isCompile == 'y' ]; then

    cd ./eship-common
    echo -e "\n\n package eship-common"
    mvn install -Dmaven.test.skip=true

    cd ../
    echo -e "\n\n 打包全部工程"
    mvn clean install -Dmaven.test.skip=true

    result=$?
    if [ $result != 0 ];then
        echo "执行mvn clean package 出错, 退出执行.........."
        exit 1
    fi

fi




echo -e "\n\n============================================================压缩拷贝打好的包, LJ@2019-12-9\n"
read -p "是否包含三方包[y/n]?" isFat
read -p "是否包含conf[y/n]?" isConf

current_date=`date +%Y%m%d%H%M%S`
targetPath=/d/parcels/

mkdir -p $targetPath # 默认将压缩包保存到D盘 parcels 或 linux的 /d/parcels/目录


projects=("eship-exportData" "eship-label-ex" "eship-customer-center")
for project in ${projects[@]};
do

    cd $project/target
    rm -fr $project/*
    mkdir $project $project/lib $project/conf

    cp -r *.jar $project/

    if [ $isFat == 'y' ]; then #包含三方包

        cp -r lib/* $project/lib/
    else
        cp -r lib/eship* $project/lib/
    fi


    if [ $isConf == 'y' ]; then #包含conf

        cp -r conf/* $project/conf/
    fi

    cat ../../22-startup.sh > $project/startup.sh
    cat ../../22-startup.sh > $project/test-startup.sh
    cat ../../20-functions.sh > $project/20-functions.sh
    setRemoteDebugInTestStartup $project #为测试startup设置调试端口

    zipFile=$project.tar.gz

    echo -e "\n\n 压缩和复制 ......"
    tar -cvf $zipFile $project
    cp -v $zipFile $targetPath

    cd ../../

done





echo -e "\n\n============================================================重新启动测试环境, LJ@2019-12-9\n"
read -p "是否重新启动测试环境[y/n]?" isTestStartup

if [[ $isTestStartup == 'y' ]];then
    cp 23-unpress-startup-test-evn.sh $targetPath #拷贝测试环境启动命令到出包目录
    cp 20-functions.sh $targetPath #拷贝测试环境启动命令到出包目录
    cd $targetPath
    sh 23-unpress-startup-test-evn.sh
fi

22-startup.sh

#!/usr/bin/env bash
source ./20-functions.sh

echo '=================================================================='
echo '获取最新的jar来运行, 对日志 和 xxl设置当前机器的局域网IP'
echo '2019-12-9 by Kee'
echo '=================================================================='



targetIp=$(getIP)


echo -e "\n\n==========================================================复制logback"

logbackFile="logback-$targetIp.xml"
cp conf/logback-prod.xml conf/$logbackFile
sed -i 's/.log</-'$targetIp'.log</g' `grep "<file>" -rl --include="$logbackFile" */`
echo "logback文件名称:$logbackFile"





echo -e "\n\n==========================================================杀死进程"
dir=$PWD
runingFile=${dir##*/}
preStopProcess=`ps -ef |grep "$runingFile.*.jar" |grep -v grep |cut -c 9-15`

if [ -z "$preStopProcess" ];then
    echo "$runingFile 没有运行"
else
    kill -9 $preStopProcess
    echo -e "\e[31m已杀死进程:$runingFile $preStopProcess \e[0m"
    sleep 5s
fi




echo -e "\n\n==========================================================启动最新的jar"
targetFile=`ls -lt | grep -E '\.jar' | head -n 1 | awk '{print $9}'`
echo -e "最新的jar:$targetFile"

source /etc/profile # 先source一下,不然会找不到环境变量java

if [[ "$targetFile" == "eship-label-ex" ]];then
    db=$1
    if [[ "$db" == "h2" ]];then
        echo "使用H2内嵌数据库启动,并且不启动xxl-job"
        nohup java -jar -Dspring.profiles.active=prod -Dlogging.config=classpath:$logbackFile  -Dxxl.job.executor.ip=$targetIp -Dspring.h2.console.enabled=true -Dspring.datasource.url=jdbc:h2:mem:cib -Dspring.datasource.username=root -Dspring.datasource.password=root -Dspring.datasource.driver-class-name=org.h2.Driver -Dspring.jpa.database-platform=org.hibernate.dialect.H2Dialect -Dxxl.job.disabled=true  $targetFile  >/dev/null 2>&1 &
    else
        nohup java -jar -Dspring.profiles.active=prod -Dlogging.config=classpath:$logbackFile  -Dxxl.job.executor.ip=$targetIp $targetFile  >/dev/null 2>&1 &
    fi

else

        nohup java -jar -Dspring.profiles.active=prod -Dlogging.config=classpath:$logbackFile  -Dxxl.job.executor.ip=$targetIp $targetFile  >/dev/null 2>&1 &
fi



sleep 2s
newProcess=`ps -ef |grep $targetFile |grep -v grep |cut -c 9-15`
echo -e "\e[31m已启动进程:$newProcess \e[0m \n\n\n\n\n\n"

 

23-unpress-startup-test-evn.sh

#!/usr/bin/env bash
source ./20-functions.sh


echo '=================================================================='
echo '全部重新启动测试环境 by lj'
echo '=================================================================='


tar -xvf eship-customer-center.tar.gz
tar -xvf eship-label-ex.tar.gz
tar -xvf eship-exportData.tar.gz



vhost=$(getIP)
echo "Modifing rabbit vhost 与 xxl 为: ${vhost}"
sed -i 's#vhost: /.*$#vhost: /'${vhost}'#'   `grep "vhost: /" -rl --include="application-test.yaml" */`
sed -i 's#ip:.*xxl-client-ip$#ip: '${vhost}' \#xxl-client-ip#'   `grep "xxl-client-ip" -rl --include="application-test.yaml" */`


cd eship-customer-center/
sh test-startup.sh

cd ../eship-label-ex/
sh test-startup.sh

cd ../eship-exportData/
sh test-startup.sh