aboutsummaryrefslogtreecommitdiff
path: root/brep
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2018-08-23 22:29:35 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2018-08-28 21:46:41 +0300
commit8a094bb0481a9c53646cc15db2e8acecafc3d10c (patch)
tree4fd7012b6a26eb852d42fba8b52bfcf8f1cf2fdd /brep
parent7e0e141273032c7afc1a9129512aa42c672fcf5d (diff)
Add basic support for CI request handling
Diffstat (limited to 'brep')
-rw-r--r--brep/.gitignore3
-rw-r--r--brep/handler/.gitignore1
-rw-r--r--brep/handler/buildfile2
-rw-r--r--brep/handler/ci/.gitignore1
-rw-r--r--brep/handler/ci/buildfile11
-rw-r--r--brep/handler/ci/ci-dir.in93
-rw-r--r--brep/handler/ci/ci.bash.in41
-rw-r--r--brep/handler/submit/.gitignore3
-rw-r--r--brep/handler/submit/buildfile2
9 files changed, 152 insertions, 5 deletions
diff --git a/brep/.gitignore b/brep/.gitignore
new file mode 100644
index 0000000..f6635ca
--- /dev/null
+++ b/brep/.gitignore
@@ -0,0 +1,3 @@
+# All our bash modules are generated from .in files so ignore them wholesale.
+#
+*.bash
diff --git a/brep/handler/.gitignore b/brep/handler/.gitignore
deleted file mode 100644
index e3cf716..0000000
--- a/brep/handler/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-handler.bash
diff --git a/brep/handler/buildfile b/brep/handler/buildfile
index 5223b5c..5f64251 100644
--- a/brep/handler/buildfile
+++ b/brep/handler/buildfile
@@ -5,6 +5,6 @@
import mods = libbutl.bash%bash{manifest-parser}
import mods += libbutl.bash%bash{manifest-serializer}
-./: bash{handler} submit/
+./: bash{handler} submit/ ci/
bash{handler}: in{handler} $mods
diff --git a/brep/handler/ci/.gitignore b/brep/handler/ci/.gitignore
new file mode 100644
index 0000000..f31b542
--- /dev/null
+++ b/brep/handler/ci/.gitignore
@@ -0,0 +1 @@
+brep-ci-dir
diff --git a/brep/handler/ci/buildfile b/brep/handler/ci/buildfile
new file mode 100644
index 0000000..c45a79b
--- /dev/null
+++ b/brep/handler/ci/buildfile
@@ -0,0 +1,11 @@
+# file : brep/handler/ci/buildfile
+# copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
+# license : MIT; see accompanying LICENSE file
+
+./: exe{brep-ci-dir}
+
+include ../
+
+exe{brep-ci-dir}: in{ci-dir} bash{ci} ../bash{handler}
+
+bash{ci}: in{ci} ../bash{handler}
diff --git a/brep/handler/ci/ci-dir.in b/brep/handler/ci/ci-dir.in
new file mode 100644
index 0000000..6a4f0af
--- /dev/null
+++ b/brep/handler/ci/ci-dir.in
@@ -0,0 +1,93 @@
+#!/usr/bin/env bash
+
+# file : brep/handler/ci/ci-dir.in
+# copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
+# license : MIT; see accompanying LICENSE file
+
+# Simple package CI request handler with directory storage.
+#
+# Keep the CI request directory unless simulating. Write the CI result
+# manifest to stdout.
+#
+usage="usage: $0 <dir>"
+
+verbose= #true
+
+trap "{ exit 1; }" ERR
+set -o errtrace # Trap ERR in functions.
+
+@import brep/handler/handler@
+@import brep/handler/ci/ci@
+
+if [ "$#" != 1 ]; then
+ error "$usage"
+fi
+
+# CI request data directory (last and the only argument).
+#
+data_dir="${!#/}"
+
+if [ -z "$data_dir" ]; then
+ error "$usage"
+fi
+
+if [ ! -d "$data_dir" ]; then
+ error "'$data_dir' does not exist or is not a directory"
+fi
+
+reference="$(basename "$data_dir")"
+
+# Parse the CI request manifest and obtain the repository URL, package names
+# with optional versions, as well as the simulate value.
+#
+manifest_parser_start "$data_dir/request.manifest"
+
+repository=
+packages=()
+simulate=
+
+while IFS=: read -ru "$manifest_parser_ofd" -d '' n v; do
+ case "$n" in
+ repository) repository="$v" ;;
+ package) packages+=("$v") ;;
+ simulate) simulate="$v" ;;
+ esac
+done
+
+manifest_parser_finish
+
+if [ -z "$repository" ]; then
+ error "repository manifest value expected"
+fi
+
+if [ -n "$simulate" -a "$simulate" != "success" ]; then
+ exit_with_manifest 400 "unrecognized simulation outcome '$simulate'"
+fi
+
+# Produce the bpkg-build(1)-like package spec for tracing.
+#
+spec=
+for p in "${packages[@]}"; do
+ if [ -n "$spec" ]; then
+ spec="$spec,"
+ fi
+ spec="$spec$p"
+done
+
+if [ -n "$spec" ]; then
+ spec="$spec@"
+fi
+
+spec="$spec$repository"
+
+if [ -n "$simulate" ]; then
+ rm -r "$data_dir"
+ trace "CI request for '$spec' is simulated"
+else
+ trace "CI request for '$spec' is queued"
+fi
+
+# The spec normally contains the full commit id and so feels too hairy for
+# including in the message.
+#
+exit_with_manifest 200 "CI request is queued"
diff --git a/brep/handler/ci/ci.bash.in b/brep/handler/ci/ci.bash.in
new file mode 100644
index 0000000..023e98e
--- /dev/null
+++ b/brep/handler/ci/ci.bash.in
@@ -0,0 +1,41 @@
+# file : brep/handler/ci/ci.bash.in
+# copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
+# license : MIT; see accompanying LICENSE file
+
+# Utility functions useful for implementing CI request handlers.
+
+if [ "$brep_handler_ci" ]; then
+ return 0
+else
+ brep_handler_ci=true
+fi
+
+@import brep/handler/handler@
+
+# Serialize the CI result manifest to stdout and exit the (sub-)shell with the
+# zero status.
+#
+reference= # Should be assigned by the handler when becomes available.
+
+function exit_with_manifest () # <status> <message>
+{
+ trace_func "$@"
+
+ local sts="$1"
+ local msg="$2"
+
+ manifest_serializer_start
+
+ manifest_serialize "" "1" # Start of manifest.
+ manifest_serialize "status" "$sts"
+ manifest_serialize "message" "$msg"
+
+ if [ -n "$reference" ]; then
+ manifest_serialize "reference" "$reference"
+ elif [ "$sts" == "200" ]; then
+ error "no reference for code $sts"
+ fi
+
+ manifest_serializer_finish
+ run exit 0
+}
diff --git a/brep/handler/submit/.gitignore b/brep/handler/submit/.gitignore
index ef91424..cbbd541 100644
--- a/brep/handler/submit/.gitignore
+++ b/brep/handler/submit/.gitignore
@@ -1,5 +1,2 @@
-submit.bash
-submit-git.bash
-
brep-submit-dir
brep-submit-git
diff --git a/brep/handler/submit/buildfile b/brep/handler/submit/buildfile
index b110a1d..fd9fe14 100644
--- a/brep/handler/submit/buildfile
+++ b/brep/handler/submit/buildfile
@@ -4,6 +4,8 @@
./: exe{brep-submit-dir} exe{brep-submit-git}
+include ../
+
exe{brep-submit-dir}: in{submit-dir} bash{submit} ../bash{handler}
exe{brep-submit-git}: in{submit-git} \