Write NC connector status to web-readable status directory

This commit is contained in:
Terranom674
2026-08-17 22:18:38 +02:00
parent 1db97e77fb
commit cf65bc0971

View File

@@ -37,6 +37,10 @@ if [[ -z "$CONNECTION_ID" ]]; then
fi fi
fi fi
PUBLIC_STATUS_DIR="/var/lib/bratonien-tools/nc-connector-status"
PUBLIC_STATUS_FILE="$PUBLIC_STATUS_DIR/connection-$CONNECTION_ID.json"
install -d -m 0755 "$PUBLIC_STATUS_DIR"
exec 9>"$LOCK_FILE" exec 9>"$LOCK_FILE"
flock -n 9 || exit 0 flock -n 9 || exit 0
@@ -49,11 +53,10 @@ ERROR_DETAIL=""
write_status() { write_status() {
local state="$1" message="$2" local state="$1" message="$2"
python3 - "$STATUS_FILE" "$state" "$message" "$AUTH_MODE" "$API_STATE" "$API_MESSAGE" "$FALLBACK_STATE" "$FALLBACK_MESSAGE" "$ERROR_DETAIL" <<'PY' python3 - "$STATUS_FILE" "$PUBLIC_STATUS_FILE" "$state" "$message" "$AUTH_MODE" "$API_STATE" "$API_MESSAGE" "$FALLBACK_STATE" "$FALLBACK_MESSAGE" "$ERROR_DETAIL" <<'PY'
import json, os, sys, tempfile, time import json, os, sys, tempfile, time
(path, state, message, auth_mode, api_state, api_message, (path, public_path, state, message, auth_mode, api_state, api_message,
fallback_state, fallback_message, error_detail) = sys.argv[1:] fallback_state, fallback_message, error_detail) = sys.argv[1:]
os.makedirs(os.path.dirname(path), exist_ok=True)
payload = { payload = {
"state": state, "state": state,
"message": message, "message": message,
@@ -63,12 +66,18 @@ payload = {
"fallback": {"state": fallback_state, "message": fallback_message}, "fallback": {"state": fallback_state, "message": fallback_message},
"error_detail": error_detail, "error_detail": error_detail,
} }
fd, temporary = tempfile.mkstemp(dir=os.path.dirname(path))
with os.fdopen(fd, "w", encoding="utf-8") as handle: def write_json(target):
json.dump(payload, handle, ensure_ascii=False) os.makedirs(os.path.dirname(target), exist_ok=True)
handle.write("\n") fd, temporary = tempfile.mkstemp(dir=os.path.dirname(target))
os.chmod(temporary, 0o644) with os.fdopen(fd, "w", encoding="utf-8") as handle:
os.replace(temporary, path) json.dump(payload, handle, ensure_ascii=False)
handle.write("\n")
os.chmod(temporary, 0o644)
os.replace(temporary, target)
write_json(path)
write_json(public_path)
PY PY
} }