2016-12-24 01:57:27 +01:00
|
|
|
|
;;;; http.scm -- HTTP API
|
2016-07-30 23:00:41 +02:00
|
|
|
|
;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
|
2017-07-31 19:27:28 +02:00
|
|
|
|
;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
|
2018-01-07 12:28:13 +01:00
|
|
|
|
;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
|
2016-07-30 23:00:41 +02:00
|
|
|
|
;;;
|
|
|
|
|
;;; This file is part of Cuirass.
|
|
|
|
|
;;;
|
|
|
|
|
;;; Cuirass 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.
|
|
|
|
|
;;;
|
|
|
|
|
;;; Cuirass 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 Cuirass. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
(define-module (cuirass http)
|
|
|
|
|
#:use-module (cuirass database)
|
|
|
|
|
#:use-module (cuirass utils)
|
2018-01-22 23:07:10 +01:00
|
|
|
|
#:use-module (cuirass logging)
|
2016-07-30 23:00:41 +02:00
|
|
|
|
#:use-module (ice-9 match)
|
|
|
|
|
#:use-module (json)
|
|
|
|
|
#:use-module (web request)
|
|
|
|
|
#:use-module (web response)
|
2018-02-14 16:48:53 +01:00
|
|
|
|
#:use-module (web server)
|
2016-07-30 23:00:41 +02:00
|
|
|
|
#:use-module (web uri)
|
2017-07-31 19:27:28 +02:00
|
|
|
|
#:export (run-cuirass-server))
|
2016-07-30 23:00:41 +02:00
|
|
|
|
|
2017-07-31 19:27:28 +02:00
|
|
|
|
(define (build->hydra-build build)
|
|
|
|
|
"Convert BUILD to an assoc list matching hydra API format."
|
2018-01-26 18:28:32 +01:00
|
|
|
|
(define (bool->int bool)
|
|
|
|
|
(if bool 1 0))
|
|
|
|
|
|
2017-07-31 19:27:28 +02:00
|
|
|
|
`((#:id . ,(assq-ref build #:id))
|
|
|
|
|
(#:project . ,(assq-ref build #:repo-name))
|
|
|
|
|
(#:jobset . ,(assq-ref build #:branch))
|
|
|
|
|
(#:job . ,(assq-ref build #:job-name))
|
|
|
|
|
(#:timestamp . ,(assq-ref build #:timestamp))
|
|
|
|
|
(#:starttime . ,(assq-ref build #:starttime))
|
|
|
|
|
(#:stoptime . ,(assq-ref build #:stoptime))
|
2018-01-24 14:01:45 +01:00
|
|
|
|
(#:derivation . ,(assq-ref build #:derivation))
|
2017-07-31 19:27:28 +02:00
|
|
|
|
(#:buildoutputs . ,(assq-ref build #:outputs))
|
|
|
|
|
(#:system . ,(assq-ref build #:system))
|
|
|
|
|
(#:nixname . ,(assq-ref build #:nix-name))
|
|
|
|
|
(#:buildstatus . ,(assq-ref build #:status))
|
2018-01-26 18:28:32 +01:00
|
|
|
|
(#:busy . ,(bool->int (eqv? (build-status started)
|
|
|
|
|
(assq-ref build #:status))))
|
2017-07-31 19:27:28 +02:00
|
|
|
|
(#:priority . 0)
|
2018-01-26 18:28:32 +01:00
|
|
|
|
(#:finished . ,(bool->int
|
|
|
|
|
(not (memv (assq-ref build #:status)
|
|
|
|
|
(list (build-status scheduled)
|
|
|
|
|
(build-status started))))))
|
2017-07-31 19:27:28 +02:00
|
|
|
|
(#:buildproducts . #nil)
|
|
|
|
|
(#:releasename . #nil)
|
|
|
|
|
(#:buildinputs_builds . #nil)))
|
|
|
|
|
|
|
|
|
|
(define (handle-build-request db build-id)
|
|
|
|
|
"Retrieve build identified by BUILD-ID in DB and convert it to hydra
|
|
|
|
|
format. Return #f is not build was found."
|
|
|
|
|
(let ((build (db-get-build db build-id)))
|
|
|
|
|
(and=> build build->hydra-build)))
|
2016-07-30 23:00:41 +02:00
|
|
|
|
|
2017-07-31 19:27:28 +02:00
|
|
|
|
(define (handle-builds-request db filters)
|
|
|
|
|
"Retrieve all builds matched by FILTERS in DB and convert them to hydra
|
|
|
|
|
format."
|
2018-01-29 15:23:51 +01:00
|
|
|
|
;; Since these requests can take several seconds (!), run them through
|
|
|
|
|
;; 'non-blocking'.
|
|
|
|
|
(let ((builds (non-blocking
|
|
|
|
|
(with-time-logging "builds request"
|
|
|
|
|
(db-get-builds db filters)))))
|
2017-07-31 19:27:28 +02:00
|
|
|
|
(map build->hydra-build builds)))
|
|
|
|
|
|
|
|
|
|
(define (request-parameters request)
|
|
|
|
|
"Parse the REQUEST query parameters and return them under the form
|
|
|
|
|
'((parameter value) ...)."
|
|
|
|
|
(let* ((uri (request-uri request))
|
|
|
|
|
(query (uri-query uri)))
|
|
|
|
|
(and query
|
|
|
|
|
(map (lambda (param)
|
|
|
|
|
(match (string-split param #\=)
|
|
|
|
|
((key param)
|
2018-02-19 22:33:28 +01:00
|
|
|
|
(let ((key-symbol (string->symbol key)))
|
2018-03-01 15:37:29 +01:00
|
|
|
|
(list key-symbol
|
|
|
|
|
(match key-symbol
|
|
|
|
|
('id (string->number param))
|
|
|
|
|
('nr (string->number param))
|
|
|
|
|
(_ param)))))))
|
2017-07-31 19:27:28 +02:00
|
|
|
|
(string-split query #\&)))))
|
2016-07-30 23:00:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Web server.
|
|
|
|
|
;;;
|
2017-07-31 19:27:28 +02:00
|
|
|
|
;;; The api is derived from the hydra one. It is partially described here :
|
|
|
|
|
;;;
|
|
|
|
|
;;; https://github.com/NixOS/hydra/blob/master/doc/manual/api.xml
|
|
|
|
|
;;;
|
2016-07-30 23:00:41 +02:00
|
|
|
|
|
|
|
|
|
(define (request-path-components request)
|
|
|
|
|
(split-and-decode-uri-path (uri-path (request-uri request))))
|
|
|
|
|
|
2016-10-28 19:38:11 +02:00
|
|
|
|
(define (url-handler request body db)
|
2017-07-31 19:27:28 +02:00
|
|
|
|
|
2016-10-28 19:38:11 +02:00
|
|
|
|
(define* (respond response #:key body (db db))
|
|
|
|
|
(values response body db))
|
2017-07-31 19:27:28 +02:00
|
|
|
|
|
|
|
|
|
(define-syntax-rule (respond-json body ...)
|
|
|
|
|
(respond '((content-type . (application/json)))
|
|
|
|
|
#:body body ...))
|
|
|
|
|
|
|
|
|
|
(define-syntax-rule (respond-text body ...)
|
|
|
|
|
(respond '((content-type . (text/plain)))
|
|
|
|
|
#:body body ...))
|
|
|
|
|
|
|
|
|
|
(define-syntax-rule (respond-json-with-error error-code message)
|
|
|
|
|
(respond
|
|
|
|
|
(build-response #:headers '((content-type . (application/json)))
|
|
|
|
|
#:code error-code)
|
|
|
|
|
#:body
|
|
|
|
|
(object->json-string
|
|
|
|
|
`((error . ,message)))))
|
|
|
|
|
|
|
|
|
|
(define (respond-build-not-found build-id)
|
|
|
|
|
(respond-json-with-error
|
|
|
|
|
404
|
|
|
|
|
(format #f "Build with ID ~a doesn't exist." build-id)))
|
|
|
|
|
|
|
|
|
|
(define (respond-build-log-not-found build)
|
|
|
|
|
(let ((drv (assq-ref build #:derivation)))
|
|
|
|
|
(respond-json-with-error
|
|
|
|
|
404
|
|
|
|
|
(format #f "The build log of derivation ~a is not available." drv))))
|
|
|
|
|
|
2018-01-26 11:22:31 +01:00
|
|
|
|
(log-message "~a ~a" (request-method request)
|
|
|
|
|
(uri-path (request-uri request)))
|
|
|
|
|
|
2018-01-26 22:27:55 +01:00
|
|
|
|
;; Reject OPTIONS, POST, etc.
|
|
|
|
|
(match (if (eq? 'GET (request-method request))
|
|
|
|
|
(request-path-components request)
|
|
|
|
|
'method-not-allowed)
|
2016-07-30 23:00:41 +02:00
|
|
|
|
(((or "jobsets" "specifications") . rest)
|
2018-02-05 22:58:12 +01:00
|
|
|
|
(respond-json (object->json-string (db-get-specifications db))))
|
2017-07-31 19:27:28 +02:00
|
|
|
|
(("build" build-id)
|
2018-02-19 22:22:03 +01:00
|
|
|
|
(let ((hydra-build (handle-build-request db (string->number build-id))))
|
2017-07-31 19:27:28 +02:00
|
|
|
|
(if hydra-build
|
|
|
|
|
(respond-json (object->json-string hydra-build))
|
|
|
|
|
(respond-build-not-found build-id))))
|
|
|
|
|
(("build" build-id "log" "raw")
|
2018-02-19 22:22:03 +01:00
|
|
|
|
(let ((build (db-get-build db (string->number build-id))))
|
2017-07-31 19:27:28 +02:00
|
|
|
|
(if build
|
2018-01-07 12:28:13 +01:00
|
|
|
|
(match (assq-ref build #:outputs)
|
|
|
|
|
(((_ (#:path . (? string? output))) _ ...)
|
|
|
|
|
;; Redirect to a /log URL, which is assumed to be served
|
|
|
|
|
;; by 'guix publish'.
|
|
|
|
|
(let ((uri (string->uri-reference
|
|
|
|
|
(string-append "/log/"
|
|
|
|
|
(basename output)))))
|
|
|
|
|
(respond (build-response #:code 302
|
|
|
|
|
#:headers `((location . ,uri)))
|
|
|
|
|
#:body "")))
|
2018-01-24 13:59:28 +01:00
|
|
|
|
(()
|
|
|
|
|
;; Not entry for BUILD-ID in the 'Outputs' table.
|
|
|
|
|
(respond-json-with-error
|
|
|
|
|
500
|
|
|
|
|
(format #f "Outputs of build ~a are unknown." build-id)))
|
2018-01-07 12:28:13 +01:00
|
|
|
|
(#f
|
|
|
|
|
(respond-build-not-found build-id)))
|
2017-07-31 19:27:28 +02:00
|
|
|
|
(respond-build-not-found build-id))))
|
|
|
|
|
(("api" "latestbuilds")
|
|
|
|
|
(let* ((params (request-parameters request))
|
|
|
|
|
;; 'nr parameter is mandatory to limit query size.
|
|
|
|
|
(valid-params? (assq-ref params 'nr)))
|
|
|
|
|
(if valid-params?
|
2018-01-23 22:54:01 +01:00
|
|
|
|
;; Limit results to builds that are "done".
|
2017-07-31 19:27:28 +02:00
|
|
|
|
(respond-json (object->json-string
|
2018-01-23 22:54:01 +01:00
|
|
|
|
(handle-builds-request db
|
|
|
|
|
`((status done)
|
2018-01-25 10:39:18 +01:00
|
|
|
|
,@params
|
|
|
|
|
(order finish-time)))))
|
2017-07-31 19:27:28 +02:00
|
|
|
|
(respond-json-with-error 500 "Parameter not defined!"))))
|
2018-01-23 23:37:13 +01:00
|
|
|
|
(("api" "queue")
|
|
|
|
|
(let* ((params (request-parameters request))
|
|
|
|
|
;; 'nr parameter is mandatory to limit query size.
|
|
|
|
|
(valid-params? (assq-ref params 'nr)))
|
|
|
|
|
(if valid-params?
|
2018-01-29 11:59:45 +01:00
|
|
|
|
(respond-json
|
|
|
|
|
(object->json-string
|
|
|
|
|
;; Use the 'status+submission-time' order so that builds in
|
|
|
|
|
;; 'running' state appear before builds in 'scheduled' state.
|
|
|
|
|
(handle-builds-request db
|
|
|
|
|
`((status pending)
|
|
|
|
|
,@params
|
|
|
|
|
(order status+submission-time)))))
|
2018-01-23 23:37:13 +01:00
|
|
|
|
(respond-json-with-error 500 "Parameter not defined!"))))
|
2018-01-26 22:27:55 +01:00
|
|
|
|
('method-not-allowed
|
|
|
|
|
;; 405 "Method Not Allowed"
|
|
|
|
|
(values (build-response #:code 405) #f db))
|
2016-10-28 19:38:11 +02:00
|
|
|
|
(_
|
|
|
|
|
(respond (build-response #:code 404)
|
|
|
|
|
#:body (string-append "Resource not found: "
|
|
|
|
|
(uri->string (request-uri request)))))))
|
2016-07-30 23:00:41 +02:00
|
|
|
|
|
2017-09-18 21:57:05 +02:00
|
|
|
|
(define* (run-cuirass-server db #:key (host "localhost") (port 8080))
|
|
|
|
|
(let* ((host-info (gethostbyname host))
|
|
|
|
|
(address (inet-ntop (hostent:addrtype host-info)
|
|
|
|
|
(car (hostent:addr-list host-info)))))
|
2018-01-22 23:07:10 +01:00
|
|
|
|
(log-message "listening on ~A:~A" address port)
|
2018-01-25 11:54:40 +01:00
|
|
|
|
|
|
|
|
|
;; Here we use our own web backend, call 'fiberized'. We cannot use the
|
|
|
|
|
;; 'fibers' backend that comes with Fibers 1.0.0 because it does its own
|
|
|
|
|
;; thread creations and calls 'run-fibers' by itself, which isn't
|
|
|
|
|
;; necessary here (and harmful).
|
2018-02-10 00:11:06 +01:00
|
|
|
|
;;
|
2018-02-14 16:48:53 +01:00
|
|
|
|
;; XXX: 'run-server' serializes client request processing, making sure
|
|
|
|
|
;; only one client is served at a time. This is not ideal, but processing
|
|
|
|
|
;; things concurrently would require having several database handles.
|
|
|
|
|
(run-server url-handler
|
|
|
|
|
'fiberized
|
|
|
|
|
`(#:host ,address #:port ,port)
|
|
|
|
|
db)))
|