2019-02-06 17:14:44 +01:00
|
|
|
;;; Guix Data Service -- Information about Guix over time
|
|
|
|
;;; Copyright © 2016, 2017, 2018, 2019 Ricardo Wurmus <rekado@elephly.net>
|
|
|
|
;;; 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 controller)
|
|
|
|
#:use-module (ice-9 match)
|
2019-02-26 00:44:32 +01:00
|
|
|
#:use-module (ice-9 vlist)
|
2019-02-06 17:14:44 +01:00
|
|
|
#:use-module (ice-9 pretty-print)
|
2019-09-23 17:44:16 +02:00
|
|
|
#:use-module (ice-9 textual-ports)
|
2019-11-24 13:59:09 +01:00
|
|
|
#:use-module (rnrs bytevectors)
|
2019-02-06 17:14:44 +01:00
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
#:use-module (srfi srfi-11)
|
|
|
|
#:use-module (srfi srfi-26)
|
|
|
|
#:use-module (web request)
|
2019-11-24 13:59:09 +01:00
|
|
|
#:use-module (web response)
|
2019-02-06 17:14:44 +01:00
|
|
|
#:use-module (web uri)
|
2019-05-16 23:28:16 +02:00
|
|
|
#:use-module (texinfo)
|
|
|
|
#:use-module (texinfo html)
|
2019-02-06 17:14:44 +01:00
|
|
|
#:use-module (squee)
|
2019-05-16 23:28:16 +02:00
|
|
|
#:use-module (json)
|
2019-09-23 17:44:16 +02:00
|
|
|
#:use-module (guix-data-service config)
|
2019-02-06 17:14:44 +01:00
|
|
|
#:use-module (guix-data-service comparison)
|
2019-06-06 21:39:06 +02:00
|
|
|
#:use-module (guix-data-service database)
|
2019-05-05 21:06:28 +02:00
|
|
|
#:use-module (guix-data-service model git-branch)
|
2019-05-05 14:35:48 +02:00
|
|
|
#:use-module (guix-data-service model git-repository)
|
2019-02-06 17:14:44 +01:00
|
|
|
#:use-module (guix-data-service model guix-revision)
|
2019-12-02 13:30:36 +01:00
|
|
|
#:use-module (guix-data-service model nar)
|
2019-03-06 23:59:27 +01:00
|
|
|
#:use-module (guix-data-service model package)
|
2019-03-11 23:11:14 +01:00
|
|
|
#:use-module (guix-data-service model package-derivation)
|
|
|
|
#:use-module (guix-data-service model package-metadata)
|
2019-03-07 09:43:16 +01:00
|
|
|
#:use-module (guix-data-service model derivation)
|
2019-03-17 23:44:09 +01:00
|
|
|
#:use-module (guix-data-service model build-status)
|
2019-03-06 23:59:27 +01:00
|
|
|
#:use-module (guix-data-service model build)
|
2019-08-31 13:42:54 +02:00
|
|
|
#:use-module (guix-data-service model lint-checker)
|
2019-09-01 13:59:45 +02:00
|
|
|
#:use-module (guix-data-service model lint-warning)
|
2019-09-07 17:19:34 +02:00
|
|
|
#:use-module (guix-data-service model utils)
|
2019-02-24 17:47:29 +01:00
|
|
|
#:use-module (guix-data-service jobs load-new-guix-revision)
|
2019-02-06 17:14:44 +01:00
|
|
|
#:use-module (guix-data-service web render)
|
2019-05-16 23:28:16 +02:00
|
|
|
#:use-module (guix-data-service web sxml)
|
2019-05-11 17:44:17 +02:00
|
|
|
#:use-module (guix-data-service web query-parameters)
|
2019-02-06 17:14:44 +01:00
|
|
|
#:use-module (guix-data-service web util)
|
2019-11-24 21:42:37 +01:00
|
|
|
#:use-module (guix-data-service web build controller)
|
2019-10-14 18:55:08 +02:00
|
|
|
#:use-module (guix-data-service web revision controller)
|
2019-10-13 22:10:10 +02:00
|
|
|
#:use-module (guix-data-service web jobs controller)
|
2019-02-06 17:14:44 +01:00
|
|
|
#:use-module (guix-data-service web view html)
|
2019-11-24 13:59:09 +01:00
|
|
|
#:use-module (guix-data-service web build-server controller)
|
2019-10-14 20:24:14 +02:00
|
|
|
#:use-module (guix-data-service web compare controller)
|
2019-10-14 18:55:08 +02:00
|
|
|
#:use-module (guix-data-service web revision controller)
|
2019-10-14 19:28:25 +02:00
|
|
|
#:use-module (guix-data-service web repository controller)
|
2019-02-06 17:14:44 +01:00
|
|
|
#:export (controller))
|
|
|
|
|
2019-05-18 21:25:34 +02:00
|
|
|
(define cache-control-default-max-age
|
|
|
|
(* 60 60 24)) ; One day
|
|
|
|
|
|
|
|
(define http-headers-for-unchanging-content
|
|
|
|
`((cache-control
|
|
|
|
. (public
|
|
|
|
(max-age . ,cache-control-default-max-age)))))
|
|
|
|
|
2019-02-06 17:14:44 +01:00
|
|
|
(define-syntax-rule (-> target functions ...)
|
|
|
|
(fold (lambda (f val) (and=> val f))
|
|
|
|
target
|
|
|
|
(list functions ...)))
|
|
|
|
|
|
|
|
(define (render-with-error-handling page message)
|
|
|
|
(apply render-html (page))
|
|
|
|
;; (catch #t
|
|
|
|
;; (lambda ()
|
|
|
|
;; (receive (sxml headers)
|
|
|
|
;; (pretty-print (page))
|
|
|
|
;; (render-html sxml headers)))
|
|
|
|
;; (lambda (key . args)
|
|
|
|
;; (format #t "ERROR: ~a ~a\n"
|
|
|
|
;; key args)
|
|
|
|
;; (render-html (error-page message))))
|
|
|
|
)
|
|
|
|
|
2019-03-17 23:44:09 +01:00
|
|
|
(define (assoc-ref-multiple alist key)
|
|
|
|
(filter-map
|
|
|
|
(match-lambda
|
|
|
|
((k . value)
|
|
|
|
(and (string=? k key)
|
|
|
|
value)))
|
|
|
|
alist))
|
|
|
|
|
2019-03-07 09:43:16 +01:00
|
|
|
(define (render-derivation conn derivation-file-name)
|
|
|
|
(let ((derivation (select-derivation-by-file-name conn
|
|
|
|
derivation-file-name)))
|
|
|
|
(if derivation
|
|
|
|
(let ((derivation-inputs (select-derivation-inputs-by-derivation-id
|
|
|
|
conn
|
|
|
|
(first derivation)))
|
|
|
|
(derivation-outputs (select-derivation-outputs-by-derivation-id
|
|
|
|
conn
|
2019-03-08 00:50:51 +01:00
|
|
|
(first derivation)))
|
2019-11-24 13:59:09 +01:00
|
|
|
(builds (select-builds-with-context-by-derivation-file-name
|
2019-03-08 00:50:51 +01:00
|
|
|
conn
|
2019-11-24 13:59:09 +01:00
|
|
|
(second derivation))))
|
2019-05-18 21:08:34 +02:00
|
|
|
(render-html
|
|
|
|
#:sxml (view-derivation derivation
|
|
|
|
derivation-inputs
|
|
|
|
derivation-outputs
|
2019-05-18 21:25:34 +02:00
|
|
|
builds)
|
|
|
|
#:extra-headers http-headers-for-unchanging-content))
|
|
|
|
|
2019-08-05 20:45:10 +02:00
|
|
|
(render-html
|
|
|
|
#:sxml (general-not-found
|
|
|
|
"Derivation not found"
|
|
|
|
"No derivation found with this file name.")
|
|
|
|
#:code 404))))
|
2019-03-07 09:43:16 +01:00
|
|
|
|
2019-11-09 21:50:53 +01:00
|
|
|
(define (render-formatted-derivation conn derivation-file-name)
|
|
|
|
(let ((derivation (select-derivation-by-file-name conn
|
|
|
|
derivation-file-name)))
|
|
|
|
(if derivation
|
|
|
|
(let ((derivation-inputs (select-derivation-inputs-by-derivation-id
|
|
|
|
conn
|
|
|
|
(first derivation)))
|
|
|
|
(derivation-outputs (select-derivation-outputs-by-derivation-id
|
|
|
|
conn
|
|
|
|
(first derivation)))
|
|
|
|
(derivation-sources (select-derivation-sources-by-derivation-id
|
|
|
|
conn
|
|
|
|
(first derivation))))
|
|
|
|
(render-html
|
|
|
|
#:sxml (view-formatted-derivation derivation
|
|
|
|
derivation-inputs
|
|
|
|
derivation-outputs
|
|
|
|
derivation-sources)
|
|
|
|
#:extra-headers http-headers-for-unchanging-content))
|
|
|
|
|
|
|
|
(render-html
|
|
|
|
#:sxml (general-not-found
|
|
|
|
"Derivation not found"
|
|
|
|
"No derivation found with this file name.")
|
|
|
|
#:code 404))))
|
|
|
|
|
2019-12-02 13:30:36 +01:00
|
|
|
(define (render-narinfos conn filename)
|
|
|
|
(let ((narinfos (select-nars-for-output
|
|
|
|
conn
|
|
|
|
(string-append "/gnu/store/" filename))))
|
|
|
|
(if (null? narinfos)
|
|
|
|
(render-html
|
|
|
|
#:sxml (general-not-found
|
|
|
|
"No nars found"
|
|
|
|
"No nars found for this output name.")
|
|
|
|
#:code 404)
|
|
|
|
|
|
|
|
(render-html
|
|
|
|
#:sxml (view-narinfos narinfos)))))
|
|
|
|
|
2019-03-07 09:43:16 +01:00
|
|
|
(define (render-store-item conn filename)
|
2019-03-08 00:50:51 +01:00
|
|
|
(let ((derivation (select-derivation-by-output-filename conn filename)))
|
|
|
|
(match derivation
|
|
|
|
(()
|
2019-11-14 22:32:47 +01:00
|
|
|
(match (select-derivation-source-file-by-store-path conn filename)
|
|
|
|
(()
|
|
|
|
(render-html
|
|
|
|
#:sxml (general-not-found
|
|
|
|
"Store item not found"
|
|
|
|
"No derivation found producing this output")
|
|
|
|
#:code 404))
|
|
|
|
((id)
|
|
|
|
(render-html
|
|
|
|
#:sxml (view-derivation-source-file filename)
|
|
|
|
#:extra-headers http-headers-for-unchanging-content))))
|
2019-03-11 23:11:14 +01:00
|
|
|
(derivations
|
2019-05-18 21:08:34 +02:00
|
|
|
(render-html
|
|
|
|
#:sxml (view-store-item filename
|
|
|
|
derivations
|
|
|
|
(map (lambda (derivation)
|
|
|
|
(match derivation
|
|
|
|
((file-name output-id rest ...)
|
|
|
|
(select-derivations-using-output
|
|
|
|
conn output-id))))
|
2019-12-01 23:44:21 +01:00
|
|
|
derivations)))))))
|
2019-03-07 09:43:16 +01:00
|
|
|
|
2019-10-06 15:23:15 +02:00
|
|
|
(define handle-static-assets
|
|
|
|
(if assets-dir-in-store?
|
|
|
|
(static-asset-from-store-renderer)
|
|
|
|
render-static-asset))
|
|
|
|
|
2019-11-24 13:59:09 +01:00
|
|
|
(define (controller request method-and-path-components
|
|
|
|
mime-types body
|
|
|
|
secret-key-base)
|
2019-06-06 21:39:06 +02:00
|
|
|
(match method-and-path-components
|
2019-09-01 19:48:05 +02:00
|
|
|
(('GET "assets" rest ...)
|
2019-10-06 15:23:15 +02:00
|
|
|
(or (handle-static-assets (string-join rest "/")
|
|
|
|
(request-headers request))
|
2019-06-06 21:39:06 +02:00
|
|
|
(not-found (request-uri request))))
|
2019-09-01 19:48:05 +02:00
|
|
|
(('GET "healthcheck")
|
2019-06-06 22:08:34 +02:00
|
|
|
(let ((database-status
|
|
|
|
(catch
|
|
|
|
#t
|
|
|
|
(lambda ()
|
|
|
|
(with-postgresql-connection
|
2019-07-12 20:45:41 +02:00
|
|
|
"web healthcheck"
|
2019-06-06 22:08:34 +02:00
|
|
|
(lambda (conn)
|
|
|
|
(number?
|
|
|
|
(string->number
|
|
|
|
(first
|
|
|
|
(count-guix-revisions conn)))))))
|
|
|
|
(lambda (key . args)
|
|
|
|
#f))))
|
|
|
|
(render-json
|
|
|
|
`((status . ,(if database-status
|
|
|
|
"ok"
|
|
|
|
"not ok")))
|
|
|
|
#:code (if (eq? database-status
|
|
|
|
#t)
|
|
|
|
200
|
|
|
|
500))))
|
2019-09-21 13:41:39 +02:00
|
|
|
(('GET "README")
|
2019-09-23 17:44:16 +02:00
|
|
|
(let ((filename (string-append (%config 'doc-dir) "/README.html")))
|
|
|
|
(if (file-exists? filename)
|
|
|
|
(render-html
|
|
|
|
#:sxml (readme (call-with-input-file filename
|
|
|
|
get-string-all)))
|
|
|
|
(render-html
|
|
|
|
#:sxml (general-not-found
|
|
|
|
"README not found"
|
|
|
|
"The README.html file does not exist")
|
|
|
|
#:code 404))))
|
2019-06-06 21:39:06 +02:00
|
|
|
(_
|
|
|
|
(with-postgresql-connection
|
2019-07-12 20:45:41 +02:00
|
|
|
"web"
|
2019-06-06 21:39:06 +02:00
|
|
|
(lambda (conn)
|
|
|
|
(controller-with-database-connection request
|
|
|
|
method-and-path-components
|
|
|
|
mime-types
|
|
|
|
body
|
2019-11-24 13:59:09 +01:00
|
|
|
conn
|
|
|
|
secret-key-base))))))
|
2019-06-06 21:39:06 +02:00
|
|
|
|
|
|
|
(define (controller-with-database-connection request
|
|
|
|
method-and-path-components
|
|
|
|
mime-types
|
|
|
|
body
|
2019-11-24 13:59:09 +01:00
|
|
|
conn
|
|
|
|
secret-key-base)
|
2019-06-16 11:27:14 +02:00
|
|
|
(define path
|
|
|
|
(uri-path (request-uri request)))
|
|
|
|
|
2019-10-13 22:10:10 +02:00
|
|
|
(define (delegate-to f)
|
2019-10-18 18:23:59 +02:00
|
|
|
(or (f request
|
|
|
|
method-and-path-components
|
|
|
|
mime-types
|
|
|
|
body
|
|
|
|
conn)
|
|
|
|
(not-found (request-uri request))))
|
2019-10-13 22:10:10 +02:00
|
|
|
|
2019-11-24 13:59:09 +01:00
|
|
|
(define (delegate-to-with-secret-key-base f)
|
|
|
|
(or (f request
|
|
|
|
method-and-path-components
|
|
|
|
mime-types
|
|
|
|
body
|
|
|
|
conn
|
|
|
|
secret-key-base)
|
|
|
|
(not-found (request-uri request))))
|
|
|
|
|
2019-05-11 23:56:25 +02:00
|
|
|
(match method-and-path-components
|
2019-07-19 21:20:52 +02:00
|
|
|
(('GET)
|
2019-05-18 21:08:34 +02:00
|
|
|
(render-html
|
|
|
|
#:sxml (index
|
|
|
|
(map
|
|
|
|
(lambda (git-repository-details)
|
|
|
|
(cons
|
|
|
|
git-repository-details
|
2019-07-22 21:51:54 +02:00
|
|
|
(all-branches-with-most-recent-commit
|
|
|
|
conn (first git-repository-details))))
|
2019-05-18 21:08:34 +02:00
|
|
|
(all-git-repositories conn)))))
|
2019-07-19 21:20:52 +02:00
|
|
|
(('GET "builds")
|
2019-11-24 21:42:37 +01:00
|
|
|
(delegate-to build-controller))
|
2019-07-19 21:20:52 +02:00
|
|
|
(('GET "statistics")
|
2019-05-18 21:08:34 +02:00
|
|
|
(render-html
|
|
|
|
#:sxml (view-statistics (count-guix-revisions conn)
|
|
|
|
(count-derivations conn))))
|
2019-10-14 18:55:08 +02:00
|
|
|
(('GET "revision" args ...)
|
|
|
|
(delegate-to revision-controller))
|
2019-10-14 19:28:25 +02:00
|
|
|
(('GET "repository" _ ...)
|
|
|
|
(delegate-to repository-controller))
|
2019-07-19 21:20:52 +02:00
|
|
|
(('GET "gnu" "store" filename)
|
2019-05-11 23:56:25 +02:00
|
|
|
;; These routes are a little special, as the extensions aren't used for
|
|
|
|
;; content negotiation, so just use the path from the request
|
|
|
|
(let ((path (uri-path (request-uri request))))
|
|
|
|
(if (string-suffix? ".drv" path)
|
|
|
|
(render-derivation conn path)
|
|
|
|
(render-store-item conn path))))
|
2019-11-09 21:50:53 +01:00
|
|
|
(('GET "gnu" "store" filename "formatted")
|
|
|
|
(if (string-suffix? ".drv" filename)
|
|
|
|
(render-formatted-derivation conn
|
|
|
|
(string-append "/gnu/store/" filename))
|
|
|
|
(not-found (request-uri request))))
|
2019-12-02 13:30:36 +01:00
|
|
|
(('GET "gnu" "store" filename "narinfos")
|
|
|
|
(render-narinfos conn filename))
|
2019-11-24 13:59:09 +01:00
|
|
|
(((or 'GET 'POST) "build-server" _ ...)
|
|
|
|
(delegate-to-with-secret-key-base build-server-controller))
|
2019-10-14 20:24:14 +02:00
|
|
|
(('GET "compare" _ ...) (delegate-to compare-controller))
|
|
|
|
(('GET "compare-by-datetime" _ ...) (delegate-to compare-controller))
|
2019-10-13 22:10:10 +02:00
|
|
|
(('GET "jobs") (delegate-to jobs-controller))
|
|
|
|
(('GET "jobs" "queue") (delegate-to jobs-controller))
|
|
|
|
(('GET "job" job-id) (delegate-to jobs-controller))
|
2019-11-24 14:44:02 +01:00
|
|
|
((method path ...)
|
2019-06-06 21:39:06 +02:00
|
|
|
(not-found (request-uri request)))))
|