From 271d63fe6c21d60c0189e4b7d4b074f8b90d3500 Mon Sep 17 00:00:00 2001 From: Yosafat Marselino Date: Fri, 16 May 2025 05:32:08 -0400 Subject: [PATCH] Add testing script, since bmw lab repo is private --- test-scripts/install_k8s.sh | 260 ++++++++++++++++++++++++++++++++++++ 1 file changed, 260 insertions(+) create mode 100644 test-scripts/install_k8s.sh diff --git a/test-scripts/install_k8s.sh b/test-scripts/install_k8s.sh new file mode 100644 index 0000000..078f999 --- /dev/null +++ b/test-scripts/install_k8s.sh @@ -0,0 +1,260 @@ +#!/bin/bash + +# Capture start time +start_time=$(date +%s) + +workspace=$(pwd) + +# Check for sudo privilege +if [ "$EUID" -ne 0 ]; then + echo "Please run as root" + exit 1 +fi + +host_name="smolite" +# read -p "Enter option (1.SMO Only; 2. NearRT Only; 3. SMO+NearRT ): " OPTION +read -p "Enter IP address: " VM_IP + +KUBEVERSION="1.32.3-1.1" +HELMVERSION="3.14.2" + +command_exists() { + command -v "$1" >/dev/null 2>&1 +} + +# Function to check internet connectivity +check_internet() { + echo "Checking internet connection..." + if ping -q -c 1 -W 1 google.com &>/dev/null; then + echo "Internet connection is active." + else + echo "Error: No internet connection. Please ensure your system has access to the internet." + exit 1 + fi +} + +# Function to check if curl is installed and install if not +check_and_install_curl() { + echo "Checking if curl is installed..." + if ! command_exists curl; then + echo "curl is not installed. Installing now..." + sudo apt update + sudo apt install -y curl + if command_exists curl; then + echo "curl has been successfully installed." + else + echo "Error: Failed to install curl. Please check your internet connection and try again." + exit 1 + fi + else + echo "curl is already installed." + fi +} + +# Function to check for existing Kubernetes cluster and prompt for removal +check_existing_cluster() { + if command_exists kubectl && kubectl cluster-info &>/dev/null; then + read -p "Existing Kubernetes cluster found. Do you want to remove it? (y/N): " remove_cluster + if [[ "$remove_cluster" != "y" && "$remove_cluster" != "Y" ]]; then + echo "Kubernetes cluster removal skipped. Exiting script." + exit 0 + fi + echo "Removing existing Kubernetes cluster..." + kubeadm reset -f + sudo apt-get -y purge kubeadm kubectl kubelet kubernetes-cni kube* containerd + sudo apt-get -y autoremove + sudo rm -rf ~/.kube + apt-get -y autoremove + fi +} + +# Function to disable swap +disable_swap() { + echo "Disabling swap..." + sudo swapon --show >/dev/null 2>&1 + if [ $? -eq 0 ]; then + sudo swapoff -a + sudo rm /swapfile + sudo sed -i 's/\/swap.img/#\/swap.img/' /etc/fstab + else + echo "No swap is currently enabled." + fi +} + +# Function to check Ubuntu version +check_ubuntu_version() { + os_version=$(lsb_release -rs) + if [ "$os_version" != "22.04" ] && [ "$os_version" != "24.04" ]; then + echo "Error: Unsupported Ubuntu version. This script supports 22.04 and 24.04 only." + exit 1 + fi +} + +# Function to handle errors +handle_error() { + echo "Error occurred at step $1. Exiting..." + exit 1 +} + +# Function to check if namespace exists +check_namespace_not_exists() { + local namespace="$1" + if kubectl get namespace "$namespace" &>/dev/null; then + echo "Namespace '$namespace' exists. Skipping steps..." + return 0 # Namespace exists + else + echo "Namespace '$namespace' does not exist." + return 1 # Namespace does not exist + fi +} + +# Check Ubuntu version +echo "Checking Ubuntu version..." +check_ubuntu_version + +# Check internet connection +check_internet + +# Check and install curl if not present +check_and_install_curl + +# Check for existing Kubernetes cluster and prompt for removal +check_existing_cluster + +# Disable swap +disable_swap + +# Script for Installing Docker,Kubernetes and Helm + +wait_for_pods_running() { + NS="$2" + CMD="kubectl get pods --all-namespaces " + if [ "$NS" != "all-namespaces" ]; then + CMD="kubectl get pods -n $2 " + fi + KEYWORD="Running" + if [ "$#" == "3" ]; then + KEYWORD="${3}.*Running" + fi + + CMD2="$CMD | grep \"$KEYWORD\" | wc -l" + NUMPODS=$(eval "$CMD2") + echo "waiting for $NUMPODS/$1 pods running in namespace [$NS] with keyword [$KEYWORD]" + while [ $NUMPODS -lt $1 ]; do + sleep 5 + NUMPODS=$(eval "$CMD2") + echo "> waiting for $NUMPODS/$1 pods running in namespace [$NS] with keyword [$KEYWORD]" + done +} + +# Step x: Edit /etc/sysctl.conf to add fs.inotify.max_user_watches and fs.inotify.max_user_instances. This shoudl be done before containerd installation +echo "===========================================================" +echo " Preping the ENV: Editing /etc/sysctl.conf..." +echo "===========================================================" + +bash -c 'echo "fs.inotify.max_user_watches=524288" >> /etc/sysctl.conf' +bash -c 'echo "fs.inotify.max_user_instances=512" >> /etc/sysctl.conf' +bash -c 'echo "fs.inotify.max_queued_events=16384" >> /etc/sysctl.conf' +bash -c 'echo "vm.max_map_count=262144" >> /etc/sysctl.conf' + +# Apply sysctl params without reboot +sudo sysctl --system + +# # Installing Docker +# echo "****************************************************************************************************************" +# echo " Installing Docker " +# echo "****************************************************************************************************************" +# apt-get install -y --allow-downgrades --allow-change-held-packages --allow-unauthenticated --ignore-hold docker.io=${DOCKERVERSION} +# cat > /etc/docker/daemon.json <