增加easzctl初始化检查

pull/485/head
gjmzj 2019-03-14 12:38:33 +08:00
parent 17dc3c3423
commit 7c43c17371
1 changed files with 13 additions and 9 deletions

View File

@ -164,10 +164,14 @@ function save_context() {
done done
echo "[INFO] save $1 ansible hosts" echo "[INFO] save $1 ansible hosts"
[ -f "$BASEPATH/hosts" ] && cp -fp $BASEPATH/hosts $BASEPATH/.cluster/$1/ if [ -f "$BASEPATH/hosts" ];then
cp -fp $BASEPATH/hosts $BASEPATH/.cluster/$1/
fi
echo "[INFO] save $1 kubeconfig" echo "[INFO] save $1 kubeconfig"
[ -f /root/.kube/config ] && cp -fp /root/.kube/config $BASEPATH/.cluster/$1/ if [ -f /root/.kube/config ];then
cp -fp /root/.kube/config $BASEPATH/.cluster/$1/
fi
} }
function install_context() { function install_context() {
@ -210,7 +214,7 @@ function checkout() {
echo $1 > $BASEPATH/.cluster/current_cluster echo $1 > $BASEPATH/.cluster/current_cluster
# check context $1, install it if existed, otherwise initialize it using default context # check context $1, install it if existed, otherwise initialize it using default context
if [ -d "$BASEPATH/.cluster/$1" ];then if [ -d "$BASEPATH/.cluster/$1" ];then
install_context $1; return 0; install_context $1;
else else
echo "[INFO] context $1 not existed, initialize it using default context" echo "[INFO] context $1 not existed, initialize it using default context"
cp -rp $BASEPATH/.cluster/default $BASEPATH/.cluster/$1 cp -rp $BASEPATH/.cluster/default $BASEPATH/.cluster/$1
@ -219,15 +223,12 @@ function checkout() {
} }
function setup() { function setup() {
if [ ! -d "$BASEPATH/.cluster" ]; then [ -d "$BASEPATH/.cluster" ] || { echo "[ERROR] invalid context, run 'easzctl checkout <cluster_name>' first"; return 1; }
echo "[ERROR] no cluster context found, run 'easzctl checkout <cluster_name>' first."
return 1
fi
CLUSTER=$(cat $BASEPATH/.cluster/current_cluster) CLUSTER=$(cat $BASEPATH/.cluster/current_cluster)
echo "[INFO] setup cluster: $CLUSTER" echo "[INFO] setup cluster: $CLUSTER"
[ -f "$BASEPATH/bin/kube-apiserver" ] || { echo "[ERROR] no binaries found, download then fist"; return 1; } [ -f "$BASEPATH/bin/kube-apiserver" ] || { echo "[ERROR] no binaries found, download then fist"; return 1; }
[ -f "$BASEPATH/hosts" ] || { echo "[ERROR] no ansible hosts found, read 'docs/setup/00-planning_and_overall_intro.md'"; return 1; } [ -f "$BASEPATH/hosts" ] || { echo "[ERROR] no ansible hosts found, read 'docs/setup/00-planning_and_overall_intro.md'"; return 1; }
echo -e "[INFO] setup begin in 15s, press 'Enter' to stop it\n:" echo -e "[INFO] setup begin in 10s, press 'Enter' to stop it\n:"
! (read -t 10 ANS) || { echo "[WARN] setup aborted"; return 1; } ! (read -t 10 ANS) || { echo "[WARN] setup aborted"; return 1; }
ansible-playbook $BASEPATH/90.setup.yml ansible-playbook $BASEPATH/90.setup.yml
echo "[INFO] save context: $CLUSTER" echo "[INFO] save context: $CLUSTER"
@ -235,6 +236,7 @@ function setup() {
} }
function list() { function list() {
[ -d "$BASEPATH/.cluster" ] || { echo "[ERROR] invalid context, run 'easzctl checkout <cluster_name>' first"; return 1; }
CLUSTER=$(cat $BASEPATH/.cluster/current_cluster) CLUSTER=$(cat $BASEPATH/.cluster/current_cluster)
save_context $CLUSTER save_context $CLUSTER
i=1 i=1
@ -251,6 +253,7 @@ function list() {
} }
function destroy() { function destroy() {
[ -d "$BASEPATH/.cluster" ] || { echo "[ERROR] invalid context, run 'easzctl checkout <cluster_name>' first"; return 1; }
CLUSTER=$(cat $BASEPATH/.cluster/current_cluster) CLUSTER=$(cat $BASEPATH/.cluster/current_cluster)
echo -e "[WARN] DELETE cluster: $CLUSTER, Continue? y/n:\n" echo -e "[WARN] DELETE cluster: $CLUSTER, Continue? y/n:\n"
read -t 15 ANS || { echo "[WARN] timeout, destroy aborted"; return 1; } read -t 15 ANS || { echo "[WARN] timeout, destroy aborted"; return 1; }
@ -258,12 +261,13 @@ function destroy() {
echo "[INFO] clean all nodes of cluster" echo "[INFO] clean all nodes of cluster"
sleep 5 sleep 5
ansible-playbook $BASEPATH/99.clean.yml ansible-playbook $BASEPATH/99.clean.yml
[ "$#" -gt 0 ] || { return 0; }
if [[ -n $1 && $1 == --purge ]];then if [[ -n $1 && $1 == --purge ]];then
echo "[INFO] delete current context" echo "[INFO] delete current context"
rm -rf $BASEPATH/.cluster/$CLUSTER rm -rf $BASEPATH/.cluster/$CLUSTER
echo default > $BASEPATH/.cluster/current_cluster
rm -rf $BASEPATH/hosts /root/.kube/* rm -rf $BASEPATH/hosts /root/.kube/*
echo "[INFO] change current context to default" echo "[INFO] change current context to default"
echo default > $BASEPATH/.cluster/current_cluster
install_context default install_context default
fi fi
else else