Skip to content

Instantly share code, notes, and snippets.

@dakra
Created April 25, 2016 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dakra/fe7d90cdf9ace250d39639dd621187df to your computer and use it in GitHub Desktop.
Save dakra/fe7d90cdf9ace250d39639dd621187df to your computer and use it in GitHub Desktop.
#!/bin/env python
# make sure to be logged in with `gcloud init`
# Add to /etc/hosts:
# python create_gcloud_hosts_entries.py | sudo tee -a /etc/hosts
from oauth2client.client import GoogleCredentials
from googleapiclient import discovery
PROJECT = 'magnetic-icon-88807'
credentials = GoogleCredentials.get_application_default()
compute = discovery.build('compute', 'v1', credentials=credentials)
def list_instances(compute, zone, project=PROJECT):
result = compute.instances().list(project=project, zone=zone).execute()
return result.get('items', [])
def get_zones(compute, project=PROJECT):
zones = compute.zones().list(project=project).execute()
return [z['name'] for z in zones['items']]
print('# atomx google cloud entries\n')
for zone in get_zones(compute):
for instance in list_instances(compute, zone):
name = instance['name']
ip = instance['networkInterfaces'][0]['accessConfigs'][0].get('natIP')
if not ip:
print('# skipping {name}'.format(name=name))
continue
print('{ip} {name} {name}.atomx {name}.atomx.com'.format(ip=ip, name=name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment