Skip to content

Instantly share code, notes, and snippets.

@dialtone
Created June 15, 2011 22:04
Show Gist options
  • Save dialtone/1028255 to your computer and use it in GitHub Desktop.
Save dialtone/1028255 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import urllib
import boto
from boto import ec2
def get_seeds(conn, cluster):
all_instances = conn.get_all_instances(filters={"tag:type": "*", "tag:cluster": cluster})
seeds = []
for reservation in all_instances:
for instance in reservation.instances:
if 'seed' in instance.tags.get('type', ''):
seeds.append(instance.private_ip_address)
return ", ".join(seeds)
def main(options, destination):
if not (options.aws_secret_key and options.aws_access_key):
return
os.environ['AWS_ACCESS_KEY_ID'] = options.aws_access_key
os.environ['AWS_SECRET_ACCESS_KEY'] = options.aws_secret_key
s3conn = boto.connect_s3()
template_url = s3conn.generate_url(10, "GET", "bucket", "/config/cassandraconf.tmpl")
template = urllib.urlopen(template_url).read()
conn = ec2.connect_to_region(region_name="us-west-1")
me = conn.get_all_instances([options.instance])[0].instances[0]
filling = dict(me.tags)
filling['hostname'] = me.private_ip_address
filling['seeds'] = get_seeds(conn, me.tags.get('cluster'))
if 'brisk' in me.tags.get('type', ''):
open('/etc/default/brisk', 'wb').write("\nHADOOP_ENABLED=1\n")
filled_template = template % filling
open(destination, 'wb').write(filled_template)
if __name__ == '__main__':
"""
e.g, cassandra_configure.py --secret=<SECRET_KEY> --api=<API_KEY>
"""
from optparse import OptionParser
usage = "usage: %prog [options] <final config destination path>"
parser = OptionParser(usage=usage)
parser.add_option("--secret", action="store", dest="aws_secret_key",
default=os.environ.get('AWS_SECRET_ACCESS_KEY'), help="AWS Secret Access Key")
parser.add_option("--api", action="store", dest="aws_access_key",
default=os.environ.get('AWS_ACCESS_KEY_ID'), help="AWS Access Key")
parser.add_option("--instance", action="store", dest="instance",
default=os.environ.get('INSTANCE_ID'), help="Instance ID of the current instance")
(options, args) = parser.parse_args()
if len(args) != 1:
raise Exception("Need one location for final configuration file destination")
main(options, args[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment