-
Notifications
You must be signed in to change notification settings - Fork 6
/
ec2host
executable file
·134 lines (101 loc) · 3.74 KB
/
ec2host
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
import os
import sys
import getopt
from sys import stderr
import boto.ec2
_ec2 = None
def short_usage():
print >>stderr, """Usage: ec2-host [-k KEY] [-s SECRET] [-r REGION] [-t TAG] [NAME]
ec2-host django8 => ec2-53-19-113-121.compute-1.amazonaws.com
Try `ec2-ssh --help' for more information."""
def full_usage():
print >>stderr, """Usage: ec2-host [-k KEY] [-s SECRET] [-r REGION] [-t TAG] [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]
-r, --region REGION Amazon EC2 Region, defaults to us-east-1 or ENV[AWS_EC2_REGION]
-t, --tag TAG Tag name for searching, defaults to 'Name'"""
def ec2_active_instances(label_tag, filters):
instances = []
kwargs = {}
if filters:
kwargs['filters'] = filters
reservations = _ec2.get_all_instances(**kwargs)
for reservation in reservations:
for instance in reservation.instances:
instance_name = instance.tags.get(label_tag)
if instance.public_dns_name:
pair = (instance_name, instance.public_dns_name)
instances.append(pair)
elif instance.ip_address:
pair = (instance_name, instance.ip_address)
instances.append(pair)
return instances
def main(argv):
try:
opts, args = getopt.getopt(argv, "hk:s:r:t:",
["help", "aws-key=", "aws-secret=", "region=", "tag="])
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_EC2_REGION")
tag = "Name"
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
elif opt in ("-t", "--tag"):
tag = 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 _ec2
_ec2 = boto.ec2.connection.EC2Connection(aws_key, aws_secret, region=region)
argc = len(args)
filters = {}
if argc == 1:
filters['tag:%s' % tag] = args[0]
elif argc > 1:
print >>stderr, "Warning: more than one name given"
sys.exit(1)
return
instances = ec2_active_instances(tag, filters)
numinstances = len(instances)
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:])