-
Notifications
You must be signed in to change notification settings - Fork 0
/
BotoConnection.py
executable file
·202 lines (162 loc) · 8.25 KB
/
BotoConnection.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/python
import time, sys, argparse, textwrap, YAML
import boto.ec2 as EC2
class BotoConnection(object):
def __init__(self, configDict, userId=778499506662):
if type(configDict) is not dict:
sys.exit("Error with BotoConnection initilization.\nMake sure the input data is dictionary type.")
self.paramDict = configDict
self.userId = userId
def __createEC2Connection(self):
regionName = self.paramDict['regionName']
awsId = self.paramDict['awsIDs'][0]
awsKey = self.paramDict['awsIDs'][1]
return EC2.connect_to_region(regionName, aws_access_key_id=awsId, aws_secret_access_key=awsKey)
# NOTE: TODO: rework method here
def isInstanceAvailable(self, ec2conn=None):
if ec2conn == None:
ec2conn = self.__createEC2Connection()
runningFilter = {'instance-state-name':'running'} # only running states
running = ec2conn.get_all_instances(filters=runningFilter)
if len(running) < self.paramDict['ec2MaxInstances']:
return True
return False
def areInstancesRunning(self, ec2conn=None):
if ec2conn == None:
ec2conn = self.__createEC2Connection()
runningFilter = {'instance-state-name':'running'} # only running states
running = ec2conn.get_all_instances(filters=runningFilter)
if len(running) == 0:
return True
return False
def getDefaultingValue(self, key, instDict):
if instDict and key in instDict:
return instDict[key]
try: # this is the else case
return self.paramDict[key]
except:
sys.exit("Neither the config yaml nor the job yaml have a value for the '%s' key" % key)
def getInstance(self, instDict=None, ec2conn=None):
if ec2conn == None:
ec2conn = self.__createEC2Connection()
ami = ec2conn.get_image( self.getDefaultingValue('amiID', instDict) )
sshKeyName = self.getDefaultingValue('sshKeyName', instDict)
securityGroups = self.getDefaultingValue('securityGroups', instDict)
instanceSize = self.getDefaultingValue('instanceSize', instDict)
# NOTE: need to catch "boto.exception.EC2ResponseError: EC2ResponseError: 400 Bad Request"
reservation = ami.run(key_name=sshKeyName, security_groups=securityGroups, instance_type=instanceSize)
curInstance = reservation.instances[-1]
while curInstance.state != 'running':
# if pending, sleep then update
if str(curInstance.state) == 'pending':
time.sleep(self.paramDict['InstPollPeriod'])
curInstance.update()
#else issue
# set the instance so that shutting it down will terminate the instance
curInstance.modify_attribute("instanceInitiatedShutdownBehavior","terminate")
return str(curInstance.id), str(curInstance.public_dns_name)
# NOTE consider non-blocking version
def isInstanceReady(self, instanceId, ec2conn=None):
if ec2conn == None:
ec2conn = self.__createEC2Connection()
instanceFilter = { 'instance-id' : instanceId } # only running states
response = ec2conn.get_all_instances(filters=instanceFilter)
while response == None: # cloud says who?
time.sleep(5) # wait and ask again
response = ec2conn.get_all_instances(filters=instanceFilter)
# get last instance in reservation
inst = response[0].instances[-1]
while inst.state == 'pending':
time.sleep(5) # wait and check state again
inst = ec2conn.get_all_instances(filters=instanceFilter)[0].instances[-1]
return
def terminateInstance(self, instanceId, stopOnly=False, ec2conn=None):
if ec2conn == None:
ec2conn = self.__createEC2Connection()
if stopOnly:
ec2conn.stop_instances([instanceId])
else:
ec2conn.terminate_instances([instanceId])
self.cleanUpVolumes(ec2conn)
def cleanUpVolumes(self, ec2conn):
try:
for v in ec2conn.get_all_volumes():
if str(v.status) == 'available':
v.delete()
except:
print 'Error Cleaning Up Volumes'
def createImage(self, instanceId, name, desc="Created by Linnaeus", ec2conn=None):
""" Creates an image of the instance blocking until img ready """
if ec2conn == None:
ec2conn = self.__createEC2Connection()
newAmi = ec2conn.create_image(instanceId, name, desc)
# get img info and wait till it is ready
img = ec2conn.get_image(newAmi)
while img.state == 'pending':
time.sleep(15)
img = ec2conn.get_image(newAmi) #img.update()?
return newAmi
def listImages(self, ec2conn=None):
""" Creates an image of the instance blocking until img ready """
if ec2conn == None:
ec2conn = self.__createEC2Connection()
for img in ec2conn.get_all_images(owners=[self.userId]):
print "%s: %s" % (img.name, img.id)
def deregisterImage(self, ami, ec2conn=None, deleteSnapshot=True):
""" Creates an image of the instance blocking until img ready """
if ec2conn == None:
ec2conn = self.__createEC2Connection()
try:
ec2conn.deregister_image(ami, delete_snapshot=deleteSnapshot)
# look for other snapshtos associate with ami
for snap in ec2conn.get_all_snapshots(owner=str(self.userId)):
if ami in snap.description:
snap.delete()
except AttributeError:
sys.exit("Could not deregister the given AMI.\nCheck value and retry.")
def listImages(args):
yamlConfig = YAML.load(args.yaml)
botoConn = BotoConnection(yamlConfig)
botoConn.listImages()
def deregisterAMI(args):
yamlConfig = YAML.load(args.yaml)
botoConn = BotoConnection(yamlConfig)
botoConn.deregisterImage(args.ami)
def startUpInstance(args):
yamlConfig = YAML.load(args.yaml)
botoConn = BotoConnection(yamlConfig)
if args.ami:
botoConn.getInstance({'amiID':args.ami})
else:
botoConn.getInstance()
def createImage(args):
yamlConfig = YAML.load(args.yaml)
botoConn = BotoConnection(yamlConfig)
newAMI = botoConn.createImage(args.instId, args.name)
print "Image created. The new ami follows: %s" % newAMI
if __name__ == "__main__":
desc = '''\
Wrapper for BotoConnection.'''
parser = argparse.ArgumentParser( formatter_class=argparse.RawTextHelpFormatter,
description=textwrap.dedent(desc) )
subparser = parser.add_subparsers() #(help='sub-command help')
# NOTE: Since not meant to be stand alone, dep on JobManager's config yaml ?
default_yaml_config = 'config/JobManager.yaml'
list_parser = subparser.add_parser('list', help="List EC2 images")
list_parser.add_argument('-yaml', type=str, default=default_yaml_config, help="YAML file used to configure manager.")
list_parser.set_defaults(func=listImages)
deregister_parser = subparser.add_parser('deregister', help="Deregister EC2 image")
deregister_parser.add_argument('-yaml', type=str, default=default_yaml_config, help="YAML file used to configure manager.")
deregister_parser.add_argument('ami', type=str, help="Image AMI being deregistered")
deregister_parser.set_defaults(func=deregisterAMI)
start_parser = subparser.add_parser('start', help="Start EC2 image")
start_parser.add_argument('-yaml', type=str, default=default_yaml_config, help="YAML file used to configure manager.")
start_parser.add_argument('-ami', type=str, default=None, help="Image AMI being deregistered")
start_parser.set_defaults(func=startUpInstance)
create_parser = subparser.add_parser('createImage', help="Create EC2 image")
create_parser.add_argument('-yaml', type=str, default=default_yaml_config, help="YAML file used to configure manager.")
create_parser.add_argument('instId', type=str, help="Instance Id for instance being used as template for image")
create_parser.add_argument('name', type=str, help="Name for image")
create_parser.set_defaults(func=createImage)
args = parser.parse_args()
args.func(args)