blob: ca44b78566bc5a59ea1a02f6855624f2973b8dc8 (
plain) (
tree)
|
|
#!/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"
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
}
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"
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_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_log() {
[ -z "$1" ] && job="$(newest_job)" || job="$1"
jobdir="$CYCHE_LOG_DIR/$job"
if [ -d "$jobdir" ]; then
printf 'Content-type: text/plain\n\n'
cat "$jobdir/log"
fi
}
api_log
|