Created
January 31, 2014 20:50
-
-
Save neilk/8742851 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 os | |
import sys | |
import getopt | |
from sys import stderr | |
import boto.ec2 | |
import boto.rds | |
_rds = None | |
def short_usage(): | |
print >>stderr, """Usage: rds-host [-k KEY] [-s SECRET] [-r REGION] [-t TAG] [NAME] | |
rds-host mydbinstance => | |
Try `rds-host --help' for more information.""" | |
def full_usage(): | |
print >>stderr, """Usage: rds-host [-k KEY] [-s SECRET] [-r REGION] [-t TAG] [-i INSTANCE_ID] [NAME] | |
Prints server host name. | |
--help display this help and exit | |
-k, --aws-key KEY Amazon rds Key, defaults to ENV[AWS_ACCESS_KEY_ID] | |
-s, --aws-secret SECRET Amazon rds Secret, defaults to ENV[AWS_SECRET_ACCESS_KEY] | |
-r, --region REGION Amazon rds Region, defaults to us-east-1 or ENV[AWS_RDS_REGION] | |
-t, --tag TAG Tag name for searching, defaults to 'Name'""" | |
def rds_active_instances(instance_name): | |
instances = [] | |
dbinstances = _rds.get_all_dbinstances() | |
for dbinstance in dbinstances: | |
print dbinstance | |
""" | |
instance_name = dbinstance | |
if instance.public_dns_name: | |
pair = (instance_name, instance.public_dns_name) | |
instances.append(pair) | |
""" | |
return instances | |
def main(argv): | |
try: | |
opts, args = getopt.getopt(argv, "hLk:s:r:", | |
["help", "aws-key=", "aws-secret=", "region="]) | |
except getopt.GetoptError, err: | |
print >>sys.stderr, err | |
short_usage() | |
sys.exit(2) | |
aws_key = os.environ.get("AWS_ACCESS_KEY_ID") | |
aws_secret = os.environ.get("AWS_SECRET_ACCESS_KEY") | |
region = os.environ.get("AWS_RDS_REGION") | |
for opt, arg in opts: | |
if opt in ("-h", "--help"): | |
full_usage() | |
sys.exit() | |
elif opt in("-k", "--aws-key"): | |
aws_key = arg | |
elif opt in("-s", "--aws-secret"): | |
aws_secret = arg | |
elif opt in ("-r", "--region"): | |
region = arg | |
if not aws_key or not aws_secret: | |
if not aws_key: | |
print >>sys.stderr,\ | |
"AWS_ACCESS_KEY_ID not set in environment and not",\ | |
"specified by --aws-key KEY or -k KEY" | |
if not aws_secret: | |
print >>sys.stderr,\ | |
"AWS_SECRET_ACCESS_KEY not set in environment and not",\ | |
"specified by --aws-secret SECRET or -s SECRET" | |
short_usage() | |
sys.exit(2) | |
region = region and boto.ec2.get_region(region, | |
aws_access_key_id=aws_key, | |
aws_secret_access_key=aws_secret) | |
global _rds | |
_rds = boto.rds.RDSConnection(aws_key, aws_secret, region=region, debug=1) | |
argc = len(args) | |
instance_name = None | |
if argc == 1: | |
instance_name = args[0] | |
elif argc > 1: | |
print >>stderr, "Warning: more than one name given" | |
sys.exit(1) | |
return | |
instances = rds_active_instances(instance_name) | |
numinstances = len(instances) | |
print "---" | |
if numinstances == 1: | |
print instances[0][1] # [1] = dns | |
sys.exit(0) | |
return | |
elif numinstances == 0 or numinstances > 1: | |
for pair in sorted(instances, key=lambda p: p[0]): | |
print "%s\t%s" % pair | |
sys.exit(0) | |
return | |
else: | |
print >>stderr, 'Unable to match "%s"' % args[0] | |
short_usage() | |
sys.exit(1) | |
return | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Author
neilk
commented
Jan 31, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment