summaryrefslogtreecommitdiff
path: root/ickweb/app.py
blob: f7ba7b720155b25d9ee06b432fe56bd68f161000 (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
import json
import logging
import logging.handlers
import urllib
import urllib.parse

import bottle
import requests
import yaml


client_id = 'ickweb'

COOKIE = 'ickweb-session'

handler = logging.handlers.SysLogHandler()
logging.basicConfig(handlers=[handler])

def create_app(our_url, controller, client_secret):
    app = bottle.Bottle()
    api = API(controller)

    parts = urllib.parse.urlparse(our_url)
    cookie_path = parts.path

    @app.route('/favicon.ico')
    def favicon():
        raise bottle.HTTPError(status=404)

    @app.route('/web')
    def root():
        cookie = get_cookie()
        if cookie is None:
            return bottle.template('login')
        api.set_token(cookie)
        return front_page()

    def get_cookie():
        return bottle.request.get_cookie(COOKIE)

    def front_page():
        return bottle.template('index', token=api.get_token())

    @app.route('/web/login')
    def login():
        scopes = [
            'openid',
            'uapi_version_get',
            'uapi_status_get',
            'uapi_projects_get',
            'uapi_projects_post',
            'uapi_projects_id_get',
            'uapi_projects_id_put',
            'uapi_projects_id_delete',
            'uapi_pipelines_get',
            'uapi_pipelines_id_get',
            'uapi_pipelines_id_delete',
            'uapi_projects_id_status_get',
            'uapi_projects_id_status_put',
            'uapi_pipelines_post',
            'uapi_pipelines_id_put',
            'uapi_builds_get',
            'uapi_builds_id_get',
            'uapi_builds_id_delete',
            'uapi_logs_get',
            'uapi_logs_id_get',
            'uapi_blobs_id_get',
            'uapi_workers_get',
            'uapi_workers_id_get',
            'uapi_notify_post',
        ]
        params = {
            'response_type': 'code',
            'scope': ' '.join(scopes),
            'client_id': client_id,
            'state': 'FIXME',
            'redirect_uri': '{}/callback'.format(our_url),
        }
        url = '{}/auth?{}'.format(controller, urllib.parse.urlencode(params))
        headers = {
            'Location': url,
        }
        print('params:', params)
        return bottle.HTTPResponse(status=302, headers=headers)

    @app.route('/web/logout')
    def logout():
        bottle.response.delete_cookie(COOKIE, path=cookie_path)
        bottle.redirect('/web')

    @app.route('/web/projects')
    def projects():
        projects = api.get_projects()
        return bottle.template('projects', projects=projects)

    @app.route('/web/projects/<name:path>')
    def show_project(name):
        project = api.get_project(name)
        params = {
            'project': project,
            'as_yaml': as_yaml(project),
        }
        return bottle.template('project', **params)

    @app.route('/web/builds')
    def builds():
        builds = api.get_builds()
        return bottle.template('builds', builds=builds)

    @app.route('/web/logs/<buildid:path>')
    def show_log(buildid):
        log = api.get_log(buildid)
        return bottle.template('log', buildid=buildid, log=log)

    @app.route('/web/callback')
    def callback():
        logging.debug('/callback called')
        code = bottle.request.query['code']
        print('code:', code)

        path = '/token'
        params = {
            'grant_type': 'authorization_code',
            'code': code,
        }
        auth = (client_id, client_secret)

        print('requesting token using code')
        r = api.POST(path, params, auth)

        obj = r.json()
        token = obj['access_token']
        print('got access token:', token)

        bottle.response.set_cookie(COOKIE, token, path=cookie_path)
        bottle.redirect('/web')

    return app


def as_yaml(obj):
    return yaml.safe_dump(obj, indent=4, default_flow_style=False)


def string_representer(dumper, data):
    style = None
    if '\n' in data:
        style = '|'
    return dumper.represent_scalar('tag:yaml.org,2002:str', data, style=style)


yaml.add_representer(str, string_representer, Dumper=yaml.SafeDumper)


class API:

    def __init__(self, api_url):
        self._url = api_url
        self.set_token(None)

    def set_token(self, token):
        self._token = token

    def get_token(self):
        return self._token

    def POST(self, path, params, auth):
        url = self.url(path)
        print('POST: url:', url)
        headers = {}
        token = self.get_token()
        if token is not None:
            headers['Authorization'] = 'Bearer {}'.format(self.get_token())
        r = requests.post(url, headers=headers, data=params, auth=auth)
        if not r.ok:
            raise bottle.HTTPError(status=r.status_code)
        return r

    def get_projects(self):
        url = self.url('/projects')
        headers = {
            'Authorization': 'Bearer {}'.format(self.get_token())
        }
        r = requests.get(url, headers=headers)
        if not r.ok:
            raise bottle.HTTPError(status=r.status_code)
        obj = r.json()
        return list(sorted(obj['projects'], key=lambda p: p['project']))

    def get_project(self, name):
        url = self.url('/projects', name)
        headers = {
            'Authorization': 'Bearer {}'.format(self.get_token())
        }
        r = requests.get(url, headers=headers)
        if not r.ok:
            raise bottle.HTTPError(status=r.status_code)
        return r.json()

    def get_builds(self):
        url = self.url('/builds')
        headers = {
            'Authorization': 'Bearer {}'.format(self.get_token())
        }
        r = requests.get(url, headers=headers)
        if not r.ok:
            raise bottle.HTTPError(status=r.status_code)
        obj = r.json()
        return list(sorted(obj['builds'], key=lambda p: p['build_id']))

    def get_log(self, buildid):
        url = self.url('/logs', buildid)
        headers = {
            'Authorization': 'Bearer {}'.format(self.get_token())
        }
        r = requests.get(url, headers=headers)
        if not r.ok:
            raise bottle.HTTPError(status=r.status_code)
        return r.text

    def url(self, *parts):
        return '{}{}'.format(self._url, '/'.join(parts))