#!/bin/bash
exec_path=$1

hostnamectl set-hostname "$K8S_HOSTNAME"
chmod +x $exec_path/soft/*
cd $exec_path/soft/
sleep 2
rm -rf /etc/kubeasz
tar xf kubeasz.tar.gz && mv kubeasz /etc/

if [ ! -e "/usr/local/bin/python" ]; then
    ln -sv /usr/libexec/platform-python /usr/bin/python
fi

echo "$KVERSION"
if [ "$KVERSION" == "1.23" ];then
    echo "$KVERSION"
    #rm -rf /etc/kubeasz/clusters/default
    sleep 1
    sed -i "s/22/${SSH_PORT}/g" /etc/kubeasz/ezdown
    sleep 1
    sed -i 's/online/offline/g' /etc/kubeasz/example/config.yml &
    sleep 1
    sed -i 's/online/offline/g' /etc/kubeasz/clusters/default/config.yml &
    sleep 5
    if [ "$SYSTEM" == "Ubuntu" ];then
        ./ezdown -D -d 20.10.9 -k v1.25.13
        sleep 5
        ./ezdown -S
        sleep 5
    else
        ./ezdown -D -d 20.10.9 -k v1.25.13
        sleep 5
        ./ezdown -S
        sleep 5
    fi
    sleep 15
    echo "docker exec -it kubeasz ezctl start-aio"
    sleep 15
    echo "#########################################"
    sleep 15
    rm -rf /etc/kubeasz/clusters/default &
    sleep 15
    docker exec -it kubeasz ezctl start-aio
    sleep 15
    echo "#########################################"
    sleep 15
elif [ "$KVERSION" == "1.24" ];then
    echo "$KVERSION"
elif [ "$KVERSION" == "1.25" ];then
    echo "$KVERSION"
fi

sleep 15

if [ ! -e "/usr/local/bin/docker-compose" ]; then
    echo "docker-compose not exist!!!"
    \cp docker-compose-linux-aarch64 /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
\cp /opt/kube/bin/kubectl /usr/local/bin/kubectl

sleep 5
kubectl delete daemonset node-local-dns -n kube-system
sleep 2
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
