@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Drain reviewed demand tasks through the shared candidate environment."""
|
||||
import json
|
||||
|
||||
import release_handoff as h
|
||||
|
||||
cd = h.cd
|
||||
coordinator = h.coordinator
|
||||
|
||||
|
||||
def release_published_slot(compose):
|
||||
directory = compose.parent
|
||||
ready = cd.read_json(cd.immutable_file(directory / 'ready.json'))
|
||||
task = h.assistant.task_record(ready['task'])
|
||||
published = cd.metadata(task, 'cd_release') or {}
|
||||
if task['status']['id'] != cd.PUBLISHED or not all(published.get(k) == ready[k] for k in ('sha', 'bundle')):
|
||||
return False
|
||||
project = 'kg-batch-' + ready['bundle'][:20]
|
||||
cd.require(ready['mode'] == 'acceptance' and cd.hash_file(directory / 'bundle/release.json') == ready['bundle'],
|
||||
'published QA archive changed')
|
||||
cd.require(set(ready['containers']) == {'api', 'web', 'worker'}, 'unexpected candidate service set')
|
||||
ids = []
|
||||
for role, name in ready['containers'].items():
|
||||
cd.require(name == project + '-' + role, 'candidate name mismatch')
|
||||
info = json.loads(cd.run(['docker', 'inspect', name], 'inspect QA container'))[0]
|
||||
labels = info['Config']['Labels']
|
||||
cd.require(labels.get('com.docker.compose.project') == project and info['Image'] == ready['images'][role],
|
||||
'candidate container identity changed')
|
||||
ids.append(info['Id'])
|
||||
cd.run(['docker', 'stop', *ids], 'retire published QA environment', timeout=90)
|
||||
info = json.loads(cd.run(['docker', 'inspect', *ids], 'verify stopped QA containers'))
|
||||
cd.require(len(info) == len(ids) and not any(row['State']['Running'] for row in info), 'QA slot still running')
|
||||
compose.rename(directory / 'compose.released.json')
|
||||
return True
|
||||
|
||||
|
||||
def in_flight(state):
|
||||
for path in state.glob('*.dispatch.json'):
|
||||
claim = cd.read_json(cd.immutable_file(path))
|
||||
key = path.name.split('.')[0]
|
||||
if (state / key / 'ready.json').is_file():
|
||||
continue
|
||||
if claim['state'] != 'queued':
|
||||
return True
|
||||
run = h.candidate.read_forge(h.candidate.API + '/actions/runs/' + str(claim['run']))
|
||||
if run['status'] != 'completed' or run.get('conclusion') == 'success':
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def drain():
|
||||
state = coordinator.STATE
|
||||
waiting = state / 'waiting'
|
||||
waiting.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
with cd.deploy_lock(waiting / 'queue.lock'):
|
||||
entries = sorted(waiting.glob('*.json'), key=lambda p: (p.stat().st_mtime_ns, p.name))
|
||||
entries = [(p, cd.read_json(cd.immutable_file(p))) for p in entries]
|
||||
entries = [(p, e) for p, e in entries if e['state'] == 'waiting_for_slot']
|
||||
if not entries:
|
||||
return 'idle'
|
||||
with cd.deploy_lock(state / 'runtime.lock'):
|
||||
for compose in state.glob('*/compose.json'):
|
||||
if not release_published_slot(compose):
|
||||
return 'occupied'
|
||||
if in_flight(state):
|
||||
return 'building'
|
||||
path, entry = entries[0]
|
||||
task = h.assistant.task_record(entry['task'])
|
||||
try:
|
||||
sha, _ = h.resolve(task, entry['event'])
|
||||
cd.require(sha == entry['sha'], 'review changed before building')
|
||||
live = h.assistant.worker.main_site_sha()
|
||||
if h.candidate.git('merge-base', live, sha, cwd=h.assistant.SOURCE, allowed=(0, 1)) == sha:
|
||||
notice = f'主站当前版本 `{live}` 已包含候选提交 `{sha}`。请核对该需求的发布与验收记录。'
|
||||
entry.update(state='included_in_live')
|
||||
else:
|
||||
result = h.prepare_from_review(task, entry['event'])
|
||||
entry.update(state='dispatched', result=result)
|
||||
notice = '候选构建已排队。' + result['url']
|
||||
except cd.CDError as exc:
|
||||
entry.update(state='blocked', reason=str(exc))
|
||||
notice = '候选准备已停止。' + str(exc)
|
||||
cd.record(path, entry)
|
||||
coordinator.add_comment(task['id'], 'queue-result ' + entry['event'], notice)
|
||||
return entry['state']
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(drain())
|
||||
Reference in New Issue
Block a user