Detecting which cloud you are in

15 views
Skip to first unread message

Jeffrey O'Neill

unread,
Oct 31, 2018, 9:36:21 AM10/31/18
to AppScale Community
Let's say you have a GAE Python 2.7 standard environment app running in GAE, AWS, and Azure, and part of your code depends on which cloud you are running in.  

For example, you need something like this:

    if cloud == "GAE":
        do_gae_stuff()
    elif cloud == "AWS":
        do_aws_stuff()
    elif cloud == "Azure":
        do_azure_stuff()
    else:
        assert False

How do you detect which cloud you are in so you can do the above?  

Chris Donati

unread,
Oct 31, 2018, 1:22:05 PM10/31/18
to appscale_...@googlegroups.com
We don't have an API that will do this at the platform level, but your application can guess the infrastructure with something like the following:

from google.appengine.api import urlfetch

METADATA_URLS = {'GCE': 'http://metadata.google.internal',
                 'EC2': 'http://169.254.169.254/latest/meta-data/',
                 'Azure': 'http://169.254.169.254/metadata/v1/InstanceInfo'}

def guess_infrastructure():
    for infrastructure, url in METADATA_URLS.items():
        try:
            response = urlfetch.fetch(url)
        except urlfetch.DownloadError:
            continue

        if response.status_code != 200:
            continue

        return infrastructure

    return None

The performance of the above can be improved with async parallel fetches, short timeouts, etc. However, you probably only need to run it once on startup (maybe in your appengine_config.py) and cache the result.

--
You received this message because you are subscribed to the Google Groups "AppScale Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to appscale_commun...@googlegroups.com.
To post to this group, send email to appscale_...@googlegroups.com.
Visit this group at https://groups.google.com/group/appscale_community.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages