summaryrefslogtreecommitdiff
path: root/ick2/workapi.py
blob: c054f0a0401061c2dd82c6ae9cacb8f6d9aa0088 (plain)
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
# 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 <http://www.gnu.org/licenses/>.


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

                build_obj = ick2.Build(build)
                graph = build_obj.get_graph()
                action_id = self._pick_next_action(graph)
                if action_id is None:  # pragma: no cover
                    return {}

                graph.set_action_status(action_id, 'building')
                action = graph.get_action(action_id)

                doing = {
                    'build_id': build_id,
                    'build_number': build['build_number'],
                    'worker': worker_id,
                    'project': build['project'],
                    'parameters': build['parameters'],
                    'action_id': action_id,
                    'step': 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 _pick_next_action(self, graph):
        action_ids = graph.find_actions('ready')
        if not action_ids:  # pragma: no cover
            return None
        return action_ids[0]

    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:
                build_obj = ick2.Build(build)
                graph = build_obj.get_graph()
                doing = worker.get('doing', {})
                self._check_work_update(doing, update)
                self._append_to_build_log(update)
                action_id = doing['action_id']

                if exit_code is not None:
                    if exit_code == 0:
                        graph.set_action_status(action_id, 'done')
                        graph.unblock()
                        if not graph.has_more_to_do():
                            build_obj.set_status('done')
                            build['status'] = 0
                    elif exit_code is not None:
                        graph.set_action_status(action_id, 'failed')
                        build_obj.set_status('failed')
                        build['status'] = 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, '')
                log['log'] = log.get('log', '') + text

    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