73 lines
1.9 KiB
Bash
73 lines
1.9 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
json_escape() {
|
|
local raw="${1//\\/\\\\}"
|
|
raw="${raw//\"/\\\"}"
|
|
raw="${raw//$'\n'/\\n}"
|
|
echo "$raw"
|
|
}
|
|
|
|
send_report() {
|
|
local status="$1"
|
|
local message="${2:-}"
|
|
local step="${3:-}"
|
|
local step_index="${4:-}"
|
|
local step_total="${5:-}"
|
|
local step_percent="null"
|
|
local step_index_json="null"
|
|
local step_total_json="null"
|
|
local step_percent_json="null"
|
|
|
|
if [[ "$step_index" =~ ^[0-9]+$ ]]; then
|
|
step_index_json="$step_index"
|
|
fi
|
|
|
|
if [[ "$step_total" =~ ^[0-9]+$ && "$step_total" -gt 0 ]]; then
|
|
step_total_json="$step_total"
|
|
fi
|
|
|
|
if [[ "$step_index_json" != "null" && "$step_total_json" != "null" ]]; then
|
|
if (( step_index > step_total )); then
|
|
step_percent_json=100
|
|
else
|
|
step_percent=$(( step_index * 100 / step_total ))
|
|
step_percent_json="$step_percent"
|
|
fi
|
|
fi
|
|
|
|
if [[ -z "${REPORT_URL:-}" || -z "${REPORT_TOKEN:-}" ]]; then
|
|
return 0
|
|
fi
|
|
|
|
local payload
|
|
payload="$(printf '{ "status":"%s", "core":"%s", "dir":"%s", "port":%s, "protocol":"%s", "service":"%s", "message":"%s", "step":"%s", "step_index":%s, "step_total":%s, "step_percent":%s }' \
|
|
"$(json_escape "$status")" \
|
|
"$(json_escape "${CORE}")" \
|
|
"$(json_escape "${SERVER_DIR}")" \
|
|
"${SERVER_PORT}" \
|
|
"$(json_escape "${CORE_PROTOCOL}")" \
|
|
"$(json_escape "${SERVICE_MODE_ACTIVE}")" \
|
|
"$(json_escape "$message")" \
|
|
"$(json_escape "$step")" \
|
|
"$step_index_json" \
|
|
"$step_total_json" \
|
|
"$step_percent_json")"
|
|
|
|
local attempt
|
|
for attempt in 1 2; do
|
|
if [[ "${DRY_RUN}" == "yes" ]]; then
|
|
info "$(t report_dry_run "$payload")"
|
|
return 0
|
|
fi
|
|
if curl -fsSL -X POST "$REPORT_URL" \
|
|
-H "Authorization: Bearer ${REPORT_TOKEN}" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$payload" >/dev/null 2>&1; then
|
|
return 0
|
|
fi
|
|
sleep 1
|
|
done
|
|
|
|
warn "$(t report_failed)"
|
|
}
|