diff --git a/scripts/lxc/clearCounter.sh b/scripts/lxc/clearCounter.sh new file mode 100755 index 0000000..fe7f8c0 --- /dev/null +++ b/scripts/lxc/clearCounter.sh @@ -0,0 +1,2 @@ +rm -fr ~/counter/* +rm -fr counter/* diff --git a/scripts/lxc/counter/us-ub01-prod-lxc-ubnoble-eno1-apache2 b/scripts/lxc/counter/us-ub01-prod-lxc-ubnoble-eno1-apache2 new file mode 100644 index 0000000..573541a --- /dev/null +++ b/scripts/lxc/counter/us-ub01-prod-lxc-ubnoble-eno1-apache2 @@ -0,0 +1 @@ +0 diff --git a/scripts/lxc/counter/us-ub01-prod-lxc-ubnoble-eno1-kubeadm b/scripts/lxc/counter/us-ub01-prod-lxc-ubnoble-eno1-kubeadm new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/scripts/lxc/counter/us-ub01-prod-lxc-ubnoble-eno1-kubeadm @@ -0,0 +1 @@ +1 diff --git a/scripts/lxc/counter/us-ub01-prod-lxc-ubnoble-eno1-microk8s b/scripts/lxc/counter/us-ub01-prod-lxc-ubnoble-eno1-microk8s new file mode 100644 index 0000000..7ed6ff8 --- /dev/null +++ b/scripts/lxc/counter/us-ub01-prod-lxc-ubnoble-eno1-microk8s @@ -0,0 +1 @@ +5 diff --git a/scripts/lxc/createHostList.sh b/scripts/lxc/createHostList.sh new file mode 100755 index 0000000..8c11b5c --- /dev/null +++ b/scripts/lxc/createHostList.sh @@ -0,0 +1 @@ +sudo lxc list|cut -d "|" -f2 |grep -v "NAME" |grep -v "+" > hosts diff --git a/scripts/lxc/createVm.sh b/scripts/lxc/createVm.sh new file mode 100755 index 0000000..b47b0c1 --- /dev/null +++ b/scripts/lxc/createVm.sh @@ -0,0 +1,124 @@ +ENV=prod +REGION=us +HOSTMACHINE=mc02 +VIRTUALMACHINE=lxc +#OSVERSION=22.04 +OSVERSION=noble +OS=ub +NETWORK=en0 +if [ "$APP" == "" ] +then + APP=kubeadm +fi + +currentHost=`hostname` +CPU=6 + +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 UB2204 $HOSTNAME --network br-eno1 -s default + #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-data disk source=$HOME/data/$HOSTNAME path=/data/ + 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 + + + diff --git a/scripts/lxc/getSeq.sh b/scripts/lxc/getSeq.sh new file mode 100755 index 0000000..89f1bc2 --- /dev/null +++ b/scripts/lxc/getSeq.sh @@ -0,0 +1,67 @@ +DIR=$PWD/counter +mkdir -p $DIR +agrc=$# +seqName=default +action="next" +if [ ! "$agrc" -le 0 ] +then +#{ +# echo "WARNING: There is no argument, sequence name is used default" +#} +#else +#{ + seqName=$1 + if [ "$2" != "" ] + then + action=$2 + fi +#} +fi +fileName=$DIR/$seqName +count=0 +returnCount=0 +if [ -f $fileName ] +then +#{ + + count=$(cat $fileName) + if [ "$action" == "current" ] + then + returnCount=$count + + elif [ "$action" == "current++" ] + then + returnCount=$count + count=`expr $count + 1` + elif [ "$action" == "++current" ] + then + count=`expr $count + 1` + returnCount=$count + elif [ "$action" == "--current" ] + then + count=`expr $count - 1` + returnCount=$count + elif [ "$action" == "current--" ] + then + returnCount=$count + count=`expr $count - 1` + elif [ "$action" == "previous" ] + then + count=`expr $count - 1` + returnCount=$count + else + count=`expr $count + 1` + returnCount=$count + fi +#} +else +#{ + #echo "initilize count" + returnCount=$count +#} +fi + +echo "$count" > $fileName +#return $count +echo $returnCount +exit 0 diff --git a/scripts/lxc/hosts b/scripts/lxc/hosts new file mode 100644 index 0000000..664a730 --- /dev/null +++ b/scripts/lxc/hosts @@ -0,0 +1,4 @@ + us-ub01-prod-lxc-ubnoble-eno1-microk8s-0 + us-ub01-prod-lxc-ubnoble-eno1-microk8s-1 + us-ub01-prod-lxc-ubnoble-eno1-microk8s-2 + us-ub01-prod-lxc-ubnoble-eno1-microk8s-3 diff --git a/scripts/lxc/multipassInstallPreRequired.sh b/scripts/lxc/multipassInstallPreRequired.sh new file mode 100755 index 0000000..5713b6b --- /dev/null +++ b/scripts/lxc/multipassInstallPreRequired.sh @@ -0,0 +1,149 @@ +function multipassExecTaskV1 +{ + host=$1 + echo $host + set -x + + machine="localhost" + if [ "$machine" == "multipass" ] + then + executor="sudo lxc exec $host --" + fi + $executor sudo apt update -y + $executor sudo apt install ansible -y + $executor mkdir repo + $executor git config --global user.email "agalyadoss@gmail.com" + $executor git config --global user.name "Agalya Ramadoss" + $executor sudo addgroup --gid 8055 boss + $executor sudo adduser --system --shell /bin/bash --uid 805501 --gid 8055 aboss + $executor sudo adduser --system --shell /bin/bash --uid 905501 --gid 8055 adoss + $executor sudo usermod -aG sudo aboss + $executor sudo usermod -aG sudo adoss + $executor sudo sh -c "echo aboss:$root_password|sudo chpasswd" + $executor sudo sh -c "echo adoss:$root_password|sudo chpasswd" + $executor sudo sed -i "s/# PasswordAuthentication yes/PasswordAuthentication yes/" /etc/ssh/ssh_config + $executor sudo sed -i "s/#PasswordAuthentication yes/PasswordAuthentication yes/" /etc/ssh/sshd_config + $executor sudo sed -i "s/#PubkeyAuthentication yes/PubkeyAuthentication yes/" /etc/ssh/sshd_config + $executor sudo sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/" /etc/ssh/sshd_config + $executor sudo sh -c "ssh-keygen -N '' -q -f ~/.ssh/id_rsa -t rsa -b 4096 -C root" + + + +} +function multipassExecTask +{ + host=$1 + echo $host + sudo lxc exec $host -- sudo apt update -y + sudo lxc exec $host -- sudo apt install ansible -y + sudo lxc exec $host -- sudo snap install multipass-sshfs -y + sudo lxc exec $host -- mkdir repo + sudo lxc exec $host -- git config --global user.email "agalyadoss@gmail.com" + sudo lxc exec $host -- git config --global user.name "Agalya Ramadoss" + sudo lxc exec $host -- sudo addgroup --gid 8055 boss + sudo lxc exec $host -- sudo adduser --system --shell /bin/bash --uid 805501 --gid 8055 aboss + sudo lxc exec $host -- sudo adduser --system --shell /bin/bash --uid 905501 --gid 8055 adoss + sudo lxc exec $host -- sudo usermod -aG sudo aboss + sudo lxc exec $host -- sudo usermod -aG sudo adoss +# sudo lxc exec $host -- sudo sh -c "echo $root_password\n$root_password|sudo passwd aboss" +# sudo lxc exec $host -- echo sh -c "$root_password|sudo passwd adoss" + sudo lxc exec $host -- sudo sh -c "echo aboss:$root_password|sudo chpasswd" + sudo lxc exec $host -- sudo sh -c "echo adoss:$root_password|sudo chpasswd" + sudo lxc exec $host -- sudo sed -i "s/# PasswordAuthentication yes/PasswordAuthentication yes/" /etc/ssh/ssh_config + sudo lxc exec $host -- sudo sed -i "s/#PasswordAuthentication yes/PasswordAuthentication yes/" /etc/ssh/sshd_config + sudo lxc exec $host -- sudo sed -i "s/#PubkeyAuthentication yes/PubkeyAuthentication yes/" /etc/ssh/sshd_config + sudo lxc exec $host -- sudo sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/" /etc/ssh/sshd_config +# sudo lxc exec $host -- sudo ssh-keygen -t rsa -b 4096 -C "root" + sudo lxc exec $host -- sudo sh -c "ssh-keygen -N '' -q -f ~/.ssh/id_rsa -t rsa -b 4096 -C root" +# sudo lxc exec $host -- sudo vim /etc/ssh/sshd_config +# sudo lxc exec $host -- sudo passwd +# sudo lxc exec $host -- sudo vim /root/.ssh/authorized_keys +# sudo lxc restart $host +# sudo lxc exec $host -- sudo vim /etc/default/kubelet + + +} +function multipassExecPostTask +{ + host=$1 + echo $host + id_rsa=`sudo lxc exec $host -- sudo cat /root/.ssh/id_rsa.pub` + for chost in `cat hosts` + do + echo $chost + sudo lxc exec $chost -- sudo sh -c "echo '$id_rsa'>> /root/.ssh/authorized_keys" + echo "sleeping for 1 seconds copy from $host to $chost " + sleep 1 + done + +} + +function multipassExecPostTaskV1 +{ + host=$1 + echo $host + machine="multipass" + if [ "$machine" == "multipass" ] + then + executor="sudo lxc exec $host --" + fi + id_rsa=`$executor sudo cat /root/.ssh/id_rsa.pub` + for chost in `cat hosts` + do + echo $chost + $executor sudo sh -c "echo '$id_rsa'>> /root/.ssh/authorized_keys" + echo "sleeping for 1 seconds copy from $host to $chost " + sleep 1 + done + +} +function multipassExecSshVerifyV1 +{ + set -x + host=$1 + echo $host + machine="localhost" + if [ "$machine" == "multipass" ] + then + executor="sudo lxc exec $host --" + fi + for chost in `cat hosts` + do + echo $chost + $executor sudo sh -c "ssh-keyscan $host >> /root/.ssh/known_hosts" + #sudo lxc exec $chost -- sudo ssh $host + #ssh-keyscan us-mc02-pac-mp-ub2204-en0-kubeadm-2 >> $HOME/.ssh/known_hosts + done +} +function multipassExecSshVerify +{ + host=$1 + echo $host + for chost in `cat hosts` + do + echo $chost + sudo lxc exec $chost -- sudo sh -c "ssh-keyscan $host >> /root/.ssh/known_hosts" + #sudo lxc exec $chost -- sudo ssh $host + #ssh-keyscan us-mc02-pac-mp-ub2204-en0-kubeadm-2 >> $HOME/.ssh/known_hosts + done +} +#Main +if [ "$root_password" == "" ] +then + echo "Please export root_password" + exit 1 +fi + +for host in `cat hosts` +do + multipassExecTask $host + echo $host +done +for host in `cat hosts` +do + multipassExecPostTask $host +done +for host in `cat hosts` +do + multipassExecSshVerify $host +done