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

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

pd=$PWD
## update config ##
clustername=$1
environment_type=$2

# 替换资源值
Xms_config="-Xms2048m"
Xmx_config="-Xmx2048m"
limits_cpu="2"
limits_memory="4Gi"
requests_cpu="200m"
requests_memory="4Gi"
# 测试环境配置减半
if [ "$environment_type" == "test" ]; then
    Xms_config="-Xms1024m"
    Xmx_config="-Xmx1024m"
    limits_cpu="1"
    limits_memory="2Gi"
    requests_cpu="100m"
    requests_memory="2Gi"
fi
sed -i s/Xms_config/$Xms_config/g $pd/fileserver/fileserver.yaml
sed -i s/Xmx_config/$Xmx_config/g $pd/fileserver/fileserver.yaml
sed -i s/limits_cpu/$limits_cpu/g $pd/fileserver/fileserver.yaml
sed -i s/limits_memory/$limits_memory/g $pd/fileserver/fileserver.yaml
sed -i s/requests_cpu/$requests_cpu/g $pd/fileserver/fileserver.yaml
sed -i s/requests_memory/$requests_memory/g $pd/fileserver/fileserver.yaml

#update setappenv
sed -i s/clustername/$clustername/g $pd/fileserver/fileserver.yaml
sed -i s/clustername/$clustername/g $pd/fileserver/fileserver-svc.yaml
# update label
sed -i "s/cluster:.*/cluster: cosmic_$clustername/g" $pd/fileserver/*.yaml

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

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

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

echo "fileserver install success!"
