#!/bin/bash
exec_path=$1

hostnamectl set-hostname "$K8S_HOSTNAME"
chmod +x $exec_path/soft/*
cd $exec_path/soft/
rm -rf /etc/kubeasz
tar xf kubeasz.tar.gz && mv kubeasz /etc/
echo "$KVERSION"
if [ "$KVERSION" == "1.23" ];then
    echo "$KVERSION"
    #rm -rf /etc/kubeasz/clusters/default
    sed -i 's/online/offline/g' /etc/kubeasz/example/config.yml
    sed -i 's/online/offline/g' /etc/kubeasz/clusters/default/config.yml
    sh ezdown -D -d 20.10.8 -k v1.23.10
    sleep 2
    sh ezdown -S
    sleep 8
    docker exec -it kubeasz ezctl start-aio
    sleep 1
elif [ "$KVERSION" == "1.24" ];then
    echo "$KVERSION"
elif [ "$KVERSION" == "1.25" ];then
    echo "$KVERSION"
fi

if [ ! -e "/usr/local/bin/docker-compose" ]; then
    echo "docker-compose not exist!!!"
    cp docker-compose-linux-x86_64.docker-compose-linux-x86_64 /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
    docker-compose -v
else
    echo "docker-compose have been existed!!!"
fi

mv /etc/kubeasz/clusters/default/yml/dashboard/kubernetes-dashboard.yaml  /etc/kubeasz/clusters/default/yml/dashboard/kubernetes-dashboard.yaml_bak
cp kubernetes-dashboard.yaml /etc/kubeasz/clusters/default/yml/dashboard/kubernetes-dashboard.yaml
chmod -R 644 /etc/kubeasz/clusters/default/yml/dashboard/kubernetes-dashboard.yaml
cd /etc/kubeasz/clusters/default/yml/dashboard

sleep 1
/etc/kubeasz/bin/kubectl apply -f kubernetes-dashboard.yaml

/opt/kube/bin/kubectl version
/opt/kube/bin/kubectl get node
/opt/kube/bin/kubectl get pod -A
/opt/kube/bin/kubectl get svc -A
\cp /opt/kube/bin/kubectl /usr/bin/kubectl

admin_id=`/etc/kubeasz/bin/kubectl get secret -A | grep admin | awk -v FS=' ' '{print $2}'`
# /etc/kubeasz/bin/kubectl describe secret -n kube-system $admin_id
\cp /root/.kube/config $exec_path/deploy-config/kubeconfig
add_token='token: '`/etc/kubeasz/bin/kubectl describe secret -n kube-system $admin_id | grep "token:" | awk -v FS=' ' '{print $2}'`
echo "$add_token" && echo "    ""$add_token" >> $exec_path/deploy-config/kubeconfig
