aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/web/build-server/controller.scm
blob: accadd2898a96df16f4c97a88c94fae34acaf42e (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
;;; 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 (guix-data-service database)
  #:use-module (guix-data-service web render)
  #:use-module (guix-data-service web query-parameters)
  #:use-module (guix-data-service jobs load-new-guix-revision)
  #:use-module (guix-data-service model build)
  #:use-module (guix-data-service model build-status)
  #:use-module (guix-data-service model nar)
  #:use-module (guix-data-service model build-server-token-seed)
  #: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
                      conn
                      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))))
      (let* ((derivation-file-name
              (assq-ref query-parameters 'derivation_file_name))
             (build
              (select-build-by-build-server-and-derivation-file-name
               conn
               build-server-id
               derivation-file-name)))
        (render-html
         #:sxml
         (view-build query-parameters
                     build)))))

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

  (define (handle-derivation-events items)
    (unless (null? items)
      (let ((build-ids
             (insert-builds conn
                            build-server-id
                            (map (lambda (item)
                                   (assoc-ref item "derivation"))
                                 items))))
        (insert-build-statuses
         conn
         build-ids
         (map
          (lambda (item-data)
            (list (assoc-ref item-data "timestamp")
                  (assoc-ref item-data "event")))
          items)))))

  (define (process-items items)
    (with-postgresql-transaction
     conn
     (lambda (conn)
       (handle-derivation-events
        (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)))))

  (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 (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 conn id)
  (render-html
   #:sxml (view-signing-key
           (select-signing-key conn id))))

(define (build-server-controller request
                                 method-and-path-components
                                 mime-types
                                 body
                                 conn
                                 secret-key-base)
  (match method-and-path-components
    (('GET "build-server" build-server-id "build")
     (let ((parsed-query-parameters
            (parse-query-parameters
             request
             `((derivation_file_name ,identity #:required)))))
       (render-build mime-types
                     conn
                     (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
                                      conn
                                      secret-key-base)))
    (('GET "build-server" "signing-key" id)
     (handle-signing-key-request conn
                                 (string->number id)))
    (_ #f)))