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