Files
telegraf-bcachefs-input/paste_log.sh
T

39 lines
1.2 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"
}
if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
paste_log "$@"
fi