#!/bin/bash
# jenkins远程执行脚本重启微服务

BASEDIR="/mdata/cooleshow-test/jenkins"
CURRENT_DIR=`pwd`

# 停止服务
function shutdown() {

    SERVER_ID=$(netstat -antp | grep :$2 | grep LISTEN | awk '{print $7}' | awk -F/ '{ print $1 }')
    if [ -n "$SERVER_ID" ]; then
        kill -9 $SERVER_ID

        echo "starting kill -9 $2...($1)"
    fi
}

function mkdirs() {

    LOG_PATH=$1
    if [ ! -x "$LOG_PATH" ]; then
        mkdir -p $LOG_PATH
    fi
}

function test() {
    echo "test $*"
}

function gateway-server() {
    
    shutdown $1 8000

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
        ${CURRENT_DIR}/libs/gateway-web.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy $1"    
}

function auth-server() {

    shutdown $1 8001

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
            ${CURRENT_DIR}/libs/auth-server.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy $1"    
}

function admin-server() {

    shutdown $1 8005

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
        ${CURRENT_DIR}/libs/user-admin.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy $1"        
}

function teacher-server() {
    
    shutdown $1 9002

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
        ${CURRENT_DIR}/libs/user-teacher.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy $1"    
}

function student-server() {
    
    shutdown $1 9001

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
        ${CURRENT_DIR}/libs/user-student.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy $1"    
}

function website-server() {
    
    shutdown $1 8020

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
        ${CURRENT_DIR}/libs/user-website.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy $1"    
}

function websocket-server() {
    
    shutdown $1 9010

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
        ${CURRENT_DIR}/libs/cooleshow-websocket.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy $1"    
}

function audio-analysis() {
    
    shutdown $1 9004

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
        ${CURRENT_DIR}/libs/audio-analysis.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy $1"    
}

function bbs-server() {

    shutdown $1 8012

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
        ${CURRENT_DIR}/libs/cooleshow-bbs.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy bbs-server"    
}


function task-server() {

    shutdown $1 8009

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
        ${CURRENT_DIR}/libs/cooleshow-task.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy task-server"    
}



function classroom-server() {
    
    shutdown $1 9999

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
        ${CURRENT_DIR}/libs/user-classroom.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy $1"    
}

function cms-server() {
    
    shutdown $1 8006

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
        ${CURRENT_DIR}/libs/cooleshow-cms.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy $1"    
}


function tenant-server() {

    shutdown $1 8021

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
        ${CURRENT_DIR}/libs/user-tenant.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy $1"   
}


function mall-admin() {
    
    shutdown $1 8010

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
        ${CURRENT_DIR}/libs/mall-admin.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy $1"    
}

function mall-server() {
    
    shutdown $1 8011

    nohup java -jar -Dspring.profiles.active=dev \
        -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m \
        ${CURRENT_DIR}/libs/mall-portal.jar > ${BASEDIR}/logs/$1/stdout.log 2>&1 &
    echo "completed deploy $1"    
}

if [ $# -le 0 ]; then
    # 解压文件
    tar -zxf *.tar.gz
    echo "tar -xvf *.tar.gz"

    SERVICE_NAME=${CURRENT_DIR}/libs/service.md
    # 服务名称匹配，重启服务
    while read LINE; do

        if [[ ${LINE:3:1} != 'x' ]]; then
            # 不需要重启服务，直接忽略跳过
            continue
        fi

        SERVICE=${LINE:6}
        #日志目录
        LOG_PATH=${BASEDIR}/logs/${SERVICE}
        mkdirs $LOG_PATH

        # 调用函数
        $SERVICE $SERVICE
        
        echo "--------------${SERVICE}-----------------"
    done < ${SERVICE_NAME}
else 
    SERVICE_NAME="exit \
        gateway-server auth-server admin-server \
        teacher-server student-server website-server websocket-server \
        audio-analysis bbs-server classroom-server cms-server \
        mall-admin mall-server task-server tenant-server"

    select SERVICE in ${SERVICE_NAME}; do

        # 退出
        if [[ $SERVICE == 'exit' ]]; then
            break
        fi

        #日志目录
        LOG_PATH=${BASEDIR}/logs/${SERVICE}
        mkdirs $LOG_PATH

        #调用函数
        $SERVICE $SERVICE

        echo "--------------${SERVICE}-----------------"
    done

fi



