#!/bin/bash

CONFIG_FILE=deploy-config/all.conf

source scripts/config_get.sh
source scripts/global_variables.sh
source scripts/utils.sh
source scripts/hardware_perfcheck.sh

local_centos_baseshell(){
    source $install_path"/scripts/"base_centos7.sh "${install_path}"
}

local_rkylinux_baseshell(){
    source $install_path"/scripts/"base_centos7.sh "${install_path}"
}

local_ubuntu_baseshell(){
    source $install_path"/scripts/"base_ubuntu.sh "${install_path}"
}

centos_remote_install_baseshell(){
    source $install_path"/scripts/"remote_install_base.sh "${install_path}"
}

rocky_remote_install_baseshell() {
    sleep 1
    #source $install_path"/scripts/"rocky_remote_install_base.sh "${install_path}"
}

ubuntu_remote_install_baseshell() {
    source $install_path"/scripts/"ubuntu_remote_install_base.sh "${install_path}"
}

remote_install_docker(){
    source $install_path"/scripts/"remote_install_docker.sh
}

local_install_docker(){
    source $install_path"/scripts/"docker_install.sh "${install_path}" "${KVERSION}"
    echo "docker container服务启动安装完毕"
}

local_install_k8s(){
    source $install_path"/scripts/"k8s_allinone_install.sh "${install_path}"
    echo "allinone k8s 安装完毕"
}

centos_cluster_install_k8s() {
    get_install_path
    source $install_path"/scripts/"centos_k8s_cluster_install.sh "${install_path}"
}

ubuntu_cluster_install_k8s() {
    get_install_path
    source $install_path"/scripts/"ubuntu_k8s_cluster_install.sh "${install_path}"
}

rocky_cluster_install_k8s() {
    get_install_path
    source $install_path"/scripts/"rocky_k8s_cluster_install.sh "${install_path}"
}

#allinone_rancher() {
#    get_install_path
#    source $install_path"/scripts/"allinone_rancher.sh   
#    rancher_check_pid_file="${install_path}/pids/rancher_check.pid"
#    if [ ! -f $rancher_check_pid_file ]; then
#        usleep
#    else
#        jobs_process=`cat $rancher_check_pid_file`
#        echo "#### old pid $jobs_process ####"
#        kill -9 $jobs_process
#        rm -rf $rancher_check_pid_file
#    fi
#    source "$install_path"/delete_rancher_error.sh &
#    jobs_process=`jobs -l | grep -v grep | awk -F " " '{print $2}'`
#    echo "#### new pid $jobs_process ####"
#    echo $jobs_process > $rancher_check_pid_file
#}

cluster_rancher() {
    get_install_path
    source $install_path"/scripts/"cluster_rancher.sh
    rancher_check_pid_file="${install_path}/pids/rancher_check.pid"
    if [ ! -f $rancher_check_pid_file ]; then
        usleep
    else
        jobs_process=`cat $rancher_check_pid_file`
        echo "#### old pid $jobs_process ####"
        kill -9 $jobs_process
        rm -rf $rancher_check_pid_file
    fi
    source "$install_path"/delete_rancher_error.sh &
    jobs_process=`jobs -l | grep -v grep | awk -F " " '{print $2}'`
    echo "#### new pid $jobs_process ####"
    echo $jobs_process > $rancher_check_pid_file
}

#allinone_skywalking() {
#    get_install_path
#    source $install_path"/scripts/"cluster_skywalking.sh
#}

cluster_skywalking() {
    get_install_path
    source $install_path"/scripts/"cluster_skywalking.sh
}

cluster_destroy_k8s() {
    get_install_path
    source $install_path"/scripts/"destroy_k8s_cluster.sh "${install_path}"
}

local_deploy_ecm(){
    rpm -ivh "$install_path"/Install_Packages/fio-3.2-1.el7.x86_64.rpm
    sleep 5
    hardware_perfcheck
    source $install_path"/scripts/"k8s_allinone_deploy.sh "${install_path}"
    echo "allinone deploy 部署完毕"
}

local_destry_k8s_allinone(){
    source $install_path"/scripts/"destroy_k8s_allinone.sh
}

local_install_harbor(){
    get_install_path
    source $install_path"/scripts/"install_harbor.sh "${install_path}"
}

get_install_path() {
    find_path=`find / -name "auto_install.sh" -type f | awk -F "auto_install.sh" '{print $1}'`
    install_path="${find_path%%/}"
    #install_path=$(pwd)
    echo "${install_path}"
    cd "${install_path}"
    chmod -R +x scripts/*
}

config_info(){
    log_info "#### get config_info function start"
    log_info "[install_type] TYPE=${TYPE}"
    log_info "#### get config_info function end"
    source $install_path"/scripts/"create_remote_env.sh
}

main(){
    echo "ssssssssssssssssssssssssssssssssssssssssssssssssssssss"
    source scripts/echo_variable.sh

    echo "ssssssssssssssssssssssssssssssssssssssssssssssssssssss"
    echo "${SYSTEM}"
    if [ "${TYPE}" == "allinone" ];then
        config_info
        echo "### exec allinone choice ################################"
        #source scripts/install_requirements.sh
        if [ "${ACTION_TYPE}" == "install" ];then
            echo "### local install base shell ######################"
	    if [ "${SYSTEM}" == "Centos" ];then
                local_centos_baseshell
            elif [ "${SYSTEM}" == "RockyLinux" ];then
                local_rkylinux_baseshell
	    elif [ "${SYSTEM}" == "Ubuntu" ];then
                local_ubuntu_baseshell
	    fi
            echo "### local install docker and container ##############"
            local_install_docker

            echo "### local install k8s allinone ######################"
            local_install_k8s 

            #echo "### install harbor ##################################"
            #local_install_harbor

            #echo "### local k8s deploy ecm6.3.0.0 #####################"
            #rpm -ivh Install_Packages/fio-3.2-1.el7.x86_64.rpm
            #sleep 5
            #local_deploy_ecm

        elif [ "${ACTION_TYPE}" == "destroy" ];then
            echo "### local destroy k8s allinone ######################"
            local_destry_k8s_allinone
        fi           
    elif [ "${TYPE}" == "cluster" ];then
        config_info
        if [ "${ACTION_TYPE}" == "install" ];then
            ONE_IP=${REGISTRY}
            echo "### 推送base shell再执行ssh ##############################"
            source scripts/env_config.sh
            source scripts/create_hosts.sh

            sleep 3
            echo "### 推送所有变量文件 shell再执行ssh && 装必备组件 ############"
            if [ "${SYSTEM}" == "Centos" ];then
		source scripts/centos_install_requirements.sh
		sleep 5
                centos_remote_install_baseshell
	    elif [ "${SYSTEM}" == "RockyLinux" ];then
		#source scripts/rocky_install_requirements.sh
		sleep 5
                rocky_remote_install_baseshell
	    elif [ "${SYSTEM}" == "Ubuntu" ];then
		echo "xxxxxxxxxxxxxxxxxx"
	        source scripts/ubuntu_install_requirements.sh
		echo "xxxxxxxxxxxxxxxxxx"
		sleep 5
                ubuntu_remote_install_baseshell
            fi
            sleep 2

            echo "### sshpass 执行docker安装并启动 #########################"
            remote_install_docker
            sleep 5
            echo "### install harbor #######################################"
            local_install_harbor

            echo "### install k8s cluster ##################################"
	    if [ "${SYSTEM}" == "Centos" ];then
                centos_cluster_install_k8s
            elif [ "${SYSTEM}" == "RockyLinux" ];then
	        rocky_cluster_install_k8s
	    elif [ "${SYSTEM}" == "Ubuntu" ];then
                ubuntu_cluster_install_k8s
            fi
            echo "### install skywalking cluster ##################################"
            if [ "${SKYWALKING_STATUS}" == "true" ];then
                echo "############ xxxxx exec skywalking xxxxx #################"
                kubectl apply -f $install_path"/Template/cluster/edoc2_namespace.yaml"
                cluster_skywalking
            fi
            sleep 1
            echo "### install rancher cluster ##################################"
            if [ "${RANCHER_STATUS}" == "true" ];then
                echo "############ xxxxx exec rancher xxxxx #################"
                cluster_rancher
            fi
            kubectl get pod -A
        elif [ "${ACTION_TYPE}" == "destroy" ];then
            cluster_destroy_k8s
        elif [ "${ACTION_TYPE}" == "recovery" ];then
            ONE_IP=${REGISTRY}
            echo "### 推送base shell再执行ssh ##############################"
            source scripts/env_config.sh
            source scripts/create_hosts.sh

            echo "### 推送所有变量文件 shell再执行ssh && 装必备组件 ############"
            if [ "${SYSTEM}" == "Centos" ];then
		source scripts/centos_install_requirements.sh
		sleep 5
                centos_remote_install_baseshell
	    elif [ "${SYSTEM}" == "RockyLinux" ];then
		#source scripts/rocky_install_requirements.sh
		sleep 5
                rocky_remote_install_baseshell
	    elif [ "${SYSTEM}" == "Ubuntu" ];then
	        source scripts/ubuntu_install_requirements.sh
		sleep 5
                ubuntu_remote_install_baseshell
            fi

            sleep 2
            echo "### sshpass 执行docker安装并启动 #########################"
            remote_install_docker
            sleep 2

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

	    echo "### install k8s cluster ##################################"
	    if [ "${SYSTEM}" == "Centos" ];then
	        centos_cluster_install_k8s
	    elif [ "${SYSTEM}" == "RockyLinux" ];then
		rocky_cluster_install_k8s
	    elif [ "${SYSTEM}" == "Ubuntu" ];then
		ubuntu_cluster_install_k8s
	    fi
            echo "### install skywalking cluster ##################################"
            if [ "${SKYWALKING_STATUS}" == "true" ];then
                echo "############ xxxxx exec skywalking xxxxx #################"
                kubectl apply -f $install_path"/Template/cluster/edoc2_namespace.yaml"
                cluster_skywalking
            fi
            sleep 1
            echo "### install rancher cluster ##################################"
            if [ "${RANCHER_STATUS}" == "true" ];then
                echo "############ xxxxx exec rancher xxxxx #################"
                cluster_rancher
            fi
            kubectl get pod -A
        fi
    fi
    log_info "=========================================================================================================="
    log_info "system_version"  
}

get_install_path
#install_type=`cat conf/default.conf|grep -vE '^$|#|\[' | grep type | grep "=" | awk -v FS='=' '{print $2}'`
#system_type=`cat conf/default.conf|grep -vE '^$|#|\[' | grep system | grep "=" | awk -v FS='=' '{print $2}'`
#system_version=`cat conf/default.conf|grep -vE '^$|#|\[' | grep version | grep "=" | awk -v FS='=' '{print $2}'`
#time_server=`cat conf/default.conf|grep -vE '^$|#|\[' | grep timeserver | grep "=" | awk -v FS='=' '{print $2}'`
#k8s_version=`cat conf/default.conf|grep -vE '^$|#|\[' | grep kversion | grep "=" | awk -v FS='=' '{print $2}'`
main

