######################################################################
####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 environment_type sub_app"
if [ $# -ne 5 ]; then
    echo "$(basename $0): Missing script argument"
    echo "$(basename $0) [clustername] [zkconfig] [nginx_vip] [environment_type] [sub_app]"
    exit 
fi

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


# 替换资源值
mservice_Xms_config="-Xms4096m"
mservice_Xmx_config="-Xmx4096m"
mservice_limits_cpu="2"
mservice_limits_memory="6Gi"
mservice_requests_cpu="2"
mservice_requests_memory="6Gi"

web_Xms_config="-Xms4096m"
web_Xmx_config="-Xmx4096m"
web_limits_cpu="2"
web_limits_memory="6Gi"
web_requests_cpu="2"
web_requests_memory="6Gi"
if [ "$environment_type" != "prod" ]; then
    mservice_Xms_config="-Xms2048m"
    mservice_Xmx_config="-Xmx2048m"
    mservice_limits_cpu="1"
    mservice_limits_memory="4Gi"
    mservice_requests_cpu="1"
    mservice_requests_memory="4Gi"

    web_Xms_config="-Xms1024m"
    web_Xmx_config="-Xmx1024m"
    web_limits_cpu="500m"
    web_limits_memory="2Gi"
    web_requests_cpu="500m"
    web_requests_memory="2Gi"
fi

sed -i s/Xms_config/$mservice_Xms_config/g $pd/mservice/*.yaml
sed -i s/Xmx_config/$mservice_Xmx_config/g $pd/mservice/*.yaml
sed -i s/limits_cpu/$mservice_limits_cpu/g $pd/mservice/*.yaml
sed -i s/limits_memory/$mservice_limits_memory/g $pd/mservice/*.yaml
sed -i s/requests_cpu/$mservice_requests_cpu/g $pd/mservice/*.yaml
sed -i s/requests_memory/$mservice_requests_memory/g $pd/mservice/*.yaml

sed -i s/Xms_config/$web_Xms_config/g $pd/web/web.yaml
sed -i s/Xmx_config/$web_Xmx_config/g $pd/web/web.yaml
sed -i s/limits_cpu/$web_limits_cpu/g $pd/web/web.yaml
sed -i s/limits_memory/$web_limits_memory/g $pd/web/web.yaml
sed -i s/requests_cpu/$web_requests_cpu/g $pd/web/web.yaml
sed -i s/requests_memory/$web_requests_memory/g $pd/web/web.yaml

#update setappenv
sed -i s/clustername/$clustername/g $pd/web/*.yaml
sed -i s/zkconfig/$zkconfig/g $pd/web/*.yaml
sed -i s/nginx_vip/$nginx_vip/g $pd/web/*.yaml
if [ $sub_app == 1 ]; then
    sed -i s/isAppSplit/true/g $pd/web/*.yaml
else
    sed -i s/isAppSplit/false/g $pd/web/*.yaml
fi

sed -i s/clustername/$clustername/g $pd/mservice/*.yaml
sed -i s/zkconfig/$zkconfig/g $pd/mservice/*.yaml
sed -i s/nginx_vip/$nginx_vip/g $pd/mservice/*.yaml
# update label
sed -i "s/cluster:.*/cluster: cosmic_$clustername/g" $pd/mservice/*.yaml
sed -i "s/cluster:.*/cluster: cosmic_$clustername/g" $pd/web/*.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
kubectl apply -f .

#create mservice service
cd $pd/mservice
if [ $sub_app == 1 ]; then
  ls | grep mservice- | awk '{print "kubectl apply -f" $1}' | sh
else
  kubectl apply -f mservice.yaml
  kubectl apply -f mservice-svc.yaml
fi

echo "web mservice install success!"
