scorpiox-bot-plugin-ports
←release-dist.shbash
362 lines · 16 KBHEADBlameDownload
1#!/usr/bin/env bash
2# release-dist.sh - build, scan and publish plugin_ports 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="ports"
33ID="plugin_ports"
34VERSION="1.0.1"
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 read as
90# findings in every historical revision of it.
91git log -p --all 2>/dev/null \
92 | grep -v '^Author: ' \
93 | grep -vE 'scan "|audit "|HITS=|grep -E|P_ROOT=|P_WT=|PK="|\$ALL' \
94 > "$SCAN_TMP" || : > "$SCAN_TMP"
95HITS=0
96scan() { # scan <label> <regex>
97 local out
98 out=$(grep -Eo "$2" "$SCAN_TMP" 2>/dev/null | sort -u | head -5 || true)
99 if [ -n "$out" ]; then
100 printf ' \033[31mHIT\033[0m %s\n' "$1"
101 printf '%s\n' "$out" | sed 's/^/ /'
102 HITS=$((HITS + 1))
103 else
104 printf ' ok %s\n' "$1"
105 fi
106}
107scan "LAN IPs 192.168.x.x" '192\.168\.[0-9]+\.[0-9]+'
108scan "private 10.x.x.x" '\b10\.[0-9]+\.[0-9]+\.[0-9]+'
109scan "private 172.16-31.x.x" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+'
110scan "GitHub PAT" 'ghp_[A-Za-z0-9]{36}'
111scan "HuggingFace token" 'hf_[A-Za-z0-9]{30,}'
112scan "embedded git credentials" 'scorpioxinc:[A-Za-z0-9]{20,}'
113scan "hard-coded ssh password" 'sshpass -p [^"$]'
114P_ROOT="/codebases"
115P_PICOBOT="${P_ROOT}/pico""bot"
116P_WT=".work""trees"
117scan "internal fleet paths" "/root/tools|${P_PICOBOT}|${P_WT}"
118scan "run-token env assignment" 'RUNNER_'"PASS="
119if [ "$HITS" -ne 0 ]; then
120 die "secret scan found $HITS problem(s) - refusing to publish"
121fi
122echo " clean: 0 hits"
123
124# ---------------------------------------------------------------- 2. build
125say "2/7 build targets"
126
127# linux-x86_64 (glibc, native)
128if command -v cc >/dev/null 2>&1; then
129 cc -O2 -std=c99 -Wall -Wextra -o "$STAGE/$ID" src/$ID.c || die "native build failed"
130 echo " built linux-x86_64 (glibc) $(wc -c < "$STAGE/$ID" | tr -d ' ') bytes"
131else
132 die "no native cc found"
133fi
134
135# linux-x86_64-musl (fully static - the universal Linux artifact)
136if command -v musl-gcc >/dev/null 2>&1; then
137 musl-gcc -O2 -std=c99 -Wall -Wextra -static -o "$STAGE/$ID.musl" src/$ID.c \
138 || die "musl build failed"
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
144# linux-arm64-musl (static aarch64 - Raspberry Pi 5 / ARM64 servers)
145if command -v aarch64-linux-gnu-gcc >/dev/null 2>&1; then
146 aarch64-linux-gnu-gcc -O2 -std=c99 -Wall -Wextra -static -o "$STAGE/$ID.arm64" src/$ID.c \
147 || die "arm64 build failed"
148 echo " built linux-arm64-musl $(wc -c < "$STAGE/$ID.arm64" | tr -d ' ') bytes"
149else
150 echo " skip linux-arm64-musl (no aarch64-linux-gnu-gcc)"
151fi
152
153# windows-x86_64 (static PE)
154if command -v x86_64-w64-mingw32-gcc >/dev/null 2>&1; then
155 x86_64-w64-mingw32-gcc -O2 -std=c99 -Wall -Wextra -static -o "$STAGE/$ID.exe" src/$ID.c \
156 -liphlpapi -lws2_32 || die "MinGW build failed"
157 [ "$(head -c 2 "$STAGE/$ID.exe")" = "MZ" ] || die "windows artifact is not a PE"
158 echo " built windows-x86_64 $(wc -c < "$STAGE/$ID.exe" | tr -d ' ') bytes"
159else
160 echo " skip windows-x86_64 (no MinGW cross compiler)"
161fi
162
163# macOS legs - Mach-O must be produced by Apple clang on a real Mac.
164# Both slices are cross-built from one host: -arch arm64 and -arch x86_64.
165if [ "$DO_MACOS" -eq 1 ] && [ -n "${MAC_HOST:-}" ]; then
166 MAC_USER="${MAC_USER:-$(whoami)}"
167 MAC_DIR="/tmp/${ID}-mac-$$"
168 echo " building macOS legs on $MAC_HOST"
169 SSH_MAC=(ssh -o StrictHostKeyChecking=no)
170 SCP_MAC=(scp -o StrictHostKeyChecking=no)
171 if [ -n "${MAC_PASS:-}" ]; then
172 SSH_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SSH_MAC[@]}")
173 SCP_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SCP_MAC[@]}")
174 fi
175 if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR' && mkdir -p '$MAC_DIR'"; then
176 if "${SCP_MAC[@]}" src/$ID.c "$MAC_USER@$MAC_HOST:$MAC_DIR/$ID.c"; then
177 # Build straight from the uploaded file - no src/ tree on the remote.
178 for slice in arm64 x86_64; do
179 if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" \
180 "cd '$MAC_DIR' && clang -O2 -std=c99 -Wall -Wextra -arch $slice -o $ID.$slice $ID.c" \
181 2>/dev/null; then
182 "${SCP_MAC[@]}" "$MAC_USER@$MAC_HOST:$MAC_DIR/$ID.$slice" "$STAGE/$ID.macos-$slice" 2>/dev/null \
183 && echo " built macos-$slice ($(wc -c < "$STAGE/$ID.macos-$slice" | tr -d ' ') bytes)"
184 else
185 echo " warn: macos-$slice build failed on $MAC_HOST"
186 fi
187 done
188 "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR'" || true
189 else
190 echo " warn: could not upload source to $MAC_HOST"
191 fi
192 else
193 echo " warn: cannot reach macOS builder $MAC_HOST"
194 fi
195else
196 echo " skip macOS legs (no MAC_HOST or --no-macos)"
197fi
198
199# ---------------------------------------------------------------- 3. manifest
200say "3/7 manifest + checksums"
201
202git archive --format=tar.gz --prefix="${ID}-${VERSION}/" \
203 -o "$STAGE/source.tar.gz" HEAD 2>/dev/null \
204 || tar -czf "$STAGE/source.tar.gz" --exclude=.git .
205echo " source.tar.gz $(wc -c < "$STAGE/source.tar.gz" | tr -d ' ') bytes"
206
207target_meta() { # target_meta <key> <file> <format> <relpath>
208 local key="$1" file="$2" fmt="$3" rel="$4"
209 [ -f "$file" ] || return 0
210 local sha size
211 sha=$(sha256sum "$file" | awk '{print $1}')
212 size=$(wc -c < "$file" | tr -d ' ')
213 printf ' "%s": {"url": "%s/%s/%s/%s", "sha256": "%s", "size_bytes": %s, "format": "%s"}' \
214 "$key" "$DIST_BASE_URL" "$SLUG" "$VERSION" "$rel" "$sha" "$size" "$fmt"
215}
216
217{
218 printf '{\n'
219 printf ' "slug": "%s",\n' "$SLUG"
220 printf ' "id": "%s",\n' "$ID"
221 printf ' "name": "ports",\n'
222 printf ' "desc": "Listening TCP",\n'
223 printf ' "title": "Local Listening Port Inspector",\n'
224 printf ' "summary": "Zero-dependency inspector for the listening TCP and UDP sockets on your node: bind address, port, loopback-versus-wildcard exposure, owning PID and command, plus a one-click 127.0.0.1 health probe that reports connect and HTTP latency. Parses /proc/net directly on Linux so it keeps working inside containers that have no ss or netstat.",\n'
225 printf ' "category": "network",\n'
226 printf ' "version": "%s",\n' "$VERSION"
227 printf ' "license": "MIT",\n'
228 printf ' "trust_notice": "Executes native C code on your node. Reads local kernel socket tables and /proc, and opens exactly one TCP connection to 127.0.0.1 when you ask it to probe a port. Never connects to a remote host and makes zero outbound network requests on its own.",\n'
229 printf ' "capabilities": ["procfs_read", "local_socket_list", "loopback_probe"],\n'
230 printf ' "git_repo_url": "https://plugingit.scorpiox.net/scorpiox-bot-plugin-%s.git",\n' "$SLUG"
231 printf ' "git_web_url": "https://plugingit.scorpiox.net/repo?name=scorpiox-bot-plugin-%s",\n' "$SLUG"
232 printf ' "git_commit": "%s",\n' "$COMMIT"
233 printf ' "source_tarball_url": "%s/%s/%s/source.tar.gz",\n' "$DIST_BASE_URL" "$SLUG" "$VERSION"
234 printf ' "source_tarball_sha256": "%s",\n' "$(sha256sum "$STAGE/source.tar.gz" | awk '{print $1}')"
235 printf ' "build_cmd_posix": "cc -O2 -std=c99 -Wall -Wextra -o %s src/%s.c",\n' "$ID" "$ID"
236 printf ' "build_cmd_win64": "x86_64-w64-mingw32-gcc -O2 -std=c99 -static -o %s.exe src/%s.c -liphlpapi -lws2_32",\n' "$ID" "$ID"
237 printf ' "published_at": "%s",\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
238 printf ' "targets": {\n'
239 first=1
240 emit() { if [ -f "$2" ]; then [ $first -eq 1 ] || printf ',\n'; first=0; target_meta "$@"; fi; }
241 emit "linux-x86_64" "$STAGE/$ID" "elf-x86_64" "linux-x86_64/$ID"
242 emit "linux-x86_64-musl" "$STAGE/$ID.musl" "elf-x86_64-static" "linux-x86_64-musl/$ID"
243 emit "linux-arm64-musl" "$STAGE/$ID.arm64" "elf-arm64-static" "linux-arm64-musl/$ID"
244 emit "macos-arm64" "$STAGE/$ID.macos-arm64" "macho-arm64" "macos-arm64/$ID"
245 emit "macos-x86_64" "$STAGE/$ID.macos-x86_64" "macho-x86_64" "macos-x86_64/$ID"
246 emit "windows-x86_64" "$STAGE/$ID.exe" "pe-x86_64" "windows-x86_64/$ID.exe"
247 printf '\n }\n}\n'
248} > "$STAGE/manifest.json"
249
250python3 -c 'import json,sys; json.load(open(sys.argv[1]))' "$STAGE/manifest.json" \
251 || die "generated manifest.json is not valid JSON"
252echo " manifest.json ok ($(python3 -c 'import json,sys; print(len(json.load(open(sys.argv[1]))["targets"]))' "$STAGE/manifest.json") targets)"
253
254# SHA256SUMS - relative paths, exactly as published
255( cd "$STAGE"
256 for f in source.tar.gz; do [ -f "$f" ] && sha256sum "$f"; done
257 [ -f "$ID" ] && sha256sum "$ID" | sed "s| $ID\$| linux-x86_64/$ID|"
258 [ -f "$ID.musl" ] && sha256sum "$ID.musl" | sed "s| $ID.musl\$| linux-x86_64-musl/$ID|"
259 [ -f "$ID.arm64" ] && sha256sum "$ID.arm64" | sed "s| $ID.arm64\$| linux-arm64-musl/$ID|"
260 [ -f "$ID.macos-arm64" ]&& sha256sum "$ID.macos-arm64"| sed "s| $ID.macos-arm64\$| macos-arm64/$ID|"
261 [ -f "$ID.macos-x86_64" ]&& sha256sum "$ID.macos-x86_64"| sed "s| $ID.macos-x86_64\$| macos-x86_64/$ID|"
262 [ -f "$ID.exe" ] && sha256sum "$ID.exe" | sed "s| $ID.exe\$| windows-x86_64/$ID.exe|"
263 true ) > "$STAGE/SHA256SUMS"
264echo " SHA256SUMS $(wc -l < "$STAGE/SHA256SUMS" | tr -d ' ') entries"
265
266# ---------------------------------------------------------------- 4. stage
267say "4/7 stage tree"
268PKG="$STAGE/pkg"
269mkdir -p "$PKG"
270cp "$STAGE/manifest.json" "$STAGE/SHA256SUMS" "$STAGE/source.tar.gz" "$PKG/"
271[ -f "$STAGE/$ID" ] && { mkdir -p "$PKG/linux-x86_64"; cp "$STAGE/$ID" "$PKG/linux-x86_64/$ID"; }
272[ -f "$STAGE/$ID.musl" ] && { mkdir -p "$PKG/linux-x86_64-musl"; cp "$STAGE/$ID.musl" "$PKG/linux-x86_64-musl/$ID"; }
273[ -f "$STAGE/$ID.arm64" ] && { mkdir -p "$PKG/linux-arm64-musl"; cp "$STAGE/$ID.arm64" "$PKG/linux-arm64-musl/$ID"; }
274[ -f "$STAGE/$ID.macos-arm64" ] && { mkdir -p "$PKG/macos-arm64"; cp "$STAGE/$ID.macos-arm64" "$PKG/macos-arm64/$ID"; }
275[ -f "$STAGE/$ID.macos-x86_64" ]&& { mkdir -p "$PKG/macos-x86_64"; cp "$STAGE/$ID.macos-x86_64" "$PKG/macos-x86_64/$ID"; }
276[ -f "$STAGE/$ID.exe" ] && { mkdir -p "$PKG/windows-x86_64"; cp "$STAGE/$ID.exe" "$PKG/windows-x86_64/$ID.exe"; }
277echo " staged: $(find "$PKG" -type f | wc -l | tr -d ' ') file(s)"
278
279if [ "$DRY_RUN" -eq 1 ]; then
280 say "dry run - leaving the staging tree in $PKG"
281 trap - EXIT
282 exit 0
283fi
284
285# ---------------------------------------------------------------- 5. upload
286say "5/7 upload to the dist server"
287[ -n "$DIST_HOST" ] || die "DIST_HOST is not set (export DIST_HOST=<ssh-target>)"
288SSH=(ssh -o StrictHostKeyChecking=no)
289SCP=(scp -o StrictHostKeyChecking=no)
290if [ -n "$DIST_PASS" ]; then
291 SSH=(scorpiox-sshpass -p "$DIST_PASS" "${SSH[@]}")
292 SCP=(scorpiox-sshpass -p "$DIST_PASS" "${SCP[@]}")
293fi
294REMOTE_DIR="$DIST_PATH/bot-plugins/$SLUG/$VERSION"
295
296"${SSH[@]}" "$DIST_HOST" "mkdir -p '$REMOTE_DIR' '$DIST_PATH/bot-plugins/$SLUG'" \
297 || die "cannot create $REMOTE_DIR on $DIST_HOST"
298# Stage to one tarball, transfer it, extract remotely. Streaming tar through
299# ssh corrupts on some OpenSSH builds (remote gzip dies with SIGPIPE), and
300# OpenSSH 9 refuses to scp a bare "." source.
301tar -czf "$STAGE/pkg.tar.gz" -C "$PKG" . || die "packaging failed"
302"${SCP[@]}" -q "$STAGE/pkg.tar.gz" "$DIST_HOST:$REMOTE_DIR/pkg.tar.gz" || die "upload failed"
303"${SSH[@]}" "$DIST_HOST" "cd '$REMOTE_DIR' && tar -xzf pkg.tar.gz && rm -f pkg.tar.gz" \
304 || die "remote extract failed"
305echo " uploaded to $DIST_HOST:$REMOTE_DIR"
306
307# latest.json = copy of this version's manifest
308"${SSH[@]}" "$DIST_HOST" "cp -f '$REMOTE_DIR/manifest.json' '$DIST_PATH/bot-plugins/$SLUG/latest.json'" \
309 || die "latest.json update failed"
310echo " updated bot-plugins/$SLUG/latest.json"
311
312# index.json - merge, preserving every sibling <slug>/latest.json
313say "index.json merge"
314"${SSH[@]}" "$DIST_HOST" "cd '$DIST_PATH/bot-plugins' && python3 - '$SLUG' <<'PY'
315import json, os, sys, glob
316slug = sys.argv[1]
317entries = {}
318for p in sorted(glob.glob('*/latest.json')):
319 s = os.path.dirname(p)
320 try:
321 entries[s] = json.load(open(p))
322 except Exception:
323 pass
324index = {
325 'ok': True,
326 'service': 'bot-plugins-dist',
327 'dist_base_url': 'https://dist.scorpiox.net/bot-plugins',
328 'count': len(entries),
329 'slugs': sorted(entries),
330 'plugins': [entries[k] for k in sorted(entries)],
331}
332open('index.json.tmp', 'w').write(json.dumps(index, indent=2) + chr(10))
333os.replace('index.json.tmp', 'index.json')
334print(' index.json now has %d plugin(s): %s' % (len(entries), ', '.join(sorted(entries))))
335PY" || die "index.json merge failed"
336
337# ---------------------------------------------------------------- 6. mirror
338say "6/7 plugingit mirror"
339if [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then
340 TOKEN=$(tr -d ' \r\n' < "$PLUGINGIT_TOKEN_FILE")
341 AUTH_REMOTE=$(printf '%s' "$PLUGINGIT_REMOTE" | sed "s|https://|https://token:${TOKEN}@|")
342 git push --all "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || die "mirror push failed"
343 git push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true
344 echo " mirrored to $PLUGINGIT_REMOTE"
345else
346 echo " skip (no $PLUGINGIT_TOKEN_FILE or --no-mirror)"
347fi
348
349# ---------------------------------------------------------------- 7. verify
350say "7/7 verify"
351CODE=$(curl -fsS -o "$STAGE/latest.check" -w '%{http_code}' \
352 "$DIST_BASE_URL/$SLUG/latest.json" || echo 000)
353if [ "$CODE" = "200" ]; then
354 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"])))' \
355 "$STAGE/latest.check" || echo " warn: latest.json is not valid JSON"
356else
357 echo " warn: $DIST_BASE_URL/$SLUG/latest.json returned HTTP $CODE"
358fi
359
360printf '\n\033[1mrelease complete\033[0m\n'
361printf ' registry %s/%s/%s/\n' "$DIST_BASE_URL" "$SLUG" "$VERSION"
362printf ' manifest %s/%s/%s/manifest.json\n' "$DIST_BASE_URL" "$SLUG" "$VERSION"