Newer
Older
clusterSetupDev / scripts / lxc / createVm.sh
ENV=prod
REGION=us
HOSTMACHINE=mc02
VIRTUALMACHINE=lxc
#OSVERSION=22.04
OSVERSION=24.04
OS=ub
NETWORK=en0
if [ "$APP" == "" ]
then 
	APP=kubeadm
fi

currentHost=`hostname`
CPU=4

function findEthEthernet 
{
	set -x
	if [ "$VIRTUALMACHINE" == "mp" ]
	then
		eth0=`multipass networks |grep ethernet | grep -v Adapter |cut -d " " -f1`
	elif [ "$VIRTUALMACHINE" == "lxc" ]
	then
		eth0=`sudo lxc  network list |grep physical|grep eno|cut -d "|" -f2`
	fi
	
	echo $eth0
}

function osVersion
{
	os=$1
	osversion=$2
	echo "${os}${osversion//./}"
}

function createVm
{
	set -x

	action=$1
	NETWORK=$2

	if [ "Gangammals-Mini.heaerieglobalsolutions.net" == "$currentHost" ] 
	then
		HOSTMACHINE="mc03"
	fi
	if [ "Shanthys-Mini.heaerieglobalsolutions.net" == "$currentHost" ] 
	then
		HOSTMACHINE="mc02"
	fi

	if [ "Agalyas-Air.heaerieglobalsolutions.net" == "$currentHost" ] 
	then
		HOSTMACHINE="mc01"
	fi
	if [ "us-ub01-prod-pc-ub2404-eth0-dc-0" == "$currentHost" ] 
	then
		HOSTMACHINE="ub01"
	fi
	HOSTNAME_PREFIX="$REGION-$HOSTMACHINE-$ENV-$VIRTUALMACHINE-`osVersion $OS $OSVERSION`-$NETWORK-$APP"
	if [ "$action" == "create" ]
	then 
		echo "create"
		SEQ=`./getSeq.sh $HOSTNAME_PREFIX current++`
	else
		SEQ=`./getSeq.sh $HOSTNAME_PREFIX --current`
	fi
	echo $SEQ
	HOSTNAME="$HOSTNAME_PREFIX-$SEQ"

	mkdir -p  ~/data/$HOSTNAME
	if [ "$action" == "create" ]
	then
		if  [ "$VIRTUALMACHINE" == "mp" ]
		then
			multipass launch $OSVERSION --name $HOSTNAME --memory 8G --disk 50G --cpus $CPU --network $NETWORK --mount ~/data/$HOSTNAME:/data/
		elif [ "$VIRTUALMACHINE" == "lxc" ]
		then
			#sudo lxc launch ubuntu:22.04 us-ub01-prod-lxc-ub2404-eth0-apache2-2  --network br-eno1 -s default
			#sudo lxc launch UB2204   us-ub01-prod-lxc-ub2404-eth0-apache2-5  --network br-eno1 -s default
			sudo lxc launch ubuntu:24.04 $HOSTNAME --network br-eno1  --vm -c limits.memory=8GiB  -c limits.cpu=$CPU --storage microk8s-storage 
			sleep 10
			#sudo lxc config device add us-ub01-prod-lxc-ubnoble-eno1-kubeadm-0 shared-data disk source=$HOME/data/us-ub01-prod-lxc-ub2404-eth0-apache2-0  path=/data/
			sudo lxc config device add $HOSTNAME  shared-var disk source=$HOME/data/$HOSTNAME path=/data/ shift=true
			sudo lxc config device set $HOSTNAME root size 30GB

			#sudo lxc stop $HOSTNAME 
			sudo lxc config set $HOSTNAME security.privileged true
			#sudo lxc start $HOSTNAME 
		fi
	else
		if  [ "$VIRTUALMACHINE" == "mp" ]
		then

			multipass delete $HOSTNAME --purge 
		elif [ "$VIRTUALMACHINE" == "lxc" ]
		then
			sudo lxc stop  $HOSTNAME 
			sudo lxc delete  $HOSTNAME 
		fi
		
	fi

}

noOfinstance=1

if [ "$1" != "" ] 
then 
	noOfinstance=$1
fi

action="create"
if [ "$2" != "" ] 
then 
	action=$2
fi


NETWORK=`findEthEthernet`
for i in $(seq 1 $noOfinstance) 
do 
	echo "$i"
	createVm $action $NETWORK
done