forked from autolab/Tango
-
Notifications
You must be signed in to change notification settings - Fork 13
/
tango.py
executable file
·355 lines (316 loc) · 14.3 KB
/
tango.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#
# Tango is a job management service that manages requests for jobs to
# be run in virtual machines. Tango consists of five main components:
#
# 1. The Restful API: This is the interface for Tango that receives
# requests from clients via HTTP. AddJob requests are converted
# into a form that the tangoServer understands and then passed on
# to an instance of the tangoServer class. (restful-tango/*)
#
# 2. The TangoServer Class: This is a class that accepts addJob requests
# from the restful server. Job requests are validated and placed in
# a job queue. This class also implements various administrative
# functions to manage instances of tangoServer. (tango.py)
#
# 3. The Job Manager: This thread runs continuously. It watches the job
# queue for new job requests. When it finds one it creates a new
# worker thread to handle the job, and assigns a preallocated or new VM
# to the job. (jobQueue.py)
#
# 4. Workers: Worker threads do the actual work of running a job. The
# process of running a job is broken down into the following steps:
# (1) initializeVM, (2) waitVM, (3) copyIn, (4) runJob, (5)
# copyOut, (6) destroyVM. The actual process involved in
# each of those steps is handled by a virtual machine management
# system (VMMS) such as Local or Amazon EC2. Each job request
# specifies the VMMS to use. The worker thread dynamically loads
# and uses the module written for that particular VMMS. (worker.py
# and vmms/*.py)
#
# 5. The Preallocator: Virtual machines can preallocated in a pool in
# order to reduce response time. Each virtual machine image has its
# own pool. Users control the size of each pool via an external HTTP
# call. Each time a machine is assigned to a job and removed from
# the pool, the preallocator creates another instance and adds it
# to the pool. (preallocator.py)
import threading, logging, time, stat, re, os
from datetime import datetime
from preallocator import Preallocator
from jobQueue import JobQueue
from jobManager import JobManager
from tangoObjects import TangoJob
from config import Config
class TangoServer:
""" TangoServer - Implements the API functions that the server accepts
"""
def __init__(self):
self.daemon = True
vmms = None
if Config.VMMS_NAME == "tashiSSH":
from vmms.tashiSSH import TashiSSH
vmms = TashiSSH()
elif Config.VMMS_NAME == "ec2SSH":
from vmms.ec2SSH import Ec2SSH
vmms = Ec2SSH()
elif Config.VMMS_NAME == "localDocker":
from vmms.localDocker import LocalDocker
vmms = LocalDocker()
elif Config.VMMS_NAME == "distDocker":
from vmms.distDocker import DistDocker
vmms = DistDocker()
self.preallocator = Preallocator({Config.VMMS_NAME: vmms})
self.jobQueue = JobQueue(self.preallocator)
if not Config.USE_REDIS:
# creates a local Job Manager if there is no persistent
# memory between processes. Otherwise, JobManager will
# be initiated separately
JobManager(self.jobQueue).start()
logging.basicConfig(
filename=Config.LOGFILE,
format="%(levelname)s|%(asctime)s|%(name)s|%(message)s",
level=Config.LOGLEVEL,
)
self.start_time = time.time()
self.log = logging.getLogger("TangoServer")
self.log.info("Starting Tango server")
def addJob(self, job):
""" addJob - Add a job to the job queue
"""
Config.job_requests += 1
self.log.debug("Received addJob request")
ret = self.__validateJob(job, self.preallocator.vmms)
self.log.info("Done validating job %s" % (job.name))
if ret == 0:
return self.jobQueue.add(job)
else:
self.jobQueue.addDead(job)
return -1
def delJob(self, id, deadjob):
""" delJob - Delete a job
@param id: Id of job to delete
@param deadjob - If 0, move the job from the live queue to the
dead queue. If non-zero, remove the job from the dead queue
and discard. Use with caution!
"""
self.log.debug("Received delJob(%d, %d) request" % (id, deadjob))
return self.jobQueue.delJob(id, deadjob)
def getJobs(self, item):
""" getJobs - Return the list of live jobs (item == 0) or the
list of dead jobs (item == -1).
"""
try:
self.log.debug("Received getJobs(%s) request" % (item))
if item == -1: # return the list of dead jobs
return self.jobQueue.deadJobs.values()
elif item == 0: # return the list of live jobs
return self.jobQueue.liveJobs.values()
else: # invalid parameter
return []
except Exception as e:
self.log.debug("getJobs: %s" % str(e))
def preallocVM(self, vm, num):
""" preallocVM - Set the pool size for VMs of type vm to num
"""
self.log.debug("Received preallocVM(%s,%d)request"
% (vm.name, num))
try:
vmms = self.preallocator.vmms[vm.vmms]
if not vm or num < 0:
return -2
if vm.image not in vmms.getImages():
self.log.error("Invalid image name")
return -3
(name, ext) = os.path.splitext(vm.image)
vm.name = name
self.preallocator.update(vm, num)
return 0
except Exception as err:
self.log.error("preallocVM failed: %s" % err)
return -1
def getVMs(self, vmms_name):
""" getVMs - return the list of VMs managed by the service vmms_name
"""
self.log.debug("Received getVMs request(%s)" % vmms_name)
try:
if vmms_name in self.preallocator.vmms:
vmms_inst = self.preallocator.vmms[vmms_name]
return vmms_inst.getVMs()
else:
return []
except Exception as err:
self.log.error("getVMs request failed: %s" % err)
return []
def delVM(self, vmName, id):
""" delVM - delete a specific VM instance from a pool
"""
self.log.debug("Received delVM request(%s, %d)" % (vmName, id))
try:
if not vmName or vmName == "" or not id:
return -1
return self.preallocator.destroyVM(vmName, id)
except Exception as err:
self.log.error("delVM request failed: %s" % err)
return -1
def getPool(self, vmName):
""" getPool - Return the current members of a pool and its free list
"""
self.log.debug("Received getPool request(%s)" % (vmName))
try:
if not vmName or vmName == "":
return []
result = self.preallocator.getPool(vmName)
return ["pool_size=%d" % len(result["pool"]),
"free_size=%d" % len(result["free"]),
"pool=%s" % result["pool"],
"free=%s" % result["free"]]
except Exception as err:
self.log.error("getPool request failed: %s" % err)
return []
def getInfo(self):
""" getInfo - return various statistics about the Tango daemon
"""
stats = {}
stats['elapsed_secs'] = time.time() - self.start_time;
stats['job_requests'] = Config.job_requests
stats['job_retries'] = Config.job_retries
stats['waitvm_timeouts'] = Config.waitvm_timeouts
stats['runjob_timeouts'] = Config.runjob_timeouts
stats['copyin_errors'] = Config.copyin_errors
stats['runjob_errors'] = Config.runjob_errors
stats['copyout_errors'] = Config.copyout_errors
stats['num_threads'] = threading.activeCount()
return stats
#
# Helper functions
#
def resetTango(self, vmms):
""" resetTango - resets Tango to a clean predictable state and
ensures that it has a working virtualization environment. A side
effect is that also checks that each supported VMMS is actually
running.
"""
self.log.debug("Received resetTango request.")
try:
# For each supported VMM system, get the instances it knows about,
# and kill those in the current Tango name space.
for vmms_name in vmms:
vobj = vmms[vmms_name]
vms = vobj.getVMs()
self.log.debug("Pre-existing VMs: %s" % [vm.name for vm in vms])
namelist = []
for vm in vms:
if re.match("%s-" % Config.PREFIX, vm.name):
vobj.destroyVM(vm)
# Need a consistent abstraction for a vm between
# interfaces
namelist.append(vm.name)
if namelist:
self.log.warning("Killed these %s VMs on restart: %s" %
(vmms_name, namelist))
for _, job in self.jobQueue.liveJobs.iteritems():
if not job.isNotAssigned():
job.makeUnassigned()
self.log.debug("job: %s, assigned: %s" %
(str(job.name), str(job.assigned)))
except Exception as err:
self.log.error("resetTango: Call to VMMS %s failed: %s" %
(vmms_name, err))
os._exit(1)
def __validateJob(self, job, vmms):
""" validateJob - validate the input arguments in an addJob request.
"""
errors = 0
# If this isn't a Tango job then bail with an error
if (not isinstance(job, TangoJob)):
return -1
# Every job must have a name
if not job.name:
self.log.error("validateJob: Missing job.name")
job.appendTrace("%s|validateJob: Missing job.name" %
(datetime.utcnow().ctime()))
errors += 1
# Check the virtual machine field
if not job.vm:
self.log.error("validateJob: Missing job.vm")
job.appendTrace("%s|validateJob: Missing job.vm" %
(datetime.utcnow().ctime()))
errors += 1
else:
if not job.vm.image:
self.log.error("validateJob: Missing job.vm.image")
job.appendTrace("%s|validateJob: Missing job.vm.image" %
(datetime.utcnow().ctime()))
errors += 1
else:
vobj = vmms[Config.VMMS_NAME]
imgList = vobj.getImages()
if job.vm.image not in imgList:
self.log.error("validateJob: Image not found: %s" %
job.vm.image)
job.appendTrace("%s|validateJob: Image not found: %s" %
(datetime.utcnow().ctime(), job.vm.image))
errors += 1
else:
(name, ext) = os.path.splitext(job.vm.image)
job.vm.name = name
if not job.vm.vmms:
self.log.error("validateJob: Missing job.vm.vmms")
job.appendTrace("%s|validateJob: Missing job.vm.vmms" %
(datetime.utcnow().ctime()))
errors += 1
else:
if job.vm.vmms not in vmms:
self.log.error("validateJob: Invalid vmms name: %s" % job.vm.vmms)
job.appendTrace("%s|validateJob: Invalid vmms name: %s" %
(datetime.utcnow().ctime(), job.vm.vmms))
errors += 1
# Check the output file
if not job.outputFile:
self.log.error("validateJob: Missing job.outputFile")
job.appendTrace("%s|validateJob: Missing job.outputFile" % (datetime.utcnow().ctime()))
errors += 1
else:
if not os.path.exists(os.path.dirname(job.outputFile)):
self.log.error("validateJob: Bad output path: %s", job.outputFile)
job.appendTrace("%s|validateJob: Bad output path: %s" %
(datetime.utcnow().ctime(), job.outputFile))
errors += 1
# Check for max output file size parameter
if not job.maxOutputFileSize:
self.log.debug("validateJob: Setting job.maxOutputFileSize "
"to default value: %d bytes", Config.MAX_OUTPUT_FILE_SIZE)
job.maxOutputFileSize = Config.MAX_OUTPUT_FILE_SIZE
# Check the list of input files
hasMakefile = False
for inputFile in job.input:
if not inputFile.localFile:
self.log.error("validateJob: Missing inputFile.localFile")
job.appendTrace("%s|validateJob: Missing inputFile.localFile" %
(datetime.utcnow().ctime()))
errors += 1
else:
if not os.path.exists(os.path.dirname(job.outputFile)):
self.log.error("validateJob: Bad output path: %s", job.outputFile)
job.appendTrace("%s|validateJob: Bad output path: %s" %
(datetime.utcnow().ctime(), job.outputFile))
errors += 1
if inputFile.destFile == 'Makefile':
hasMakefile = True
# Check if input files include a Makefile
if not hasMakefile:
self.log.error("validateJob: Missing Makefile in input files.")
job.appendTrace("%s|validateJob: Missing Makefile in input files." % (datetime.utcnow().ctime()))
errors+=1
# Check if job timeout has been set; If not set timeout to default
if not job.timeout or job.timeout <= 0:
self.log.debug("validateJob: Setting job.timeout to"
" default config value: %d secs", Config.RUNJOB_TIMEOUT)
job.timeout = Config.RUNJOB_TIMEOUT
# Any problems, return an error status
if errors > 0:
self.log.error("validateJob: Job rejected: %d errors" % errors)
job.appendTrace("%s|validateJob: Job rejected: %d errors" %
(datetime.utcnow().ctime(), errors))
return -1
else:
return 0