#!/bin/bash

get_env_info() {
    find_path=`find / -name "auto_install.sh" -type f | awk -F "auto_install.sh" '{print $1}'`
    install_path="${find_path%%/}"
    echo "================================================"
    echo `"${install_path}"/scripts/remote_env.sh`
    source "${install_path}"/scripts/remote_env.sh
}

get_node_list_name() {
    MasterNodes="${K8S_MASTER01} ${K8S_MASTER02} ${K8S_MASTER03}"
    WorkNodes="${K8S_NODE01} ${K8S_NODE02} ${K8S_NODE03}"
}

get_node_ip() {
    k8s_lb_ip=$K8S_MASTER_LB_IP
    master01_ip=$K8S_MASTER_IP01
    master02_ip=$K8S_MASTER_IP02
    master03_ip=$K8S_MASTER_IP03
    node01_ip=$K8S_NODE_IP01
    node02_ip=$K8S_NODE_IP02
    node03_ip=$K8S_NODE_IP03
    k8s_service_net=$K8S_CLUSTER_NET
    k8s_service_ip=$K8S_SERVICE_IP
    k8s_cluster_dns=$K8S_CLUSTER_DNS
    #SSH_PORT=$SSH_PORT
    master_eth01=$MASTER_ETH01
    master_eth02=$MASTER_ETH02
    master_eth03=$MASTER_ETH03
    cluster_cidr=$DOCKER0_NETWORK
}

install_cluster_rancher() {
    get_env_info
    get_node_list_name
    get_node_ip
    for NODE in $WorkNodes; do
        echo "==============================="
        echo $NODE;
        sleep 1;
        sshpass ssh -p $SSH_PORT root@${NODE} 'docker login -u admin -p edoc2edoc2 registry.edoc2.com:5000';
        sshpass ssh -p $SSH_PORT root@${NODE} 'docker pull registry.edoc2.com:5000/library/rancher-agent:v2.7.0';
        sshpass ssh -p $SSH_PORT root@${NODE} 'docker pull registry.edoc2.com:5000/library/mirrored-bci-micro:15.4.14.3';
        sshpass ssh -p $SSH_PORT root@${NODE} 'docker pull registry.edoc2.com:5000/library/shell:v0.1.18';
        sshpass ssh -p $SSH_PORT root@${NODE} 'docker pull registry.edoc2.com:5000/library/busybox:1.28';
        sshpass ssh -p $SSH_PORT root@${NODE} 'docker pull registry.edoc2.com:5000/library/rancher:v2.7.0';
        sshpass ssh -p $SSH_PORT root@${NODE} 'docker pull registry.edoc2.com:5000/rancher/rancher-agent:v2.7.0';
        sshpass ssh -p $SSH_PORT root@${NODE} 'docker pull registry.edoc2.com:5000/rancher/rancher:v2.7.0';
        sshpass ssh -p $SSH_PORT root@${NODE} 'docker pull registry.edoc2.com:5000/rancher/shell:v0.1.18';
        sshpass ssh -p $SSH_PORT root@${NODE} 'docker pull registry.edoc2.com:5000/rancher/fleet:v0.5.0';
        sshpass ssh -p $SSH_PORT root@${NODE} 'docker pull registry.edoc2.com:5000/rancher/gitjob:v0.1.32';
        sshpass ssh -p $SSH_PORT root@${NODE} 'docker pull registry.edoc2.com:5000/rancher/fleet-agent:v0.5.0';
    done
    sleep 3
    cd "${install_path}"/Template/rancher/
    kubectl apply -f rancher_namespace.yaml
    sleep 1
    kubectl -n cattle-system create secret tls tls-rancher-ingress --cert=./tls.crt --key=./tls.key
    kubectl -n cattle-system create secret generic tls-ca --from-file=./cacerts.pem
    cd "${install_path}"/Template/rancher/rancher
    helm install rancher --namespace cattle-system --create-namespace --set systemDefaultRegistry=registry.cn-hangzhou.aliyuncs.com --set ingress.tls.source=secret --set hostname=edoc2v5.rancher.pre.com --set privateCA=false -f values.yaml .    
}

install_cluster_rancher

# helm uninstall rancher -n cattle-system
# helm uninstall fleet -n cattle-fleet-system
# helm uninstall fleet-agent-local -n cattle-fleet-local-system
# helm uninstall fleet-crd -n cattle-fleet-system
# helm uninstall rancher-webhook -n cattle-system
