Scripting cloud detection..

Scripting cloud detection..

The last iteration of the auto deployment script threw up an interetsting question, how to recognise if a script is running on a cloud VM, local VM or physical machine? This is important as the script used curl against an external source (ifconfig.io) to determine the external IP. This would be ok for a cloud VM as you want the public IP for access, however it won’t work for say a homelab VM running on ESXi. I don’t want the external IP in that case, I need the local LAN IP.

I tried a variety of methods to determine this, looking at OS release info, seeing if there was anything unique between running Ubuntu on AWS, Azure, GCP and VMware…but I couldn’t find any differences. There also wasn’t any unique variables set, such as region, API ket etc… I then looked at matching IP ranges for the external IP, but that was just a huge task, especially when looking at global IP ranges across multiple vendors.

Finally I found a project called FACTER by puppet, it’s a ruby gem add-on that can determine the cloud provider and output a string of the vendor ID. I did try some logic based upon matching the vendor ID and doing a select action, but it was just easier to have a binary solution, if facter found an ID then the VM was running in the cloud, if not then it must be local. Cloud VM’s would return the public IP, whilst local VM’s would return only the LAN IP.:

pod=$(kubectl get po -n kasten-io |grep gateway | awk '{print $1}' )
kubectl expose po $pod -n kasten-io --type=LoadBalancer --port=8000 --name=k10-dashboard
port=$(kubectl get svc -n kasten-io |grep k10-dashboard | cut -d':' -f2- | cut -f1 -d'/' )
echo ""
get_public_ip=$(curl -s ifconfig.me)
get_local_ip=$(hostname -I | awk '{print $1}')
cloud_id=$(facter cloud |grep provider | cut -d'"' -f 2)
    if [[ ! -z $cloud_id ]]; then
        echo "Running on a cloud virtual machine"
        echo -e "$G K10 dashboard can be accessed on http://"$get_public_ip":"$port"/k10/#/"
    else
	echo "Running on a local machine / virtual machine"
	echo -e "$G K10 dashboard can be accessed on http://"$get_local_ip":"$port"/k10/#/"
    fi

As you can see from the above code block, I use this to find the K10 exposed port on the load balancer and then determine the correct IP, to allow the user to see the correct URL for access to the K10 dashboard.

Hopefully this makes it a little easier for complete novices to deploy the cluster and experiment with K10.

You can read more about FACTER here:

https://www.puppet.com/docs/puppet/7/facter.html