fix(provision): reconcile channel pricing and hosted access
This commit is contained in:
@@ -15,6 +15,10 @@ REMOTE="${REMOTE:-ubuntu@43.155.133.187}"
|
||||
CRM_BASE="${CRM_BASE:-http://127.0.0.1:18088}"
|
||||
HOST_BASE="${HOST_BASE:-http://127.0.0.1:18087}"
|
||||
CRM_HOST_BASE="${CRM_HOST_BASE:-$HOST_BASE}"
|
||||
HOST_NAME="${HOST_NAME:-remote43-current-host}"
|
||||
REMOTE_HOST_ENV_FILE="${REMOTE_HOST_ENV_FILE:-/home/ubuntu/sub2api-host-validation-fresh-deepseek-20260519_115244/.env}"
|
||||
REMOTE_PG_CONTAINER="${REMOTE_PG_CONTAINER:-sub2api-relaymgr-pg}"
|
||||
REMOTE_REDIS_CONTAINER="${REMOTE_REDIS_CONTAINER:-sub2api-relaymgr-redis}"
|
||||
PACK_PATH="${PACK_PATH:-/home/ubuntu/sub2api-cn-relay-manager/packs/openai-cn-pack}"
|
||||
ROOT="${ROOT:-$ROOT_DIR/artifacts/real-host-acceptance}"
|
||||
ART="${ART:-$ROOT/$(date +%Y%m%d_%H%M%S)_remote43_${provider_id}_key_import}"
|
||||
@@ -22,6 +26,8 @@ MIN_BALANCE="${MIN_BALANCE:-10}"
|
||||
SUBSCRIPTION_DAYS="${SUBSCRIPTION_DAYS:-30}"
|
||||
SUBSCRIPTION_NOTES="${SUBSCRIPTION_NOTES:-hermes remote subscription validation}"
|
||||
mkdir -p "$ART"
|
||||
REMOTE_PG_CONTAINER_Q="$(printf '%q' "$REMOTE_PG_CONTAINER")"
|
||||
REMOTE_REDIS_CONTAINER_Q="$(printf '%q' "$REMOTE_REDIS_CONTAINER")"
|
||||
|
||||
if [[ -n "$key_file" ]]; then
|
||||
upstream_key="$(tr -d '\r\n' < "$key_file")"
|
||||
@@ -40,18 +46,67 @@ ssh_cmd() {
|
||||
ssh -i "$KEY" -o StrictHostKeyChecking=no "$REMOTE" "$cmd"
|
||||
}
|
||||
|
||||
crm_curl_json() {
|
||||
local method="$1"
|
||||
local path="$2"
|
||||
local payload="${3:-}"
|
||||
if [[ -n "$payload" ]]; then
|
||||
curl -fsS -X "$method" \
|
||||
-H "Authorization: Bearer $crm_token" \
|
||||
-H 'Content-Type: application/json' \
|
||||
"${CRM_BASE}${path}" \
|
||||
-d "$payload"
|
||||
else
|
||||
curl -fsS -X "$method" \
|
||||
-H "Authorization: Bearer $crm_token" \
|
||||
"${CRM_BASE}${path}"
|
||||
fi
|
||||
}
|
||||
|
||||
fetch_remote_host_bearer_token() {
|
||||
ssh_cmd "python3 - <<'PY'
|
||||
from pathlib import Path
|
||||
import json, subprocess, sys
|
||||
|
||||
env_path = Path(${REMOTE_HOST_ENV_FILE@Q})
|
||||
host_base = ${HOST_BASE@Q}
|
||||
vals = {}
|
||||
for line in env_path.read_text().splitlines():
|
||||
if '=' not in line:
|
||||
continue
|
||||
key, value = line.split('=', 1)
|
||||
vals[key] = value
|
||||
|
||||
payload = json.dumps({
|
||||
'email': vals['ADMIN_EMAIL'],
|
||||
'password': vals['ADMIN_PASSWORD'],
|
||||
'turnstile_token': '',
|
||||
}, ensure_ascii=False)
|
||||
res = subprocess.run([
|
||||
'curl', '-fsS', '-H', 'Content-Type: application/json', '-X', 'POST',
|
||||
host_base.rstrip('/') + '/api/v1/auth/login', '-d', payload,
|
||||
], text=True, capture_output=True)
|
||||
obj = json.loads(res.stdout)
|
||||
token = (obj.get('data') or {}).get('access_token', '')
|
||||
if not token:
|
||||
print(res.stdout, file=sys.stderr)
|
||||
raise SystemExit('missing access_token from remote host login')
|
||||
print(token)
|
||||
PY"
|
||||
}
|
||||
|
||||
remote_pg_exec() {
|
||||
local sql="$1"
|
||||
local encoded
|
||||
encoded="$(printf '%s' "$sql" | base64 -w0)"
|
||||
ssh_cmd "printf '%s' '$encoded' | base64 -d | sudo -n docker exec -i sub2api-relaymgr-pg psql -U sub2api -d sub2api"
|
||||
ssh_cmd "printf '%s' '$encoded' | base64 -d | sudo -n docker exec -i $REMOTE_PG_CONTAINER_Q psql -U sub2api -d sub2api"
|
||||
}
|
||||
|
||||
remote_pg_query() {
|
||||
local sql="$1"
|
||||
local encoded
|
||||
encoded="$(printf '%s' "$sql" | base64 -w0)"
|
||||
ssh_cmd "printf '%s' '$encoded' | base64 -d | sudo -n docker exec -i sub2api-relaymgr-pg psql -U sub2api -d sub2api -At -F $'\t'"
|
||||
ssh_cmd "printf '%s' '$encoded' | base64 -d | sudo -n docker exec -i $REMOTE_PG_CONTAINER_Q psql -U sub2api -d sub2api -At -F $'\t'"
|
||||
}
|
||||
|
||||
remote_fetch_group_state() {
|
||||
@@ -59,11 +114,12 @@ remote_fetch_group_state() {
|
||||
local user_id="$2"
|
||||
local api_key="$3"
|
||||
local output_path="$4"
|
||||
local encoded
|
||||
encoded="$(python3 - "$group_id" "$user_id" "$api_key" <<'PY'
|
||||
import json, sys
|
||||
local sql
|
||||
sql="$(python3 - "$group_id" "$user_id" "$api_key" <<'PY'
|
||||
import sys
|
||||
|
||||
group_id, user_id, api_key = sys.argv[1:4]
|
||||
api_key_literal = "'" + api_key.replace("'", "''") + "'"
|
||||
query = f"""
|
||||
WITH group_row AS (
|
||||
SELECT row_to_json(g) AS data FROM groups g WHERE g.id = {group_id}
|
||||
@@ -74,7 +130,7 @@ subscription_row AS (
|
||||
ORDER BY s.id DESC LIMIT 1
|
||||
),
|
||||
key_row AS (
|
||||
SELECT row_to_json(k) AS data FROM api_keys k WHERE k.key = {json.dumps(api_key)}
|
||||
SELECT row_to_json(k) AS data FROM api_keys k WHERE k.key = {api_key_literal}
|
||||
)
|
||||
SELECT json_build_object(
|
||||
'group_id', {group_id},
|
||||
@@ -86,7 +142,7 @@ SELECT json_build_object(
|
||||
print(query)
|
||||
PY
|
||||
)"
|
||||
ssh_cmd "printf '%s' '$encoded' | base64 -d | sudo -n docker exec -i sub2api-relaymgr-pg psql -U sub2api -d sub2api -At -F ''" > "$output_path"
|
||||
remote_pg_query "$sql" > "$output_path"
|
||||
}
|
||||
|
||||
python3 - "$ART/00-local-key-source.json" "$key_source" "$provider_id" "$upstream_key" <<'PY'
|
||||
@@ -100,11 +156,17 @@ pathlib.Path(path).write_text(json.dumps({
|
||||
}, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
PY
|
||||
|
||||
crm_token="$(ssh_cmd "grep ^SUB2API_CRM_ADMIN_TOKEN= /home/ubuntu/sub2api-cn-relay-manager/.env.remote | cut -d= -f2-")"
|
||||
crm_token="${crm_token##*$'\n'}"
|
||||
admin_key="$(ssh_cmd "sudo -n docker exec sub2api-relaymgr-pg psql -U sub2api -d sub2api -Atc \"select value from settings where key='admin_api_key';\"")"
|
||||
admin_key="${admin_key##*$'\n'}"
|
||||
admin_uid="$(ssh_cmd "sudo -n docker exec sub2api-relaymgr-pg psql -U sub2api -d sub2api -Atc \"select id from users where role='admin' order by id asc limit 1;\"")"
|
||||
crm_token="${CRM_ADMIN_TOKEN:-}"
|
||||
if [[ -z "$crm_token" ]]; then
|
||||
crm_token="$(ssh_cmd "grep ^SUB2API_CRM_ADMIN_TOKEN= /home/ubuntu/sub2api-cn-relay-manager/.env.remote | cut -d= -f2-")"
|
||||
crm_token="${crm_token##*$'\n'}"
|
||||
fi
|
||||
host_bearer_token="${HOST_BEARER_TOKEN:-}"
|
||||
if [[ -z "$host_bearer_token" ]]; then
|
||||
host_bearer_token="$(fetch_remote_host_bearer_token)"
|
||||
host_bearer_token="${host_bearer_token##*$'\n'}"
|
||||
fi
|
||||
admin_uid="$(ssh_cmd "sudo -n docker exec $REMOTE_PG_CONTAINER_Q psql -U sub2api -d sub2api -Atc \"select id from users where role='admin' order by id asc limit 1;\"")"
|
||||
admin_uid="${admin_uid##*$'\n'}"
|
||||
sub_uid="$(remote_pg_query "select id from users where email like 'relay-sub-%@sub2api.local' and not exists (select 1 from user_subscriptions s where s.user_id=users.id and s.deleted_at is null) order by id desc limit 1;")"
|
||||
sub_uid="${sub_uid##*$'\n'}"
|
||||
@@ -208,12 +270,36 @@ pathlib.Path(path).write_text(json.dumps({
|
||||
}, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
PY
|
||||
|
||||
payload="$(python3 - "$CRM_HOST_BASE" "$admin_key" "$PACK_PATH" "$provider_id" "$upstream_key" "$sub_key" "$sub_uid" "$SUBSCRIPTION_DAYS" <<'PY'
|
||||
create_host_payload="$(python3 - "$HOST_NAME" "$CRM_HOST_BASE" "$host_bearer_token" <<'PY'
|
||||
import json, sys
|
||||
host_base, admin_key, pack_path, provider_id, upstream_key, sub_key, sub_uid, subscription_days = sys.argv[1:9]
|
||||
name, base_url, bearer_token = sys.argv[1:4]
|
||||
print(json.dumps({
|
||||
'name': name,
|
||||
'base_url': base_url,
|
||||
'auth': {'type': 'bearer', 'token': bearer_token},
|
||||
}, ensure_ascii=False))
|
||||
PY
|
||||
)"
|
||||
hosts_payload="$(crm_curl_json GET "/api/hosts")"
|
||||
existing_host_json="$(printf '%s' "$hosts_payload" | python3 -c 'import json, sys
|
||||
base_url = sys.argv[1]
|
||||
payload = json.load(sys.stdin)
|
||||
for host in payload.get("hosts", []):
|
||||
if host.get("base_url") == base_url:
|
||||
print(json.dumps(host, ensure_ascii=False))
|
||||
break' "$CRM_HOST_BASE")"
|
||||
if [[ -n "$existing_host_json" ]]; then
|
||||
printf '%s\n' "$existing_host_json" > "$ART/01a-create-host.json"
|
||||
else
|
||||
crm_curl_json POST "/api/hosts" "$create_host_payload" > "$ART/01a-create-host.json"
|
||||
fi
|
||||
|
||||
payload="$(python3 - "$CRM_HOST_BASE" "$host_bearer_token" "$PACK_PATH" "$provider_id" "$upstream_key" "$sub_key" "$sub_uid" "$SUBSCRIPTION_DAYS" <<'PY'
|
||||
import json, sys
|
||||
host_base, host_bearer_token, pack_path, provider_id, upstream_key, sub_key, sub_uid, subscription_days = sys.argv[1:9]
|
||||
print(json.dumps({
|
||||
'host_base_url': host_base,
|
||||
'host_api_key': admin_key,
|
||||
'host_bearer_token': host_bearer_token,
|
||||
'pack_path': pack_path,
|
||||
'provider_id': provider_id,
|
||||
'keys': [upstream_key],
|
||||
@@ -226,9 +312,11 @@ print(json.dumps({
|
||||
PY
|
||||
)"
|
||||
|
||||
ssh_cmd "curl -sS -D /tmp/import_headers.txt -o /tmp/import_body.json -X POST -H 'Authorization: Bearer $crm_token' -H 'Content-Type: application/json' $CRM_BASE/api/providers/$provider_id/import -d $(printf %q "$payload")"
|
||||
ssh_cmd "cat /tmp/import_headers.txt" > "$ART/02-import.headers.txt"
|
||||
ssh_cmd "cat /tmp/import_body.json" > "$ART/03-import.body.json"
|
||||
curl -sS -D "$ART/02-import.headers.txt" -o "$ART/03-import.body.json" -X POST \
|
||||
-H "Authorization: Bearer $crm_token" \
|
||||
-H 'Content-Type: application/json' \
|
||||
"$CRM_BASE/api/providers/$provider_id/import" \
|
||||
-d "$payload"
|
||||
|
||||
batch_id="$(python3 - "$ART/03-import.body.json" <<'PY'
|
||||
import json, sys, pathlib
|
||||
@@ -237,7 +325,7 @@ print(obj['batch_id'])
|
||||
PY
|
||||
)"
|
||||
|
||||
ssh_cmd "curl -sS -H 'Authorization: Bearer $crm_token' $CRM_BASE/api/import-batches/$batch_id" > "$ART/04-batch-detail-initial.json"
|
||||
crm_curl_json GET "/api/import-batches/$batch_id" > "$ART/04-batch-detail-initial.json"
|
||||
subscription_group_id="$(python3 - "$ART/03-import.body.json" "$ART/04-batch-detail-initial.json" <<'PY'
|
||||
import json, pathlib, sys
|
||||
import_obj = json.loads(pathlib.Path(sys.argv[1]).read_text())
|
||||
@@ -270,7 +358,7 @@ remote_pg_exec "$prep_sql" > "$ART/06-subscription-access-prep.psql.txt"
|
||||
printf 'auth_cache_key=%s\n' "$auth_cache_key"
|
||||
printf 'balance_cache_key=%s\n' "$balance_cache_key"
|
||||
printf 'subscription_cache_key=%s\n' "$subscription_cache_key"
|
||||
ssh_cmd "sudo -n docker exec sub2api-relaymgr-redis redis-cli DEL $auth_cache_key $balance_cache_key $subscription_cache_key"
|
||||
ssh_cmd "sudo -n docker exec $REMOTE_REDIS_CONTAINER_Q redis-cli DEL $auth_cache_key $balance_cache_key $subscription_cache_key"
|
||||
} > "$ART/07-redis-targeted-invalidation.txt"
|
||||
remote_fetch_group_state "$subscription_group_id" "$sub_uid" "$sub_key" "$ART/08-subscription-group-state.json"
|
||||
|
||||
@@ -299,26 +387,27 @@ print(json.dumps({
|
||||
}, ensure_ascii=False))
|
||||
PY
|
||||
)"
|
||||
ssh_cmd "curl -sS -D /tmp/models_headers.txt -o /tmp/models_body.json -H 'Authorization: Bearer *** $HOST_BASE/v1/models"
|
||||
ssh_cmd "curl -sS -D /tmp/models_headers.txt -o /tmp/models_body.json -H 'Authorization: Bearer $sub_key' $HOST_BASE/v1/models"
|
||||
ssh_cmd "cat /tmp/models_headers.txt" > "$ART/09-models.headers.txt"
|
||||
ssh_cmd "cat /tmp/models_body.json" > "$ART/10-models.body.json"
|
||||
|
||||
ssh_cmd "curl -sS -D /tmp/chat_headers.txt -o /tmp/chat_body.json -H 'Authorization: Bearer *** -H 'Content-Type: application/json' $HOST_BASE/v1/chat/completions -d $(printf %q "$probe_payload")"
|
||||
ssh_cmd "curl -sS -D /tmp/chat_headers.txt -o /tmp/chat_body.json -H 'Authorization: Bearer $sub_key' -H 'Content-Type: application/json' $HOST_BASE/v1/chat/completions -d $(printf %q "$probe_payload")"
|
||||
ssh_cmd "cat /tmp/chat_headers.txt" > "$ART/11-chat.headers.txt"
|
||||
ssh_cmd "cat /tmp/chat_body.json" > "$ART/12-chat.body.json"
|
||||
|
||||
ssh_cmd "curl -sS -H 'Authorization: Bearer *** $CRM_BASE/api/providers/$provider_id/status" > "$ART/13-provider-status.json"
|
||||
ssh_cmd "curl -sS -H 'Authorization: Bearer *** $CRM_BASE/api/providers/$provider_id/access/status" > "$ART/14-access-status.json"
|
||||
crm_curl_json GET "/api/providers/$provider_id/status" > "$ART/13-provider-status.json"
|
||||
crm_curl_json GET "/api/providers/$provider_id/access/status" > "$ART/14-access-status.json"
|
||||
preview_payload="$(python3 - "$provider_id" <<'PY'
|
||||
import json, sys
|
||||
print(json.dumps({'provider_id': sys.argv[1], 'mode': 'subscription'}, ensure_ascii=False))
|
||||
PY
|
||||
)"
|
||||
ssh_cmd "curl -sS -X POST -H 'Authorization: Bearer *** -H 'Content-Type: application/json' $CRM_BASE/api/providers/$provider_id/access/preview -d $(printf %q "$preview_payload")" > "$ART/15-access-preview.json"
|
||||
ssh_cmd "curl -sS -H 'Authorization: Bearer *** $CRM_BASE/api/import-batches/$batch_id" > "$ART/16-batch-detail-final.json"
|
||||
crm_curl_json POST "/api/providers/$provider_id/access/preview" "$preview_payload" > "$ART/15-access-preview.json"
|
||||
crm_curl_json GET "/api/import-batches/$batch_id" > "$ART/16-batch-detail-final.json"
|
||||
|
||||
python3 - "$ART" "$provider_id" "$batch_id" "$subscription_group_id" "$model_name" <<'PY'
|
||||
import json, pathlib, sys
|
||||
|
||||
art=pathlib.Path(sys.argv[1])
|
||||
provider_id=sys.argv[2]
|
||||
batch_id=int(sys.argv[3])
|
||||
|
||||
@@ -150,10 +150,13 @@ PY
|
||||
)"
|
||||
|
||||
if RESP_EXISTING_HOST="$(curl_json GET "/api/hosts/$HOST_NAME" 2>/dev/null)"; then
|
||||
RESP_CREATE_HOST="$RESP_EXISTING_HOST"
|
||||
else
|
||||
RESP_CREATE_HOST="$(curl_json POST /api/hosts "$CREATE_HOST_PAYLOAD")"
|
||||
EXISTING_BASE_URL="$(printf '%s' "$RESP_EXISTING_HOST" | json_get base_url || true)"
|
||||
if [[ -n "$EXISTING_BASE_URL" && "$EXISTING_BASE_URL" != "$HOST_BASE_URL" ]]; then
|
||||
echo "existing host $HOST_NAME points to $EXISTING_BASE_URL, expected $HOST_BASE_URL" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
RESP_CREATE_HOST="$(curl_json POST /api/hosts "$CREATE_HOST_PAYLOAD")"
|
||||
save_json 01-create-host "$RESP_CREATE_HOST"
|
||||
HOST_ID="$(printf '%s' "$RESP_CREATE_HOST" | json_get host_id || true)"
|
||||
HOST_ID="${HOST_ID:-$HOST_NAME}"
|
||||
|
||||
@@ -59,6 +59,10 @@ run_test_real_host_acceptance_after_import_hook() {
|
||||
set -euo pipefail
|
||||
url=""
|
||||
for arg in "$@"; do
|
||||
if [[ "$arg" == *'***'* ]]; then
|
||||
echo "unexpected redacted auth placeholder in curl args: $*" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ "$arg" == http://* || "$arg" == https://* ]]; then
|
||||
url="$arg"
|
||||
fi
|
||||
@@ -68,6 +72,9 @@ done
|
||||
exit 1
|
||||
}
|
||||
case "$url" in
|
||||
*/api/hosts)
|
||||
printf '%s\n' '{"host_id":"test-host"}'
|
||||
;;
|
||||
*/api/hosts/test-host)
|
||||
printf '%s\n' '{"host_id":"test-host"}'
|
||||
;;
|
||||
@@ -118,6 +125,8 @@ EOF
|
||||
PACK_PATH="/tmp/openai-pack" \
|
||||
PROVIDER_ID="deepseek" \
|
||||
HOST_API_KEY="host-key" \
|
||||
REMOTE_PG_CONTAINER="fresh-pg" \
|
||||
REMOTE_REDIS_CONTAINER="fresh-redis" \
|
||||
MODE="partial" \
|
||||
ACCESS_MODE="subscription" \
|
||||
ACCESS_API_KEY="user-key" \
|
||||
@@ -143,13 +152,96 @@ run_test_import_remote43_provider_subscription_prep() {
|
||||
psql_sql="$artifact_dir/prep.sql"
|
||||
mkdir -p "$fakebin"
|
||||
|
||||
cat > "$fakebin/curl" <<'EOF'
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
headers_file=""
|
||||
body_file=""
|
||||
url=""
|
||||
prev=""
|
||||
for arg in "$@"; do
|
||||
if [[ "$arg" == *'***'* ]]; then
|
||||
echo "unexpected redacted auth placeholder in curl args: $*" >&2
|
||||
exit 1
|
||||
fi
|
||||
case "$prev" in
|
||||
-D)
|
||||
headers_file="$arg"
|
||||
prev=""
|
||||
continue
|
||||
;;
|
||||
-o)
|
||||
body_file="$arg"
|
||||
prev=""
|
||||
continue
|
||||
;;
|
||||
esac
|
||||
case "$arg" in
|
||||
-D|-o)
|
||||
prev="$arg"
|
||||
continue
|
||||
;;
|
||||
http://*|https://*)
|
||||
url="$arg"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
write_headers() {
|
||||
[[ -n "$headers_file" ]] && printf '%s\n' 'HTTP/1.1 200 OK' > "$headers_file"
|
||||
}
|
||||
|
||||
write_body() {
|
||||
local body="$1"
|
||||
if [[ -n "$body_file" ]]; then
|
||||
printf '%s\n' "$body" > "$body_file"
|
||||
else
|
||||
printf '%s\n' "$body"
|
||||
fi
|
||||
}
|
||||
|
||||
case "$url" in
|
||||
*/api/hosts)
|
||||
write_body '{"host_id":"remote43-current-host"}'
|
||||
;;
|
||||
*/api/providers/deepseek/import)
|
||||
write_headers
|
||||
write_body '{"batch_id":123,"batch_status":"partially_succeeded","access_status":"broken","provider_status":"ready","accepted_keys_count":1,"group":{"id":"7","name":"DeepSeek 默认分组"}}'
|
||||
;;
|
||||
*/api/import-batches/123)
|
||||
write_body '{"managed_resources":[{"ResourceType":"group","HostResourceID":"7","ResourceName":"DeepSeek 默认分组"}]}'
|
||||
;;
|
||||
*/api/providers/deepseek/status)
|
||||
write_body '{"status":"ready"}'
|
||||
;;
|
||||
*/api/providers/deepseek/access/status)
|
||||
write_body '{"latest_access_status":"subscription_ready"}'
|
||||
;;
|
||||
*/api/providers/deepseek/access/preview)
|
||||
write_body '{"available":true}'
|
||||
;;
|
||||
*)
|
||||
echo "unexpected curl url: $url" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
EOF
|
||||
chmod +x "$fakebin/curl"
|
||||
|
||||
cat > "$fakebin/ssh" <<'EOF'
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
log_dir="${FAKE_REMOTE_LOG_DIR:?missing FAKE_REMOTE_LOG_DIR}"
|
||||
cmd="${*: -1}"
|
||||
printf '%s\n' "$cmd" >> "$log_dir/ssh-log.txt"
|
||||
if [[ "$cmd" == *'***'* ]]; then
|
||||
echo "unexpected redacted auth placeholder in ssh command: $cmd" >&2
|
||||
exit 1
|
||||
fi
|
||||
case "$cmd" in
|
||||
*"/api/v1/auth/login"*)
|
||||
printf '%s\n' 'host-bearer-token'
|
||||
;;
|
||||
*"grep ^SUB2API_CRM_ADMIN_TOKEN="*)
|
||||
printf '%s\n' 'crm-token'
|
||||
;;
|
||||
@@ -210,10 +302,10 @@ case "$cmd" in
|
||||
*"/api/providers/deepseek/reconcile"*)
|
||||
printf '%s\n' '{"status":"in_sync"}'
|
||||
;;
|
||||
*"sudo -n docker exec -i sub2api-relaymgr-pg psql -U sub2api -d sub2api -At -F ''"*)
|
||||
*"sudo -n docker exec -i fresh-pg psql -U sub2api -d sub2api -At -F ''"*)
|
||||
printf '%s\n' '{"group_id":7,"subscription":{"status":"active"},"key":{"group_id":7}}'
|
||||
;;
|
||||
*"sudo -n docker exec -i sub2api-relaymgr-pg psql -U sub2api -d sub2api"*)
|
||||
*"sudo -n docker exec -i fresh-pg psql -U sub2api -d sub2api"*)
|
||||
CMD="$cmd" LOG_DIR="$log_dir" python3 - <<'PY'
|
||||
import base64, os, re, pathlib, sys
|
||||
cmd = os.environ['CMD']
|
||||
@@ -222,18 +314,24 @@ match = re.search(r"printf '%s' '([^']+)' \| base64 -d", cmd)
|
||||
if not match:
|
||||
raise SystemExit(f'failed to extract base64 payload from: {cmd}')
|
||||
sql = base64.b64decode(match.group(1)).decode()
|
||||
log_dir.joinpath('prep.sql').write_text(sql, encoding='utf-8')
|
||||
if "select id from users where email like 'relay-sub-%@sub2api.local' and not exists" in sql:
|
||||
print('')
|
||||
elif "select k.key from users u join api_keys k on k.user_id=u.id" in sql and "not exists" in sql:
|
||||
print('')
|
||||
elif "UPDATE users" in sql and "INSERT INTO user_subscriptions" in sql:
|
||||
log_dir.joinpath('prep.sql').write_text(sql, encoding='utf-8')
|
||||
print('')
|
||||
elif "INSERT INTO users" in sql and "INSERT INTO api_keys" in sql:
|
||||
log_dir.joinpath('create-user.sql').write_text(sql, encoding='utf-8')
|
||||
print('84\tuser-key-fresh')
|
||||
elif "SELECT json_build_object(" in sql:
|
||||
log_dir.joinpath('group-state.sql').write_text(sql, encoding='utf-8')
|
||||
print('{"group_id":7,"subscription":{"status":"active"},"key":{"group_id":7}}')
|
||||
else:
|
||||
print('')
|
||||
PY
|
||||
;;
|
||||
*"sudo -n docker exec sub2api-relaymgr-redis redis-cli DEL apikey:auth:"*" billing:balance:"*" billing:sub:"*":7"*)
|
||||
*"sudo -n docker exec fresh-redis redis-cli DEL apikey:auth:"*" billing:balance:"*" billing:sub:"*":7"*)
|
||||
printf '%s\n' '3'
|
||||
;;
|
||||
*)
|
||||
@@ -254,6 +352,8 @@ EOF
|
||||
ROOT="$artifact_dir/root" \
|
||||
ART="$artifact_dir/run" \
|
||||
PACK_PATH="/tmp/openai-pack" \
|
||||
REMOTE_PG_CONTAINER="fresh-pg" \
|
||||
REMOTE_REDIS_CONTAINER="fresh-redis" \
|
||||
UPSTREAM_KEY="upstream-test-key" \
|
||||
SUBSCRIPTION_DAYS=30 \
|
||||
MIN_BALANCE=10 \
|
||||
@@ -274,7 +374,10 @@ EOF
|
||||
assert_contains "$invalidation_log" "auth_cache_key=apikey:auth:"
|
||||
assert_contains "$invalidation_log" "balance_cache_key=billing:balance:84"
|
||||
assert_contains "$invalidation_log" "subscription_cache_key=billing:sub:84:7"
|
||||
local models_body chat_body
|
||||
local subscription_state models_body chat_body
|
||||
subscription_state="$(cat "$artifact_dir/run/08-subscription-group-state.json")"
|
||||
assert_contains "$subscription_state" '"group_id":7'
|
||||
assert_contains "$subscription_state" '"status":"active"'
|
||||
models_body="$(cat "$artifact_dir/run/10-models.body.json")"
|
||||
chat_body="$(cat "$artifact_dir/run/12-chat.body.json")"
|
||||
assert_contains "$models_body" '"id":"gpt-4"'
|
||||
|
||||
Reference in New Issue
Block a user