(address . guix-patches@gnu.org)(name . Mathieu Othacehe)(address . m.othacehe@gmail.com)
* bin/evaluate.in (fill-job): New procedure.
(main): Use it to fill informations (nix-name, system) that will later be
added to database.
* doc/cuirass.texi (Sections)[Web API]: New section describing the HTTP API.
(Database)[Derivation]: Add system and nix_name fields.
(Database)[Builds]: Add id, status, timestamp, starttime and stoptime
fields. Remove output field.
(Database)[Outputs]: New table describing the build outputs.
* src/cuirass/base.scm (build-packages): Add new fields to build object before
adding it to database.
* src/cuirass/database.scm (db-get-build, db-get-builds): New procedures to get
a build by id from database and a list of builds using filter parameters
respectively.
* src/cuirass/http.scm (spec->json-string): Move it to utils.scm and rename it
object->json-string.
(object->json-scm): Move it utils.scm.
(handle-*-request): New helpers procedures.
(request-parameters): New procedure to parse a request query.
(url-handler): Add new API's.
* src/cuirass/utils.scm (object->json-scm, object->json-string): Exported
procedures moved from http.scm.
* src/schema.sql (Outputs) : New table.
(Derivations): Add system and nix_name columns.
(Builds): Remove output column and add id, status, timestamp, starttime and
stoptime columns.
---
Hi,
Here's a first draft adding partial support for Hydra API in Cuirass.
It can be tested using curl or, better, with Emacs Guix.
The following elisp will change hydra url to a local running Cuirass server.
(setq guix-hydra-url "http://127.0.0.1:8080/")
Then, it should be possible to use M-x guix-hydra-latest-builds.
The commands guix-hydra-jobsets and guix-hydra-queued-builds won't function
because the associated API's are not implemented yet.
There's a problem with /build/:build-id/log/raw API because it is trying to fork
while multiple threads are running (because of decompressed-port function). It seems
to work but a warning message is printed.
Thanks,
Mathieu
bin/evaluate.in | 18 +++-
doc/cuirass.texi | 242 ++++++++++++++++++++++++++++++++++++++++++++++-
src/cuirass/base.scm | 43 ++++++---
src/cuirass/database.scm | 139 ++++++++++++++++++++++++---
src/cuirass/http.scm | 137 +++++++++++++++++++++------
src/cuirass/utils.scm | 22 ++++-
src/schema.sql | 17 +++-
7 files changed, 558 insertions(+), 60 deletions(-)
Toggle diff (527 lines)
diff --git a/bin/evaluate.in b/bin/evaluate.in
index d1d0767..858c34e 100644
--- a/bin/evaluate.in
+++ b/bin/evaluate.in
@@ -28,9 +28,22 @@ exec ${GUILE:-@GUILE@} --no-auto-compile -e main -s "$0" "$@"
(use-modules (cuirass)
(ice-9 match)
(ice-9 pretty-print)
+ (srfi srfi-26)
(guix build utils)
+ (guix derivations)
(guix store))
+(define (fill-job job eval-id)
+ "Given JOB assoc list, add EVAL-ID to it. Also process #:nix-name and
+ #:system from derivation stored in JOB."
+ (let ((drv (read-derivation-from-file
+ (assq-ref job #:derivation))))
+ ((compose
+ (cut acons #:eval-id eval-id <>)
+ (cut acons #:nix-name (derivation-name drv) <>)
+ (cut acons #:system (derivation-system drv) <>))
+ job)))
+
(define* (main #:optional (args (command-line)))
(match args
((command load-path guix-package-path cachedir specstr database)
@@ -73,8 +86,9 @@ exec ${GUILE:-@GUILE@} --no-auto-compile -e main -s "$0" "$@"
(pretty-print
(map (lambda (thunk)
(let* ((job (call-with-time-display thunk))
- ;; Keep track of SPEC id in the returned jobs.
- (job* (acons #:eval-id eval-id job)))
+ ;; Fill job with informations that will later be
+ ;; added to database.
+ (job* (fill-job job eval-id)))
(db-add-derivation db job*)
job*))
thunks)
diff --git a/doc/cuirass.texi b/doc/cuirass.texi
index 12bc02f..2392e2f 100644
--- a/doc/cuirass.texi
+++ b/doc/cuirass.texi
@@ -11,6 +11,7 @@ This manual is for Cuirass version @value{VERSION}, a build automation
server.
Copyright @copyright{} 2016, 2017 Mathieu Lirzin
+Copyright @copyright{} 2017 Mathieu Othacehe
@quotation
Permission is granted to copy, distribute and/or modify this document
@@ -56,6 +57,7 @@ Tutorial sections:
Reference sections:
* Invocation:: How to run Cuirass.
* Database:: About the database schema.
+* Web API:: Description of the Web API.
* Contributing:: Your help needed!
* GNU Free Documentation License:: The license of this manual.
@@ -312,6 +314,13 @@ This field holds the @code{id} of an evaluation from the
@item job_name
This text field holds the name of the job.
+
+@item system
+This text field holds the system name of the derivation.
+
+@item nix_name
+This text field holds the name of the derivation.
+
@end table
@section Builds
@@ -322,6 +331,9 @@ that builds are not in a one to one relationship with derivations in
order to keep track of non-deterministic compilations.
@table @code
+@item id
+This is an automatically incrementing numeric identifier.
+
@item derivation
This text field holds the absolute name of the derivation file that
resulted in this build.
@@ -334,11 +346,233 @@ belongs.
@item log
This text field holds the absolute file name of the build log file.
-@item output
-This text field holds the absolute directory name of the build output or
-@code{NULL} if the build failed.
+@item status
+This integer field holds the build status of the derivation.
+
+@item timestamp
+This integer field holds a timestamp taken at build creation time.
+
+@item starttime
+This integer field holds a timestamp taken at build start time.
+Currently, it has the same value as the @code{timestamp} above.
+
+@item stoptime
+This integer field holds a timestamp taken at build stop time.
+Currently, it has the same value as the @code{timestamp} above.
+
+@end table
+
+@section Outputs
+@cindex outputs, database
+
+This table keep tracks for every eventual build outputs. Each build
+stored in @code{Builds} table may have zero (if it has failed), one or
+multiple outputs.
+
+@table @code
+@item build
+This field holds the @code{id} of a build from the
+@code{Builds} table.
+
+@item name
+This text field holds the name of the output.
+
+@item path
+This text field holds the path of the output.
+
+@end table
+
+@c *********************************************************************
+@node Web API
+@chapter Web API
+@cindex web api
+
+Cuirass web API is derived from Hydra one, see @url{https://github.com/NixOS/hydra/blob/master/doc/manual/api.xml, Hydra API description}.
+
+For now only a subset of this API is implemented.
+
+@section API description.
+@cindex description, json
+
+@subsection Build informations.
+
+It is possible to query Cuirass web server for build informations. The
+dedicated API is @code{"/build/:build-id"} where @code{build-id} is the
+unique id associated to the build in database.
+
+For instance, querying a local Cuirass web server can be done with
+@code{curl} and @code{jq} to format the JSON response :
+
+@example
+$ curl -s "http://localhost:8080/build/2" | jq
+
+@{
+ "id": 2,
+ "project": "guix",
+ "jobset": "master",
+ "job": "acpica-20150410-job",
+ "timestamp": 1501347493,
+ "starttime": 1501347493,
+ "stoptime": 1501347493,
+ "buildoutputs": @{
+ "out": @{
+ "path": "/gnu/store/6g3njhfzqpdm335s7qhvmwvs5l7gcbq1-acpica-20150410"
+ @}
+ @},
+ "system": "x86_64-linux",
+ "nixname": "acpica-20150410",
+ "buildstatus": 0,
+ "busy": 0,
+ "priority": 0,
+ "finished": 1,
+ "buildproducts": null,
+ "releasename": null,
+ "buildinputs_builds": null
+@}
+@end example
+
+If requested @code{build-id} is not known, the HTTP code 404 is
+answered with a JSON error message. For example :
+
+@example
+$ curl -s "http://localhost:8080/build/fff"
+
+@{"error" : "Build with ID fff doesn't exist."@}
+@end example
+
+The nominal output is a JSON object whose fields are described
+hereafter.
+
+@table @code
+@item id
+The unique build id.
+
+@item project
+The associated specification name, as a string.
+
+@item jobset
+The associated specification branch, as a string.
+
+@item job
+The associated job-name, as a string.
+
+@item timestamp
+Timestamp taken at build creation time.
+
+@item starttime
+Timestamp taken at build start time.
+
+@item stoptime
+Timestamp taken at build stop time.
+
+@item buildoutputs
+Build outputs as a JSON object. The keys names are referring to the
+eventual output names. The associated value is another JSON object which
+only key is @code{path}. @code{path} value is the output directory in
+store as a string.
+
+@item system
+System name of the build, as a string.
+
+@item nixname
+Derivation name, as a string.
+
+@item buildstatus
+Build status, as an integer. Possible values are :
+
+@example
+0 -> succeded
+1 -> failed
+2 -> failed dependency
+3 -> failed other
+4 -> cancelled
+@end example
+
+@item busy
+Whether the build is pending, as an integer (not implemented yet).
+
+@item priority
+Build priority, as an integer (not implemented yet).
+
+@item finished
+Build finished, as an integer (not implemented yet : always 1).
+
+@item buildproducts
+Build products in store as a JSON object (not implemented yet).
+
+@item releasename
+Unknown, not implemented yet.
+
+@item buildinputs_builds
+Inputs used for the build, as a JSON object (not implemented yet).
+
@end table
+@subsection Build raw log output.
+
+It is possible to ask Cuirass for the raw build output log with the API
+@code{"/build/:build-id/log/raw"} where @code{build-id} is the
+unique id associated to the build in database.
+
+The output is a raw text, for example :
+
+@example
+$ curl http://localhost:8080/build/2/log/raw
+
+starting phase `set-SOURCE-DATE-EPOCH'
+phase `set-SOURCE-DATE-EPOCH' succeeded after 0.0 seconds
+starting phase `set-paths'
+...
+@end example
+
+If requested @code{build-id} is not known, the HTTP code 404 is
+answered with a JSON error message. For example :
+
+@example
+$ curl -s "http://localhost:8080/build/fff/log/raw"
+
+@{"error" : "Build with ID fff doesn't exist."@}
+@end example
+
+@subsection Latest builds.
+
+The list of latest builds can be obtained with the API
+@code{"/api/latestbuilds"}. The output is a JSON array of
+builds. Builds are represented as in @code{"/build/:build-id"} API.
+
+This request accepts a mandatory parameter and multiple optional ones.
+
+@table @code
+@item nr
+Limit query result to nr elements. This parameter is @emph{mandatory}.
+
+@item project
+Filter query result to builds with the given @code{project}.
+
+@item jobset
+Filter query result to builds with the given @code{jobset}.
+
+@item job
+Filter query result to builds with the given @code{job} name.
+
+@item system
+Filter query result to builds with the given @code{system}.
+
+@end table
+
+For example, to ask for the ten last builds :
+
+@example
+$ curl "http://localhost:8080/api/latestbuilds?nr=10"
+@end example
+
+or the five last builds which project is ``guix'' and jobset ``master' :
+
+@example
+$ curl "http://localhost:8080/api/latestbuilds?nr=5&project=guix&jobset=master"
+@end example
+
+If no builds matching given parameters are found and empty JSON array is returned.
@c *********************************************************************
@node Contributing
@@ -346,7 +580,7 @@ This text field holds the absolute directory name of the build output or
Everyone is welcome to contribute to Cuirass. You can report bugs, send
patches and share your ideas with others by sending emails the
-@email{bug-cuirass@@framalistes.org, mailing list}.
+@email{guix-devel@@gnu.org, mailing list}.
Development is done using the Git distributed version control system.
Thus, access to the repository is not strictly necessary. We welcome
diff --git a/src/cuirass/base.scm b/src/cuirass/base.scm
index 326a530..15d2284 100644
--- a/src/cuirass/base.scm
+++ b/src/cuirass/base.scm
@@ -30,6 +30,7 @@
#:use-module (ice-9 popen)
#:use-module (ice-9 rdelim)
#:use-module (ice-9 receive)
+ #:use-module (srfi srfi-1)
#:use-module (srfi srfi-19)
#:use-module (srfi srfi-34)
#:export (;; Procedures.
@@ -154,25 +155,41 @@ directory and the sha1 of the top level commit in this directory."
(define (build-packages store db jobs)
"Build JOBS and return a list of Build results."
+
+ (define hydra-build-status
+ ;; Build status as expected by hydra compatible API's.
+ '((succeeded . 0)
+ (failed . 1)
+ (failed-dependency . 2)
+ (failed-other . 3)
+ (cancelled . 4)))
+
(define (register job)
(let* ((name (assq-ref job #:job-name))
(drv (assq-ref job #:derivation))
(eval-id (assq-ref job #:eval-id))
;; XXX: How to keep logs from several attempts?
(log (log-file store drv))
- (outputs (match (derivation-path->output-paths drv)
- (((names . items) ...)
- (filter (lambda (item)
- (valid-path? store item))
- items)))))
- (for-each (lambda (output)
- (let ((build `((#:derivation . ,drv)
- (#:eval-id . ,eval-id)
- (#:log . ,log)
- (#:output . ,output))))
- (db-add-build db build)))
- outputs)
- (format #t "~{~A ~}\n" outputs)
+ (outputs (filter-map (lambda (res)
+ (match res
+ ((name . path)
+ (and (valid-path? store path)
+ `(,name . ,path)))))
+ (derivation-path->output-paths drv)))
+ (cur-time (time-second (current-time time-utc))))
+ (let ((build `((#:derivation . ,drv)
+ (#:eval-id . ,eval-id)
+ (#:log . ,log)
+ (#:status .
+ ,(match (length outputs)
+ (0 (assq-ref hydra-build-status 'failed))
+ (_ (assq-ref hydra-build-status 'succeeded))))
+ (#:outputs . ,outputs)
+ ;;; XXX: For now, we do not know start/stop build time.
+ (#:timestamp . ,cur-time)
+ (#:starttime . ,cur-time)
+ (#:stoptime . ,cur-time))))
+ (db-add-build db build))
build))
;; Pass all the jobs at once so we benefit from as much parallelism as
diff --git a/src/cuirass/database.scm b/src/cuirass/database.scm
index 804b8c2..5f60fac 100644
--- a/src/cuirass/database.scm
+++ b/src/cuirass/database.scm
@@ -1,5 +1,6 @@
;;; database.scm -- store evaluation and build results
;;; Copyright © 2016, 2017 Mathieu Lirzin <mthl@gnu.org>
+;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
;;;
;;; This file is part of Cuirass.
;;;
@@ -21,6 +22,7 @@
#:use-module (cuirass utils)
#:use-module (ice-9 match)
#:use-module (ice-9 rdelim)
+ #:use-module (srfi srfi-1)
#:use-module (sqlite3)
#:export (;; Procedures.
assq-refs
@@ -35,6 +37,8 @@
db-add-derivation
db-get-derivation
db-add-build
+ db-get-build
+ db-get-builds
read-sql-file
read-quoted-string
sqlite-exec
@@ -147,10 +151,12 @@ INSERT OR IGNORE INTO Specifications (repo_name, url, load_path, file, \
(define (db-add-derivation db job)
"Store a derivation result in database DB and return its ID."
(sqlite-exec db "\
-INSERT OR IGNORE INTO Derivations (derivation, job_name, evaluation)\
- VALUES ('~A', '~A', '~A');"
+INSERT OR IGNORE INTO Derivations (derivation, job_name, system, nix_name, evaluation)\
+ VALUES ('~A', '~A', '~A', '~A', '~A');"
(assq-ref job #:derivation)
(assq-ref job #:job-name)
+ (assq-ref job #:system)
+ (assq-ref job #:nix-name)
(assq-ref job #:eval-id)))
(define (db-get-derivation db id)
@@ -182,15 +188,126 @@ string."
(else (loop (cons char chars)))))))
(define (db-add-build db build)
- "Store BUILD in database DB."
- (sqlite-exec db "\
-INSERT INTO Builds (derivation, evaluation, log, output)\
- VALUES ('~A', '~A', '~A', '~A');"
- (assq-ref build #:derivation)
- (assq-ref build #:eval-id)
- (assq-ref build #:log)
- (assq-ref build #:output))
- (last-insert-rowid db))
+ "Store BUILD in database DB. BUILS eventual outputs are stored
+in the OUTPUTS table."
+ (let* ((build-exec
+ (sqlite-exec db "\
+INSERT INTO Builds (derivation, evaluation, log, status, timestamp, starttime, stoptime)\
+ VALUES ('~A', '~A', '~A', '~A', '~A', '~A', '~A');"
+ (assq-ref build #:derivation)
+ (assq-ref build #:eval-id)
+ (assq-ref build #:log)
+ (assq-ref build #:status)
+ (assq-ref build #:timestamp)
+ (assq-ref build #:starttime)
+ (assq-ref build #:stoptime)))
+ (build-id (last-insert-rowid db)))
+ (for-each (lambda (output)
+ (match output
+ ((name . path)
+ (sqlite-exec db "\
+INSERT INTO Outputs (build, name, path) VALUES ('~A', '~A', '~A');"
+ build-id name path))))
+ (assq-ref build #:outputs))
+ build-id))
+
+(define (db-get-outputs db build-id)
+ "Retrieve the OUTPUTS of the build identified by BUILD-ID in DB database."
+ (let loop ((rows
+ (sqlite-exec db "SELECT name, path FROM Outputs WHERE build='~A';"
+ build-id))
+ (outputs '()))
+ (match rows
+ (() outputs)
+ ((#(name path)
+ . rest)
+ (loop rest
+ (cons `(,name . ((#:path . ,path)))
+ outputs))))))
+
+(define db-build-request "\
+SELECT Builds.id, Builds.timestamp, Builds.starttime, Builds.stoptime, Builds.log, Builds.status,\
+Derivations.job_name, Derivations.system, Derivations.nix_name,\
+Specifications.repo_name, Specifications.branch \
+FROM Builds \
+INNER JOIN Derivations ON Builds.derivation = Derivations.derivation and Builds.evaluation = Derivations.evaluation \
+INNER JOIN Evaluations ON Derivations.evaluation = Evaluations.id \
+INNER JOIN Specifications ON Evaluations.specification = Specifications.repo_name")
+
+(define (db-format-build db build)
+ (match build
+ (#(id timestamp starttime stoptime log status job-name system
+ nix-name repo-name branch)
+ `((#:id . ,id)
+ (#:timestamp . ,timestamp)
+ (#:starttime . ,starttime)
+ (#:stoptime . ,stoptime)
+ (#:log . ,log)
+ (#:status . ,status)
+ (#:job-name . ,job-name)
+ (#:system . ,system)
+ (#:nix-name . ,nix-name)
+ (#:repo-name . ,repo-name)
+ (#:outputs . ,(db-get-outputs db id))
+ (#:branch . ,branch)))))
+
+(define (db-get-build db id)
+ "Retrieve a build in database DB which corresponds to ID."
+ (let ((res (sqlite-exec db (string-append db-build-request
+ " WHERE Builds.id='~A';") id)))
+ (match res
+ ((build)
+ (db-format-build db build))
+ (() #f))))
+
+(define (db-get-builds db filters)
+ "Retrieve all builds in database DB which are matched by given FILTERS.
+FILTERS is an
This message was truncated. Download the full message here.