#!/bin/bash

es_version=$2

if [ ! -z ${CONTAINERD}  ];then
   log_info "change containerd hostpath"
   sed -i s#/var/lib/containerd#${CONTAINERD}#g /etc/containerd/config.toml 
   systemctl daemon-reload
   systemctl restart containerd.service
fi

sleep 10
sed -i 's/30000/5000/g' /etc/systemd/system/kube-apiserver.service
sed -i 's/32767/39999/g' /etc/systemd/system/kube-apiserver.service
systemctl daemon-reload
sleep 8
echo "############### systemctl restart kube-apiserver.service ##########"
systemctl restart kube-apiserver.service &
sleep 15
cd "${install_path}"
sleep 15

#function deploy_minio(){
#    log_info "deploy storage minio"
#    mkdir -p ${MINIO_PV_PATH}
#    tar -xf Install_Packages/minio.tar.gz -C ${MINIO_PV_PATH}
#    sed -i "s/STORAGEPLATFORM: \"0\"/STORAGEPLATFORM: \"1000\"/g" Template/standalone/configmap.yaml
#    sed -i "s#MINIODATA#${MINIO_PV_PATH}#g" Template/standalone/middleware/edoc2_minio.yaml
#    kubectl apply -f Template/standalone/middleware/edoc2_minio.yaml
#    sleep 30
#}
#function deploy_nas(){
#    if [ "${NAS_SERVER}" == "127.0.0.1" ];then
#    mkdir -p ${NAS_PATH}
#    chmod 777 "${install_path}"/Install_Packages/nfs-utils-1.3.0-0.68.el7.x86_64.rpm
#    chmod 777 "${install_path}"/Install_Packages/rpcbind-0.2.0-49.el7.src.rpm
#    rpm -ivh "${install_path}"/Install_Packages/nfs-utils-1.3.0-0.68.el7.x86_64.rpm --force --nodeps
#    rpm -ivh "${install_path}"/Install_Packages/rpcbind-0.2.0-49.el7.src.rpm --force --nodeps
#    sleep 5
#    sed -i "s#NAS_PATH#${NAS_PATH}#g"  Template/standalone/app/nas/*.yaml
#    systemctl restart rpcbind.service
#    sleep 2
#    systemctl restart nfs.service
#    sleep 5
#    systemctl enable nfs.service
#    mkdir -p /data/nas/{edoc2Docs,edoc2}
#    cat << EOF > /etc/exports
#${NAS_PATH} *(rw,sync,no_root_squash)
#EOF
#    exportfs -rv
#    systemctl restart nfs.service
#    systemctl restart nfs
#    sleep 10
#    else
#        sed -i "s#NAS_PATH#${NAS_PATH}#g"  Template/standalone/app/nas/*.yaml
#        sed -i "s#127.0.0.1#${NAS_SERVER}#g" Template/standalone/app/nas/*.yaml
#    fi     
#}

sleep 5
tar xf "${install_path}"/Install_Packages/nerdctl-0.18.0-linux-amd64.tar.gz -C /usr/local/sbin/
chmod -R 755 /usr/local/sbin/nerdctl
mkdir -p /etc/nerdctl/
sleep 10
cat > /etc/nerdctl/nerdctl.toml << 'EOF'
namespace      = "k8s.io"
insecure_registry = true
cni_path  = "/data/kube/bin"
EOF
sleep 10

#######################################################################################
#cd /usr/local/harbor && docker-compose up -d

#cd "${install_path}"
#docker login -u admin -p edoc2edoc2 registry.edoc2.com:5000
#sleep 5
#######################################################################################
#docker pull registry.edoc2.com:5000/edoc2v5/elasticsearch:prod620-v6.7.1.12
#docker pull registry.edoc2.com:5000/library/minio:prodv3
#docker pull registry.edoc2.com:5000/edoc2v5/redis:prod600-v4.0.14.8
#docker pull registry.edoc2.com:5000/edoc2v5/redis:v6.2.12-01
#docker pull registry.edoc2.com:5000/edoc2v5/mysql:prod620-v8.0.17.18
#docker pull registry.edoc2.com:5000/edoc2v5/rabbitmq:v3.7.17.2
#docker pull registry.edoc2.com:5000/library/busybox:1.28
#######################################################################################
cd Install_Packages/standalone_imgs/
useradd rabbitmq &
sleep 1
tar -xf elasticsearch_prod620-v6.7.1.12.tar.xz && docker load -i elasticsearch_prod620-v6.7.1.12.tar
#tar -xf elasticsearch_prod660-v7.17.4.1.tar.xz && docker load -i elasticsearch_prod660-v7.17.4.1.tar
tar -xf rabbitmq_v3.11.11-nv1.tar.xz && docker load -i rabbitmq_v3.11.11-nv1.tar
tar -xf redis_v6.2.12-01.tar.xz && docker load -i redis_v6.2.12-01.tar
tar -xf mysql_prod620-v8.0.17.18.tar.xz && docker load -i mysql_prod620-v8.0.17.18.tar 
sleep 2
cd "${install_path}"
sleep 5
echo 1 >/proc/sys/vm/drop_caches
sleep 2
echo 2 >/proc/sys/vm/drop_caches

create_namespace_config(){
    sleep 30
    kubectl apply -f Template/standalone/edoc2_namespace.yaml &
    sleep 5
    kubectl apply -f Template/standalone/mconfigmap.yaml &
}


deploy_middleware(){ 
    log_info "middleware depoly"
    kubectl label nodes $ONE_IP node.labels.nodelabels=Middleware &
    kubectl apply -f Template/standalone/mconfigmap.yaml &
    sleep 2
    mkdir -p /home/data/es
    mkdir -p /home/data/esbackup
    mkdir -p /home/data/minio
    mkdir -p /home/data/mysql
    mkdir -p /home/data/dbbackup
    mkdir -p /home/data/rabbitmq
    mkdir -p /home/data/redis
    sleep 5
    if [ "${es_version}" == "es6" ];then
        kubectl apply -f Template/standalone/middleware/edoc2_es.yaml
    fi
    if [ "${es_version}" == "es7" ];then
        kubectl apply -f Template/standalone/middleware/edoc2_es7.6.yaml
    fi
    sleep 3
    kubectl apply -f Template/standalone/middleware/edoc2_mysql.yaml
    sleep 3
    chown -R rabbitmq:rabbitmq /home/data/rabbitmq &
    chmod -R 777 /home/data/rabbitmq &
    sleep 3
    kubectl apply -f Template/standalone/middleware/edoc2_rabbitmq.yaml
    sleep 1
    kubectl apply -f Template/standalone/middleware/edoc2_redis.yaml
}

######### rbac secret deploy ###########
deploy_rbac_secret(){
    kubectl apply -f Template/standalone/rbac/edoc2-ns-reader-rbac.yaml
    kubectl apply -f Template/standalone/secret/edoc2-secret.yaml
    sleep 15
    echo 1 >/proc/sys/vm/drop_caches
    echo 2 >/proc/sys/vm/drop_caches
}

#kubectl create ns edoc2

# sed namespace此处
sed -i "s/edoc2/$NAMESPACE/g" Template/standalone/edoc2_namespace.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/standalone/middleware/*.yaml
sleep 1
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/standalone/rbac/edoc2-ns-reader-rbac.yaml
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/standalone/secret/edoc2-secret.yaml
sleep 5

cat << EOF > Template/standalone/mconfigmap.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: menvfile
  namespace: $NAMESPACE
data:
  CONTAINER_PLATFORM: "kubernetes"
  LOGGER_ISDEBUG: "false"
  LOGGER_MINCALLTIMESPAN: "1000"
  ContainerContent: "https://$ONE_IP:6443"
  PUBLISH_EXTERNAL_ADDRESS: "http://localhost/"
  ContainerType: "0"
EOF

sleep 2
sed -i "s/namespace: edoc2/namespace: $NAMESPACE/g" Template/standalone/mconfigmap.yaml &
sleep 1
sed -i "s/.edoc2.svc/.${NAMESPACE}.svc/g" Template/standalone/mconfigmap.yaml &

create_namespace_config
deploy_rbac_secret
deploy_middleware
if [  ! -d "/opt/standalone/" ];then
   \cp -rf Template/standalone/ /opt/
else
   log_error "allinone standalone is extis"
   \cp -rf Template/standalone/ /opt/
   #exit 77
fi
#deploy_edoc2_service

sleep 5
kubectl get pod -A
echo "##### 清理log4jar残存 ###########################"
source "${install_path}"/scripts/clean_log4jar.sh &
