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
|
# 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 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': ick2.VersionAPI,
'/builds': ick2.BuildsAPI,
'/logs': ick2.LogAPI,
'/projects': ProjectAPI,
'/work': ick2.WorkAPI,
'/workers': ick2.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 ProjectAPI(ick2.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', []),
}
|