scorpiox-bot-plugin-sysmon
←release-dist.shbash
362 lines · 16 KBHEADBlameDownload
1#!/usr/bin/env bash
2# release-dist.sh — build, scan and publish plugin_sysmon 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="sysmon"
30ID="plugin_sysmon"
31VERSION="1.0.2"
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
40for arg in "$@"; do
41 case "$arg" in
42 --dry-run) DRY_RUN=1 ;;
43 --no-macos) DO_MACOS=0 ;;
44 --no-mirror) DO_MIRROR=0 ;;
45 -h|--help) sed -n '2,30p' "$0"; exit 0 ;;
46 *) echo "unknown argument: $arg" >&2; exit 2 ;;
47 esac
48done
49
50DIST_HOST="${DIST_HOST:-}"
51DIST_PASS="${DIST_PASS:-}"
52DIST_PATH="${DIST_PATH:-/data/dist}"
53PLUGINGIT_TOKEN_FILE="${PLUGINGIT_TOKEN_FILE:-/etc/scorpiox-plugingit.token}"
54PLUGINGIT_REMOTE="${PLUGINGIT_REMOTE:-https://plugingit.scorpiox.net/scorpiox-bot-plugin-${SLUG}.git}"
55
56STAGE="/tmp/${SLUG}-dist-${VERSION}-$$"
57mkdir -p "$STAGE"
58trap 'rm -rf "$STAGE"' EXIT
59
60say() { printf '\n\033[1m== %s ==\033[0m\n' "$1"; }
61die() { printf '\033[31mERROR:\033[0m %s\n' "$1" >&2; exit 1; }
62
63# ---------------------------------------------------------------- 0. preflight
64say "0/7 preflight"
65
66if command -v scorpiox-cli-botplugin >/dev/null 2>&1; then
67 say "using scorpiox-cli-botplugin publish"
68 exec scorpiox-cli-botplugin publish "$ROOT"
69fi
70echo " scorpiox-cli-botplugin not installed — running standalone pipeline"
71
72[ -n "$(git status --porcelain 2>/dev/null)" ] && \
73 echo " note: working tree is dirty; source.tar.gz comes from the last commit"
74COMMIT=$(git rev-parse HEAD 2>/dev/null || echo "unknown")
75echo " version $VERSION"
76echo " commit $COMMIT"
77
78# ---------------------------------------------------------------- 1. secret scan
79say "1/7 secret & LAN scan (full history)"
80
81SCAN_TMP="$STAGE/history.txt"
82# Scan every added/removed line of every commit. Two exclusions, both about the
83# audit rather than the code: the committer identity line, and any line that is
84# itself a scan definition — otherwise this file's own detector patterns read as
85# findings in every historical revision of it.
86git log -p --all 2>/dev/null \
87 | grep -v '^Author: ' \
88 | grep -vE 'scan "|HITS=|grep -E|P_ROOT=|P_WT=|\$ALL' \
89 > "$SCAN_TMP" || : > "$SCAN_TMP"
90HITS=0
91scan() { # scan <label> <regex>
92 local out
93 out=$(grep -Eo "$2" "$SCAN_TMP" 2>/dev/null | sort -u | head -5 || true)
94 if [ -n "$out" ]; then
95 printf ' \033[31mHIT\033[0m %s\n' "$1"
96 printf '%s\n' "$out" | sed 's/^/ /'
97 HITS=$((HITS + 1))
98 else
99 printf ' ok %s\n' "$1"
100 fi
101}
102scan "LAN IPs 192.168.x.x" '192\.168\.[0-9]+\.[0-9]+'
103scan "private 10.x.x.x" '\b10\.[0-9]+\.[0-9]+\.[0-9]+'
104scan "private 172.16-31.x.x" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+'
105scan "GitHub PAT" 'ghp_[A-Za-z0-9]{36}'
106scan "HuggingFace token" 'hf_[A-Za-z0-9]{30,}'
107scan "embedded git credentials" 'scorpioxinc:[A-Za-z0-9]{20,}'
108scan "hard-coded ssh password" 'sshpass -p [^"$]'
109P_ROOT="/codebases"
110P_PICOBOT="${P_ROOT}/pico""bot"
111P_WT=".work""trees"
112scan "internal fleet paths" "/root/tools|${P_PICOBOT}|${P_WT}"
113scan "run-token env assignment" 'RUNNER_'"PASS="
114if [ "$HITS" -ne 0 ]; then
115 die "secret scan found $HITS problem(s) — refusing to publish"
116fi
117echo " clean: 0 hits"
118
119# ---------------------------------------------------------------- 2. build
120say "2/7 build targets"
121
122build_ok() { [ -f "$1" ]; }
123
124# linux-x86_64 (glibc, native)
125if command -v cc >/dev/null 2>&1; then
126 cc -O2 -std=c99 -Wall -Wextra -o "$STAGE/$ID" src/$ID.c || die "native build failed"
127 echo " built linux-x86_64 (glibc) $(wc -c < "$STAGE/$ID" | tr -d ' ') bytes"
128else
129 die "no native cc found"
130fi
131
132# linux-x86_64-musl (fully static — the universal Linux artifact)
133if command -v musl-gcc >/dev/null 2>&1; then
134 musl-gcc -O2 -std=c99 -Wall -Wextra -static -o "$STAGE/$ID.musl" src/$ID.c \
135 || die "musl build failed"
136 echo " built linux-x86_64-musl $(wc -c < "$STAGE/$ID.musl" | tr -d ' ') bytes"
137else
138 echo " skip linux-x86_64-musl (no musl-gcc)"
139fi
140
141# linux-arm64-musl (static aarch64 — Pi 5 / ARM64 servers)
142if command -v aarch64-linux-musl-gcc >/dev/null 2>&1; then
143 aarch64-linux-musl-gcc -O2 -std=c99 -Wall -Wextra -static -o "$STAGE/$ID.arm64" src/$ID.c \
144 || die "arm64 musl build failed"
145 echo " built linux-arm64-musl $(wc -c < "$STAGE/$ID.arm64" | tr -d ' ') bytes"
146else
147 echo " skip linux-arm64-musl (no aarch64-linux-musl-gcc)"
148fi
149
150# windows-x86_64 (static PE)
151if command -v x86_64-w64-mingw32-gcc >/dev/null 2>&1; then
152 x86_64-w64-mingw32-gcc -O2 -std=c99 -Wall -Wextra -static -o "$STAGE/$ID.exe" src/$ID.c -lpsapi \
153 || die "MinGW build failed"
154 [ "$(head -c 2 "$STAGE/$ID.exe")" = "MZ" ] || die "windows artifact is not a PE"
155 echo " built windows-x86_64 $(wc -c < "$STAGE/$ID.exe" | tr -d ' ') bytes"
156else
157 echo " skip windows-x86_64 (no MinGW cross compiler)"
158fi
159
160# macOS legs — Mach-O must be produced by Apple clang on a real Mac.
161# Both slices are cross-built from one host: -arch arm64 and -arch x86_64.
162if [ "$DO_MACOS" -eq 1 ] && [ -n "${MAC_HOST:-}" ]; then
163 MAC_USER="${MAC_USER:-$(whoami)}"
164 MAC_DIR="/tmp/${ID}-mac-$$"
165 echo " building macOS legs on $MAC_HOST"
166 SSH_MAC=(ssh -o StrictHostKeyChecking=no)
167 SCP_MAC=(scp -o StrictHostKeyChecking=no)
168 if [ -n "${MAC_PASS:-}" ]; then
169 SSH_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SSH_MAC[@]}")
170 SCP_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SCP_MAC[@]}")
171 fi
172 if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR' && mkdir -p '$MAC_DIR'"; then
173 if "${SCP_MAC[@]}" src/$ID.c "$MAC_USER@$MAC_HOST:$MAC_DIR/$ID.c"; then
174 # Build straight from the uploaded file — no src/ tree on the remote.
175 for slice in arm64 x86_64; do
176 if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" \
177 "cd '$MAC_DIR' && clang -O2 -std=c99 -Wall -Wextra -arch $slice -o $ID.$slice $ID.c" \
178 2>/dev/null; then
179 "${SCP_MAC[@]}" "$MAC_USER@$MAC_HOST:$MAC_DIR/$ID.$slice" "$STAGE/$ID.macos-$slice" 2>/dev/null \
180 && echo " built macos-$slice ($(wc -c < "$STAGE/$ID.macos-$slice" | tr -d ' ') bytes)"
181 else
182 echo " warn: macos-$slice build failed on $MAC_HOST"
183 fi
184 done
185 "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR'" 2>/dev/null || true
186 else
187 echo " warn: mac scp failed"
188 fi
189 else
190 echo " warn: mac host unreachable"
191 fi
192else
193 echo " skip macOS legs (set MAC_HOST/MAC_USER/MAC_PASS to include them)"
194fi
195
196# ---------------------------------------------------------------- 3. package
197say "3/7 package"
198
199git archive --format=tar.gz --prefix="${ID}-${VERSION}/" \
200 -o "$STAGE/source.tar.gz" HEAD 2>/dev/null \
201 || tar -czf "$STAGE/source.tar.gz" --exclude=.git .
202echo " source.tar.gz $(wc -c < "$STAGE/source.tar.gz" | tr -d ' ') bytes"
203
204target_meta() { # target_meta <key> <file> <format> <relpath> <name>
205 local key="$1" file="$2" fmt="$3" rel="$4" name="$5"
206 [ -f "$file" ] || return 0
207 local sha size
208 sha=$(sha256sum "$file" | awk '{print $1}')
209 size=$(wc -c < "$file" | tr -d ' ')
210 printf ' "%s": {"url": "%s/%s/%s/%s", "sha256": "%s", "size_bytes": %s, "format": "%s"}' \
211 "$key" "$DIST_BASE_URL" "$SLUG" "$VERSION" "$rel" "$sha" "$size" "$fmt"
212}
213
214{
215 printf '{\n'
216 printf ' "slug": "%s",\n' "$SLUG"
217 printf ' "id": "%s",\n' "$ID"
218 printf ' "name": "sysmon",\n'
219 printf ' "desc": "CPU & memory",\n'
220 printf ' "title": "Local System Monitor",\n'
221 printf ' "summary": "Zero-dependency local CPU, per-core load, memory, swap and top-process monitor for Linux, macOS and Windows nodes - sampled live from kernel counters in one C99 binary.",\n'
222 printf ' "category": "system",\n'
223 printf ' "version": "%s",\n' "$VERSION"
224 printf ' "license": "MIT",\n'
225 printf ' "trust_notice": "Executes native C code on your node. Reads local /proc, mach or Win32 counters for CPU, memory and processes; makes zero outbound network requests and opens no sockets.",\n'
226 printf ' "capabilities": ["procfs_read", "local_process_list"],\n'
227 printf ' "git_repo_url": "https://plugingit.scorpiox.net/scorpiox-bot-plugin-%s.git",\n' "$SLUG"
228 printf ' "git_web_url": "https://plugingit.scorpiox.net/repo?name=scorpiox-bot-plugin-%s",\n' "$SLUG"
229 printf ' "git_commit": "%s",\n' "$COMMIT"
230 printf ' "source_tarball_url": "%s/%s/%s/source.tar.gz",\n' "$DIST_BASE_URL" "$SLUG" "$VERSION"
231 printf ' "source_tarball_sha256": "%s",\n' "$(sha256sum "$STAGE/source.tar.gz" | awk '{print $1}')"
232 printf ' "build_cmd_posix": "cc -O2 -std=c99 -Wall -Wextra -o %s src/%s.c",\n' "$ID" "$ID"
233 printf ' "build_cmd_win64": "x86_64-w64-mingw32-gcc -O2 -std=c99 -static -o %s.exe src/%s.c -lpsapi",\n' "$ID" "$ID"
234 printf ' "published_at": "%s",\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
235 printf ' "targets": {\n'
236 first=1
237 emit() { if [ -f "$2" ]; then [ $first -eq 1 ] || printf ',\n'; first=0; target_meta "$@"; fi; }
238 emit "linux-x86_64" "$STAGE/$ID" "elf-x86_64" "linux-x86_64/$ID" "$ID"
239 emit "linux-x86_64-musl" "$STAGE/$ID.musl" "elf-x86_64-static" "linux-x86_64-musl/$ID" "$ID"
240 emit "linux-arm64-musl" "$STAGE/$ID.arm64" "elf-arm64-static" "linux-arm64-musl/$ID" "$ID"
241 emit "macos-arm64" "$STAGE/$ID.macos-arm64" "macho-arm64" "macos-arm64/$ID" "$ID"
242 emit "macos-x86_64" "$STAGE/$ID.macos-x86_64" "macho-x86_64" "macos-x86_64/$ID" "$ID"
243 emit "windows-x86_64" "$STAGE/$ID.exe" "pe-x86_64" "windows-x86_64/$ID.exe" "$ID"
244 printf '\n }\n}\n'
245} > "$STAGE/manifest.json"
246
247python3 -c 'import json,sys; json.load(open(sys.argv[1]))' "$STAGE/manifest.json" \
248 || die "generated manifest.json is not valid JSON"
249echo " manifest.json ok"
250
251# SHA256SUMS — relative paths, exactly as published
252( cd "$STAGE"
253 for f in source.tar.gz; do [ -f "$f" ] && sha256sum "$f"; done
254 [ -f "$ID" ] && sha256sum "$ID" | sed "s| $ID\$| linux-x86_64/$ID|"
255 [ -f "$ID.musl" ] && sha256sum "$ID.musl" | sed "s| $ID.musl\$| linux-x86_64-musl/$ID|"
256 [ -f "$ID.arm64" ] && sha256sum "$ID.arm64" | sed "s| $ID.arm64\$| linux-arm64-musl/$ID|"
257 [ -f "$ID.macos-arm64" ]&& sha256sum "$ID.macos-arm64"| sed "s| $ID.macos-arm64\$| macos-arm64/$ID|"
258 [ -f "$ID.macos-x86_64" ]&& sha256sum "$ID.macos-x86_64"| sed "s| $ID.macos-x86_64\$| macos-x86_64/$ID|"
259 [ -f "$ID.exe" ] && sha256sum "$ID.exe" | sed "s| $ID.exe\$| windows-x86_64/$ID.exe|"
260 true ) > "$STAGE/SHA256SUMS"
261echo " SHA256SUMS $(wc -l < "$STAGE/SHA256SUMS" | tr -d ' ') entries"
262
263# ---------------------------------------------------------------- 4. stage
264say "4/7 stage tree"
265PKG="$STAGE/pkg"
266mkdir -p "$PKG/linux-x86_64" "$PKG/linux-x86_64-musl" "$PKG/linux-arm64-musl" \
267 "$PKG/macos-arm64" "$PKG/macos-x86_64" "$PKG/windows-x86_64"
268cp -f "$STAGE/$ID" "$PKG/linux-x86_64/$ID"
269[ -f "$STAGE/$ID.musl" ] && cp -f "$STAGE/$ID.musl" "$PKG/linux-x86_64-musl/$ID"
270[ -f "$STAGE/$ID.arm64" ] && cp -f "$STAGE/$ID.arm64" "$PKG/linux-arm64-musl/$ID"
271[ -f "$STAGE/$ID.macos-arm64" ]&& cp -f "$STAGE/$ID.macos-arm64" "$PKG/macos-arm64/$ID"
272[ -f "$STAGE/$ID.macos-x86_64" ]&&cp -f "$STAGE/$ID.macos-x86_64" "$PKG/macos-x86_64/$ID"
273[ -f "$STAGE/$ID.exe" ] && cp -f "$STAGE/$ID.exe" "$PKG/windows-x86_64/$ID.exe"
274cp -f "$STAGE/manifest.json" "$STAGE/SHA256SUMS" "$STAGE/source.tar.gz" "$PKG/"
275chmod +x "$PKG"/*/$ID 2>/dev/null || true
276find "$PKG" -type f | sed 's/^/ /'
277
278if [ "$DRY_RUN" -eq 1 ]; then
279 say "dry run — staged at $STAGE/pkg (kept for inspection)"
280 trap - EXIT
281 exit 0
282fi
283
284# ---------------------------------------------------------------- 5. upload
285say "5/7 upload"
286
287[ -n "$DIST_HOST" ] || die "DIST_HOST is not set — cannot upload"
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://x-access-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"