#!/bin/sh start=`date +%s.%N` export HOME=/opt/webhooks export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin random=$1 # Setup VPN /usr/local/bin/nmctl context set k2c --endpoint=https://api.vpn.k2c.live --username=adelorenzo --password=Nina2022! /usr/local/bin/nmctl context use k2c ipv4=`/usr/bin/python3 /opt/webhooks/ipv4_gen.py` /usr/local/bin/nmctl network create --name="$random" --ipv4_addr="$ipv4" --udp_hole_punch="1" vpn_key=`/usr/local/bin/nmctl keys create $random 20 | jq -c '.accessstring' | sed 's/^.//' | sed 's/.$//'` # Setup LXD Virtual Network lxc project create $random -c features.images=false lxc project switch $random lxc --project $random network create vn-$random --target 001.k2c.live lxc --project $random network create vn-$random --target 002.k2c.live lxc --project $random network create vn-$random --target 003.k2c.live lxc --project $random network create vn-$random tunnel.lan.protocol=vxlan lxc --project $random profile create microk8s cat /opt/webhooks/default.profile | lxc --project $random profile edit default cat /opt/webhooks/microk8s.profile | lxc --project $random profile edit microk8s # Create container nodes i=1 while [ $i -le 3 ] do lxc --project $random launch -p default -p microk8s local:k2c-vpn k2c-$i-$random --network vn-$random --config limits.cpu=1 --config limits.memory=1536MiB i=$(($i+1)) done # Install VPN client nodeup1=`lxc --project $random ls | grep k2c-1-$random | awk '{print $4}'` nodeup2=`lxc --project $random ls | grep k2c-2-$random | awk '{print $4}'` nodeup3=`lxc --project $random ls | grep k2c-3-$random | awk '{print $4}'` echo $nodeup1 $nodeup2 $nodeup3 while true do if [ "$nodeup1" != "RUNNING" ] && [ "$nodeup2" != "RUNNING" ] && [ "$nodeup3" != "RUNNING" ] then echo -ne 'Containers not ready\r' else echo -ne 'Containers running\r' break fi sleep 1 done v=1 while [ $v -le 3 ] do lxc --project $random exec k2c-$v-$random -- bash -c "netclient join -t $vpn_key" v=$(($v+1)) done # Install microk8s m=1 while [ $m -le 3 ] do lxc --project $random exec k2c-$m-$random -- bash -c "snap install microk8s --classic" lxc --project $random exec k2c-$m-$random -- bash -c "snap alias microk8s.kubectl kubectl" m=$(($m+1)) done # Add worker nodes to the cluster #status1=`lxc --project $random exec k2c-1-$random -- kubectl get nodes -o jsonpath='{.items[*].status.conditions[?(@.type=="Ready")].status}' | tr ' ' '\n' | sort | uniq -c | grep "True" | awk '{print $1}'` #status2=`lxc --project $random exec k2c-2-$random -- kubectl get nodes -o jsonpath='{.items[*].status.conditions[?(@.type=="Ready")].status}' | tr ' ' '\n' | sort | uniq -c | grep "True" | awk '{print $1}'` #status3=`lxc --project $random exec k2c-3-$random -- kubectl get nodes -o jsonpath='{.items[*].status.conditions[?(@.type=="Ready")].status}' | tr ' ' '\n' | sort | uniq -c | grep "True" | awk '{print $1}'` #echo $status1 $status2 $status3 #while true #do # if [[ "$status1" -eq 1 ]] && [[ "$status2" -eq 1 ]] && [[ "$status3" -eq 1 ]]; # then # echo -ne 'Nodes are up\r' # break # else # echo -ne 'Nodes are not ready\r' # fi # sleep 1 #done lxc exec k2c-1-$random -- bash -c 'microk8s status --wait-ready' add_worker1=`lxc --project $random exec k2c-1-$random -- bash -c 'microk8s add-node' | sed -n '5p'` lxc --project $random exec k2c-2-$random -- $add_worker1 lxc exec k2c-1-$random -- bash -c 'microk8s status --wait-ready' add_worker2=`lxc --project $random exec k2c-1-$random -- bash -c 'microk8s add-node' | sed -n '5p'` lxc --project $random exec k2c-3-$random -- $add_worker2 # Generate the kubeconfig vpn1_ip=`nmctl node list | grep $random | grep k2c-1-$random | awk '{print $4}'` node1_ip=`lxc --project $random exec k2c-1-$random -- bash -c 'microk8s config' | grep server | cut -c21- | sed 's/.\{6\}$//'` lxc --project $random exec k2c-1-$random -- bash -c 'microk8s config' > /tmp/"$random"_kubeconfig.yaml sed -i "s/$node1_ip/$vpn1_ip/g" /tmp/"$random"_kubeconfig.yaml sed -i "s/microk8s/$random/g" /tmp/"$random"_kubeconfig.yaml # Upload kubeconfig random_rev=`curl -X GET https://adelorenzo:dimi2014@couchdb.oe74.cloud/k2c_deploy/$random | jq -c '._rev' | sed 's/^.//' | sed 's/.$//'` curl -H "Content-Type: text/x-yaml" -X PUT --data-binary @/tmp/${random}_kubeconfig.yaml --user "adelorenzo:dimi2014" "https://couchdb.oe74.cloud/k2c_deploy/${random}/${random}_kubeconfig.yaml?rev=$random_rev" # Setup VPN relay netmaker1=`nmctl node list | grep $random | grep netmaker-1 | awk '{print $16}'` k2c_1_ip=`nmctl node list | grep $random | grep k2c-1-$random | awk '{print $4}'` k2c_2_ip=`nmctl node list | grep $random | grep k2c-2-$random | awk '{print $4}'` k2c_3_ip=`nmctl node list | grep $random | grep k2c-3-$random | awk '{print $4}'` nmctl node create_relay $random $netmaker1 $k2c_1_ip,$k2c_2_ip,$k2c_3_ip # Notify setup end=`date +%s.%N` runtime=$( echo "$end - $start" | bc -l ) curl -d "Cluster ${random} is ready and it took ${runtime} seconds to prepare." https://n.oe74.cloud/k2c # Finish the cluster setup lxc --project $random exec k2c-1-$random -- bash -c 'microk8s enable dns rbac metrics-server hostpath-storage' # Optional - generate external VPN client wireguard config netmaker1=`nmctl node list | grep $random | grep netmaker-1 | awk '{print $16}'` nmctl node create_ingress $random $netmaker1 nmctl ext_client create $random $netmaker1 --id $random