From 8ead8ec8ac5e4bad5d4f03de005f89212fd8a495 Mon Sep 17 00:00:00 2001 From: Malfurious Date: Thu, 9 Nov 2023 13:55:16 -0500 Subject: dmt: Implement API endpoint /api/status This returns high-level information describing the current state of the system. The intent of the `version` field is primarily to act as a "page should be reloaded" indicator to the front-end javascript code. This would inform the client to reload (possibly) new markup or script whenever the version field changes. This field could return an accurate version spec of cychedelic or DMT, however just returns a random string at the moment which is tied to the DMT docker image build. Signed-off-by: Malfurious --- dmt/Dockerfile | 1 + dmt/dmt | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100755 dmt/dmt diff --git a/dmt/Dockerfile b/dmt/Dockerfile index 6d53838..f440d5e 100644 --- a/dmt/Dockerfile +++ b/dmt/Dockerfile @@ -14,6 +14,7 @@ ENV GOPATH="/usr/local" COPY . . RUN go install github.com/uriel/cgd@latest +RUN tr -dc a-z /version EXPOSE 80 CMD ["cgd", "-a", ":80", "-c", "dmt"] diff --git a/dmt/dmt b/dmt/dmt new file mode 100755 index 0000000..9de6e37 --- /dev/null +++ b/dmt/dmt @@ -0,0 +1,36 @@ +#!/bin/bash -a + +# cychedelic DMT daemon +# cychedelic is an automated Continuous Deployment solution. See the README for +# more information. + +CYCHE_LOG_DIR="/data/logs" +CYCHE_STATUS_FILE="/data/status" +CYCHE_VERSION_FILE="/version" + +integer() { + [ "$1" -eq "$1" ] >/dev/null 2>&1 +} + +newest_job() { + ls -A "$CYCHE_LOG_DIR" | sort -rn | head -n1 +} + +oldest_job() { + ls -A "$CYCHE_LOG_DIR" | sort -n | head -n1 +} + +api_status() { + status=$(cat "$CYCHE_STATUS_FILE") + [ "$status" == "" ] && active="false" || active="true" + integer "$status" && job="$status" || job="-1" + + printf 'Content-type: text/json\n\n{' + printf '"version":"%s",' "$(cat "$CYCHE_VERSION_FILE")" + printf '"active":%s,' "$active" + printf '"job":%i,' "$job" + printf '"newest":%i,' "$(newest_job)" + printf '"oldest":%i}' "$(oldest_job)" +} + +api_status -- cgit v1.2.3