0
0
#!/usr/bin/env bash
# On the staging VM, as stg, after dev up: the driver accounts (accounts.sh) on the network's bidder PDS and the suite's target
# file (the targets/prod.json shape) for the market network instance stg. Usage: target.sh <run> > target-staging.json
# restartSshMaxS 20: with --network a restarted invocation re-places itself as a network child (~7 s) and its requester
# becomes Ready again (~7 s) before ssh; prod keeps 10.
set -euo pipefail
. "$(dirname "$0")/accounts.sh"
run=$1
S=${SWC:-/home/stg/stg/socialweb-computer-linux-amd64} ST=${SWC_STATE:-/home/stg/stg/s} I=${SWC_INSTANCE:-stg} C=${SWC_CREDS:-/home/stg}
N=(--instance "$I" --state-dir "$ST")
st=$($S dev status "${N[@]}" 2>/dev/null)
eval "$($S dev env "${N[@]}" 2>/dev/null)"
pds=$(jq -r '.network.status.bidderPdsUrl' <<<"$st")
host() { jq -r --arg n "$1" '(.network.status.network.hosts // [])[] | select(.name == $n) | .address' <<<"$st"; }
plc=$(jq -r '.network.status.network.plcDirectoryUrl' <<<"$st")
# the network's own atirc (status.network.atircUrl, dev up --network-atirc-url; atirc.sh serves it): unset, the target names none
atirc=$(jq -r '.network.status.network.atircUrl // empty' <<<"$st")
accounts='{}'
# the multi layout's trust.sh made the accounts (and their vouches) before the probes: reuse them (plan 0047)
# (only the drivers: the gateway's requester and the ssh gateway's operator, gateway.sh, are not accounts of the target)
[ -f "${SWC_DIR:-/home/stg/stg}/accounts.json" ] && accounts=$(jq -c --arg d "$STG_DRIVERS" "$STG_PICK_DRIVERS" "${SWC_DIR:-/home/stg/stg}/accounts.json")
for a in $STG_DRIVERS; do
[ "$(jq -r --arg a "$a" '.[$a].did // empty' <<<"$accounts")" ] && continue
h="$a-${run: -4}$(date +%s | tail -c 5).bob.test" pw=$(head -c 24 /dev/urandom | base64 | tr -dc A-Za-z0-9) c=$C/creds-$a
did=$(curl -fsS -H 'content-type: application/json' "$pds/xrpc/com.atproto.server.createAccount" \
-d "$(jq -nc --arg h "$h" --arg p "$pw" '{handle: $h, password: $p, email: ($h + "@staging.invalid")}')" | jq -r .did)
P=$(tr a-z A-Z <<<"$a")
(umask 077 && printf 'ATPROTO_PASSWORD=%s\n%s_ATPROTO_HANDLE=%s\n%s_ATPROTO_PASSWORD=%s\n' "$pw" "$P" "$h" "$P" "$pw" > "$c")
accounts=$(jq -c --arg a "$a" --arg h "$h" --arg d "$did" --arg c "$c" --arg p "$P" '.[$a] = {handle: $h, did: $d, creds: $c, oauthAccount: $p, throwaway: true}' <<<"$accounts")
done
# who plays which role of the system tests (accounts.sh): a role with no account here is not in the target
roles=$(jq -nc --arg r "$STG_ROLES" '$r | split(" ") | map(split("=") | {key: .[0], value: .[1]}) | from_entries')
bidders=$(jq -c '[(.network.status.apps // [])[] | select(.kind == "MarketBidder") | {name, did}]' <<<"$st" | jq -c '.[]' | while read -r b; do
n=$(jq -r .name <<<"$b")
op=$(curl -fsS "$plc/$(jq -r .did <<<"$b")" | jq -r '[(.alsoKnownAs // [])[] | select(startswith("https://ident.link/agent-of/"))][0] // "" | ltrimstr("https://ident.link/agent-of/")')
jq -c --arg op "$op" --arg kc "$KUBECONFIG" --arg ws "root:$n" '. + {operator: $op, kcp: {kubeconfig: $kc, workspace: $ws, via: {kind: "direct"}}}' <<<"$b"
done | jq -sc .)
# multi-VM: the bidders joined from other VMs are not apps of this network; the host wrote them (their DIDs, and their kcp's admin kubeconfig
# fetched over the overlay) to bidders.json, so the entries that read or fault a bidder's kcp find the winner in the target
J=${SWC_DIR:-/home/stg/stg}/bidders.json
[ -f "$J" ] && bidders=$(jq -sc 'add' <(echo "$bidders") "$J")
# the network's one XRPC entry: its front (status.network.xrpcFront) serves the PLC, the relay and every PDS by name
# (their ingress Exposures, *.stg.test, plan 0050); the test host sends every named request there, trusting the network CA (the run root)
ca=$C/network-ca.pem
jq -r '.network.status.network.caBundle' <<<"$st" > "$ca"
front=$(jq -r '.network.status.network.xrpcFront // empty' <<<"$st")
[ -n "$front" ] || front=$(host dispatcher-tls.default.relay.svc.kcp.local)
# an ingress Exposure's https name, from the network's kcp root (curl on the admin token: no kubectl on a staging VM)
ktoken=$(awk '/ token:/{print $2; exit}' "$KUBECONFIG") kserver=$(awk '/ server:/{print $2; exit}' "$KUBECONFIG")
named() { curl -fsSk -H "Authorization: Bearer $ktoken" "${kserver%%/clusters/*}/clusters/root/apis/exposure.socialweb.computer/v1alpha1/exposures/$1" | jq -er .status.address; }
# the gateway and the ssh gateway on the gateway VM (gateway.sh wrote gateway.json: url, aud, requesterDid, kcp; ssh, sshPort, sshWeb, log); none: names that resolve nowhere, so an
# entry that needs the gateway fails at its first call
gw='{}'
[ -f "${SWC_DIR:-/home/stg/stg}/gateway.json" ] && gw=$(cat "${SWC_DIR:-/home/stg/stg}/gateway.json")
# what was resolved from the status, to the suite's log (target-staging.log): an empty front, host list or bidder list shows here rather than as a later failure
echo "target.sh resolved: bidderPds=${pds:-<none>} plc=${plc:-<none>} atirc=${atirc:-<none>} gateway=$(jq -r '.url // "<none>"' <<<"$gw") xrpcFront=${front:-<none>} hosts=$(jq '(.network.status.network.hosts // []) | length' <<<"$st") apps=$(jq '(.network.status.apps // []) | length' <<<"$st") bidders=$(jq 'length' <<<"$bidders") status.network keys: $(jq -c '(.network.status.network // {}) | keys' <<<"$st") status keys: $(jq -c '(.network.status // {}) | keys' <<<"$st")" >&2
# 0009's page: the suite's page/ (the host's SWC_PAGE_REPO) with pinned node and Chrome, page.sh; a failed tool install is in
# this log and fails 0009's round with what is missing, not the target
page=$("$(dirname "$0")/page.sh" || true)
# the OAuth sign-ins (oauth.signin, page.signin: scripts/oauth-signin.ts) run with the suite's pinned deno and Chrome
# (tools.sh), the target's tools; a failed install is in this log and fails the sign-in's entry, not the target
tools=$("$(dirname "$0")/tools.sh" deno chrome || true)
# pds stays the bidder PDS's own URL: the requester under test (run request) signs in there, and it does not go through the front
jq -n --arg plc "$(named svc-plc-plc-ingress)" --arg pds "$pds" --arg relay "$(named svc-relay-relay-ingress)" \
--arg front "$front" --arg ca "$ca" --arg agent "$([ -f "${SWC_DIR:-/home/stg/stg}/agent-config.toml" ] && echo "${SWC_DIR:-/home/stg/stg}/agent-config.toml")" \
--argjson accounts "$accounts" --argjson roles "$roles" --argjson bidders "$bidders" --argjson gw "$gw" --arg s "$S" --arg i "$I" --arg st "$ST" --arg atirc "$atirc" --arg page "$page" --arg tools "$tools" '{
name: "staging", restartSshMaxS: 20, rootHosts: ["root@127.0.0.1"], plc: $plc, pds: $pds, relay: $relay, xrpcFront: {address: $front, ca: $ca}, requester: ("path:" + $s),
network: {instance: $i, stateDir: $st}, accounts: $accounts, roles: $roles, bidders: $bidders,
gateway: ({url: "https://gateway.staging.invalid", aud: "did:web:gateway.staging.invalid", ssh: "gateway.staging.invalid"} + $gw)
} + (if $agent != "" then {agentConfig: $agent} else {} end) + (if $atirc != "" then {atirc: $atirc} else {} end) + (if $page != "" then {pageRepo: $page} else {} end) + (if $tools != "" then {tools: $tools} else {} end)'