Created
April 25, 2016 04:25
-
-
Save dakra/fe7d90cdf9ace250d39639dd621187df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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