#!/bin/bash

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
systemctl restart kube-apiserver.service
sleep 90
install_path=$1
sleep 1
cd "${install_path}"
#docker load -i Install_Packages/minio.tar
sleep 3
mkdir -p /home/nas/minio
tar -xf Install_Packages/minio.tar.gz -C /home/nas/

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
sleep 3
cat /etc/profile | grep nerdctl
nerdctl_status=$?
echo $nerdctl_status
if [ ${nerdctl_status} == "1" ];then
    echo "alias docker='nerdctl --namespace k8s.io'"  >> /etc/profile
    echo "alias docker-compose='nerdctl compose'"  >> /etc/profile
    source  /etc/profile
fi
mkdir -p /etc/nerdctl/
cat > /etc/nerdctl/nerdctl.toml << 'EOF'
namespace      = "k8s.io"
insecure_registry = true
cni_path  = "/data/kube/bin"
EOF
sleep 5
# docker login harbor
#docker login -u admin -p edoc2edoc2 registry.edoc2.com:5000
nerdctl login -u admin -p edoc2edoc2 registry.edoc2.com:5000 --insecure-registry
sleep 5
nerdctl pull registry.edoc2.com:5000/edoc2v5/initdatabase:20230111150019_0daa60430d6c21d0 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/elasticsearch:prod620-v6.7.1.12 --insecure-registry
nerdctl pull registry.edoc2.com:5000/library/minio:prodv3 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/redis:prod600-v4.0.14.8 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/mysql:prod620-v8.0.17.18 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/edoc2:20230111150019_88ff6f6bdda5e1a9 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/wf:20220915102447 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/orgsync:20230111150019_afbdb39104d5330a --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/perm:20230111150019_2e7b3bd7710a18bc --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/org:20230111150019_6f00277080fca20e --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/auth:20230111150019_0611bae486153659 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/decryption:20230111063621_e7f09724b469ca7e --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/content:20230111030656_17ebac9853b252b9 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/transport:20230111030656_85b13305d24a51e7 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/inbiz:20221228171522_d5649f41ab3f2f92 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/clientsync:20230111152354 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/watermark:20230111070314_e973da935d2923c2 --insecure-registry
nerdctl pull 192.168.251.78/edoc2v5/apm:20221201162315 --insecure-registry
nerdctl pull 192.168.251.78/edoc2v5/logstash:v6.7.1.15 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/nginx:202301161322 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/metricbeat:6.7.1 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/metricmiddle:ecm-apm-v6.2.0.0 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/heartbeat:ecm-apm-v6.2.0.0 --insecure-registry
nerdctl pull 192.168.251.78/edoc2v5/filebeat:v6.7.1.2 --insecure-registry
nerdctl pull registry.edoc2.com:5000/edoc2v5/weboffice:ecm-v6.3.0.0 --insecure-registry
nerdctl pull registry.edoc2.com:5000/library/busybox:1.28 --insecure-registry
sleep 5
echo 1 >/proc/sys/vm/drop_caches
sleep 2
echo 2 >/proc/sys/vm/drop_caches

# kubectl create configmap envfile-config --from-file=./env/envfile.env
create_namespace_config(){
    sed -i "s/STORAGEPLATFORM: \"0\"/STORAGEPLATFORM: \"1000\"/g" Template/standalone/*.yaml
    sleep 1
    kubectl create -f Template/standalone/edoc2_namespace.yaml
    kubectl apply -f Template/standalone/configmap.yaml
    sleep 1
}

############# middleware depoly ##############
deploy_middleware(){
    kubectl label nodes $ONE_IP node.labels.nodelabels=Middleware
    kubectl apply -f Template/standalone/configmap.yaml
    sleep 2
    mkdir -p /home/edoc2/macrowing/edoc2v5/data/mysql
    mkdir -p /home/edoc2/macrowing/edoc2v5/data/dbbackup
    mkdir -p /home/edoc2/macrowing/edoc2v5/data/redis
    mkdir -p /home/edoc2/macrowing/edoc2v5/data/rabbitmq
    mkdir -p /home/edoc2/macrowing/edoc2v5/data/es
    mkdir -p /home/edoc2/macrowing/edoc2v5/data/esbackup
    sleep 3
    kubectl apply -f Template/standalone/middleware/edoc2_es.yaml
    sleep 3
    kubectl apply -f Template/standalone/middleware/edoc2_mysql.yaml
    sleep 3
    kubectl apply -f Template/standalone/middleware/edoc2_rabbitmq.yaml
    sleep 3
    kubectl apply -f Template/standalone/middleware/edoc2_redis.yaml
    sleep 5
    echo 1 >/proc/sys/vm/drop_caches
    echo 2 >/proc/sys/vm/drop_caches
    sleep 5
    if [ $STORAGE_MODE == "s3" ];then
        sed -i "s/127.0.0.1/$ONE_IP/g" Template/standalone/configmap.yaml
        sleep 5
        kubectl apply -f Template/standalone/middleware/edoc2_minio.yaml
        sleep 30
    fi
}

deploy_edoc2_service(){
############## ecm app deploy ##############
    kubectl label nodes $ONE_IP node.drive.nodelabels=InDrive
    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
    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
/data/nas/edoc2Docs *(rw,sync,no_root_squash)
/data/nas/edoc2     *(rw,sync,no_root_squash)
EOF
    exportfs -rv
    sleep 5
    systemctl restart nfs.service
    sleep 1
    systemctl restart nfs
    sleep 10

    # nfs /data/nas/edoc2Docs  /data/nas/edoc2
    kubectl apply -f Template/standalone/configmap.yaml
    sleep 2
    kubectl apply -f Template/standalone/app/initdatabase.yaml
    sleep 30
    kubectl apply -f Template/standalone/app/auth.yaml
    sleep 30
    kubectl apply -f Template/standalone/app/decryption.yaml
    sleep 30
    kubectl apply -f Template/standalone/app/watermark.yaml
    sleep 30
    kubectl apply -f Template/standalone/app/clientsync.yaml
    sleep 30
    kubectl apply -f Template/standalone/app/inbiz.yaml
    sleep 30
    kubectl apply -f Template/standalone/app/org.yaml
    sleep 30
    kubectl apply -f Template/standalone/app/orgsync.yaml
    sleep 30
    kubectl apply -f Template/standalone/app/perm.yaml
    sleep 30
    kubectl apply -f Template/standalone/app/wf.yaml
    sleep 30
    if [ $STORAGE_MODE == "s3" ];then
        kubectl apply -f Template/standalone/app/s3_content.yaml
    fi
    if [ $STORAGE_MODE == "nas" ];then
        kubectl apply -f Template/standalone/app/nfs_content.yaml
    fi
    sleep 60
    echo 1 >/proc/sys/vm/drop_caches
    echo 2 >/proc/sys/vm/drop_caches
    if [ $STORAGE_MODE == "s3" ];then
        kubectl apply -f Template/standalone/app/s3_transport.yaml
    fi
    if [ $STORAGE_MODE == "nas" ];then
        kubectl apply -f Template/standalone/app/nfs_transport.yaml
    fi
    sleep 60
    echo 1 >/proc/sys/vm/drop_caches
    echo 2 >/proc/sys/vm/drop_caches
    sleep 5
    kubectl apply -f Template/standalone/app/weboffice.yaml
    sleep 60
    echo 1 >/proc/sys/vm/drop_caches
    echo 2 >/proc/sys/vm/drop_caches
    sleep 5
    if [ $STORAGE_MODE == "s3" ];then
        kubectl apply -f Template/standalone/app/s3_edoc2.yaml
    fi
    if [ $STORAGE_MODE == "nas" ];then
        kubectl apply -f Template/standalone/app/nfs_edoc2.yaml
    fi
    sleep 60
    echo 1 >/proc/sys/vm/drop_caches
    echo 2 >/proc/sys/vm/drop_caches
    sleep 5
    kubectl apply -f Template/standalone/app/edoc2-nginx.yaml
    sleep 60
    echo 1 >/proc/sys/vm/drop_caches
    echo 2 >/proc/sys/vm/drop_caches
}

######### apm deploy ###########
deploy_apm(){
    sleep 60
    sed -i "s/127.0.0.1/$ONE_IP/g" Template/standalone/apm/*.yaml 
    docker load -i Install_Packages/beats.metricbeat.6.7.1.tar
    kubectl apply -f Template/standalone/apm/metricmiddle.yaml
    sleep 5
    kubectl apply -f Template/standalone/apm/filebeat.yaml
    sleep 5
    kubectl apply -f Template/standalone/apm/heatbeat.yaml
    sleep 5
    kubectl apply -f Template/standalone/apm/logstash.yaml
    sleep 30
    echo 1 >/proc/sys/vm/drop_caches
    echo 2 >/proc/sys/vm/drop_caches
    kubectl apply -f Template/standalone/apm/apm.yaml
    sleep 5
    kubectl apply -f Template/standalone/apm/metricbeat-kubernetes-system.yaml
    sleep 5
    kubectl apply -f Template/standalone/apm/metricbeat-kubernetes.yaml
    sleep 5
    echo 1 >/proc/sys/vm/drop_caches
    echo 2 >/proc/sys/vm/drop_caches
}

######### 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
}


#sed -i "s/$ONE_IP   registry.edoc2.com/#$ONE_IP   registry.edoc2.com/g" /etc/hosts

#rm -rf Template/standalone
#tar -xf Template/standalone.tar.gz -C Template/

cat << EOF > ${install_path}/standalone/cluster/configmap.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: envfile
  namespace: edoc2
data:
  CONTAINER_PLATFORM: "kubernetes"
  LOGGER_ISDEBUG: "false"
  LOGGER_MINCALLTIMESPAN: "1000"
  ContainerContent: "https://$ONE_IP:6443"
  PUBLISH_EXTERNAL_ADDRESS: "http://localhost/"
  ContainerType: "0"
  STORAGEPLATFORM: "1000"
  HTTP_PROTOCOL_TYPE: "http"
  S3_Url: "http://minio:5000"
  S3_AccessKey: "admin"
  S3_SecretKey: "root123456"
  S3_Bucket: "edoc2"
  S3_SignatureVersion: "4"
  Ceph_HealthUrl: "S3HealthUrl"
  OSS_ISCNAME: "false"
  ESURL: "http://es-0.es.edoc2.svc.cluster.local:9200"
  CACHEMODULE: "redis"
  ISUDC: "0"
  PRODUCTION: "edoc2"
  INBIZ: "true"
  INBIZURL: "http://inbiz"
  INBIZGRPCURL: "http://inbiz:5003"
  ECMURL: "http://edoc2"
  ECMGRPCURL: "http://edoc2:5002"
  ISTITLE: "0"
  WF: "false"
  WEBOFFICE: "false"
  LB_RR: "true"
  APM_ESURL: "http://es-0.es.edoc2.svc.cluster.local:9200"
  MiddlewareIsCluster: "0"
  IsUsedGRPC: "1"
  ELASTICSEARCH_HOSTS: "http://es-0.es.edoc2.svc.cluster.local:9200"
  LS_JAVA_OPTS: "-Xms4g -Xmx4g"
  COLLECT_LOG_SERVICES: '["edoc2","content","unionsync","orgsync","transport","regionsync","decryption","edrms","inbiz","auth","perm","org"]'
  API_LOG_MODULE: '["WebAPI","CoreAPI","DB","EDoc2.PermCtrl","EDoc2.Cache","EDRMS-DB","EDRMS-WEBAPI"]'
  ELASTICSEARCH_HOST: "es-0.es.edoc2.svc.cluster.local"
  ELASTICSEARCH_PORT: "9200"
  MQEndPoint: "rabbitmq-0.es.edoc2.svc.cluster.local:5672"
  #Region_TSAP: {Region_TSAP}
  #DatabaseType: 1
  DatabaseServerName: mysql
  SyncDatabaseServerName: mysql
  RedisHostName: "redis"
  RedisPassword: "ZgDSvOoY5Q"
  #DatabaseServerPort: 1433
  #DatabaseUserName: sa
  #DatabasePassword: 123456
  #IsSendNotNeedIndexToMQ: true
  #InWiseIndexQueueName: index_inwise_queue|true
  #OCR_MAXFILESIZE: 10,80
  #NUMS: 25
  #ThreadCheck: true
  #IsUpgrade: 6.2.0.0
  #ENCRYPTENGINE: SmartSec
  #SharedStorageId: 0
  #DecryptDebug: 1
  #DECRYPTGRPCURL: http://decryption:9090
  #data.conf: "Server{\n\tlisten 80;\n\tservername www.pzz.com;\n\troot /usr/share/nginx/html;}"
EOF

create_namespace_config
deploy_rbac_secret
deploy_middleware
deploy_edoc2_service
if [ "$APM_STATUS" == "true" ];then
    echo "apm true"
    deploy_apm
else
    echo "apm false"
fi
