Created
November 23, 2016 11:39
-
-
Save kornysietsma/227adcbbb74565737c259390949353d9 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 | |
try: | |
import boto3 | |
except ImportError: | |
print 'Please install boto3 - either set up a virtualenv, or run: pip install boto3' | |
exit(1) | |
import sys | |
cmd = 'unknown' | |
if len(sys.argv) == 2: | |
name = sys.argv[1] | |
cmd = 'state' | |
elif len(sys.argv) == 3: | |
name = sys.argv[1] | |
cmd = sys.argv[2] | |
else: | |
print 'please specify a name or name filter (i.e. "Foo*") and optionally a single command "state", "start" or "stop". Beware, this might start or stop many machines - be careful! Also filters are case sensitive.' | |
exit(1) | |
ec2 = boto3.resource('ec2') | |
instances = list(ec2.instances.filter( | |
Filters=[{'Name': 'tag:Name', 'Values': [name]}])) | |
if len(instances) == 0: | |
print "can't find any instances" | |
exit(1) | |
for i in instances: | |
name = "unknown" | |
for tag in i.tags: | |
if tag['Key'] == 'Name': | |
name = tag['Value'] | |
if cmd == 'start': | |
i.start() | |
print 'Starting', name, 'state:', i.state['Name'] | |
elif cmd == 'stop': | |
i.stop() | |
print 'Stopping', name, 'state:', i.state['Name'] | |
elif cmd == 'state': | |
print 'Server', name, 'state:', i.state['Name'] | |
else: | |
print "Unknown command:", cmd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment