aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/web/build-server/controller.scm
blob: 911e41d4647cee9b539c33b4b270d011fcc4514a (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
;;; Guix Data Service -- Information about Guix over time
;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
;;;
;;; 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/>.

(define-module (guix-data-service web build-server controller)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 match)
  #:use-module (rnrs bytevectors)
  #:use-module (json)
  #:use-module (fibers)
  #:use-module (guix-data-service utils)
  #:use-module (guix-data-service database)
  #:use-module (guix-data-service substitutes)
  #:use-module (guix-data-service web render)
  #:use-module (guix-data-service web query-parameters)
  #:use-module (guix-data-service web controller)
  #:use-module (guix-data-service jobs load-new-guix-revision)
  #:use-module (guix-data-service model utils)
  #:use-module (guix-data-service model build)
  #:use-module (guix-data-service model build-server)
  #:use-module (guix-data-service model build-status)
  #:use-module (guix-data-service model blocked-builds)
  #:use-module (guix-data-service model nar)
  #:use-module (guix-data-service model build-server-token-seed)
  #:use-module (guix-data-service web util)
  #:use-module (guix-data-service web view html)
  #:use-module (guix-data-service web jobs html)
  #:use-module (guix-data-service web build-server html)
  #:export (build-server-controller))

(define (render-build mime-types
                      build-server-id
                      query-parameters)
  (if (any-invalid-query-parameters? query-parameters)
      (case (most-appropriate-mime-type
             '(application/json text/html)
             mime-types)
        ((application/json)
         (render-json
          `((error . "invalid query"))))
        (else
         (render-html
          #:sxml (view-build query-parameters
                             #f
                             #f))))
      (let* ((derivation-file-name
              (assq-ref query-parameters 'derivation_file_name))
             (build-server-build-id
              (assq-ref query-parameters 'build_server_build_id))
             (build
              (with-resource-from-pool (connection-pool) conn
                (if build-server-build-id
                    (select-build-by-build-server-and-build-server-build-id
                     conn
                     build-server-id
                     build-server-build-id)
                    (select-build-by-build-server-and-derivation-file-name
                     conn
                     build-server-id
                     derivation-file-name)))))
        (if build
            (render-html
             #:sxml
             (view-build query-parameters
                         build
                         (match build
                           ((build-server-url build-server-build-id
                                              derivation-file-name statuses)
                            (if (member
                                 (assoc-ref (last (vector->list statuses))
                                            "status")
                                 '("failed-dependency"
                                   "scheduled")) ; scheduled, because the
                                                 ; guix-build-coordinator
                                                 ; doesn't mark builds as
                                                 ; failed-dependency
                                (with-resource-from-pool (connection-pool) conn
                                  (select-required-builds-that-failed
                                   conn
                                   build-server-id
                                   derivation-file-name))
                                #f)))))
            (render-html
             #:sxml (general-not-found
                     "Build not found"
                     "No build found for this build server and derivation.")
             #:code 404)))))

(define (render-build-servers mime-types
                              build-servers)
  (render-html
   #:sxml
   (view-build-servers build-servers)))

(define (render-build-server mime-types
                             build-server)
  (render-html
   #:sxml
   (view-build-server build-server)))

(define (handle-build-event-submission parsed-query-parameters
                                       build-server-id-string
                                       body
                                       secret-key-base)
  (define build-server-id
    (string->number build-server-id-string))

  (define (spawn-fiber-for-handler handler)
    (spawn-fiber
     (lambda ()
       (with-resource-from-pool (connection-pool) conn
         (with-exception-handler
             (lambda (exn)
               (simple-format
                (current-error-port)
                "exception in build event handler: ~A\n"
                exn))
           (lambda ()
             (with-throw-handler #t
               (lambda ()
                 (handler conn))
               (lambda _
                 (display (backtrace) (current-error-port))
                 (display "\n" (current-error-port)))))
           #:unwind? #t)))))

  (define (with-build-ids-for-status data
                                     build-ids
                                     statuses
                                     handler)
    (let ((ids
           (delete-duplicates
            (filter-map
             (lambda (build-id item-data)
               (if (and (string=? (assoc-ref item-data "type")
                                  "build")
                        (member (assoc-ref item-data "event")
                                statuses))
                   build-id
                   #f))
             build-ids
             data)
            =)))
      (unless (null? ids)
        (handler ids))))

  (define (handle-derivation-events conn items)
    (if (null? items)
        '()
        (let ((build-ids
               (insert-builds
                conn
                build-server-id
                (map (lambda (item)
                       (assoc-ref item "derivation"))
                     items)
                (map (lambda (item)
                       (and=>
                        (assoc-ref item "derivation_outputs")
                        (lambda (outputs)
                          (map
                           (lambda (output)
                             `((path           . ,(assoc-ref output "output"))
                               (hash_algorithm
                                . ,(or (assoc-ref output "hash_algorithm")
                                       NULL))
                               (hash           . ,(or (assoc-ref output "hash")
                                                      NULL))
                               (recursive      . ,(assoc-ref output "recursive"))))
                           (vector->list outputs)))))
                     items)
                (map (lambda (item)
                       (assoc-ref item "build_id"))
                     items))))
          (insert-build-statuses
           conn
           build-ids
           (map
            (lambda (item-data)
              (list (assoc-ref item-data "timestamp")
                    (assoc-ref item-data "event")))
            items)
           #:transaction? #f)

          build-ids)))

  (define (process-items items)
    (define filtered-items
      (filter (lambda (item)
                (let ((type (assoc-ref item "type")))
                  (if type
                      (string=? type "build")
                      (begin
                        (simple-format
                         (current-error-port)
                         "warning: unknown type for event: ~A\n"
                         item)
                        #f))))
              items))

    (let ((build-ids
           (with-resource-from-pool (reserved-connection-pool) conn
             (with-postgresql-transaction
              conn
              (lambda (conn)
                (handle-derivation-events
                 conn
                 filtered-items))))))

      (with-build-ids-for-status
       items
       build-ids
       '("succeeded")
       (lambda (ids)
         (spawn-fiber-for-handler
          (lambda (conn)
            (handle-removing-blocking-build-entries-for-successful-builds
             conn ids)))

         (request-query-of-build-server-substitutes build-server-id
                                                    ids)))

      (with-build-ids-for-status
       items
       build-ids
       '("scheduled")
       (lambda (ids)
         (spawn-fiber-for-handler
          (lambda (conn)
            (handle-blocked-builds-entries-for-scheduled-builds conn ids)))))

      (with-build-ids-for-status
       items
       build-ids
       '("failed" "failed-dependency" "canceled")
       (lambda (ids)
         (spawn-fiber-for-handler
          (lambda (conn)
            (handle-populating-blocked-builds-for-build-failures conn ids)))))))

  (if (any-invalid-query-parameters? parsed-query-parameters)
      (render-json
       '((error . "no token provided"))
       #:code 400)
      (let ((provided-token (assq-ref parsed-query-parameters 'token))
            (permitted-tokens
             (with-resource-from-pool (reserved-connection-pool) conn
               (compute-tokens-for-build-server conn
                                                secret-key-base
                                                build-server-id))))
        (if (member provided-token
                    (map cdr permitted-tokens)
                    string=?)
            (catch
              'json-invalid
              (lambda ()
                (let ((body-string (utf8->string body)))
                  (let* ((body-json (json-string->scm body-string))
                         (items (and=> (assoc-ref body-json "items")
                                       vector->list)))
                    (cond
                     ((eq? items #f)
                      (render-json
                       '((error . "missing items key"))
                       #:code 400))
                     ((null? items)
                      (render-json
                       '((error . "no items to process"))
                       #:code 400))
                     (else
                      (catch
                        #t
                        (lambda ()
                          (process-items items)
                          (no-content))
                        (lambda (key . args)
                          (simple-format (current-error-port)
                                         "error processing events: ~A: ~A\n"
                                         key
                                         args)
                          (for-each (lambda (item)
                                      (simple-format (current-error-port)
                                                     "  ~A\n" item))
                                    items)
                          (render-json
                           '((error . "could not process events"))
                           #:code 500))))))))
              (lambda (key . args)
                (render-json
                 '((error . "could not parse body as JSON"))
                 #:code 400)))
            (render-json
             '((error . "error"))
             #:code 403)))))

(define (handle-signing-key-request id)
  (render-html
   #:sxml (view-signing-key
           (with-resource-from-pool (connection-pool) conn
             (select-signing-key conn id)))))

(define (build-server-controller request
                                 method-and-path-components
                                 mime-types
                                 body
                                 secret-key-base)
  (match method-and-path-components
    (('GET "build-servers")
     (let ((build-servers
            (with-resource-from-pool (connection-pool) conn
              select-build-servers)))
       (render-build-servers mime-types
                             build-servers)))
    (('GET "build-server" build-server-id)
     (let ((build-server
            (with-resource-from-pool (connection-pool) conn
              (lambda (conn)
                (select-build-server conn (string->number
                                           build-server-id))))))
       (if build-server
           (render-build-server mime-types
                                build-server)
           (general-not-found "Build server not found" ""))))
    (('GET "build-server" build-server-id "build")
     (let ((parsed-query-parameters
            (parse-query-parameters
             request
             `((derivation_file_name  ,identity)
               (build_server_build_id ,identity)))))
       (render-build mime-types
                     (string->number build-server-id)
                     parsed-query-parameters)))
    (('POST "build-server" build-server-id "build-events")
     (let ((parsed-query-parameters
            (parse-query-parameters
             request
             `((token ,identity #:required)))))
       (handle-build-event-submission parsed-query-parameters
                                      build-server-id
                                      body
                                      secret-key-base)))
    (('GET "build-server" "signing-key" id)
     (handle-signing-key-request (string->number id)))
    (_ #f)))