CHAT_OUTPUT_BEGIN
COMMAND_ID=READER-VM130-EXEC-INTERFACE-AUDIT-332
STATUS=OK
RC=0
HOST=pve01
MODE=read-only
COMPONENT=cluster-audit
REFERENCE_REGISTER_CHECK=OK
REFERENCE_SHA256=5763f2f8edc75fcf6f3951f8c95896d1cc112ceccb6cf5b64a383f540a4deb8e
ERROR_REGISTER_CHECK=OK
ERROR_REGISTER_SHA256=08b3a769b8e38eaa1acec915352fb328180d70caa7ff6cd9d1ec0da426262752
COMMAND_SHA256=c3afb515a991225c64ea90bd9e285a44f09af255097fca7a30cae3c70af84c3e
DUPLICATE_FAILED_COMMAND_BLOCKED=false
EXECUTION_STARTED=true
CHANGES_MADE=false
SANITIZED=yes
SECRETS_INCLUDED=no
PRIVATE_ADDRESSES_INCLUDED=no
RAW_EVIDENCE_SHA256=761533b89826503b8ca2118991abaa2198d219e3e62eb6b18caf6d6b4518ad56
SANITIZED_OUTPUT_SHA256=761533b89826503b8ca2118991abaa2198d219e3e62eb6b18caf6d6b4518ad56
OUTPUT_BEGIN
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/test
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/sha256sum
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/awk
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/sed
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/bash
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/stat
DETAIL=VM130_EXEC_SHA256=1959773393723b013af7c93cd29a7b833fd607c903d044e2fcef6accaa29c275
DETAIL=VM130_EXEC_INTEGRITY=PASS
DETAIL=VM130_EXEC_METADATA=root:root|750|1333|regular file
DETAIL=VM130_EXEC_FIRST_LINE=#!/usr/bin/bash
DETAIL=VM130_EXEC_SYNTAX=PASS
DETAIL=VM130_EXEC_LINE_COUNT=53
DETAIL=VM130_EXEC_SOURCE_BEGIN
#!/usr/bin/bash
set -Eeuo pipefail

readonly SSH=/usr/bin/ssh
readonly JQ=/usr/bin/jq
readonly MKTEMP=/usr/bin/mktemp
readonly RM=/usr/bin/rm
readonly CAT=/usr/bin/cat
readonly HOST=root@pve03
readonly VMID=130

if (($# == 0)); then
    echo "VM130_EXEC_ERROR=NO_COMMAND" >&2
    exit 64
fi

tmpdir="$("$MKTEMP" -d /tmp/homelab-vm130-exec.XXXXXX)"
cleanup() {
    "$RM" -rf -- "$tmpdir"
}
trap cleanup EXIT

printf -v remote_cmd "%q " /usr/sbin/qm guest exec "$VMID" -- "$@"

set +e
"$SSH" -o BatchMode=yes -o ConnectTimeout=8 "$HOST" "$remote_cmd" >"$tmpdir/qga.json" 2>"$tmpdir/ssh.err"
ssh_rc=$?
set -e

if ((ssh_rc != 0)); then
    echo "VM130_EXEC_ERROR=SSH_FAILURE" >&2
    echo "VM130_EXEC_SSH_RC=$ssh_rc" >&2
    "$CAT" "$tmpdir/ssh.err" >&2
    exit 125
fi

if ! "$JQ" -e 'type=="object" and (.exited == 1) and (.exitcode | type == "number")' "$tmpdir/qga.json" >/dev/null 2>&1; then
    echo "VM130_EXEC_ERROR=INVALID_QGA_JSON" >&2
    "$CAT" "$tmpdir/ssh.err" >&2
    "$CAT" "$tmpdir/qga.json" >&2
    exit 126
fi

"$JQ" -j '."out-data" // ""' "$tmpdir/qga.json"
"$JQ" -j '."err-data" // ""' "$tmpdir/qga.json" >&2

guest_rc="$("$JQ" -r '.exitcode' "$tmpdir/qga.json")"
if [[ ! "$guest_rc" =~ ^[0-9]+$ ]] || ((guest_rc > 255)); then
    echo "VM130_EXEC_ERROR=INVALID_GUEST_EXITCODE" >&2
    exit 126
fi

exit "$guest_rc"
DETAIL=VM130_EXEC_SOURCE_END
DETAIL=VM130_EXEC_SOURCE_TRUNCATED=NO
CHANGES_MADE=NO

OUTPUT_END
CHAT_OUTPUT_END
