Repository navigation
Expand file tree
/
Copy pathsmoke_artifact_transfer.sh
More file actions
executable file
·590 lines (511 loc) · 17.6 KB
/
Copy pathsmoke_artifact_transfer.sh
File metadata and controls
executable file
·590 lines (511 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#!/usr/bin/env bash
set -euo pipefail
# ============================================================================
# WebCodex - Artifact Transfer Smoke
#
# Default mode is documentation-only: it prints the required environment and
# checklist, then exits without reading secrets or contacting a server.
#
# Active mode:
# WEBCODEX_SMOKE_RUN=1 \
# WEBCODEX_PUBLIC_URL="https://webcodex.example.com" \
# WEBCODEX_TOKEN="<wc_pat_or_allowed_shared_key>" \
# WEBCODEX_SMOKE_PROJECT_ID="agent:<client_id>:<smoke-project>" \
# bash scripts/smoke_artifact_transfer.sh
#
# This script never prints the token. It uses a pre-registered smoke project
# and writes only fixed smoke artifact paths before deleting them again.
# ============================================================================
DEFAULT_ARTIFACT_PATH="artifacts/smoke/webcodex-artifact-transfer.txt"
DEFAULT_ABORT_PATH="artifacts/smoke/webcodex-artifact-transfer-abort.txt"
print_checklist() {
cat <<EOF
[smoke] Artifact transfer deployment smoke checklist
Default mode only prints this checklist. It does not read token variables,
contact a server, write files, or delete files.
To run the HTTP smoke explicitly:
WEBCODEX_SMOKE_RUN=1 \\
WEBCODEX_PUBLIC_URL="https://webcodex.example.com" \\
WEBCODEX_TOKEN="<wc_pat_or_allowed_shared_key>" \\
WEBCODEX_SMOKE_PROJECT_ID="agent:<client_id>:<smoke-project>" \\
bash scripts/smoke_artifact_transfer.sh
`WEBCODEX_SMOKE_PROJECT_ID` is required in active mode and must name an
explicitly registered smoke project.
Optional environment:
WEBCODEX_SMOKE_ARTIFACT_PATH default: $DEFAULT_ARTIFACT_PATH
WEBCODEX_SMOKE_ABORT_PATH default: $DEFAULT_ABORT_PATH
SMOKE_TIMEOUT default: 20 seconds per HTTP call
Preconditions:
1. The public WebCodex URL is reachable.
2. The token is a managed wc_pat_* token or a deployment-allowed shared key.
Do not use wc_agent_*; that token type is only for webcodex-runner.
3. The smoke project is registered, Runner-backed, online, and a git repo.
4. The smoke project is disposable and clean before the run.
Checks covered by active mode:
1. GET /healthz reports Server readiness.
2. Bounded discovery works through /api/tools/list and tool_manifest.
3. begin_artifact_upload, upload_artifact_chunk, and artifact_upload_finish.
4. read_project_artifact_metadata and read_project_artifact.
5. abort_artifact_upload cleanup for a second temporary upload.
6. delete_project_files cleanup of the committed smoke artifact.
7. get_git_status and read_workspace_changes report a clean worktree after cleanup.
Active mode refuses non-smoke project ids unless
WEBCODEX_SMOKE_ALLOW_NON_SMOKE_PROJECT=1 is set. Custom artifact paths must stay
under artifacts/smoke/ unless WEBCODEX_SMOKE_ALLOW_CUSTOM_PATHS=1 is set.
EOF
}
if [ "${WEBCODEX_SMOKE_RUN:-0}" != "1" ]; then
print_checklist
exit 0
fi
BASE_URL="${WEBCODEX_PUBLIC_URL:-${BASE_URL:-}}"
TOKEN="${WEBCODEX_TOKEN:-${TOKEN:-}}"
PROJECT_ID="${WEBCODEX_SMOKE_PROJECT_ID:-${PROJECT_ID:-}}"
ARTIFACT_PATH="${WEBCODEX_SMOKE_ARTIFACT_PATH:-$DEFAULT_ARTIFACT_PATH}"
ABORT_PATH="${WEBCODEX_SMOKE_ABORT_PATH:-$DEFAULT_ABORT_PATH}"
TIMEOUT="${SMOKE_TIMEOUT:-20}"
if [ -z "$BASE_URL" ]; then
echo "[smoke] WEBCODEX_PUBLIC_URL (or BASE_URL) is required" >&2
exit 2
fi
if [ -z "$TOKEN" ]; then
echo "[smoke] WEBCODEX_TOKEN (or TOKEN) is required" >&2
exit 2
fi
if [ -z "$PROJECT_ID" ]; then
echo "[smoke] WEBCODEX_SMOKE_PROJECT_ID (or PROJECT_ID) is required in active mode" >&2
exit 2
fi
case "$PROJECT_ID" in
*smoke*) ;;
*)
if [ "${WEBCODEX_SMOKE_ALLOW_NON_SMOKE_PROJECT:-0}" != "1" ]; then
echo "[smoke] refusing non-smoke project id; set WEBCODEX_SMOKE_ALLOW_NON_SMOKE_PROJECT=1 to override" >&2
exit 2
fi
;;
esac
case "$ARTIFACT_PATH:$ABORT_PATH" in
artifacts/smoke/*:artifacts/smoke/*) ;;
*)
if [ "${WEBCODEX_SMOKE_ALLOW_CUSTOM_PATHS:-0}" != "1" ]; then
echo "[smoke] refusing artifact paths outside artifacts/smoke/; set WEBCODEX_SMOKE_ALLOW_CUSTOM_PATHS=1 to override" >&2
exit 2
fi
;;
esac
if ! command -v curl >/dev/null 2>&1; then
echo "[smoke] curl is required" >&2
exit 2
fi
if ! command -v python3 >/dev/null 2>&1; then
echo "[smoke] python3 is required" >&2
exit 2
fi
BASE_URL="${BASE_URL%/}"
PASS=0
WARN=0
FAIL=0
log() { printf '[smoke] %s\n' "$*"; }
pass() { PASS=$((PASS + 1)); printf '[smoke][ok] %s\n' "$*"; }
warn() { WARN=$((WARN + 1)); printf '[smoke][warn] %s\n' "$*" >&2; }
fail() { FAIL=$((FAIL + 1)); printf '[smoke][FAIL] %s\n' "$*" >&2; }
AUTH_HEADER_FILE="$(mktemp -t webcodex-artifact-smoke-auth-XXXXXX)"
trap 'rm -f "$AUTH_HEADER_FILE"' INT TERM EXIT
printf 'Authorization: Bearer %s\n' "$TOKEN" > "$AUTH_HEADER_FILE"
chmod 600 "$AUTH_HEADER_FILE"
api_get() {
local path="$1"
curl -sS --max-time "$TIMEOUT" \
-H @"$AUTH_HEADER_FILE" \
"${BASE_URL}${path}" 2>/dev/null || true
}
api_post() {
local path="$1"
local body="${2:-{}}"
curl -sS --max-time "$TIMEOUT" \
-H @"$AUTH_HEADER_FILE" \
-H "Content-Type: application/json" \
-X POST "${BASE_URL}${path}" \
-d "$body" 2>/dev/null || true
}
json_get() {
local json="$1"
local path="$2"
python3 - "$json" "$path" <<'PY'
import json
import sys
try:
obj = json.loads(sys.argv[1])
except Exception:
print("")
sys.exit(0)
cur = obj
for part in sys.argv[2].split("."):
if part == "":
break
if isinstance(cur, list):
try:
cur = cur[int(part)]
except Exception:
print("")
sys.exit(0)
elif isinstance(cur, dict):
if part not in cur:
print("")
sys.exit(0)
cur = cur[part]
else:
print("")
sys.exit(0)
if isinstance(cur, (dict, list)):
print(json.dumps(cur, separators=(",", ":")))
elif cur is None:
print("")
else:
print(cur)
PY
}
tool_call_body() {
local tool="$1"
local params_json="${2:-{}}"
python3 - "$tool" "$params_json" <<'PY'
import json
import sys
tool = sys.argv[1]
params = json.loads(sys.argv[2])
print(json.dumps({"tool": tool, "params": params}, separators=(",", ":")))
PY
}
call_tool() {
local tool="$1"
local params_json="${2:-{}}"
local body
body="$(tool_call_body "$tool" "$params_json")"
api_post /api/tools/call "$body"
}
body_preview() {
local body="$1"
body="${body//$'\n'/ }"
printf '%s' "${body:0:300}"
}
success_true() {
local body="$1"
[ "$(json_get "$body" success)" = "True" ]
}
check_success() {
local label="$1"
local body="$2"
local error
if success_true "$body"; then
pass "$label"
return 0
fi
error="$(json_get "$body" error)"
if [ -z "$error" ]; then
error="$(body_preview "$body")"
fi
fail "$label failed: $error"
return 1
}
json_contains_strings() {
local json="$1"
local needle="$2"
python3 - "$json" "$needle" <<'PY'
import json
import sys
try:
obj = json.loads(sys.argv[1])
except Exception:
sys.exit(1)
needle = sys.argv[2]
def walk(value):
if value == needle:
return True
if isinstance(value, dict):
return any(walk(v) for v in value.values())
if isinstance(value, list):
return any(walk(v) for v in value)
return False
sys.exit(0 if walk(obj) else 1)
PY
}
json_tools_include() {
local json="$1"
local path="$2"
shift 2
python3 - "$json" "$path" "$@" <<'PY'
import json
import sys
try:
obj = json.loads(sys.argv[1])
except Exception as exc:
print(f"invalid JSON: {exc}")
sys.exit(1)
cur = obj
for part in sys.argv[2].split("."):
if not part:
continue
if isinstance(cur, dict):
cur = cur.get(part)
elif isinstance(cur, list):
cur = cur[int(part)]
else:
cur = None
if cur is None:
print(f"missing path {sys.argv[2]}")
sys.exit(1)
names = set()
if isinstance(cur, list):
for item in cur:
if isinstance(item, dict) and isinstance(item.get("name"), str):
names.add(item["name"])
elif isinstance(item, str):
names.add(item)
missing = [name for name in sys.argv[3:] if name not in names]
if missing:
print("missing " + ", ".join(missing))
sys.exit(1)
print("ok")
PY
}
make_json_object() {
python3 - "$@" <<'PY'
import json
import sys
if len(sys.argv[1:]) % 2 != 0:
raise SystemExit("expected key/value pairs")
obj = {}
items = sys.argv[1:]
for key, value in zip(items[0::2], items[1::2]):
if value == "__true__":
obj[key] = True
elif value == "__false__":
obj[key] = False
elif value == "__null__":
obj[key] = None
elif value.startswith("__int__:"):
obj[key] = int(value[len("__int__:"):])
elif value.startswith("__json__:"):
obj[key] = json.loads(value[len("__json__:"):])
else:
obj[key] = value
print(json.dumps(obj, separators=(",", ":")))
PY
}
check_clean_git_status() {
local label="$1"
local body
body="$(call_tool get_git_status "$(make_json_object project "$PROJECT_ID")")"
if ! check_success "$label get_git_status succeeds" "$body"; then
return 1
fi
local exit_code
local stdout
exit_code="$(json_get "$body" output.exit_code)"
stdout="$(json_get "$body" output.stdout)"
if [ "$exit_code" = "0" ] && [ -z "$stdout" ]; then
pass "$label get_git_status is clean"
else
fail "$label get_git_status is not clean or not a git repo (exit_code=${exit_code}, stdout=$(body_preview "$stdout"))"
return 1
fi
body="$(call_tool read_workspace_changes "$(make_json_object project "$PROJECT_ID" include_diff __false__)")"
if ! check_success "$label read_workspace_changes succeeds" "$body"; then
return 1
fi
if [ "$(json_get "$body" output.git_available)" = "True" ] && \
[ "$(json_get "$body" output.clean)" = "True" ]; then
pass "$label read_workspace_changes reports git_available=true and clean=true"
else
fail "$label read_workspace_changes is not clean/git-backed (git_available=$(json_get "$body" output.git_available), clean=$(json_get "$body" output.clean))"
return 1
fi
}
log "artifact transfer smoke against $BASE_URL"
log "project: $PROJECT_ID"
log "artifact path: $ARTIFACT_PATH"
log "abort path: $ABORT_PATH"
payload_info="$(python3 - <<'PY'
import base64
import hashlib
import json
data = b"WebCodex artifact transfer smoke\n"
print(json.dumps({
"bytes": len(data),
"sha256": hashlib.sha256(data).hexdigest(),
"base64": base64.b64encode(data).decode("ascii"),
}, separators=(",", ":")))
PY
)"
PAYLOAD_BYTES="$(json_get "$payload_info" bytes)"
PAYLOAD_SHA256="$(json_get "$payload_info" sha256)"
PAYLOAD_BASE64="$(json_get "$payload_info" base64)"
# ---------------------------------------------------------------------------
# Preflight: read-only schema, discovery, project, and git cleanliness checks.
# ---------------------------------------------------------------------------
log "---- preflight ----"
if api_get /healthz >/dev/null; then
pass "/healthz reports Server readiness"
else
fail "/healthz did not report Server readiness"
fi
body="$(api_post /api/tools/list '{"summary_only":true,"category":"artifact","limit":20}')"
if json_tools_include "$body" tools \
begin_artifact_upload upload_artifact_chunk finish_artifact_upload \
abort_artifact_upload read_project_artifact_metadata read_project_artifact_chunk >/dev/null; then
pass "bounded listRuntimeTools summary exposes artifact transfer tools"
else
fail "bounded listRuntimeTools summary missing expected artifact tools ($(body_preview "$body"))"
fi
body="$(call_tool read_tool_manifest "$(make_json_object category artifact include_recommended_flows __false__ include_risk_summary __false__)")"
if check_success "read_tool_manifest(category=artifact) succeeds" "$body"; then
if json_tools_include "$body" output.tools \
begin_artifact_upload upload_artifact_chunk finish_artifact_upload \
abort_artifact_upload read_project_artifact_metadata read_project_artifact_chunk >/dev/null; then
pass "read_tool_manifest(category=artifact) exposes artifact transfer tools"
else
fail "read_tool_manifest(category=artifact) missing expected artifact tools"
fi
fi
body="$(api_post /api/tools/call '{"tool":"list_projects","params":{}}')"
if check_success "list_projects succeeds" "$body"; then
if json_contains_strings "$body" "$PROJECT_ID"; then
pass "list_projects includes $PROJECT_ID"
else
fail "list_projects does not include $PROJECT_ID"
fi
fi
check_clean_git_status "preflight" || true
if [ "$FAIL" -ne 0 ]; then
log "preflight failed; artifact write and cleanup smoke will not run"
log "---- summary ----"
log "passed: $PASS"
log "warned: $WARN"
log "failed: $FAIL"
exit 1
fi
# ---------------------------------------------------------------------------
# Artifact upload/read/abort/delete smoke against fixed smoke paths.
# ---------------------------------------------------------------------------
log "---- artifact upload/read/cleanup ----"
upload_id=""
abort_upload_id=""
artifact_committed=0
begin_params="$(make_json_object \
project "$PROJECT_ID" \
path "$ARTIFACT_PATH" \
expected_bytes "__int__:$PAYLOAD_BYTES" \
expected_sha256 "$PAYLOAD_SHA256" \
mime_type text/plain \
overwrite __true__)"
body="$(call_tool begin_artifact_upload "$begin_params")"
if check_success "begin_artifact_upload succeeds" "$body"; then
upload_id="$(json_get "$body" output.upload_id)"
if [ -n "$upload_id" ]; then
pass "begin_artifact_upload returns upload_id"
else
fail "begin_artifact_upload did not return upload_id"
fi
fi
if [ -n "$upload_id" ]; then
chunk_params="$(make_json_object \
project "$PROJECT_ID" \
path "$ARTIFACT_PATH" \
upload_id "$upload_id" \
offset __int__:0 \
content_base64 "$PAYLOAD_BASE64")"
body="$(call_tool upload_artifact_chunk "$chunk_params")"
check_success "upload_artifact_chunk succeeds" "$body" || true
finish_params="$(make_json_object \
project "$PROJECT_ID" \
path "$ARTIFACT_PATH" \
upload_id "$upload_id")"
body="$(call_tool finish_artifact_upload "$finish_params")"
if check_success "finish_artifact_upload succeeds" "$body"; then
artifact_committed=1
if [ "$(json_get "$body" output.sha256)" = "$PAYLOAD_SHA256" ]; then
pass "finish_artifact_upload verifies expected sha256"
else
fail "finish_artifact_upload sha256 mismatch"
fi
fi
fi
if [ -n "$upload_id" ] && [ "$artifact_committed" -eq 0 ]; then
abort_unfinished_params="$(make_json_object \
project "$PROJECT_ID" \
path "$ARTIFACT_PATH" \
upload_id "$upload_id")"
body="$(call_tool abort_artifact_upload "$abort_unfinished_params")"
check_success "abort_artifact_upload cleanup for unfinished upload succeeds" "$body" || true
fi
if [ "$artifact_committed" -eq 1 ]; then
metadata_params="$(make_json_object project "$PROJECT_ID" path "$ARTIFACT_PATH")"
body="$(call_tool read_project_artifact_metadata "$metadata_params")"
if check_success "read_project_artifact_metadata succeeds" "$body"; then
if [ "$(json_get "$body" output.bytes)" = "$PAYLOAD_BYTES" ] && \
[ "$(json_get "$body" output.sha256)" = "$PAYLOAD_SHA256" ]; then
pass "read_project_artifact_metadata reports expected bytes and sha256"
else
fail "read_project_artifact_metadata mismatch"
fi
fi
read_params="$(make_json_object \
project "$PROJECT_ID" \
path "$ARTIFACT_PATH" \
offset __int__:0 \
length "__int__:$PAYLOAD_BYTES")"
body="$(call_tool read_project_artifact_chunk "$read_params")"
if check_success "read_project_artifact_chunk succeeds" "$body"; then
if [ "$(json_get "$body" output.content_base64)" = "$PAYLOAD_BASE64" ] && \
[ "$(json_get "$body" output.eof)" = "True" ]; then
pass "read_project_artifact_chunk returns expected base64 segment"
else
fail "read_project_artifact_chunk content/eof mismatch"
fi
fi
fi
abort_begin_params="$(make_json_object \
project "$PROJECT_ID" \
path "$ABORT_PATH" \
expected_bytes __int__:1 \
mime_type text/plain \
overwrite __true__)"
body="$(call_tool begin_artifact_upload "$abort_begin_params")"
if check_success "begin_artifact_upload for abort cleanup succeeds" "$body"; then
abort_upload_id="$(json_get "$body" output.upload_id)"
fi
if [ -n "$abort_upload_id" ]; then
abort_params="$(make_json_object \
project "$PROJECT_ID" \
path "$ABORT_PATH" \
upload_id "$abort_upload_id")"
body="$(call_tool abort_artifact_upload "$abort_params")"
if check_success "abort_artifact_upload cleanup succeeds" "$body"; then
if [ "$(json_get "$body" output.aborted)" = "True" ]; then
pass "abort_artifact_upload reports aborted=true"
else
fail "abort_artifact_upload did not report aborted=true"
fi
fi
fi
delete_params="$(python3 - "$PROJECT_ID" "$ARTIFACT_PATH" <<'PY'
import json
import sys
print(json.dumps({
"project": sys.argv[1],
"paths": [sys.argv[2]],
}, separators=(",", ":")))
PY
)"
body="$(call_tool delete_project_files "$delete_params")"
check_success "delete_project_files cleanup succeeds" "$body" || true
check_clean_git_status "post-cleanup" || true
log "---- summary ----"
log "passed: $PASS"
log "warned: $WARN"
log "failed: $FAIL"
if [ "$FAIL" -ne 0 ]; then
exit 1
fi
log "artifact transfer smoke PASSED"