######################################################################
####function: install web mservice
####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.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

#update setappenv
sed -i s/clustername/$clustername/g $pd/web/web.yaml
sed -i s/clustername/$clustername/g $pd/web/web-svc.yaml
sed -i s/zkconfig/$zkconfig/g $pd/web/web.yaml
sed -i s/nginx_vip/$nginx_vip/g $pd/web/web.yaml
sed -i s/clustername/$clustername/g $pd/mservice/mservice.yaml
sed -i s/clustername/$clustername/g $pd/mservice/mservice-svc.yaml
sed -i s/zkconfig/$zkconfig/g $pd/mservice/mservice.yaml
sed -i s/nginx_vip/$nginx_vip/g $pd/mservice/mservice.yaml

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

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

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

echo "web mservice install success!"
