Skip to content

Commit 1f78b4b

Browse files
committed
added -r (region) option to kill_instance command
1 parent 3c0787e commit 1f78b4b

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

bin/kill_instance

100644100755
Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
#!/usr/bin/env python
2-
def kill_instance(instance_id):
3-
"""Kill an instance given it's instance ID"""
4-
import boto
5-
ec2 = boto.connect_ec2()
6-
print "Stopping instance: %s" % instance_id
7-
ec2.terminate_instances([instance_id])
2+
3+
import sys
4+
from optparse import OptionParser
5+
6+
import boto
7+
from boto.ec2 import regions
8+
9+
10+
11+
def kill_instance(region, ids):
12+
"""Kill an instances given it's instance IDs"""
13+
# Connect the region
14+
ec2 = boto.connect_ec2(region=region)
15+
for instance_id in ids:
16+
print "Stopping instance: %s" % instance_id
17+
ec2.terminate_instances([instance_id])
818

919

1020
if __name__ == "__main__":
11-
import sys
12-
kill_instance(sys.argv[1])
21+
parser = OptionParser(usage="kill_instance [-r] id [id ...]")
22+
parser.add_option("-r", "--region", help="Region (default us-east-1)", dest="region", default="us-east-1")
23+
(options, args) = parser.parse_args()
24+
if not args:
25+
parser.print_help()
26+
sys.exit(1)
27+
for r in regions():
28+
if r.name == options.region:
29+
region = r
30+
break
31+
else:
32+
print "Region %s not found." % options.region
33+
sys.exit(1)
34+
35+
kill_instance(region, args)

0 commit comments

Comments
 (0)