aboutsummaryrefslogtreecommitdiff
path: root/guix-build-coordinator/client-communication.scm
blob: 797af73a9617e2bd7b0664136a1a74c1e9bbf68e (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 Build Coordinator
;;;
;;; Copyright © 2020 Christopher Baines <mail@cbaines.net>
;;;
;;; This file is part of the guix-build-coordinator.
;;;
;;; The Guix Build Coordinator is free software; you can redistribute
;;; it and/or modify it under the terms of the GNU General Public
;;; License as published by the Free Software Foundation; either
;;; version 3 of the License, or (at your option) any later version.
;;;
;;; The Guix Build Coordinator 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 General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with the guix-data-service.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (guix-build-coordinator client-communication)
  #:use-module (srfi srfi-11)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (ice-9 exceptions)
  #:use-module (rnrs bytevectors)
  #:use-module (json)
  #:use-module (logging logger)
  #:use-module (web uri)
  #:use-module (web client)
  #:use-module (web request)
  #:use-module (web response)
  #:use-module (system repl error-handling)
  #:use-module (guix store)
  #:use-module (guix derivations)
  #:use-module (guix-build-coordinator utils)
  #:use-module (guix-build-coordinator utils fibers)
  #:use-module (guix-build-coordinator datastore)
  #:use-module (guix-build-coordinator coordinator)
  #:export (start-client-request-server

            send-submit-build-request
            request-build-details
            request-output-details
            request-agents-list
            request-failed-builds-with-blocking-count-list
            send-create-agent-request
            send-create-agent-password-request))

(define (start-client-request-server secret-key-base
                                     host
                                     port
                                     build-coordinator
                                     substitutes-channel)
  (call-with-error-handling
   (lambda ()
     (run-server/patched
      (lambda (request body)
        (log-msg (build-coordinator-logger build-coordinator)
                 'INFO
                 (format #f "~4a ~a\n"
                         (request-method request)
                         (uri-path (request-uri request))))
        (apply values
               (controller request
                           (cons (request-method request)
                                 (split-and-decode-uri-path
                                  (uri-path (request-uri request))))
                           body
                           secret-key-base
                           build-coordinator
                           substitutes-channel)))
      #:host host
      #:port port))
   #:on-error 'backtrace))

(define (controller request
                    method-and-path-components
                    raw-body
                    secret-key-base
                    build-coordinator
                    substitutes-channel)
  (define datastore
    (build-coordinator-datastore build-coordinator))

  (define body
    (if raw-body
        (json-string->scm (utf8->string raw-body))
        '()))

  (define (controller-thunk)
    (match method-and-path-components
      (('GET "build" uuid)
       (match (datastore-find-build datastore uuid)
         (#f
          (render-json '((error . "no build found"))
                       #:code 404))
         (build-details
          (let ((derivation-inputs
                 (map
                  (lambda (derivation-input-details)
                    (let ((builds (datastore-list-builds-for-output
                                   datastore
                                   (assq-ref derivation-input-details
                                             'output))))
                      `(,@derivation-input-details
                        (builds . ,(list->vector builds)))))
                  (datastore-find-derivation-inputs
                   datastore
                   (assq-ref build-details 'derivation-name))))
                (setup-failures
                 (map
                  (lambda (setup-failure)
                    `(,@setup-failure
                      ,@(if (string=? (assq-ref setup-failure 'failure-reason)
                                      "missing_inputs")
                            `((missing-inputs
                               . ,(list->vector
                                   (map
                                    (lambda (missing-input)
                                      (let ((builds-for-missing-input
                                             (datastore-list-builds-for-output
                                              datastore
                                              missing-input)))
                                        `((missing-input . ,missing-input)
                                          (builds . ,(list->vector
                                                      builds-for-missing-input)))))
                                    (datastore-list-setup-failure-missing-inputs
                                     datastore
                                     (assq-ref setup-failure 'id))))))
                            '())))
                  (datastore-list-setup-failures-for-build
                   datastore
                   (assq-ref build-details 'uuid)))))
            (render-json
             `(,@build-details
               (derivation-inputs . ,(list->vector derivation-inputs))
               (setup-failures    . ,(list->vector setup-failures))))))))
      (('GET "builds" "blocking")
       (render-json
        `((builds
           . ,(list->vector
               (datastore-list-failed-builds-with-blocking-count datastore))))))
      (('GET "output" output-components ...)
       (let* ((output (string-append
                       "/" (string-join output-components "/")))
              (builds (datastore-list-builds-for-output datastore
                                                        output)))
         (render-json
          `((builds . ,(list->vector builds))))))
      (('GET "agents")
       (render-json
        `((agents . ,(list->vector (datastore-list-agents datastore))))))
      (('POST "agents")
       (let ((uuid (new-agent
                    datastore
                    #:requested-uuid (assoc-ref body "requested-uuid")
                    #:description (assoc-ref body "description"))))
         (render-json
          `((agent-id . ,uuid)))))
      (('POST "agent" agent-id "passwords")
       (let ((password (new-agent-password
                        datastore
                        #:agent agent-id)))
         (render-json
          `((new-password . ,password)))))
      (('POST "builds")
       (let ((derivation-file (assoc-ref body "derivation")))
         (let ((derivation-database-entry
                (datastore-find-derivation datastore derivation-file)))
           (unless derivation-database-entry
             (unless (with-store store
                       (valid-path? store derivation-file))
               (call-with-worker-thread
                substitutes-channel
                (lambda ()
                  (let ((raw-substitute-urls
                         (assoc-ref body "substitute-urls")))
                    (substitute-derivation derivation-file
                                           #:substitute-urls
                                           (and=> raw-substitute-urls
                                                  vector->list))))))
             (datastore-store-derivation
              datastore
              (read-derivation-from-file derivation-file))))

         (let ((submit-build-result
                (apply
                 submit-build
                 `(,build-coordinator
                   ,derivation-file
                   ,@(let ((priority (assoc-ref body "priority")))
                       (if priority
                           `(#:priority ,priority)
                           '()))
                   ,@(if (assoc-ref body "ignore-if-build-for-derivation-exists")
                         '(#:ignore-if-build-for-derivation-exists? #t)
                         '())
                   ,@(if (assoc-ref body "ignore-if-build-for-outputs-exists")
                         '(#:ignore-if-build-for-outputs-exists? #t)
                         '())
                   ,@(if (assoc-ref
                          body "ensure-all-related-derivation-outputs-have-builds")
                         '(#:ensure-all-related-derivation-outputs-have-builds? #t)
                         '())
                   ,@(if (assoc-ref body "tags")
                         `(#:tags
                           ,(map
                             (lambda (tag)
                               (cons (assoc-ref tag "key")
                                     (assoc-ref tag "value")))
                             (vector->list (assoc-ref body "tags"))))
                         '())))))
           (render-json submit-build-result))))
      (_
       (render-json
        "not-found"
        #:code 404))))

  (call-with-error-handling
   controller-thunk
   #:on-error 'backtrace
   #:post-error (lambda args
                  (match method-and-path-components
                    ((method path-components ...)
                     (simple-format
                      (current-error-port)
                      "error: when processing: /~A ~A\n"
                      method (string-join path-components "/"))))
                  (render-json
                   `((error . ,(simple-format #f "~A" args)))
                   #:code 500))))

(define* (render-json json #:key (extra-headers '())
                      (code 200))
  (list (build-response
         #:code code
         #:headers (append extra-headers
                           '((content-type . (application/json))
                             (vary . (accept)))))
        (lambda (port)
          (scm->json json port))))

(define* (send-request coordinator-uri
                       method
                       path
                       #:optional
                       request-body)
  (let-values (((response body)
                (http-request (string->uri
                               (string-append coordinator-uri path))
                              #:method method
                              #:body (and=> request-body scm->json-string)
                              #:decode-body? #f)))
    (if (>= (response-code response) 400)
        (begin
          (simple-format
           (current-error-port)
           "error: coordinator-http-request: ~A ~A: ~A\n"
           method path (response-code response))
          (let ((body
                 (catch #t
                   (lambda ()
                     (if (equal? '(application/json (charset . "utf-8"))
                                 (response-content-type response))
                         (json-string->scm (utf8->string body))
                         (utf8->string body)))
                   (lambda (key . args)
                     (simple-format
                      (current-error-port)
                      "error decoding body ~A ~A\n"
                      key args)
                     #f))))
            (raise-exception
             (make-exception-with-message
              body))))
        (values
         (json-string->scm (utf8->string body))
         response))))

(define (send-submit-build-request
         coordinator-uri
         derivation-file-name
         substitute-urls
         requested-uuid
         priority
         ignore-if-build-for-derivation-exists?
         ignore-if-build-for-outputs-exists?
         ensure-all-related-derivation-outputs-have-builds?
         tags)
  (send-request coordinator-uri
                'POST
                "/builds"
                `((derivation      . ,derivation-file-name)
                  (priority        . ,priority)
                  ,@(if substitute-urls
                        `((substitute-urls . ,(list->vector substitute-urls)))
                        '())
                  ,@(if ignore-if-build-for-derivation-exists?
                        '((ignore-if-build-for-derivation-exists . #t))
                        '())
                  ,@(if ignore-if-build-for-outputs-exists?
                        '((ignore-if-build-for-outputs-exists . #t))
                        '())
                  ,@(if ensure-all-related-derivation-outputs-have-builds?
                        '((ensure-all-related-derivation-outputs-have-builds . #t))
                        '())
                  ,@(if (null? tags)
                        '()
                        `((tags . ,(list->vector
                                    (map (match-lambda
                                           ((key . value)
                                            `((key   . ,key)
                                              (value . ,value))))
                                         tags))))))))

(define (request-build-details coordinator-uri
                               uuid)
  (send-request coordinator-uri
                'GET
                (string-append "/build/" uuid)))

(define (request-output-details coordinator-uri
                                output)
  (send-request coordinator-uri
                'GET
                (string-append "/output" output)))

(define (request-agents-list coordinator-uri)
  (send-request coordinator-uri
                'GET
                (string-append "/agents")))

(define (request-failed-builds-with-blocking-count-list coordinator-uri)
  (send-request coordinator-uri
                'GET
                (string-append "/builds/blocking")))

(define* (send-create-agent-request coordinator-uri
                                    #:key
                                    requested-uuid
                                    description)
  (send-request coordinator-uri
                'POST
                "/agents"
                `(,@(if requested-uuid
                        `((requested-uuid . ,requested-uuid))
                        '())
                  ,@(if description
                        `((description . ,description))
                        '()))))

(define (send-create-agent-password-request coordinator-uri
                                            agent-id)
  (send-request coordinator-uri
                'POST
                (string-append "/agent/" agent-id "/passwords")))