From bc9a915a2709fe7c50f826183ce4d2f6b1816052 Mon Sep 17 00:00:00 2001 From: Malfurious Date: Thu, 9 Nov 2023 18:01:47 -0500 Subject: dmt: Implement API endpoint /api/job/{jobnum} This returns all details related to the requested job number as well as a tail of the job log file. If `jobnum` is omited, details of the latest job are returned. Signed-off-by: Malfurious --- dmt/config.sh | 2 ++ dmt/dmt | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 dmt/config.sh diff --git a/dmt/config.sh b/dmt/config.sh new file mode 100644 index 0000000..a19e14f --- /dev/null +++ b/dmt/config.sh @@ -0,0 +1,2 @@ +# Number of lines of text to show per job on the main page +CYCHE_LOG_TAIL_LENGTH=25 diff --git a/dmt/dmt b/dmt/dmt index 9de6e37..7923580 100755 --- a/dmt/dmt +++ b/dmt/dmt @@ -7,11 +7,17 @@ CYCHE_LOG_DIR="/data/logs" CYCHE_STATUS_FILE="/data/status" CYCHE_VERSION_FILE="/version" +source config.sh integer() { [ "$1" -eq "$1" ] >/dev/null 2>&1 } +escape() { + sed 's/\\/\\\\/g; s/\r/\\r/g; s/\t/\\t/g; s/"/\\"/g' \ + | awk '{printf "%s\\n", $0}' +} + newest_job() { ls -A "$CYCHE_LOG_DIR" | sort -rn | head -n1 } @@ -20,6 +26,10 @@ oldest_job() { ls -A "$CYCHE_LOG_DIR" | sort -n | head -n1 } +log_tail() { + tail -n "$CYCHE_LOG_TAIL_LENGTH" "$CYCHE_LOG_DIR/$1/log" | sed 's/\r//g' +} + api_status() { status=$(cat "$CYCHE_STATUS_FILE") [ "$status" == "" ] && active="false" || active="true" @@ -33,4 +43,26 @@ api_status() { printf '"oldest":%i}' "$(oldest_job)" } -api_status +api_job() { + [ -z "$1" ] && job="$(newest_job)" || job="$1" + jobdir="$CYCHE_LOG_DIR/$job" + + if [ -d "$jobdir" ]; then + status=$(cat "$CYCHE_STATUS_FILE") + + result=$( ([ "$status" == "$job" ] && echo "active") \ + || ([ -f "$jobdir/fail" ] && echo "fail") \ + || echo "pass" ) + + printf 'Content-type: text/json\n\n{' + printf '"job":%i,' "$job" + printf '"hash":"%s",' "$(cat "$jobdir/hash")" + printf '"reason":"%s",' "$(cat "$jobdir/reason")" + printf '"service":"%s",' "$(cat "$jobdir/service")" + printf '"time":%i,' "$(cat "$jobdir/time")" + printf '"result":"%s",' "$result" + printf '"log":"%s"}' "$(log_tail "$job" | escape)" + fi +} + +api_job -- cgit v1.2.3