Skip to content

Instantly share code, notes, and snippets.

@Glamdring
Last active July 26, 2020 09:40
Show Gist options
  • Save Glamdring/d317ff191a223d2bcf7c92fa5fdf3476 to your computer and use it in GitHub Desktop.
Save Glamdring/d317ff191a223d2bcf7c92fa5fdf3476 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import sys
from boto import ec2, cloudformation
import boto.utils
def get_group_info(ec2conn, instance_id) :
reservations = ec2conn.get_all_instances(instance_id)
instance = [i for r in reservations for i in r.instances][0]
if instance.tags.has_key('aws:autoscaling:groupName'):
print 'Using autoscaling group name to define group info'
autoscale_group = instance.tags['aws:autoscaling:groupName']
private_ip = instance.private_ip_address
filters = {'tag:aws:autoscaling:groupName': '%s*' % autoscale_group, 'instance-state-name': 'running' }
reservations = ec2conn.get_all_instances(filters=filters)
instances = [i for r in reservations for i in r.instances]
sorted_instances = sorted(instances, key=lambda i: (i.launch_time, i.id))
first_node = sorted_instances[0]
node_number = [si.id for si in sorted_instances].index(instance.id) + 1
return private_ip, first_node, len(sorted_instances), autoscale_group, node_number
else:
return None
def is_stack_complete(stack_name, region_name):
conn = cloudformation.connect_to_region(region_name)
stack_details = conn.describe_stacks(stack_name)[0]
return stack_details.stack_status == 'CREATE_COMPLETE'
instance_data = boto.utils.get_instance_metadata()
instance_id = instance_data["instance-id"]
# connect to region of the current instance rather than default of us-east-1
zone = instance_data['placement']['availability-zone']
region_name = zone[:-1]
print "Connecting to region %s" % region_name
ec2conn = ec2.connect_to_region(region_name)
private_ip, first_node, nodes_total, stack_id, node_number = get_group_info(ec2conn, instance_id)
stack_complete = is_stack_complete(sys.argv[1], region_name)
print "Stack complete? %s" % stack_complete
print "Instance belongs to %s. Finding first node" % stack_id
print "Node number for this machine is %s." % node_number
print "Node count for this cluster is %s." % nodes_total
print "First node in the cluster is %s" % first_node.private_ip_address
print "Private IP for this machine is %s." % private_ip
with open('/etc/cassandra/conf/cassandra.yaml', 'r+') as file:
data = file.read()
data = data.replace('${private_ip}', private_ip)
data = data.replace('${seeds}', first_node.private_ip_address)
# if stack is not complete, auto_bootstrap should be false, because this means a new cluster is formed
data = data.replace('${auto_bootstrap}', str(stack_complete).lower())
file.seek(0)
file.write(data)
file.truncate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment