代码改变世界

自动化部署服务脚本shell脚本

2012-10-30 17:51  Patrick.Lee  阅读(561)  评论(0编辑  收藏  举报

转自:http://sunboy522.blog.163.com/blog/static/455443720124244426563/

 

#!/bin/bash

tree=$1
select=$2
if [ "$tree" == "" ]; then
    echo "Must input tag(trunk),for example:ci.sh trunk"
    exit 1
elif [ "$tree" != "trunk" ]; then
    echo "tag is error,input trunk"
    exit 1
fi

ROOT_PATH=/home/co_channel
if [ "$tree" == "trunk" ]; then
    SRC_PATH=/home/co_channel/code/trunk   
fi
SERVER_PATH=/home/co_channel/tomcat
SCRIPT_PATH=/home/co_channel/script

#Build Project
build(){
    cd $PROJECT_PATH
    mvn clean
    svn up
    mvn package -Dmaven.test.skip=true
}
#Install Project
install(){
    cd $PROJECT_PATH
    mvn clean
    svn up
    mvn -U install -Dmaven.test.skip=true
}
#Deploy Web Project and Restart Web Server
deploy(){
    echo $SERVER_PATH
    num=`ps aux|grep java|grep co_channel|grep -v grep|grep -v cronolog|awk '{print $2}'`
    echo $num

    if [[ $num -gt 0 ]]
    then
            kill -9 $num
            sleep 5
            echo "stop tomcat success"
    fi

    rm -f -R $DEPLOY_PATH

    cp -f -R $BUILD_PATH $DEPLOY_PATH

    $CATALINA_HOME/bin/startup.sh

    echo "start tomcat success"
}
deployNoRestart(){
    rm -f -R $DEPLOY_PATH
    cp -f -R $BUILD_PATH $DEPLOY_PATH
}
restart(){
    num=`ps aux|grep java|grep co_channel|grep -v grep|grep -v cronolog|awk '{print $2}'`
    if [[ $num -gt 0 ]]
    then
            kill -9 $num
            sleep 5
            echo "stop tomcat success"
    fi
    $CATALINA_HOME/bin/startup.sh

    echo "start tomcat success"
}
echo "Build and Deploy Project"
echo "---------------------------------"
echo "   Choose One Project           -"
echo "---------------------------------"
echo "0)  restart                     -"
echo "1)  channel_parent              -"
echo "2)  channel_core                -"
echo "3)  channel_rest                -"
echo "4)  channel_web                 -"
echo "5)  channel_script              -"
echo "6)  channel_backweb             -"
echo "---------------------------------"
if [ "$select" == "" ]; then
    read select
fi

case $select in
        0)

        echo "Restart"
        restart   
    ;;
        1)
            echo "Parent Project"
        PROJECT_NAME=channel_parent
        PROJECT_PATH="$SRC_PATH"/$PROJECT_NAME
        echo $PROJECT_NAME
        echo $PROJECT_PATH
        install
    ;;
        2)
        echo "Core Project"
                PROJECT_NAME=channel_core
                PROJECT_PATH="$SRC_PATH"/$PROJECT_NAME
                echo $PROJECT_NAME
                echo $PROJECT_PATH
                install
    ;;
        3)
                echo "Rest Project"
                PROJECT_NAME=channel_rest
                PROJECT_PATH="$SRC_PATH"/$PROJECT_NAME
                BUILD_PATH="$PROJECT_PATH"/target/"$PROJECT_NAME"
                DEPLOY_PATH="$SERVER_PATH"/webapps/"$PROJECT_NAME"
                echo $PROJECT_NAME
                echo $PROJECT_PATH
                echo $BUILD_PATH
                echo $DEPLOY_PATH

                cp -f -R "$SRC_PATH"/channel_core/src/main/resources/* "$SRC_PATH"/$PROJECT_NAME/src/main/resources
                build
                deploy
        ;;
        4)
            echo "Web Project"
                PROJECT_NAME=channel_web
                PROJECT_PATH="$SRC_PATH"/$PROJECT_NAME
                BUILD_PATH="$PROJECT_PATH"/target/"$PROJECT_NAME"
                DEPLOY_PATH="$SERVER_PATH"/webapps/"$PROJECT_NAME"
                echo $PROJECT_NAME
                echo $PROJECT_PATH
                echo $BUILD_PATH
                echo $DEPLOY_PATH
        cp -f -R "$SRC_PATH"/channel_core/src/main/resources/* "$SRC_PATH"/$PROJECT_NAME/src/main/resources
        build   
        deploy
    ;;
    5)
            echo "Script"
            PROJECT_NAME=channel_script
            PROJECT_PATH="$SRC_PATH"/$PROJECT_NAME
            BUILD_PATH="$PROJECT_PATH"/target/"$PROJECT_NAME"
            DEPLOY_PATH="$SCRIPT_PATH"
            echo $PROJECT_NAME
            echo $PROJECT_PATH
            echo $BUILD_PATH
            echo $DEPLOY_PATH
            build
            deployNoRestart
            chmod a+x $DEPLOY_PATH/**/*.sh           
    ;;     
     6)
            echo "Web Back Project"
                PROJECT_NAME=channel_backweb
                PROJECT_PATH="$SRC_PATH"/$PROJECT_NAME
                BUILD_PATH="$PROJECT_PATH"/target/"$PROJECT_NAME"
                DEPLOY_PATH="$SERVER_PATH"/webapps/"$PROJECT_NAME"
                echo $PROJECT_NAME
                echo $PROJECT_PATH
                echo $BUILD_PATH
                echo $DEPLOY_PATH
        cp -f -R "$SRC_PATH"/channel_core/src/main/resources/* "$SRC_PATH"/$PROJECT_NAME/src/main/resources
        build
        deploy
    ;; 
esac
echo "Bye !!!"
exit 0