scorpiox-bot-plugin-memory
←release-dist.shbash
383 lines · 17 KBHEADBlameDownload
1#!/usr/bin/env bash
2# release-dist.sh - build, scan and publish plugin_memory to the plugin registry.
3#
4# Two paths:
5# A. Preferred: if `scorpiox-cli-botplugin` is installed, hand the whole job to it
6# (`scorpiox-cli-botplugin publish .`), which does the same work with a richer
7# CLI and is what publishes the connector fields (kind/mcp/daemon/menus/
8# verified) into all four dist documents.
9# B. Standalone (this script): secret scan -> multi-target build ->
10# source.tar.gz -> manifest.json + SHA256SUMS -> upload -> latest.json /
11# index.json -> plugingit mirror.
12#
13# Nothing here contains a host address or a credential. Everything environment-specific
14# comes from the environment, so this file stays safe to publish:
15#
16# DIST_HOST ssh target for the dist server (required for upload)
17# DIST_PASS ssh password for DIST_HOST (optional, defaults to ssh-agent/key)
18# DIST_PATH remote web root of the dist server (default /data/dist)
19# MAC_HOST ssh target for a macOS builder (optional: macos-arm64/x86_64)
20# MAC_USER / MAC_PASS (optional)
21# PLUGINGIT_TOKEN_FILE file holding the plugingit write token (default /etc/scorpiox-plugingit.token)
22# PLUGINGIT_REMOTE public mirror URL to push into
23#
24# Usage:
25# ./release-dist.sh # full release
26# ./release-dist.sh --dry-run # build + stage in /tmp, no upload
27# ./release-dist.sh --no-macos # skip the macOS legs
28# ./release-dist.sh --no-mirror # skip the plugingit mirror
29# ./release-dist.sh --standalone # use this script even if the CLI is installed
30
31set -euo pipefail
32
33SLUG="memory"
34ID="plugin_memory"
35VERSION="1.0.0"
36DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins"
37
38ROOT="$(cd "$(dirname "$0")" && pwd)"
39cd "$ROOT"
40
41DRY_RUN=0
42DO_MACOS=1
43DO_MIRROR=1
44STANDALONE=0
45for arg in "$@"; do
46 case "$arg" in
47 --standalone) STANDALONE=1 ;;
48 --dry-run) DRY_RUN=1 ;;
49 --no-macos) DO_MACOS=0 ;;
50 --no-mirror) DO_MIRROR=0 ;;
51 -h|--help) sed -n '2,30p' "$0"; exit 0 ;;
52 *) echo "unknown argument: $arg" >&2; exit 2 ;;
53 esac
54done
55
56DIST_HOST="${DIST_HOST:-}"
57DIST_PASS="${DIST_PASS:-}"
58DIST_PATH="${DIST_PATH:-/data/dist}"
59PLUGINGIT_TOKEN_FILE="${PLUGINGIT_TOKEN_FILE:-/etc/scorpiox-plugingit.token}"
60PLUGINGIT_REMOTE="${PLUGINGIT_REMOTE:-https://plugingit.scorpiox.net/scorpiox-bot-plugin-${SLUG}.git}"
61
62STAGE="/tmp/${SLUG}-dist-${VERSION}-$$"
63mkdir -p "$STAGE"
64trap 'rm -rf "$STAGE"' EXIT
65
66say() { printf '\n\033[1m== %s ==\033[0m\n' "$1"; }
67die() { printf '\033[31mERROR:\033[0m %s\n' "$1" >&2; exit 1; }
68
69# ---------------------------------------------------------------- 0. preflight
70say "0/7 preflight"
71
72if [ "$STANDALONE" -eq 0 ] && command -v scorpiox-cli-botplugin >/dev/null 2>&1; then
73 say "using scorpiox-cli-botplugin publish"
74 exec scorpiox-cli-botplugin publish "$ROOT"
75fi
76echo " running the standalone pipeline (dist + plugingit mirror)"
77
78if [ -n "$(git status --porcelain 2>/dev/null)" ]; then
79 echo " note: working tree is dirty; source.tar.gz comes from the last commit"
80fi
81COMMIT=$(git rev-parse HEAD 2>/dev/null || echo "unknown")
82echo " version $VERSION"
83echo " commit $COMMIT"
84
85# ---------------------------------------------------------------- 1. secret scan
86say "1/7 secret & LAN scan (full history)"
87
88SCAN_TMP="$STAGE/history.txt"
89# Scan every added/removed line of every commit. Two exclusions, both about the
90# audit rather than the code: the committer identity line, and any line that is
91# itself a scan definition - otherwise this file's own detector patterns (and the
92# identical checks in tests/run.sh) read as findings in every historical revision
93# of them. A real secret on any other line still hits.
94git log -p --all 2>/dev/null \
95 | grep -v '^Author: ' \
96 | grep -vE 'scan "|HITS=|grep -E|P_ROOT=|P_WT=|\$ALL' \
97 > "$SCAN_TMP" || : > "$SCAN_TMP"
98HITS=0
99scan() { # scan <label> <regex>
100 local out
101 out=$(grep -Eo "$2" "$SCAN_TMP" 2>/dev/null | sort -u | head -5 || true)
102 if [ -n "$out" ]; then
103 printf ' \033[31mHIT\033[0m %s\n' "$1"
104 printf '%s\n' "$out" | sed 's/^/ /'
105 HITS=$((HITS + 1))
106 else
107 printf ' ok %s\n' "$1"
108 fi
109}
110scan "LAN IPs 192.168.x.x" '192\.168\.[0-9]+\.[0-9]+'
111scan "private 10.x.x.x" '\b10\.[0-9]+\.[0-9]+\.[0-9]+'
112scan "private 172.16-31.x.x" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+'
113scan "GitHub PAT" 'ghp_[A-Za-z0-9]{36}'
114scan "HuggingFace token" 'hf_[A-Za-z0-9]{30,}'
115scan "embedded git credentials" 'scorpioxinc:[A-Za-z0-9]{20,}'
116scan "hard-coded ssh password" 'sshpass -p [^"$]'
117P_ROOT="/codebases"
118P_PICOBOT="${P_ROOT}/pico""bot"
119P_WT=".work""trees"
120scan "internal fleet paths" "/root/tools|${P_PICOBOT}|${P_WT}"
121scan "run-token env assignment" 'RUNNER_'"PASS="
122if [ "$HITS" -ne 0 ]; then
123 die "secret scan found $HITS problem(s) - refusing to publish"
124fi
125echo " clean: 0 hits"
126
127# ---------------------------------------------------------------- 2. build
128say "2/7 build targets"
129
130# Every target is the Makefile's own command line, so what ships is what the
131# suite was tested against. yyjson is compiled in; no target needs any library.
132make native >/dev/null || die "native build failed"
133cp -f "$ID" "$STAGE/$ID"
134echo " built linux-x86_64 (glibc) $(wc -c < "$STAGE/$ID" | tr -d ' ') bytes"
135
136if command -v musl-gcc >/dev/null 2>&1; then
137 make musl >/dev/null || die "musl build failed"
138 cp -f "$ID.musl" "$STAGE/$ID.musl"
139 echo " built linux-x86_64-musl $(wc -c < "$STAGE/$ID.musl" | tr -d ' ') bytes"
140else
141 echo " skip linux-x86_64-musl (no musl-gcc)"
142fi
143
144if command -v aarch64-linux-musl-gcc >/dev/null 2>&1; then
145 ARM64_CC=aarch64-linux-musl-gcc make arm64 >/dev/null || die "arm64 musl build failed"
146 cp -f "$ID.arm64" "$STAGE/$ID.arm64"
147 echo " built linux-arm64-musl $(wc -c < "$STAGE/$ID.arm64" | tr -d ' ') bytes"
148elif command -v aarch64-linux-gnu-gcc >/dev/null 2>&1; then
149 ARM_CC=aarch64-linux-gnu-gcc make arm64 >/dev/null || die "arm64 build failed"
150 cp -f "$ID.arm64" "$STAGE/$ID.arm64"
151 echo " built linux-arm64-static $(wc -c < "$STAGE/$ID.arm64" | tr -d ' ') bytes"
152else
153 echo " skip linux-arm64-musl (no aarch64 cross compiler)"
154fi
155
156if command -v x86_64-w64-mingw32-gcc >/dev/null 2>&1; then
157 make win64 >/dev/null || die "MinGW build failed"
158 cp -f "$ID.exe" "$STAGE/$ID.exe"
159 [ "$(head -c 2 "$STAGE/$ID.exe")" = "MZ" ] || die "windows artifact is not a PE"
160 echo " built windows-x86_64 $(wc -c < "$STAGE/$ID.exe" | tr -d ' ') bytes"
161else
162 echo " skip windows-x86_64 (no MinGW cross compiler)"
163fi
164
165# macOS legs - Mach-O must be produced by Apple clang on a real Mac. The repo
166# layout is preserved on the builder because api.c includes "ui_html.h" from
167# next to the .c and yyjson.h from ../vendor.
168if [ "$DO_MACOS" -eq 1 ] && [ -n "${MAC_HOST:-}" ]; then
169 MAC_USER="${MAC_USER:-$(whoami)}"
170 MAC_DIR="/tmp/${ID}-mac-$$"
171 echo " building macOS legs on $MAC_HOST"
172 SSH_MAC=(ssh -o StrictHostKeyChecking=no)
173 SCP_MAC=(scp -o StrictHostKeyChecking=no)
174 if [ -n "${MAC_PASS:-}" ]; then
175 SSH_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SSH_MAC[@]}")
176 SCP_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SCP_MAC[@]}")
177 fi
178 if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" \
179 "rm -rf '$MAC_DIR' && mkdir -p '$MAC_DIR/src' '$MAC_DIR/vendor' '$MAC_DIR/tools'"; then
180 if "${SCP_MAC[@]}" -q src/*.c src/*.h "$MAC_USER@$MAC_HOST:$MAC_DIR/src/" &&
181 "${SCP_MAC[@]}" -q vendor/yyjson.c vendor/yyjson.h "$MAC_USER@$MAC_HOST:$MAC_DIR/vendor/" &&
182 "${SCP_MAC[@]}" -q tools/gen_ui.py "$MAC_USER@$MAC_HOST:$MAC_DIR/tools/" &&
183 "${SCP_MAC[@]}" -q src/ui.html Makefile "$MAC_USER@$MAC_HOST:$MAC_DIR/"; then
184 if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "cd '$MAC_DIR' && python3 tools/gen_ui.py" 2>/dev/null; then
185 for slice in arm64 x86_64; do
186 if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" \
187 "cd '$MAC_DIR' && clang -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -I src -I vendor -arch $slice -o $ID.$slice src/plugin_memory.c src/graph.c src/tools.c src/api.c vendor/yyjson.c" \
188 2>/dev/null; then
189 "${SCP_MAC[@]}" -q "$MAC_USER@$MAC_HOST:$MAC_DIR/$ID.$slice" "$STAGE/$ID.macos-$slice" \
190 && echo " built macos-$slice ($(wc -c < "$STAGE/$ID.macos-$slice" | tr -d ' ') bytes)"
191 else
192 echo " warn: macos-$slice build failed on $MAC_HOST"
193 fi
194 done
195 else
196 echo " warn: gen_ui.py failed on $MAC_HOST (python3 missing?)"
197 fi
198 else
199 echo " warn: mac scp failed"
200 fi
201 "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR'" 2>/dev/null || true
202 else
203 echo " warn: mac host unreachable"
204 fi
205else
206 echo " skip macOS legs (set MAC_HOST/MAC_USER/MAC_PASS to include them)"
207fi
208
209# ---------------------------------------------------------------- 3. package
210say "3/7 package"
211
212git archive --format=tar.gz --prefix="${ID}-${VERSION}/" \
213 -o "$STAGE/source.tar.gz" HEAD 2>/dev/null \
214 || tar -czf "$STAGE/source.tar.gz" --exclude=.git .
215echo " source.tar.gz $(wc -c < "$STAGE/source.tar.gz" | tr -d ' ') bytes"
216
217target_meta() { # target_meta <key> <file> <format> <relpath>
218 local key="$1" file="$2" fmt="$3" rel="$4"
219 [ -f "$file" ] || return 0
220 local sha size
221 sha=$(sha256sum "$file" | awk '{print $1}')
222 size=$(wc -c < "$file" | tr -d ' ')
223 printf ' "%s": {"url": "%s/%s/%s/%s", "sha256": "%s", "size_bytes": %s, "format": "%s"}' \
224 "$key" "$DIST_BASE_URL" "$SLUG" "$VERSION" "$rel" "$sha" "$size" "$fmt"
225}
226
227# summary / title / trust_notice are taken from plugin.json so there is exactly
228# one place that says what this plugin does.
229
230{
231 printf '{\n'
232 printf ' "slug": "%s",\n' "$SLUG"
233 printf ' "id": "%s",\n' "$ID"
234 printf ' "name": "%s",\n' "$SLUG"
235 printf ' "desc": "Knowledge graph",\n'
236 printf ' "title": %s,\n' "$(python3 -c "import json;print(json.dumps(json.load(open('plugin.json'))['title']))")"
237 printf ' "summary": %s,\n' "$(python3 -c "import json;print(json.dumps(json.load(open('plugin.json'))['summary']))")"
238 printf ' "category": "devtools",\n'
239 printf ' "version": "%s",\n' "$VERSION"
240 printf ' "license": "MIT",\n'
241 printf ' "kind": "mcp",\n'
242 printf ' "mcp": true,\n'
243 printf ' "daemon": false,\n'
244 printf ' "menus": false,\n'
245 printf ' "verified": "live",\n'
246 printf ' "trust_notice": %s,\n' "$(python3 -c "import json;print(json.dumps(json.load(open('plugin.json'))['trust_notice']))")"
247 printf ' "capabilities": ["fs_read_connector_dir", "fs_write_connector_dir"],\n'
248 printf ' "git_repo_url": "https://plugingit.scorpiox.net/scorpiox-bot-plugin-%s.git",\n' "$SLUG"
249 printf ' "git_web_url": "https://plugingit.scorpiox.net/repo?name=scorpiox-bot-plugin-%s",\n' "$SLUG"
250 printf ' "git_commit": "%s",\n' "$COMMIT"
251 printf ' "source_tarball_url": "%s/%s/%s/source.tar.gz",\n' "$DIST_BASE_URL" "$SLUG" "$VERSION"
252 printf ' "source_tarball_sha256": "%s",\n' "$(sha256sum "$STAGE/source.tar.gz" | awk '{print $1}')"
253 printf ' "build_cmd_posix": "make",\n'
254 printf ' "build_cmd_win64": "make win64",\n'
255 printf ' "published_at": "%s",\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
256 printf ' "targets": {\n'
257 first=1
258 emit() { if [ -f "$2" ]; then [ $first -eq 1 ] || printf ',\n'; first=0; target_meta "$@"; fi; }
259 emit "linux-x86_64" "$STAGE/$ID" "elf-x86_64" "linux-x86_64/$ID"
260 emit "linux-x86_64-musl" "$STAGE/$ID.musl" "elf-x86_64-static" "linux-x86_64-musl/$ID"
261 emit "linux-arm64-musl" "$STAGE/$ID.arm64" "elf-arm64-static" "linux-arm64-musl/$ID"
262 emit "macos-arm64" "$STAGE/$ID.macos-arm64" "macho-arm64" "macos-arm64/$ID"
263 emit "macos-x86_64" "$STAGE/$ID.macos-x86_64" "macho-x86_64" "macos-x86_64/$ID"
264 emit "windows-x86_64" "$STAGE/$ID.exe" "pe-x86_64" "windows-x86_64/$ID.exe"
265 printf '\n }\n}\n'
266} > "$STAGE/manifest.json"
267
268python3 -c 'import json,sys; json.load(open(sys.argv[1]))' "$STAGE/manifest.json" \
269 || die "generated manifest.json is not valid JSON"
270echo " manifest.json ok"
271
272# SHA256SUMS - relative paths, exactly as published
273( cd "$STAGE"
274 for f in source.tar.gz; do [ -f "$f" ] && sha256sum "$f"; done
275 [ -f "$ID" ] && sha256sum "$ID" | sed "s| $ID\$| linux-x86_64/$ID|"
276 [ -f "$ID.musl" ] && sha256sum "$ID.musl" | sed "s| $ID.musl\$| linux-x86_64-musl/$ID|"
277 [ -f "$ID.arm64" ] && sha256sum "$ID.arm64" | sed "s| $ID.arm64\$| linux-arm64-musl/$ID|"
278 [ -f "$ID.macos-arm64" ] && sha256sum "$ID.macos-arm64" | sed "s| $ID.macos-arm64\$| macos-arm64/$ID|"
279 [ -f "$ID.macos-x86_64" ]&& sha256sum "$ID.macos-x86_64" | sed "s| $ID.macos-x86_64\$| macos-x86_64/$ID|"
280 [ -f "$ID.exe" ] && sha256sum "$ID.exe" | sed "s| $ID.exe\$| windows-x86_64/$ID.exe|"
281 true ) > "$STAGE/SHA256SUMS"
282echo " SHA256SUMS $(wc -l < "$STAGE/SHA256SUMS" | tr -d ' ') entries"
283
284# ---------------------------------------------------------------- 4. stage
285say "4/7 stage tree"
286PKG="$STAGE/pkg"
287mkdir -p "$PKG/linux-x86_64" "$PKG/linux-x86_64-musl" "$PKG/linux-arm64-musl" \
288 "$PKG/macos-arm64" "$PKG/macos-x86_64" "$PKG/windows-x86_64"
289cp -f "$STAGE/$ID" "$PKG/linux-x86_64/$ID"
290[ -f "$STAGE/$ID.musl" ] && cp -f "$STAGE/$ID.musl" "$PKG/linux-x86_64-musl/$ID"
291[ -f "$STAGE/$ID.arm64" ] && cp -f "$STAGE/$ID.arm64" "$PKG/linux-arm64-musl/$ID"
292[ -f "$STAGE/$ID.macos-arm64" ] && cp -f "$STAGE/$ID.macos-arm64" "$PKG/macos-arm64/$ID"
293[ -f "$STAGE/$ID.macos-x86_64" ]&& cp -f "$STAGE/$ID.macos-x86_64" "$PKG/macos-x86_64/$ID"
294[ -f "$STAGE/$ID.exe" ] && cp -f "$STAGE/$ID.exe" "$PKG/windows-x86_64/$ID.exe"
295cp -f "$STAGE/manifest.json" "$STAGE/SHA256SUMS" "$STAGE/source.tar.gz" "$PKG/"
296chmod +x "$PKG"/*/"$ID" 2>/dev/null || true
297find "$PKG" -type f | sed 's/^/ /'
298
299if [ "$DRY_RUN" -eq 1 ]; then
300 say "dry run - staged at $STAGE/pkg (kept for inspection)"
301 trap - EXIT
302 exit 0
303fi
304
305# ---------------------------------------------------------------- 5. upload
306say "5/7 upload"
307
308[ -n "$DIST_HOST" ] || die "DIST_HOST is not set - cannot upload"
309SSH=(ssh -o StrictHostKeyChecking=no)
310SCP=(scp -o StrictHostKeyChecking=no)
311if [ -n "$DIST_PASS" ]; then
312 SSH=(scorpiox-sshpass -p "$DIST_PASS" "${SSH[@]}")
313 SCP=(scorpiox-sshpass -p "$DIST_PASS" "${SCP[@]}")
314fi
315REMOTE_DIR="$DIST_PATH/bot-plugins/$SLUG/$VERSION"
316
317"${SSH[@]}" "$DIST_HOST" "mkdir -p '$REMOTE_DIR' '$DIST_PATH/bot-plugins/$SLUG'" \
318 || die "cannot create $REMOTE_DIR on $DIST_HOST"
319# Stage to one tarball, transfer it, extract remotely. Streaming tar through
320# ssh corrupts on some OpenSSH builds (remote gzip dies with SIGPIPE), and
321# OpenSSH 9 refuses to scp a bare "." source.
322tar -czf "$STAGE/pkg.tar.gz" -C "$PKG" . || die "packaging failed"
323"${SCP[@]}" -q "$STAGE/pkg.tar.gz" "$DIST_HOST:$REMOTE_DIR/pkg.tar.gz" || die "upload failed"
324"${SSH[@]}" "$DIST_HOST" "cd '$REMOTE_DIR' && tar -xzf pkg.tar.gz && rm -f pkg.tar.gz" \
325 || die "remote extract failed"
326echo " uploaded to $DIST_HOST:$REMOTE_DIR"
327
328# latest.json = copy of this version's manifest
329"${SSH[@]}" "$DIST_HOST" "cp -f '$REMOTE_DIR/manifest.json' '$DIST_PATH/bot-plugins/$SLUG/latest.json'" \
330 || die "latest.json update failed"
331echo " updated bot-plugins/$SLUG/latest.json"
332
333# index.json - merge, preserving every sibling <slug>/latest.json
334say "index.json merge"
335"${SSH[@]}" "$DIST_HOST" "cd '$DIST_PATH/bot-plugins' && python3 - '$SLUG' <<'PY'
336import json, os, sys, glob
337slug = sys.argv[1]
338entries = {}
339for p in sorted(glob.glob('*/latest.json')):
340 s = os.path.dirname(p)
341 try:
342 entries[s] = json.load(open(p))
343 except Exception:
344 pass
345index = {
346 'ok': True,
347 'service': 'bot-plugins-dist',
348 'dist_base_url': 'https://dist.scorpiox.net/bot-plugins',
349 'count': len(entries),
350 'slugs': sorted(entries),
351 'plugins': [entries[k] for k in sorted(entries)],
352}
353open('index.json.tmp', 'w').write(json.dumps(index, indent=2) + chr(10))
354os.replace('index.json.tmp', 'index.json')
355print(' index.json now has %d plugin(s): %s' % (len(entries), ', '.join(sorted(entries))))
356PY" || die "index.json merge failed"
357
358# ---------------------------------------------------------------- 6. mirror
359say "6/7 plugingit mirror"
360if [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then
361 TOKEN=$(tr -d ' \r\n' < "$PLUGINGIT_TOKEN_FILE")
362 AUTH_REMOTE=$(printf '%s' "$PLUGINGIT_REMOTE" | sed "s|https://|https://x-access-token:${TOKEN}@|")
363 git push --all "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || die "mirror push failed"
364 git push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true
365 echo " mirrored to $PLUGINGIT_REMOTE"
366else
367 echo " skip (no $PLUGINGIT_TOKEN_FILE or --no-mirror)"
368fi
369
370# ---------------------------------------------------------------- 7. verify
371say "7/7 verify"
372CODE=$(curl -fsS -o "$STAGE/latest.check" -w '%{http_code}' \
373 "$DIST_BASE_URL/$SLUG/latest.json" || echo 000)
374if [ "$CODE" = "200" ]; then
375 python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(" latest.json OK - %s v%s, %d target(s), kind=%s mcp=%s menus=%s verified=%s" % (d["slug"], d["version"], len(d["targets"]), d.get("kind"), d.get("mcp"), d.get("menus"), d.get("verified")))' \
376 "$STAGE/latest.check" || echo " warn: latest.json is not valid JSON"
377else
378 echo " warn: $DIST_BASE_URL/$SLUG/latest.json returned HTTP $CODE"
379fi
380
381printf '\n\033[1mrelease complete\033[0m\n'
382printf ' registry %s/%s/%s/\n' "$DIST_BASE_URL" "$SLUG" "$VERSION"
383printf ' manifest %s/%s/%s/manifest.json\n' "$DIST_BASE_URL" "$SLUG" "$VERSION"