Files
telegraf-bcachefs-input/paste_log.sh
T

74 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# Source this file for paste_log(), or execute it with a filename.
paste_log()
{
if [[ $# -ne 1 || ! -f ${1:-} ]]; then
echo 'Usage: paste_log <existing-file>' >&2
return 2
fi
local pods pod name temporary status
pods=$(kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}') || return
local -a matches=()
while IFS= read -r pod; do
[[ $pod == brunner-ninja* ]] && matches+=("$pod")
done <<< "$pods"
if [[ ${#matches[@]} -ne 1 ]]; then
echo "Expected one brunner-ninja pod, found ${#matches[@]}" >&2
return 1
fi
temporary=$(mktemp "${TMPDIR:-/tmp}/XXXXXXXXXXXX.log") || return
name=${temporary##*/}
# Resolve local paths so kubectl does not interpret a colon as a pod path.
local source
source=$(realpath -- "$1") || { rm -f -- "$temporary"; return 1; }
if kubectl cp "$source" "${matches[0]}:/srv/http/pastes/$name"; then
status=0
else
status=$?
fi
rm -f -- "$temporary"
((status == 0)) || return "$status"
printf 'https://brunner.ninja/pastes/%s\n' "$name"
}
# Upload a completed report under an unpredictable directory name. The remote
# rename makes the published URL visible only after the entire copy succeeds.
paste_directory()
{
if [[ $# -ne 1 || ! -f $1/index.html || ! -f $1/kent_report.log || ! -d $1/outputs ]]; then
echo 'Usage: paste_directory <report-directory>' >&2
return 2
fi
local pods pod source temporary name status
local -a matches=()
pods=$(kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}') || return
while IFS= read -r pod; do
[[ $pod == brunner-ninja* ]] && matches+=("$pod")
done <<< "$pods"
if [[ ${#matches[@]} -ne 1 ]]; then
echo "Expected one brunner-ninja pod, found ${#matches[@]}" >&2
return 1
fi
source=$(realpath -- "$1") || return
temporary=$(mktemp -d "${TMPDIR:-/tmp}/kent-report-XXXXXXXXXXXX") || return
name=${temporary##*/}
# Only publish report artifacts, not raw trace.dat or unrelated local files.
if cp -R -- "$source/index.html" "$source/kent_report.log" "$source/manifest.json" "$source/outputs" "$temporary/" &&
chmod -R a+rX "$temporary" &&
kubectl cp "$temporary" "${matches[0]}:/srv/http/pastes/.$name.uploading" &&
kubectl exec "${matches[0]}" -- mv -- "/srv/http/pastes/.$name.uploading" "/srv/http/pastes/$name"; then
status=0
else
status=$?
fi
rm -rf -- "$temporary"
((status == 0)) || return "$status"
printf 'https://brunner.ninja/pastes/%s/\n' "$name"
}
if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
paste_log "$@"
fi