summaryrefslogtreecommitdiff
path: root/ick2/controllerapi.py
blob: 5cf5d64b17f8085c8d662cf7ad2cdd4b951eda8c (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
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# Copyright (C) 2017  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 apifw


import ick2


class ControllerAPI:

    def __init__(self):
        self._state = ick2.ControllerState()

    def get_state_directory(self):
        return self._state.get_state_directory()

    def set_state_directory(self, dirname):
        self._state.set_state_directory(dirname)

    def find_missing_route(self, missing_path):  # pragma: no cover
        apis = {
            '/version': VersionAPI,
            '/builds': BuildsAPI,
            '/projects': ProjectAPI,
            '/work': WorkAPI,
            '/workers': WorkerAPI,
        }

        routes = []
        for path in apis:
            self._state.load_resources(path[1:])
            api = apis[path](self._state)
            routes.extend(api.get_routes(path))
        ick2.log.log('info', msg_texg='Found routes', routes=routes)
        return routes


class APIbase:  # pragma: no cover

    def __init__(self, state):
        self._state = state

    def get_routes(self, path):
        return [
            {
                'method': 'POST',
                'path': path,
                'callback': self.POST(self.create),
            },
            {
                'method': 'GET',
                'path': path,
                'callback': self.GET(self.list),
            },
            {
                'method': 'GET',
                'path': '{}/<name>'.format(path),
                'callback': self.GET(self.show),
            },
            {
                'method': 'PUT',
                'path': '{}/<name>'.format(path),
                'callback': self.PUT(self.update),
            },
            {
                'method': 'DELETE',
                'path': '{}/<name>'.format(path),
                'callback': self.DELETE(self.delete),
            },
        ]

    def GET(self, callback):
        def wrapper(content_type, body, **kwargs):
            ick2.log.log(
                'trace', msg_text='GET called', kwargs=kwargs,
                content_type=content_type, body=body)
            try:
                if 'raw_uri_path' in kwargs:
                    del kwargs['raw_uri_path']
                    body = callback(**kwargs)
            except ick2.NotFound as e:
                return not_found(e)
            return OK(body)
        return wrapper

    def POST(self, callback):
        def wrapper(content_type, body, **kwargs):
            ick2.log.log(
                'trace', msg_text='POST called', kwargs=kwargs,
                content_type=content_type, body=body)
            body = callback(body)
            ick2.log.log('trace', msg_text='returned body', body=repr(body))
            return created(body)
        return wrapper

    def PUT(self, callback):
        def wrapper(content_type, body, **kwargs):
            ick2.log.log(
                'trace', msg_text='PUT called', kwargs=kwargs,
                content_type=content_type, body=body)
            if 'raw_uri_path' in kwargs:
                del kwargs['raw_uri_path']
            try:
                body = callback(body, **kwargs)
            except ick2.NotFound as e:
                return not_found(e)
            except ick2.WrongPipelineStatus as e:
                ick2.log.log(
                    'error',
                    msg_text='Wrong state for pipeline',
                    exception=str(e))
                return bad_request(e)
            ick2.log.log('trace', msg_text='returned body', body=repr(body))
            return OK(body)
        return wrapper

    def DELETE(self, callback):
        def wrapper(content_type, body, **kwargs):
            ick2.log.log(
                'trace', msg_text='DELETE called', kwargs=kwargs,
                content_type=content_type, body=body)
            try:
                if 'raw_uri_path' in kwargs:
                    del kwargs['raw_uri_path']
                body = callback(**kwargs)
            except ick2.NotFound as e:
                return not_found(e)
            return OK(body)
        return wrapper

    def create(self, body):
        raise NotImplementedError()

    def update(self, body, name):
        raise NotImplementedError()

    def delete(self, name):
        raise NotImplementedError()

    def list(self):
        raise NotImplementedError()

    def show(self, name):
        raise NotImplementedError()


class VersionAPI(APIbase):

    def __init__(self, state):
        super().__init__(state)

    def get_routes(self, path):  # pragma: no cover
        return [
            {
                'method': 'GET',
                'path': path,
                'callback': self.GET(self.get_version),
            }
        ]

    def get_version(self):
        return {'version': ick2.__version__}

    def create(self, *args):  # pragma: no cover
        pass

    def update(self, *args):  # pragma: no cover
        pass

    def delete(self, *args):  # pragma: no cover
        pass

    def list(self):  # pragma: no cover
        pass

    def show(self, *args):  # pragma: no cover
        pass


class ResourceApiBase(APIbase):

    def __init__(self, type_name, state):
        super().__init__(state)
        self._type_name = type_name

    def list(self):
        return {
            self._type_name: self._state.get_resources(self._type_name),
        }

    def show(self, name):
        return self._state.get_resource(self._type_name, name)

    def create(self, body):
        return self._state.add_resource(
            self._type_name, self.get_resource_name(body), body)

    def get_resource_name(self, resource):  # pragma: no cover
        raise NotImplementedError

    def update(self, body, name):
        return self._state.update_resource(self._type_name, name, body)

    def delete(self, name):
        self._state.remove_resource(self._type_name, name)


class WorkerAPI(ResourceApiBase):  # pragma: no cover

    def __init__(self, state):
        super().__init__('workers', state)

    def get_resource_name(self, resource):
        return resource['worker']


class BuildsAPI(ResourceApiBase):  # pragma: no cover

    def __init__(self, state):
        super().__init__('builds', state)

    def get_resource_name(self, resource):
        return resource['build']

    def create(self, body):  # pragma: no cover
        raise MethodNotAllowed('Creating builds directly is not allowed')

    def update(self, body, name):  # pragma: no cover
        raise MethodNotAllowed('Updating builds directly is not allowed')


class ProjectAPI(ResourceApiBase):

    def __init__(self, state):
        super().__init__('projects', state)

    def get_resource_name(self, resource):
        return resource['project']

    def get_routes(self, path):  # pragma: no cover
        return super().get_routes(path) + self.get_pipeline_routes(path)

    def get_pipeline_routes(self, path):  # pragma: no cover
        pipeline_path = '{}/<project>/pipelines/<pipeline>'.format(path)
        builds_path = '{}/<project>/builds'.format(path)
        return [
            {
                'method': 'GET',
                'path': pipeline_path,
                'callback': self.GET(self.get_pipeline),
            },
            {
                'method': 'PUT',
                'path': pipeline_path,
                'callback': self.PUT(self.set_pipeline_callback),
            },
            {
                'method': 'GET',
                'path': builds_path,
                'callback': self.GET(self.get_builds),
            },
        ]

    def get_pipeline(self, project, pipeline):
        p = self._state.get_resource(self._type_name, project)
        for pl in p['pipelines']:
            if pl['name'] == pipeline:
                return {
                    'status': pl.get('status', 'idle'),
                }
        raise ick2.NotFound()

    def set_pipeline_callback(
            self, body, project, pipeline):  # pragma: no cover
        return self.set_pipeline(body['status'], project, pipeline)

    def set_pipeline(self, state, project, pipeline):
        allowed_changes = {
            'idle': 'triggered',
            'triggered': 'building',
            'building': 'idle',
        }
        p = self._state.get_resource(self._type_name, project)
        for pl in p['pipelines']:
            if pl['name'] == pipeline:
                old_state = pl.get('status', 'idle')
                if allowed_changes[old_state] != state:
                    raise ick2.WrongPipelineStatus(state)
                pl['status'] = state
                self._state.update_resource(self._type_name, project, p)
                return {'status': state}
        raise ick2.NotFound()

    def get_builds(self, project):
        p = self._state.get_resource(self._type_name, project)
        return {
            'project': project,
            'builds': p.get('builds', []),
        }


class WorkAPI(APIbase):

    def __init__(self, state):
        super().__init__(state)
        self._type_name = 'work'

    def get_routes(self, path):  # pragma: no cover
        return [
            {
                'method': 'GET',
                'path': '{}/<worker>'.format(path),
                'callback': self.GET(self.get_work),
            },
            {
                'method': 'POST',
                'path': path,
                'callback': self.POST(self.update_work),
            },
        ]

    def get_work(self, worker):
        worker_state = self._get_worker(worker)
        if not worker_state.get('doing'):
            project, pipeline = self._pick_triggered_pipeline()
            if project is None:
                doing = {}
            else:
                pipeline['status'] = 'building'
                self._update_project(project)

                build_id = self._start_build(project, pipeline, worker)

                index = 0
                doing = {
                    'build_id': build_id,
                    'worker': worker,
                    'project': project['project'],
                    'pipeline': pipeline['name'],
                    'step': pipeline['actions'][index],
                    'step_index': index,
                }

            worker_state = {
                'worker': worker,
                'doing': doing,
            }
            self._update_worker(worker_state)

        return worker_state['doing']

    def _get_worker(self, worker):  # pragma: no cover
        try:
            return self._state.get_resource('workers', worker)
        except ick2.NotFound:
            return {
                'worker': worker,
            }

    def _update_worker(self, worker_state):
        self._state.update_resource(
            'workers', worker_state['worker'], worker_state)

    def _pick_triggered_pipeline(self):
        projects = self._get_projects()
        for project in projects:
            for pipeline in project['pipelines']:
                if pipeline.get('status') == 'triggered':
                    return project, pipeline
        return None, None

    def _get_projects(self):
        return self._state.get_resources('projects')

    def _update_project(self, project):
        self._state.update_resource('projects', project['project'], project)

    def update_work(self, update):
        if 'worker' not in update:  # pragma: no cover
            raise BadUpdate('no worker specified')

        worker_state = self._get_worker(update['worker'])
        doing = worker_state.get('doing', {})
        self._check_work_update(doing, update)

        project, pipeline = self._get_pipeline(
            update['project'], update['pipeline'])
        self._append_to_build_log(update)

        ick2.log.log(
            'trace',
            msg_text='xxx update_work',
            update=update,
            project=project,
            pipeline=pipeline,
            doing=doing)

        if update.get('exit_code') == 0:
            ick2.log.log('trace', msg_texg='xxx finishing step')
            index = doing['step_index'] + 1
            actions = pipeline['actions']
            if index >= len(actions):
                pipeline['status'] = 'idle'
                doing = {}
                self._finish_build(update)
            else:
                doing['step_index'] = index
                doing['step'] = actions[index]
            self._update_project(project)

            worker_state = {
                'worker': update['worker'],
                'doing': doing,
            }
            self._update_worker(worker_state)

    def _check_work_update(self, doing, update):  # pragma: no cover
        must_match = ['worker', 'project', 'pipeline', 'build_id']
        for name in must_match:
            if name not in update:
                raise BadUpdate('{} not specified'.format(name))
            if doing.get(name) != update[name]:
                raise BadUpdate(
                    '{} differs from current work: {} vs {}'.format(
                        name, doing.get(name), update[name]))

    def _get_pipeline(self, project, pipeline):  # pragma: no cover
        projects = self._get_projects()
        for p in projects:
            for pl in p['pipelines']:
                if pl.get('name') == pipeline:
                    return p, pl
        raise ick2.NotFound()

    def _start_build(self, project, pipeline, worker):
        ick2.log.log('info', msg_text='Starting new build')
        build_id = 1
        build = {
            'build_id': build_id,
            'worker': worker,
            'project': project['project'],
            'pipeline': pipeline['name'],
            'status': 'building',
        }
        self._state.add_resource('builds', str(build_id), build)
        return build_id

    def _append_to_build_log(self, update):
        pass

    def _finish_build(self, update):
        build = self._state.get_resource('builds', str(update['build_id']))
        build['status'] = update['exit_code']
        self._state.update_resource('builds', str(update['build_id']), build)

    def create(self, *args, **kwargs):  # pragma: no cover
        pass

    def update(self, *args, **kwargs):  # pragma: no cover
        pass

    def list(self, *args, **kwargs):  # pragma: no cover
        pass

    def show(self, *args, **kwargs):  # pragma: no cover
        pass

    def delete(self, *args, **kwargs):  # pragma: no cover
        pass


class BadUpdate(Exception):  # pragma: no cover

    def __init__(self, how):
        super().__init__('Work update is BAD: {}'.format(how))


class MethodNotAllowed(Exception):  # pragma: no cover

    def __init__(self, wat):
        super().__init__(wat)


def response(status_code, body, headers):  # pragma: no cover
    obj = {
        'status': status_code,
        'body': body,
        'headers': headers,
    }
    return apifw.Response(obj)


def OK(body):  # pragma: no cover
    headers = {
        'Content-Type': 'application/json',
    }
    return response(apifw.HTTP_OK, body, headers)


def not_found(error):  # pragma: no cover
    headers = {
        'Content-Type': 'text/plain',
    }
    return response(apifw.HTTP_NOT_FOUND, str(error), headers)


def bad_request(error):  # pragma: no cover
    headers = {
        'Content-Type': 'text/plain',
    }
    return response(apifw.HTTP_BAD_REQUEST, str(error), headers)


def created(body):  # pragma: no cover
    headers = {
        'Content-Type': 'application/json',
    }
    return response(apifw.HTTP_CREATED, body, headers)