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

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

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

# 使用openjdk的需要修改参数
if type -p java > /dev/null; then
    if [[ $(java -version 2>&1) == *"OpenJDK"* ]]; then
        sed -i 's/Xss256k/Xss328k/g' $pd/mc/mc.yaml
    fi
else
    echo "Java未安装"
fi
#update setappenv
sed -i s/clustername/$clustername/g $pd/mc/mc.yaml
sed -i s/clustername/$clustername/g $pd/mc/mc-svc.yaml
sed -i s/zkconfig/$zkconfig/g $pd/mc/mc.yaml
sed -i s/nginx_vip/$nginx_vip/g $pd/mc/mc.yaml

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

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

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

echo "mc install success!"
