Created
November 15, 2016 15:56
-
-
Save nazarsa/882c72dbf5ac70c20e9df5a80fbcc839 to your computer and use it in GitHub Desktop.
awsschedule.py
This file contains 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
# instance.py | |
#!/bin/python | |
import argparse | |
import sys | |
import boto.ec2 | |
def check_arg(args=None): | |
parser = argparse.ArgumentParser(description='args : start/start, instance-id') | |
parser.add_argument('-o', '--op', | |
help='operation type', | |
required='True', | |
default='stop') | |
parser.add_argument('-i', '--iid', | |
help='instance id', | |
required='True', | |
default='') | |
results = parser.parse_args(args) | |
return (results.op, | |
results.iid) | |
def manage_instance(op,iid): | |
instance_id = [] | |
instance_id.append(unicode(iid)) | |
conn = boto.ec2.connect_to_region('us-west-1') | |
#print 'conn=',conn | |
if op == 'stop': | |
#print 'stop: instance_id=',instance_id | |
conn.stop_instances(instance_id,False) | |
elif op == 'start': | |
#print 'start: instance_id=',instance_id | |
conn.start_instances(instance_id) | |
else: | |
#print 'else' | |
pass | |
if __name__ == '__main__': | |
''' | |
Input : instance id & operation | |
How it works : should work with cron on srv-01-0001-instance-manager. For example, | |
0 7 * * * /usr/bin/python /home/ubuntu/EC2/instance.py -i i-k172af8d -o start | |
0 11 * * * /usr/bin/python /home/ubuntu/EC2/instance.py -i i-k172af8d -o stop | |
''' | |
op, iid = check_arg(sys.argv[1:]) | |
#print 'op=',op,' iid=',iid | |
manage_instance(op,iid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment