Created
May 30, 2017 14:50
-
-
Save ddaan/8644885118f6d9f93a59c6bc61b12f92 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
#!/usr/bin/env python | |
import sys | |
import requests | |
def get_info(terrible_key, cluster, product=None): | |
if product: | |
url = 'https://terrible.sanomaservices.nl/env/list/{}/{}'.format(cluster, product) | |
else: | |
url = 'https://terrible.sanomaservices.nl/env/list/{}'.format(cluster) | |
return requests.get(url, headers={'terrible-key': terrible_key}) | |
def print_hostnames(environment_info): | |
for env in environment_info: | |
if 'Ec2' in env: | |
for machine in env.get('Ec2', []): | |
print('Host {}-{}-{}'.format( | |
env['InfraJSON'].get('Product'), | |
env['InfraJSON'].get('Environment'), | |
machine['Name']) | |
) | |
print(' Hostname {}'.format(machine['Ip'])) | |
if __name__ == "__main__": | |
if len(sys.argv) == 4: | |
info = get_info(sys.argv[1], sys.argv[2], sys.argv[3]) | |
elif len(sys.argv) == 3: | |
info = get_info(sys.argv[1], sys.argv[2]) | |
else: | |
print('Generate ssh config hostnames for terrible') | |
print('') | |
print('Usage: ./get_host_names.py <terrible_key> <cluster> <product>') | |
print('') | |
print('example: ./get_host_name.py ABCD1234 cluster product >> ~/.ssh/config') | |
sys.exit(2) | |
if info.status_code == 200: | |
print_hostnames(info.json()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment