samedi 22 août 2020

How to Handle Timed Out Error Message in Bash Script for Vagrant Up Command

So I have a init script in which I am trying to handle some errors that pop-up and/or occur when a vagrant machine is trying to come up.

init.sh Script:

#!/bin/bash

# Timeout Error for SSH on Vagrantfile
TIMEOUT=$(echo "Timed out")

# Node Variables 
MASTER=$(pwd | awk -F/ '{print $5}')-k8s-master
NODE_1=$(pwd | awk -F/ '{print $5}')-node-1
NODE_2=$(pwd | awk -F/ '{print $5}')-node-2

# Initialize Cluster
while true; do
    read -p "Do you wish to initialize the cluster in $pwd (yes/no)?" yn
    case $yn in
        [Yy]* ) vagrant up $MASTER --[no-]destroy-on-error; \
        if (($TIMEOUT));
            then vagrant destroy $MASTER -f;
                vagrant up $MASTER --[no-]destroy-on-error;
        elif (($TIMEOUT));
            then vagrant destroy $NODE_1 -f;
                vagrant up $NODE_1 --[no-]destroy-on-error;
        elif (($TIMEOUT));
            then vagrant destroy $NODE_2 -f;
                vagrant up $NODE_2 --[no-]destroy-on-error;
        else (($TIMEOUT=FALSE));
            echo "Node Provisioned";
        fi
        break;; \
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done

# Prepare Join Command
vagrant ssh -c "kubeadm token create --print-join-command > /tmp/temp_join_token.txt; \
        cat /tmp/temp_join_token.txt; \
        exit" \
$(pwd | awk -F/ '{print $5}')-k8s-master

# Copy the file within Directory opn Local Host
vagrant scp $(pwd | awk -F/ '{print $5}')-k8s-master:/tmp/temp_join_token.txt .

# Copy the File to Guest Machines
vagrant scp $(pwd)/temp_join_token.txt $(pwd | awk -F/ '{print $5}')-node-1:/home/vagrant
vagrant scp $(pwd)/temp_join_token.txt $(pwd | awk -F/ '{print $5}')-node-2:/home/vagrant

# Delete Token on Local host
rm $(pwd)/temp_join_token.txt

# Echo Completion
echo "Cluster has been initialized and Join Command is inside the worker nodes on home directory"

The basic premise of the script is just to vagrant up on a machine within a particular directory. I have an interactive prompt to prompt the user for a yes or no to "provision a cluster" from the Vagrantfile in that directory. However, when in that directory and vagrant up is initialized, sometimes I will get an error message as such:

Timed out...

The message is rather long, so I just put the first two words that come up, but it's due to SSH not being provisioned on the machine. However, I have a solution:

  1. If vagrant up encounters the timeout message for Master node: destroy the machine that failed
vagrant destroy $MASTER -f
  1. If vagrant up encounters the timeout message for Node-1 worker node destroy the machine that failed:
vagrant destroy $NODE_1 -f
  1. If vagrant up encounters the timeout message for Node-2 worker node destroy the machine that failed:
vagrant destroy $NODE_2 -f

I know my while loop syntax for the prompt works, but the main focus is creating a conditional for-loop for those three nodes, that if it fails due to a $TIMEOUT, destroy it and bring it back up again until it gets provisioned.

Can I get some help? Perhaps my if-then statement approach in the while loop is wrong, not really sure. I also know my syntax is wrong, so I welcome any and all approaches.

Aucun commentaire:

Enregistrer un commentaire