######################################################################
####function: install mservice-qing
####author:danial_chen
####create date:2020-03
####modify date:2020-03
######################################################################

#!/bin/bash
set -e
echo "[`date +'%F %H:%M:%S'`] [INFO] useage :  sh installmservice-qing.sh clustername zkconfig nginx_vip environment_type"
if [ $# -ne 4 ]; then
    echo "$(basename $0): Missing script argument"
    echo "$(basename $0) [clustername] [zkconfig] [nginx_vip] [environment_type]"
    exit 
fi

pd=$PWD
## update config ##
clustername=$1
zkconfig=$2
nginx_vip=$3
environment_type=$4


# 替换资源值
Xms_config="-Xms3072m"
Xmx_config="-Xmx3072m"
limits_cpu="2"
limits_memory="6Gi"
requests_cpu="200m"
requests_memory="6Gi"
# 测试环境配置减半
if [ "$environment_type" == "test" ]; then
    Xms_config="-Xms1536m"
    Xmx_config="-Xmx1536m"
    limits_cpu="1"
    limits_memory="3Gi"
    requests_cpu="100m"
    requests_memory="3Gi"
fi
sed -i s/Xms_config/$Xms_config/g $pd/mservice-qing/mservice-qing.yaml
sed -i s/Xmx_config/$Xmx_config/g $pd/mservice-qing/mservice-qing.yaml
sed -i s/limits_cpu/$limits_cpu/g $pd/mservice-qing/mservice-qing.yaml
sed -i s/limits_memory/$limits_memory/g $pd/mservice-qing/mservice-qing.yaml
sed -i s/requests_cpu/$requests_cpu/g $pd/mservice-qing/mservice-qing.yaml
sed -i s/requests_memory/$requests_memory/g $pd/mservice-qing/mservice-qing.yaml

#update setappenv
sed -i s/clustername/$clustername/g $pd/mservice-qing/mservice-qing.yaml
sed -i s/zkconfig/$zkconfig/g $pd/mservice-qing/mservice-qing.yaml
sed -i s/nginx_vip/$nginx_vip/g $pd/mservice-qing/mservice-qing.yaml
# update label
sed -i "s/cluster:.*/cluster: cosmic_$clustername/g" $pd/mservice-qing/*.yaml

#create namespaces
ns=`kubectl get ns |grep $clustername |wc -l`
if [ $ns -eq 0 ];then
    kubectl create ns $clustername
fi

#create mservice-qing service
cd $pd/mservice-qing
qing=`kubectl get deploy -n $clustername |grep mservice-qing |wc -l`
if [ $qing -ne 0 ];then
    kubectl delete -f .
fi

# create and check if running
kubectl create -f .
#sleep 15
#result=`kubectl get pods -n $clustername| grep mservice-qing|grep Running|wc -l`
#if [ $result -ne 1 ];then
#    echo "create mservice-qing failed "
#    exit 1
#fi

echo "mc web mservice mservice-qing fileserver install success!"
