summaryrefslogtreecommitdiff
path: root/ick2/workapi.py
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2018-04-19 22:31:01 +0300
committerLars Wirzenius <liw@liw.fi>2018-04-19 22:31:01 +0300
commitf11a8ecd409f280759af7227db52b1e87e389092 (patch)
tree191dc9e4dda2444fb65bcf097056ef37d5a685bc /ick2/workapi.py
parent04e899ded340bc0fd3afd6b0a62dff22c182e735 (diff)
downloadick2-f11a8ecd409f280759af7227db52b1e87e389092.tar.gz
Change: trigger a project, build all pipelines in the project
Diffstat (limited to 'ick2/workapi.py')
-rw-r--r--ick2/workapi.py69
1 files changed, 21 insertions, 48 deletions
diff --git a/ick2/workapi.py b/ick2/workapi.py
index f1352d0..fa9c2bc 100644
--- a/ick2/workapi.py
+++ b/ick2/workapi.py
@@ -22,7 +22,7 @@ class WorkAPI(ick2.APIbase):
super().__init__(state)
self._workers = ick2.Workers(state)
self._projects = ick2.Projects(state)
- self._pinstances = ick2.PipelineInstances(state)
+ self._ps = ick2.ProjectStatus(state)
self._builds = ick2.Builds(state)
self._type_name = 'work'
@@ -43,15 +43,13 @@ class WorkAPI(ick2.APIbase):
def get_work(self, worker, **kwargs):
worker_state = self._workers.get_worker(worker)
if not worker_state.get('doing'):
- project, pipeline = self._pick_triggered_pipeline()
+ project = self._pick_triggered_project()
if project is None:
doing = {}
else:
- pipeline['status'] = 'building'
- self._update_pipeline(project, pipeline)
+ self._set_project_status(project['project'], 'building')
- build_id, build_no = self._start_build(
- project, pipeline, worker)
+ build_id, build_no = self._start_build(project, worker)
self._start_log(build_id)
build = self._get_build(build_id)
actions = build['actions']
@@ -62,7 +60,6 @@ class WorkAPI(ick2.APIbase):
'build_number': build_no,
'worker': worker,
'project': project['project'],
- 'pipeline': pipeline['pipeline'],
'parameters': project.get('parameters', {}),
'step': actions[current_action],
'log': '/logs/{}'.format(build_id),
@@ -86,27 +83,20 @@ class WorkAPI(ick2.APIbase):
old_build_no=old_build_no, build_no=build_no)
return build_no
- def _pick_triggered_pipeline(self):
+ def _pick_triggered_project(self):
projects = self._projects.get_projects()
for project in projects:
- for name in project['pipelines']:
- pl = self._pinstances.get_instance(project['project'], name)
- if pl.get('status') == 'triggered':
- pt = self._state.get_resource('pipelines', name)
- return project, pt
- return None, None
-
- def _update_pipeline(self, project, pipeline_instance):
- project_name = project['project']
- pipeline_name = pipeline_instance['pipeline']
- try:
- self._pinstances.update_instance(
- project_name, pipeline_name, pipeline_instance)
- except ick2.NotFound: # pragma: no cover
- self._pinstances.add_instance(
- project_name, pipeline_name, pipeline_instance)
-
- def _start_build(self, project, pipeline, worker):
+ ps = self._ps.get_instance(project['project'])
+ if ps.get('status') == 'triggered':
+ return project
+ return None
+
+ def _set_project_status(self, project_name, status):
+ ps = self._ps.get_instance(project_name)
+ ps['status'] = status
+ self._ps.update_instance(project_name, ps)
+
+ def _start_build(self, project, worker):
build_no = self._pick_build_number(project)
build_id = '{}/{}'.format(project['project'], build_no)
@@ -124,7 +114,6 @@ class WorkAPI(ick2.APIbase):
'log': '/logs/{}'.format(build_id),
'worker': worker,
'project': project['project'],
- 'pipeline': pipeline['pipeline'],
'parameters': parameters,
'status': 'building',
'actions': actions,
@@ -159,8 +148,8 @@ class WorkAPI(ick2.APIbase):
doing = worker_state.get('doing', {})
self._check_work_update(doing, update)
- project, pipeline = self._get_pipeline(
- update['project'], update['pipeline'])
+ project_name = update['project']
+
self._append_to_build_log(update)
exit_code = update.get('exit_code')
@@ -170,8 +159,7 @@ class WorkAPI(ick2.APIbase):
actions = build['actions']
current_action = build['current_action']
if current_action + 1 >= len(actions):
- pipeline['status'] = 'idle'
- self._update_pipeline(project, pipeline)
+ self._set_project_status(project_name, 'idle')
doing = {}
self._finish_build(update)
else:
@@ -188,8 +176,7 @@ class WorkAPI(ick2.APIbase):
elif exit_code is not None:
assert isinstance(exit_code, int)
assert exit_code != 0
- pipeline['status'] = 'idle'
- self._update_pipeline(project, pipeline)
+ self._set_project_status(project_name, 'idle')
self._finish_build(update)
worker_state = {
@@ -199,7 +186,7 @@ class WorkAPI(ick2.APIbase):
self._workers.update_worker(worker_state)
def _check_work_update(self, doing, update): # pragma: no cover
- must_match = ['worker', 'project', 'pipeline', 'build_id']
+ must_match = ['worker', 'project', 'build_id']
for name in must_match:
if name not in update:
raise ick2.BadUpdate('{} not specified'.format(name))
@@ -208,20 +195,6 @@ class WorkAPI(ick2.APIbase):
'{} differs from current work: {} vs {}'.format(
name, doing.get(name), update[name]))
- def _get_pipeline(self, project, pipeline): # pragma: no cover
- projects = [
- p
- for p in self._projects.get_projects()
- if p['project'] == project
- ]
- if not projects:
- raise ick2.NotFound()
- p = projects[0]
- if pipeline not in p['pipelines']:
- raise ick2.NotFound()
- pt = self._state.get_resource('pipelines', pipeline)
- return p, pt
-
def _append_to_build_log(self, update):
build_id = update['build_id']
log = self._state.get_resource('log', str(build_id))