Skip to content

Commit

Permalink
Add ability to generate correct hosts file
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarjog committed May 18, 2016
1 parent 9e05cfa commit 559fc56
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions genhosts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python
import socket
import sys
import yaml
from boto3.session import Session


def genhosts(elbip, sysdomain, outfile=sys.stdout):
SYS_PREFIXES = [
'console',
'uaa',
'apps',
'login']

print >>outfile, "#"*16, "Generated for /etc/hosts by cfawsinit", "#"*16

print >>outfile, elbip, sysdomain
for prefix in SYS_PREFIXES:
print >>outfile, elbip, prefix+"."+sysdomain
print >>outfile, "#"*16, "Generated for /etc/hosts by cfawsinit", "#"*16


def get_elbip(elb, stackname):
lbname = stackname + "-pcf-elb"
resp = elb.describe_load_balancers(
LoadBalancerNames=[lbname])
if len(resp.get('LoadBalancerDescriptions', [])) == 0:
raise Exception(lbname + " Loadbalacer could not be found")
dnsname = resp['LoadBalancerDescriptions'][0]['DNSName']
return socket.gethostbyname(dnsname)


def get_args():
import argparse
argp = argparse.ArgumentParser()
argp.add_argument('--profile')
argp.add_argument('--stack-name')
argp.add_argument('--outfile')
argp.add_argument('--prepared-cfg')
argp.add_argument('--system-domain')
argp.add_argument('--region', default='us-east-1')
return argp


def fix_args(args):
if args.prepared_cfg is not None:
opts = yaml.load(open(args.prepared_cfg, 'rt'))
args.system_domain = args.system_domain or opts["system_domain"]
args.stack_name = args.stack_name or opts["stack-name"]
args.region = opts["region"]

if args.outfile is not None:
args.outfile = open(args.outfile, "wt")


def main(argv):
args = get_args().parse_args(argv)

if args.prepared_cfg is None and\
args.system_domain is None:
print ("Either --prepared-cfg or "
"(--system-domain and --stack-name) are required")
return -1
fix_args(args)
session = Session(profile_name=args.profile, region_name=args.region)
elb = session.client("elb")

genhosts(
get_elbip(elb, args.stack_name),
args.system_domain,
args.outfile)


if __name__ == "__main__":
import sys
sys.exit(main(sys.argv[1:]))

0 comments on commit 559fc56

Please sign in to comment.