Skip to content

Instantly share code, notes, and snippets.

@neilk
Created January 31, 2014 20:50
Show Gist options
  • Save neilk/8742851 to your computer and use it in GitHub Desktop.
Save neilk/8742851 to your computer and use it in GitHub Desktop.
#!/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:])
@neilk
Copy link
Author

neilk commented Jan 31, 2014

neilk@neilk ...projects/rds-host master* $ bin/rds-host 
Traceback (most recent call last):
  File "bin/rds-host", line 127, in <module>
    main(sys.argv[1:])
  File "bin/rds-host", line 104, in main
    instances = rds_active_instances(instance_name)
  File "bin/rds-host", line 36, in rds_active_instances
    dbinstances = _rds.get_all_dbinstances()
  File "/Library/Python/2.7/site-packages/boto/rds/__init__.py", line 134, in get_all_dbinstances
    [('DBInstance', DBInstance)])
  File "/Library/Python/2.7/site-packages/boto/connection.py", line 1141, in get_list
    raise self.ResponseError(response.status, response.reason, body)
boto.exception.BotoServerError: BotoServerError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>NoSuchVersion</Code><Message>The requested version (2013-09-09) of service AmazonEC2 does not exist</Message></Error></Errors><RequestID>e597e208-621b-4048-a132-c4e2d7d25ad8</RequestID></Response>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment