-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworker.py
509 lines (403 loc) · 17 KB
/
worker.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# -*- coding: utf-8 -*-
import glob
import json
import logging as log
import os
import pexpect
import sys
import threading
import time
import yaml
from pathlib import Path
# Local modules
import db
import github
import job
import logger as ibl # Do not confuse this with the logging above
import settings
import status
import utils
###############################################################################
# Pexpect
###############################################################################
last_cd = None
export_history = []
def get_yaml_cmd(yml_iter):
cmd = yml_iter.get('cmd', None)
exp = yml_iter.get('exp', None)
check_ret = yml_iter.get('chkret', "y")
to = yml_iter.get('timeout', 3)
# Force check_ret to None if there is exp.
if exp is not None:
check_ret = None
log.debug("cmd: {}, exp: {}, chkret: {}, timeout: {}".format(
cmd, exp, check_ret, to))
return cmd, exp, check_ret, to
def do_pexpect(child, cmd=None, exp=None, check_retval=None, timeout=5,
error_pos=1):
if exp is not None and check_retval is not None:
log.error("Cannot expect both a string and return value at the time!")
return False
if cmd is not None:
log.debug("Sending: {}".format(cmd))
# Save the last cd command for other build stages
if cmd.startswith("cd "):
global last_cd
last_cd = cmd
if cmd.startswith("export "):
global export_history
export_history.append(cmd)
# Append echo $? to the command in case the user specified that the
# return value from a command should be checked
if check_retval is not None:
child.sendline("{};echo $?".format(cmd))
else:
child.sendline(cmd)
# Start with a clean expect list
e = []
if exp is not None:
# In the yaml file there could be standalone lines or there could be
# a list if expected output.
if isinstance(exp, list):
e = exp + [pexpect.TIMEOUT]
else:
e.append(exp)
e.append(pexpect.TIMEOUT)
elif check_retval is not None:
# A good return value contains a single '0' followed by IBART on next
# line.
e.append('\r\n0\r\nIBART')
e.append(pexpect.TIMEOUT)
# A bad line is any number not starting with '0' followed by IBART on
# the next line.
e.append(r'\r\n[^0]\d*\r\nIBART')
if exp is not None or check_retval is not None:
log.debug("Expecting (exp): {} (timeout={}, error={})".format(
e, timeout, error_pos))
r = child.expect(e, timeout)
log.debug("Got: {} (value >= {} is considered an error)".format(
r, error_pos))
if r >= error_pos:
return False
return True
def export_variables(child, job):
"""This function exports any information that could be used by the one
writing the yaml scripts for a particular job. Typically one want to export
the information coming from GitHub here."""
exported_variables = [
"export PR_SHA1={}".format(job.pr_sha1()),
"export PR_NUMBER={}".format(job.pr_number()),
"export PR_NAME={}".format(job.pr_name()),
"export PR_FULL_NAME={}".format(job.pr_full_name()),
"export PR_CLONE_URL={}".format(job.pr_clone_url()),
"export PR_BRANCH={}".format(job.pr_branch())
]
for e in exported_variables:
child.sendline(e)
child.expect('')
def spawn_pexpect_child(job):
global last_cd
global export_history
if job is None:
log.error("Trying to spawn child with no Job")
return
# In some cases there seems to be left over from PS1 even though we export
# a plain IBART both in the rcfile as well as exporting it. This trick
# by setting TERM seems to resolve that issue.
os.environ["TERM"] = "dumb"
rcfile = '{}/.bashrc'.format(os.getcwd())
child = pexpect.spawnu('/bin/bash', ['--rcfile', rcfile],
encoding='utf-8', codec_errors='ignore')
# Make environment variables available in YAML job definitions.
export_variables(child, job)
# Export previous exports to the new shell
if export_history:
for e in export_history:
child.sendline(e)
# Go to last known 'cd' directory.
# NOTE!!! This must be here after doing the exports, otherwise things like
# $ cd $SOME_VARIABLE will probably fail.
if last_cd is not None:
child.sendline(last_cd)
child.sendline('export PS1="IBART $ "')
r = child.expect(['IBART \$ ', pexpect.TIMEOUT], 2)
if r > 0:
log.error("Could not set PS1\n{}".format(child.before))
return None
return child
def terminate_child(child):
child.close()
def get_job_definitions():
return sorted([jd for jd in glob.glob("jobdefs/*.yaml")])
def run_teardown(yml_config):
rcfile = '--rcfile {}/.bashrc'.format(os.getcwd())
child = pexpect.spawnu('/bin/bash', ['--rcfile', rcfile],
encoding='utf-8', codec_errors='ignore')
try:
yml_iter = yml_config['teardown']
except KeyError:
# Probably no teardown in the config
return
# Stripped down version of do_pexpect, which only errors on timeout.
for i in yml_iter:
c, exp, cr, to = get_yaml_cmd(i)
child.sendline(c)
e = []
if exp is not None:
e.append(exp)
e.append(pexpect.TIMEOUT)
r = child.expect(e, to)
error_pos = 1
log.debug("Got: {} (value >= {} is considered an error)".format(
r, error_pos))
if r >= error_pos:
log.error("Failed doing teardown, cmd: {}".format(c))
child.close()
class JobThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self, job):
super(JobThread, self).__init__()
self._stop_event = threading.Event()
self.job = job
def stop(self):
log.debug("Stopping PR {}".format(self.job.pr_number()))
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
def start_job(self):
jobdefs = get_job_definitions()
# Just local to save some typing further down
payload = self.job.payload
# To prevent old logs from showing up on the web-page, start by
# removing all of them.
ibl.clear_logfiles(payload)
for jd in jobdefs:
log.info("Start clone, build ... sequence for {}".format(self.job))
# Replace .yaml with .zip
full_log_file = Path(jd).name.replace(".yaml", ".zip")
log.debug("full_log_file: {}".format(full_log_file))
with open(jd, 'r') as yml:
yml_config = yaml.safe_load(yml)
# Loop all defined values
for k, logtype in ibl.log2str.items():
try:
yml_iter = yml_config[logtype]
except KeyError:
continue
child = spawn_pexpect_child(self.job)
current_log_file = "{}/{}.log".format(settings.log_dir(),
logtype)
with open(current_log_file, 'w') as f:
child.logfile_read = f
if yml_iter is None:
ibl.store_logfile(payload, current_log_file,
full_log_file)
continue
for i in yml_iter:
log.debug("")
c, e, cr, to = get_yaml_cmd(i)
if not do_pexpect(child, c, e, cr, to):
terminate_child(child)
run_teardown(yml_config)
log.error("job type: {} failed!".format(logtype))
ibl.store_logfile(payload, current_log_file,
full_log_file)
github.update_state(payload, "failure", "Stage {} "
"failed!".format(logtype))
return status.FAIL
if self.stopped():
terminate_child(child)
run_teardown(yml_config)
log.debug("job type: {} cancelled!".format(
logtype))
ibl.store_logfile(payload, current_log_file,
full_log_file)
github.update_state(payload, "failure", "Job was "
"stopped by user (stage {})!"
"".format(logtype))
return status.CANCEL
ibl.store_logfile(payload, current_log_file, full_log_file)
run_teardown(yml_config)
github.update_state(payload, "success", "All good!")
return status.SUCCESS
def run(self):
"""This is the main function for running a complete clone, build, flash
and test job."""
global export_history
current_status = status.d[status.RUNNING]
log.debug("Job/{} : {}".format(current_status, self.job))
time_start = time.time()
pr_id = self.job.pr_id()
pr_sha1 = self.job.pr_sha1()
db.update_job(pr_id, pr_sha1, current_status, "N/A")
github.update_state(self.job.payload, "pending", "Job running!")
current_status = status.d[self.start_job()]
export_history.clear()
running_time = utils.get_running_time(time_start)
log.debug("Job/{} : {} --> {}".format(current_status, self.job,
running_time))
db.update_job(pr_id, pr_sha1, current_status, running_time)
###############################################################################
# Worker
###############################################################################
worker_thread = None
class WorkerThread(threading.Thread):
"""Thread class responsible to adding and running jobs."""
def __init__(self, group=None, target=None, name=None, args=(),
kwargs=None):
threading.Thread.__init__(self, group=group, target=target, name=name)
self.args = args
self.kwargs = kwargs
self.q = []
self.job_dict = {}
self.jt = None
self.lock = threading.Lock()
def user_add(self, pr_id, pr_sha1):
if pr_id is None or pr_sha1 is None:
log.error("Missing pr_id or pr_sha1 when trying to submit user "
"job")
return
with self.lock:
log.info("Got user initiated add {}/{}".format(pr_id, pr_sha1))
payload = db.get_payload_from_pr_id(pr_id, pr_sha1)
if payload is None:
log.error("Didn't find payload for ID:{}".format(pr_id))
return
pr_id_sha1 = "{}-{}".format(pr_id, pr_sha1)
self.q.append(pr_id_sha1)
self.job_dict[pr_id_sha1] = job.Job(payload, True)
db.update_job(pr_id, pr_sha1, status.d[status.PENDING], "N/A")
github.update_state(payload, "pending", "Job added to queue")
def add(self, payload):
"""Responsible of adding new jobs the the job queue."""
if payload is None:
log.error("Missing payload when trying to add job")
return
pr_id = github.pr_id(payload)
pr_number = github.pr_number(payload)
pr_sha1 = github.pr_sha1(payload)
pr_full_name = github.pr_full_name(payload)
with self.lock:
log.info("Got GitHub initiated add {}/{} --> PR#{}".format(
pr_id, pr_sha1, pr_number))
# Check whether the jobs in the current queue touches the same PR
# number as this incoming request does.
for i, elem in enumerate(self.q):
job_in_queue = self.job_dict[elem]
# Remove existing jobs as long as they are not user initiated
# jobs.
if (job_in_queue.pr_number() == pr_number and
job_in_queue.pr_full_name() == pr_full_name):
if not job_in_queue.user_initiated:
log.debug("Non user initiated job found in queue, "
"removing {}".format(elem))
del self.q[i]
db.update_job(job_in_queue.pr_id(),
job_in_queue.pr_sha1(),
status.d[status.CANCEL], "N/A")
github.update_state(job_in_queue.payload,
"failure", "Job cancelled!")
# Check whether current job also should be stopped (i.e, same
# PR, but _not_ user initiated).
if (self.jt is not None and
self.jt.job.pr_number() == pr_number and
self.jt.job.pr_full_name == pr_full_name and not
self.jt.job.user_initiated):
log.debug("Non user initiated job found running, "
"stopping {}".format(self.jt.job))
self.jt.stop()
pr_id_sha1 = "{}-{}".format(pr_id, pr_sha1)
self.q.append(pr_id_sha1)
new_job = job.Job(payload, False)
self.job_dict[pr_id_sha1] = new_job
db.add_build_record(new_job.payload)
db.update_job(pr_id, pr_sha1, status.d[status.PENDING], "N/A")
github.update_state(payload, "pending", "Job added to queue")
def cancel(self, pr_id, pr_sha1):
force_update = True
# Stop pending jobs
for i, elem in enumerate(self.q):
job_in_queue = self.job_dict[elem]
if (job_in_queue.pr_id() == pr_id and
job_in_queue.pr_sha1() == pr_sha1):
log.debug("Got a stop from web {}/{}".format(pr_id, pr_sha1))
del self.q[i]
db.update_job(job_in_queue.pr_id(), job_in_queue.pr_sha1(),
status.d[status.CANCEL], "N/A")
force_update = False
# Stop the running job
if self.jt is not None:
if (self.jt.job.pr_id() == pr_id and
self.jt.job.pr_sha1() == pr_sha1):
log.debug("Got a stop from web {}/{}".format(pr_id, pr_sha1))
self.jt.stop()
force_update = False
# If it wasn't in the queue nor running, then just update the status
if force_update:
db.update_job(pr_id, pr_sha1, status.d[status.CANCEL], "N/A")
payload = db.get_payload_from_pr_id(pr_id, pr_sha1)
github.update_state(payload, "failure", "Job cancelled!")
def run(self):
"""Main function taking care of running all jobs in the job queue."""
while(True):
time.sleep(3)
if len(self.q) > 0:
with self.lock:
pr_id = self.q.pop(0)
job = self.job_dict[pr_id]
log.debug("Handling job: {}".format(pr_id))
self.jt = JobThread(job)
self.jt.start()
self.jt.join()
self.jt = None
return
def initialize_worker_thread():
"""Initialize the main thread responsible for adding and running jobs."""
global worker_thread
# Return if thread is already running.
if worker_thread is not None:
return
worker_thread = WorkerThread()
worker_thread.setDaemon(True)
worker_thread.start()
log.info("Worker thread has been started")
###############################################################################
# Logger
###############################################################################
def initialize_logger():
LOG_FMT = ("[%(levelname)s] %(funcName)s():%(lineno)d %(message)s")
log.basicConfig(
filename=settings.log_file(),
level=log.DEBUG,
format=LOG_FMT)
###############################################################################
# Main
###############################################################################
initialized = False
def initialize():
global initialized
if not initialized:
initialize_logger()
db.initialize()
initialize_worker_thread()
log.info("Initialize done!")
initialized = True
def user_add(pr_id, pr_sha1):
initialize()
worker_thread.user_add(pr_id, pr_sha1)
def add(payload):
initialize()
if payload is None:
log.error("Cannot add job without payload")
return False
worker_thread.add(payload)
return True
def cancel(pr_id, pr_sha1):
initialize()
if pr_id is None or pr_sha1 is None:
log.error("Trying to stop a job without a PR number")
elif worker_thread is None:
log.error("Threads are not initialized")
else:
worker_thread.cancel(pr_id, pr_sha1)