# Copyright (C) 2017-2018 Lars Wirzenius # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . import ick2 class WorkAPI(ick2.APIbase): def __init__(self, state): super().__init__(state) self._trans = ick2.TransactionalState(state) def get_routes(self, path): # pragma: no cover return [ { 'method': 'GET', 'path': '{}'.format(path), 'callback': self.GET(self.get_work), }, { 'method': 'POST', 'path': path, 'callback': self.POST(self.update_work), }, ] def get_work(self, **kwargs): worker_id = self._get_client_id(**kwargs) with self._trans.modify('workers', worker_id) as worker: doing = worker.get('doing') if doing: return doing build_id = self._pick_build(worker_id) if build_id is None: return {} with self._trans.modify('builds', build_id) as build: build['status'] = 'building' build['worker'] = worker_id self._start_log(build_id) actions = build['actions'] current_action = build['current_action'] doing = { 'build_id': build_id, 'build_number': build['build_number'], 'worker': worker_id, 'project': build['project'], 'parameters': build['parameters'], 'step': actions[current_action], 'log': build['log'], } worker.from_dict({ 'worker': worker_id, 'doing': doing, }) return worker['doing'] def _get_client_id(self, **kwargs): claims = kwargs.get('claims', {}) client_id = claims.get('aud') if client_id is None: # pragma: no cover raise ick2.ClientIdMissing() return client_id def _pick_build(self, worker): def on_worker(build): return build.get('worker') == worker def status(build): return build.get('status') def is_building(build): return status(build) == 'building' def is_triggered(build): return status(build) == 'triggered' builds = self._trans.get_resources('builds') return (self._find_build(builds, on_worker, is_building) or self._find_build(builds, is_triggered)) def _find_build(self, builds, *preds): for build in builds: if all(pred(build) for pred in preds): return build['build_id'] return None def _start_log(self, build_id): ick2.log.log('info', msg_text='Starting new log', build_id=build_id) with self._trans.new('log', build_id) as r: r.from_dict({ 'build_id': build_id, 'log': '', }) def update_work(self, update, **kwargs): try: worker_id = update['worker'] build_id = update['build_id'] project_name = update['project'] exit_code = update.get('exit_code') except KeyError as e: # pragma: no cover raise ick2.BadUpdate(str(e)) with self._trans.modify('workers', worker_id) as worker: with self._trans.modify('builds', build_id) as build: doing = worker.get('doing', {}) self._check_work_update(doing, update) self._append_to_build_log(update) if exit_code is not None: if exit_code == 0: action = self._move_to_next_action(build) if action is None: doing = {} self._finish_build(build, exit_code) else: doing['step'] = action worker.from_dict({ 'worker': worker_id, 'doing': doing, }) elif exit_code is not None: self._finish_build(build, exit_code) worker.from_dict({ 'worker': worker_id, 'doing': {}, }) def _check_work_update(self, doing, update): # pragma: no cover must_match = ['worker', 'project', 'build_id'] for name in must_match: if name not in update: raise ick2.BadUpdate('{} not specified'.format(name)) if doing.get(name) is not None and doing.get(name) != update[name]: raise ick2.BadUpdate( '{} differs from current work: {} vs {}'.format( name, doing.get(name), update[name])) def _append_to_build_log(self, update): build_id = update['build_id'] with self._trans.modify('log', build_id) as log: for stream in ['stdout', 'stderr']: text = update.get(stream, '') if text is not None: log['log'] += text def _move_to_next_action(self, build): actions = build['actions'] current_action = build['current_action'] if current_action + 1 >= len(actions): return None index = current_action + 1 build['current_action'] = index return actions[index] def _finish_build(self, build, exit_code): build['status'] = exit_code build['current_action'] = None def create(self, body, **kwargs): # pragma: no cover pass def update(self, body, name, **kwargs): # pragma: no cover pass def list(self, **kwargs): # pragma: no cover pass def show(self, name, **kwargs): # pragma: no cover pass def delete(self, name, **kwargs): # pragma: no cover pass