187 lines
4.7 KiB
Bash
Executable file
187 lines
4.7 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# Reusable HTTP and graph client helpers for local unix-socket amduatd usage.
|
|
|
|
graph_helpers_init() {
|
|
if [[ $# -lt 1 ]]; then
|
|
echo "usage: graph_helpers_init ROOT_DIR" >&2
|
|
return 1
|
|
fi
|
|
GRAPH_HELPERS_ROOT_DIR="$1"
|
|
GRAPH_HELPERS_HTTP="${GRAPH_HELPERS_ROOT_DIR}/build/amduatd_http_unix"
|
|
GRAPH_HELPERS_USE_HTTP=0
|
|
|
|
if command -v curl >/dev/null 2>&1; then
|
|
if curl --help 2>/dev/null | grep -q -- '--unix-socket'; then
|
|
GRAPH_HELPERS_USE_HTTP=0
|
|
else
|
|
GRAPH_HELPERS_USE_HTTP=1
|
|
fi
|
|
else
|
|
GRAPH_HELPERS_USE_HTTP=1
|
|
fi
|
|
|
|
if [[ "${GRAPH_HELPERS_USE_HTTP}" -eq 1 && ! -x "${GRAPH_HELPERS_HTTP}" ]]; then
|
|
echo "missing http transport (need curl --unix-socket or build/amduatd_http_unix)" >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
graph_http_get() {
|
|
local sock="$1"
|
|
local path="$2"
|
|
shift 2
|
|
if [[ "${GRAPH_HELPERS_USE_HTTP}" -eq 1 ]]; then
|
|
"${GRAPH_HELPERS_HTTP}" --sock "${sock}" --method GET --path "${path}" "$@"
|
|
else
|
|
curl --silent --show-error --fail \
|
|
--unix-socket "${sock}" \
|
|
"$@" \
|
|
"http://localhost${path}"
|
|
fi
|
|
}
|
|
|
|
graph_http_get_allow() {
|
|
local sock="$1"
|
|
local path="$2"
|
|
shift 2
|
|
if [[ "${GRAPH_HELPERS_USE_HTTP}" -eq 1 ]]; then
|
|
"${GRAPH_HELPERS_HTTP}" --sock "${sock}" --method GET --path "${path}" --allow-status "$@"
|
|
else
|
|
curl --silent --show-error \
|
|
--unix-socket "${sock}" \
|
|
"$@" \
|
|
"http://localhost${path}"
|
|
fi
|
|
}
|
|
|
|
graph_http_post() {
|
|
local sock="$1"
|
|
local path="$2"
|
|
local data="$3"
|
|
shift 3
|
|
if [[ "${GRAPH_HELPERS_USE_HTTP}" -eq 1 ]]; then
|
|
"${GRAPH_HELPERS_HTTP}" --sock "${sock}" --method POST --path "${path}" --data "${data}" "$@"
|
|
else
|
|
curl --silent --show-error --fail \
|
|
--unix-socket "${sock}" \
|
|
"$@" \
|
|
--data-binary "${data}" \
|
|
"http://localhost${path}"
|
|
fi
|
|
}
|
|
|
|
graph_http_post_allow() {
|
|
local sock="$1"
|
|
local path="$2"
|
|
local data="$3"
|
|
shift 3
|
|
if [[ "${GRAPH_HELPERS_USE_HTTP}" -eq 1 ]]; then
|
|
"${GRAPH_HELPERS_HTTP}" --sock "${sock}" --method POST --path "${path}" --data "${data}" --allow-status "$@"
|
|
else
|
|
curl --silent --show-error \
|
|
--unix-socket "${sock}" \
|
|
"$@" \
|
|
--data-binary "${data}" \
|
|
"http://localhost${path}"
|
|
fi
|
|
}
|
|
|
|
graph_wait_for_ready() {
|
|
local sock="$1"
|
|
local pid="$2"
|
|
local log_path="$3"
|
|
local i
|
|
for i in $(seq 1 120); do
|
|
if ! kill -0 "${pid}" >/dev/null 2>&1; then
|
|
if [[ -f "${log_path}" ]] && grep -q "bind: Operation not permitted" "${log_path}"; then
|
|
echo "skip: bind not permitted for unix socket" >&2
|
|
return 77
|
|
fi
|
|
if [[ -f "${log_path}" ]]; then
|
|
cat "${log_path}" >&2
|
|
fi
|
|
return 1
|
|
fi
|
|
if [[ -S "${sock}" ]] && graph_http_get "${sock}" "/v1/meta" >/dev/null 2>&1; then
|
|
return 0
|
|
fi
|
|
sleep 0.1
|
|
done
|
|
return 1
|
|
}
|
|
|
|
graph_batch_ingest() {
|
|
local sock="$1"
|
|
local space="$2"
|
|
local payload="$3"
|
|
graph_http_post "${sock}" "/v2/graph/batch" "${payload}" \
|
|
--header "Content-Type: application/json" \
|
|
--header "X-Amduat-Space: ${space}"
|
|
}
|
|
|
|
graph_changes_sync_once() {
|
|
local sock="$1"
|
|
local space="$2"
|
|
local cursor="$3"
|
|
local limit="$4"
|
|
local path="/v2/graph/changes?limit=${limit}"
|
|
if [[ -n "${cursor}" ]]; then
|
|
path+="&since_cursor=${cursor}"
|
|
fi
|
|
graph_http_get "${sock}" "${path}" --header "X-Amduat-Space: ${space}"
|
|
}
|
|
|
|
graph_subgraph_fetch() {
|
|
local sock="$1"
|
|
local space="$2"
|
|
local root="$3"
|
|
local max_depth="$4"
|
|
local predicates="${5:-}"
|
|
local path="/v2/graph/subgraph?roots[]=${root}&max_depth=${max_depth}&dir=outgoing&limit_nodes=256&limit_edges=256"
|
|
if [[ -n "${predicates}" ]]; then
|
|
path+="&predicates[]=${predicates}"
|
|
fi
|
|
graph_http_get "${sock}" "${path}" --header "X-Amduat-Space: ${space}"
|
|
}
|
|
|
|
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
|
if [[ $# -lt 1 ]]; then
|
|
echo "usage: $0 COMMAND ..." >&2
|
|
echo "commands: batch-ingest, sync-once, subgraph" >&2
|
|
exit 2
|
|
fi
|
|
cmd="$1"
|
|
shift
|
|
: "${AMDUATD_ROOT:?set AMDUATD_ROOT to repo root}"
|
|
graph_helpers_init "${AMDUATD_ROOT}"
|
|
case "${cmd}" in
|
|
batch-ingest)
|
|
if [[ $# -ne 3 ]]; then
|
|
echo "usage: $0 batch-ingest SOCK SPACE PAYLOAD_JSON" >&2
|
|
exit 2
|
|
fi
|
|
graph_batch_ingest "$1" "$2" "$3"
|
|
;;
|
|
sync-once)
|
|
if [[ $# -ne 4 ]]; then
|
|
echo "usage: $0 sync-once SOCK SPACE CURSOR LIMIT" >&2
|
|
exit 2
|
|
fi
|
|
graph_changes_sync_once "$1" "$2" "$3" "$4"
|
|
;;
|
|
subgraph)
|
|
if [[ $# -lt 4 || $# -gt 5 ]]; then
|
|
echo "usage: $0 subgraph SOCK SPACE ROOT MAX_DEPTH [PREDICATE]" >&2
|
|
exit 2
|
|
fi
|
|
graph_subgraph_fetch "$1" "$2" "$3" "$4" "${5:-}"
|
|
;;
|
|
*)
|
|
echo "unknown command: ${cmd}" >&2
|
|
exit 2
|
|
;;
|
|
esac
|
|
fi
|