#!/bin/bash

# 重启deployment
# 第一个参数是命名空间，第二个参数是deployment名字
function restart_deployment() {
    kubectl rollout restart deployment $2 -n$1
}

# 放到scripts\cosmic\KDCC_installtool\kingdeetmp\nginx-appstatic\store\installappstore.sh里面了
# 修改jar包配置，防止建库接口报错
function modify_jar() {
    echo -e "开始解压 bos-corelib.zip"
    # 解压压缩包
    if [ -d /var/appstatic/appstore/mc/bos/bos-corelib ]
    then
      rm -rf /var/appstatic/appstore/mc/bos/bos-corelib
    fi
    unzip -o -d /var/appstatic/appstore/mc/bos/ /var/appstatic/appstore/mc/bos/bos-corelib.zip > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
        echo -e "unzip bos-corelib.zip failed!"
        exit 3
    fi
    # 切换路径
    cd /var/appstatic/appstore/mc/bos/bos-corelib
    pwd
    # 提取jar包文件
    echo -e "查看 bos-mq-1.0.jar"
    jar -xf bos-mq-1.0.jar `jar -tf bos-mq-1.0.jar|grep mqqueueconfig.xml`
    exists=`grep -q "exeDataBase_service" kd/bos/mq/mqqueueconfig.xml; echo $?`
    # 0表示存在，直接返回不再添加
    if [ $exists -eq 0 ]; then
        echo -e "mq package exist, no need to add"
        cd .. && rm -rf /var/appstatic/appstore/mc/bos/bos-corelib
        exit 0
    fi
    echo -e "mq package not exist, add to mqqueueconfig.xml"
    #修改文件内容
    CONTENT='        <region name=\"mc\">\
                <queue name=\"exeDataBase_service\">\
                  <consumer class=\"kd.bos.mc.datacenter.async.ExecuteDataBaseConsumer\"></consumer>\
                </queue>\
            </region>'
    sed -i '/<\/root>/i\'"$CONTENT" kd/bos/mq/mqqueueconfig.xml > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
      echo -e "add mq package failed"
      exit 7
    fi
    echo "add mq package success"
    # 更新jar包
    jar uf bos-mq-1.0.jar kd/bos/mq/mqqueueconfig.xml > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
      echo -e "update jar bos-mq-1.0.jar failed"
      exit 8
    fi
    echo -e "update jar bos-mq-1.0.jar success"
    # 删除文件
    rm -rf kd
    # 重新压缩文件
    zip -r bos-corelib.zip *
    # 移动压缩文件
    mv -f bos-corelib.zip ../bos-corelib.zip
    # 删除文件
    cd .. && rm -rf bos-corelib
}
