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