Navigation Menu

Skip to content

Commit

Permalink
Add region option or use EC2_REGION from environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
barnybug committed Oct 25, 2011
1 parent 1173b68 commit 8ae2eeb
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions bin/ec2-host
Expand Up @@ -6,25 +6,26 @@ import getopt

from sys import stderr

import boto
import boto, boto.ec2


_ec2 = None


def short_usage():
print >>stderr, """Usage: ec2-host [-k KEY] [-s SECRET] [NAME]
print >>stderr, """Usage: ec2-host [-k KEY] [-s SECRET] [-r REGION] [NAME]
ec2-host django8 => ec2-53-19-113-121.compute-1.amazonaws.com
Try `ssh-distillery --help' for more information."""


def full_usage():
print >>stderr, """Usage: ec2-host [-k KEY] [-s SECRET] [NAME]
print >>stderr, """Usage: ec2-host [-k KEY] [-s SECRET] [-r REGION] [NAME]
Prints server host name.
--help display this help and exit
-k, --aws-key KEY Amazon EC2 Key, defaults to ENV[AWS_ACCESS_KEY_ID]
-s, --aws-secret SECRET Amazon EC2 Secret, defaults to ENV[AWS_SECRET_ACCESS_KEY]"""
-s, --aws-secret SECRET Amazon EC2 Secret, defaults to ENV[AWS_SECRET_ACCESS_KEY]
-r, --region REGION Amazon EC2 Region, defaults to ENV[EC2_REGION] or otherwise us-east-1"""


def list_instances():
Expand Down Expand Up @@ -62,15 +63,16 @@ def ec2_instances(**kwargs):

def main(argv):
try:
opts, args = getopt.getopt(argv, "hLk:s:",
["help", "aws-key", "aws-secret"])
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("EC2_REGION", 'us-east-1')

for opt, arg in opts:
if opt in ("-h", "--help"):
Expand All @@ -80,6 +82,8 @@ def main(argv):
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:
Expand All @@ -96,7 +100,7 @@ def main(argv):
sys.exit(2)

global _ec2
_ec2 = boto.connect_ec2(aws_key, aws_secret)
_ec2 = boto.ec2.connect_to_region(region, aws_access_key_id=aws_key, aws_secret_access_key=aws_secret)

argc = len(args)
if argc == 0:
Expand Down

0 comments on commit 8ae2eeb

Please sign in to comment.