Skip to content

Commit

Permalink
support openstack metadata (#186)
Browse files Browse the repository at this point in the history
add initial support for running spilo in kubernetes on openstack

Fixes #183
  • Loading branch information
roll4life authored and CyberDem0n committed Sep 29, 2017
1 parent 393d673 commit 8b04274
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions postgres-appliance/configure_spilo.py
Expand Up @@ -18,6 +18,7 @@

PROVIDER_AWS = "aws"
PROVIDER_GOOGLE = "google"
PROVIDER_OPENSTACK = "openstack"
PROVIDER_LOCAL = "local"
PROVIDER_UNSUPPORTED = "unsupported"
USE_KUBERNETES = os.environ.get('KUBERNETES_SERVICE_HOST') is not None
Expand Down Expand Up @@ -210,16 +211,21 @@ def deep_update(a, b):

def get_provider():
try:
logging.info("Figuring out my environment (Google? AWS? Local?)")
logging.info("Figuring out my environment (Google? AWS? Openstack? Local?)")
r = requests.get('http://169.254.169.254', timeout=2)
if r.headers.get('Metadata-Flavor', '') == 'Google':
return PROVIDER_GOOGLE
else:
r = requests.get('http://169.254.169.254/latest/meta-data/ami-id') # should be only accessible on AWS
if r.ok:
return PROVIDER_AWS
else:
return PROVIDER_UNSUPPORTED
try:
r = requests.get('http://169.254.169.254/openstack/latest/meta_data.json') # accessible on Openstack, will fail on AWS
if r.ok:
return PROVIDER_OPENSTACK
except Exception:
r = requests.get('http://169.254.169.254/latest/meta-data/ami-id') # is accessible from both AWS and Openstack, Possiblity of misidentification if previous try fails
if r.ok:
return PROVIDER_AWS
else:
return PROVIDER_UNSUPPORTED
except (requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError):
logging.info("Could not connect to 169.254.169.254, assuming local Docker setup")
return PROVIDER_LOCAL
Expand All @@ -240,8 +246,8 @@ def get_instance_metadata(provider):
mapping = {'zone': 'zone'}
if not USE_KUBERNETES:
mapping.update({'id': 'id'})
elif provider == PROVIDER_AWS:
url = 'http://instance-data/latest/meta-data'
elif provider == PROVIDER_AWS or provider == PROVIDER_OPENSTACK:
url = 'http://169.254.169.254/latest/meta-data'
mapping = {'zone': 'placement/availability-zone'}
if not USE_KUBERNETES:
mapping.update({'ip': 'local-ipv4', 'id': 'instance-id'})
Expand Down Expand Up @@ -506,6 +512,7 @@ def main():

provider = os.environ.get('DEVELOP', '').lower() in ['1', 'true', 'on'] and PROVIDER_LOCAL or get_provider()
placeholders = get_placeholders(provider)
logging.info('Looks like your running %s', provider )

if provider == PROVIDER_LOCAL and not USE_KUBERNETES:
write_etcd_configuration(placeholders)
Expand Down

0 comments on commit 8b04274

Please sign in to comment.