#!/bin/bash

CONFIG_FILE=deploy-config/all.conf

source scripts/utils.sh
source scripts/config_get.sh
source scripts/global_variables.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}"
}

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

local_deploy_ecm(){
    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
    if [ ! -f /usr/local/harbor/harbor.yml ];then
        cd $install_path
    else
        cd /usr/local/harbor
        docker-compose down
        cd $install_path
        rm -rf /usr/local/harbor
    fi
    sleep 5
    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 "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"
    source scripts/echo_variable.sh

    echo "ssssssssssssssssssssssssssssssssssssssssssssssssssssss"
    echo "${SYSTEM}"
    if [ "${TYPE}" == "allinone" ];then
        config_info
        echo "### exec allinone choice ################################"
        #source scripts/install_requirements.sh
        get_install_path
        sleep 5
        #source scripts/hardware_perfcheck.sh
        if [ "${SYSTEM}" == "Ubuntu" ] || [ "${SYSTEM}" == "ubuntu" ];then
            apt autoremove -y fio* &
            sleep 9
            dpkg -i "$install_path"/Install_Packages/ubuntu18.04_deb/fio-3.30.deb &
            sleep 9
            #hardware_perfcheck
        else
            yum remove fio* -y &
            sleep 9
            rpm -ivh "$install_path"/Install_Packages/fio-3.2-1.el7.x86_64.rpm &
            sleep 9
            hardware_perfcheck
        fi
        sleep 1
        if [ "${ACTION_TYPE}" == "install" ];then
            echo "### install harbor ##################################"
            local_install_harbor

            echo "### local k8s deploy app service ####################"
            local_deploy_ecm 
        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

