服務器部署shell腳本

咱們的WEB是用的DUBBO,controller層rpc調用的service的方法,controller的部署方法跟Service不太同樣,由於controller屬於暴露的接口,須要關聯nginx。Service層作了業務拆分因此有DUBBO啓動,也有tomcat啓動,可是基本套路都是同樣的:先讓tomcat或者dubbo編譯成class文件,在用rsync 同步到各個服務器就行了,最後ping一下index.jsp 反沒返回200,完活。linux

結構目錄:nginx

先上個controller層的:git

#!/bin/bash


### init config
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
source /etc/profile


#!/bin/bash

#各類路徑按照本身路徑改一下就行
PRE_DIR=`dirname $0`
PROJECT_DIR=/data/web/hf_app/build.club
PROJECT_NAME=tiger
PROJECT_CLUB=tiger/tiger-club/tiger-club-provider
DOC_ROOT=$PROJECT_DIR/ROOT
WAR_FILE=$PROJECT_DIR/$PROJECT_CLUB/target/tiger-club.war
TOMCAT=/usr/local/app/club-tomcat
DATE=`date "+%Y%m%d%H%M%S"`
STR_DATE=`date "+%s"`
backup="backup"

#backup
tar -zcvf ROOT."$DATE".tar.gz $PROJECT_DIR/ROOT
mv ROOT.$DATE.tar.gz $backup/
#deploy
echo
echo Deploy:
echo

cd $PROJECT_DIR/$PROJECT_NAME
git clean -d -fx
git checkout master
git pull
if [ ! -z $1 ]
then
    git branch -D test
    git fetch ssh://xxxx(git地址)   /tiger refs/changes/$1 && git checkout FETCH_HEAD
    git checkout -b test
else
    git  checkout online_161216
    git pull
    git branch -D test
    git checkout -b test
fi
mvn clean package -DskipTests -U 
rm -rf $PROJECT_DIR/ROOT
mkdir -p  $PROJECT_DIR/ROOT
unzip -q $WAR_FILE -d $PROJECT_DIR/ROOT



cp $PROJECT_DIR/config-toolkit.properties $DOC_ROOT/WEB-INF/classes/properties
#這裏主要是防止配置文件更改帶來的麻煩,先在目錄下作一個線上環境的配置文件,這樣不管提交的是什麼都會覆蓋掉,保證一個安全


#find $PROJECT_DIR/ROOT -name "*.vm" |xargs sed -i 's/szone/'$STR_DATE'/g'

#restart tomcat
#echo
echo Restart tomcat:
ps aux |grep -v grep |grep $TOMCAT|awk '{print $2}'|xargs kill -9
rm -rf $TOMCAT/work/Catalina
# sleep 10s
echo "Waiting shutdown Tomcat, Sleep 10s..."
sleep 10s
echo "Start to startup Tomcat..."
$TOMCAT/bin/startup.sh

這樣controller 就變成了class文件,再去同步到各個機器上:web

#!/bin/bash

PROJECT="wolfapi"
DST_SERVER="0.0.0.1,0.0.0.2"(同步IP)
SRC_SERVER="0.0.0.1"(本機IP)
NGX_UPSTREAM_CONF="/usr/local/app/nginx/conf/vhosts/upstream.conf"
NGX_UPSTREAM="api"
DST_PORT="8071"
DEPLOY_PATH="/data/web/hf_app/build.wolfapi/ROOT/"
TARGET_CONFIG_PATH="/data/web/hf_app/build.wolfapi/ROOT/WEB-INF/classes/"
SOURCE_CONFIG_PATH="hefan/build.wolfapi/sync/dubbo.properties"
RSYNC_PATH="hefan/build.wolfapi/ROOT/"
TOMCAT="/usr/local/app/tomcat/"
RETURN_CODE="200"
PORT="1922"

nginx(){
line_num=`ssh -t -p $PORT  $2 "cat $NGX_UPSTREAM_CONF  -n |sed  -n '/$NGX_UPSTREAM/,/}/ p'|grep 127.0.0.1"|awk '{print $1}'`
case $1 in
        up)
		server="server 127.0.0.1:$DST_PORT;"
		ssh -t -p $PORT $2 "sed '$line_num s/.*/$server/' $NGX_UPSTREAM_CONF -i"
        ;;
        down)
		server="server 127.0.0.1:$DST_PORT down;"
		ssh -t -p $PORT $2 "sed '$line_num s/.*/$server/' $NGX_UPSTREAM_CONF -i"
        ;;
esac
ssh -t -p $PORT $2 "/usr/local/app/nginx/sbin/nginx -s reload "
}

### sync file to server
### $1 for dst server ip addr
sync(){
	ssh -t -p $PORT $1 "rsync -avP --delete $SRC_SERVER::$RSYNC_PATH $DEPLOY_PATH; \
		rsync -avP --delete $SRC_SERVER::$SOURCE_CONFIG_PATH $TARGET_CONFIG_PATH"
}

### restart tomcat
### $1 for dst server ip addr
restart(){
	ssh -t -p $PORT $1 "ps aux |grep -v grep |grep $TOMCAT|awk '{print \$2}'|xargs kill -9 "
	ssh -p $PORT $1 "source /etc/profile ; $TOMCAT/bin/startup.sh"
}

### success check
check(){
        STATUS=1
        for i in `seq 1 60`;do
                code=`curl -I http://$1:$DST_PORT/index.jsp --connect-timeout 2|grep HTTP|awk '{print $2}' 2>/dev/null`
                if [ $code -eq $RETURN_CODE ];then
                        STATUS=0
                        break
                else
			echo "$1 tomcat has not started , please wait............"
                        sleep 1
                fi
        done
}

check_compile_status(){
	if [ "`ls -A $DEPLOY_PATH`" = "" ]; then
		echo "$DEPLOY_PATH is indeed empty"
		exit 1
	else
		echo "$DEPLOY_PATH is not empty, go on..."
	fi
}

### script start


for IP in $DST_SERVER;do
	check_compile_status
        nginx down $IP
        sync $IP
        restart $IP
        check $IP
        if [ $STATUS -eq 0 ];then
                nginx up $IP
        else
                echo "fuck u"
                #echo $1 $IP sync error |mail -s "Sync error" ninglijun@guolele.com  -- -f ninglijun@guolele.com -F ""
        fi
done

同步的時候多了一個關於nginx 的配置,service層一個原理,只是不用陪Nginx,若是使用dubbo啓動的話就是maven命令去編譯成class文件,只不過不能去驗證起沒啓動.shell

一樣的結構:api

service層:tomcat

#!/bin/bash


### init config
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
source /etc/profile


#!/bin/bash
PRE_DIR=`dirname $0`
PROJECT_DIR=/data/web/hf_app/build.club
PROJECT_NAME=tiger
PROJECT_CLUB=tiger/tiger-club/tiger-club-provider
DOC_ROOT=$PROJECT_DIR/ROOT
WAR_FILE=$PROJECT_DIR/$PROJECT_CLUB/target/tiger-club.war
#TOMCAT=/usr/local/app/club-tomcat
DATE=`date "+%Y%m%d%H%M%S"`
STR_DATE=`date "+%s"`
backup="backup"

#backup
tar -zcvf ROOT."$DATE".tar.gz $PROJECT_DIR/ROOT
mv ROOT.$DATE.tar.gz $backup/
#deploy
echo
echo Deploy:
echo

cd $PROJECT_DIR/$PROJECT_NAME
git clean -d -fx
git checkout master
git pull
if [ ! -z $1 ]
then
    git branch -D test
    git fetch ssh://xxx.xxx refs/changes/$1 && git checkout FETCH_HEAD
    git checkout -b test
else
    git  checkout qa
    git pull
    git branch -D test
    git checkout -b test
fi
mvn clean package -DskipTests -U 
rm -rf $PROJECT_DIR/ROOT
mkdir -p  $PROJECT_DIR/ROOT
unzip -q $WAR_FILE -d $PROJECT_DIR/ROOT
cp $PROJECT_DIR/config-toolkit.properties $DOC_ROOT/WEB-INF/classes/properties
#find $PROJECT_DIR/ROOT -name "*.vm" |xargs sed -i 's/szone/'$STR_DATE'/g'

#restart tomcat
#echo
echo Restart tomcat:
ps aux |grep -v grep |grep $TOMCAT|awk '{print $2}'|xargs kill -9
#rm -rf $TOMCAT/work/Catalina
# sleep 10s
echo "Waiting shutdown Tomcat, Sleep 10s..."
sleep 10s
echo "Start to startup Tomcat..."
$TOMCAT/bin/startup.sh

service同步:安全

#!/bin/bash

PROJECT="club"
DST_SERVER="10.45.141.129"
SRC_SERVER="10.51.120.132"
DST_PORT="8055"
DEPLOY_PATH="/data/web/hf_app/build.club/ROOT/"
TARGET_CONFIG_PATH="/data/web/hf_app/build.club/ROOT/WEB-INF/classes/"
SOURCE_CONFIG_PATH="hefan/build.club/sync/config-toolkit.properties"
RSYNC_PATH="hefan/build.club/ROOT/"
TOMCAT="/usr/local/app/tomcat/"
RETURN_CODE="200"
PORT="1922"

### sync file to server
### $1 for dst server ip addr
sync(){
	ssh -t -p $PORT $1 "rsync -avP --delete $SRC_SERVER::$RSYNC_PATH $DEPLOY_PATH; \
		rsync -avP --delete $SRC_SERVER::$SOURCE_CONFIG_PATH $TARGET_CONFIG_PATH"
}

### restart tomcat
### $1 for dst server ip addr
restart(){
	ssh -t -p $PORT $1 "ps aux |grep -v grep |grep $TOMCAT|awk '{print \$2}'|xargs kill -9 "
	ssh -p $PORT $1 "source /etc/profile ; $TOMCAT/bin/startup.sh"
}

### success check
check(){
        STATUS=1
        for i in `seq 1 60`;do
                code=`curl -I http://$1:$DST_PORT/index.jsp --connect-timeout 2|grep HTTP|awk '{print $2}' 2>/dev/null`
                if [ $code -eq $RETURN_CODE ];then
                        STATUS=0
                        break
                else
			echo "$1 tomcat has not started , please wait............"
                        sleep 1
                fi
        done
}

check_compile_status(){
	if [ "`ls -A $DEPLOY_PATH`" = "" ]; then
		echo "$DEPLOY_PATH is indeed empty"
		exit 1
	else
		echo "$DEPLOY_PATH is not empty, go on..."
	fi
}

### script start


for IP in $DST_SERVER;do
	check_compile_status
        sync $IP
        restart $IP
        check $IP
        if [ $STATUS -eq 0 ];then
        	echo "$IP $PROJECT gkd_application started....."
	else
                echo "fuck u"
        fi
done

git 地址:https://git.oschina.net/abstergo/shell-linuxbash

相關文章
相關標籤/搜索