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])
|
||||
|
||||
Reference in New Issue
Block a user