Skip to content

Instantly share code, notes, and snippets.

@JohnCEarls
Created December 29, 2013 22:25
Show Gist options
  • Save JohnCEarls/8175530 to your computer and use it in GitHub Desktop.
Save JohnCEarls/8175530 to your computer and use it in GitHub Desktop.
Get the regions by name for ec2 connection Also a script to rsync with other nodes in starcluster using the alias tag.
import boto.ec2
def get_region(region_name):
for region in boto.ec2.regions():
if region_name == region.name:
return region
conn = boto.connect_ec2(region=get_region( 'us-east-1'))
#!/usr/bin/env python
import sys
import boto
import boto.ec2
import boto.utils
import re
import logging
import subprocess
def get_region(region_name):
for region in boto.ec2.regions():
if region_name == region.name:
logging.info("node region[%s]" % region.name)
return region
def get_my_alias():
identity = boto.utils.get_instance_identity()
conn = boto.connect_ec2(region=get_region( identity['document']['region']))
mytags = conn.get_all_tags(filters={'resource-id':identity['document']['instanceId'], 'key':'alias'})
for tag in mytags:
if tag.name == 'alias':
logging.info("my alias[%s]" % tag.value)
return tag.value
def get_nodes():
nodes = []
my_alias = get_my_alias()
identity = boto.utils.get_instance_identity()
conn = boto.connect_ec2(region=get_region( identity['document']['region']))
m = re.match(r'(?P<cluster>\w*-?)(master|node\d\d\d)', my_alias)
if m:
mycluster = m.groupdict()['cluster']
aliases = conn.get_all_tags(filters={'resource-type':'instance', 'key':'alias'})
for alias in aliases:
my_str = mycluster + r'((master)|(node\d\d\d))'
if re.match(my_str, alias.value) and not alias.value == my_alias :
nodes.append(alias.value)
logging.info( "Cluster nodes [%s]" % ' ,'.join( nodes ))
return nodes
def rsync_home( nodes ):
for node in nodes:
node_str = 'sgeadmin@%s:~/' % node
command = ['rsync', '-avz', '~/', node_str]
logging.info("Command: %s" % ' '.join(command))
result = subprocess.check_output(' '.join(command), shell=True)
logging.info("Response:")
logging.info("=" * 50)
logging.info(result)
if __name__ == '__main__':
logging.basicConfig(filename='rsync.log',level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment