scorpiox-bot-plugin-crontab
←release-dist.shbash
350 lines · 15 KBHEADBlameDownload
1#!/usr/bin/env bash
2# release-dist.sh — build, scan and publish plugin_crontab 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="crontab"
30ID="plugin_crontab"
31VERSION="1.0.1"
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 "|audit "|HITS=|grep -E|P_ROOT=|P_WT=|PK="|\$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# windows-x86_64 (static PE)
142if command -v x86_64-w64-mingw32-gcc >/dev/null 2>&1; then
143 x86_64-w64-mingw32-gcc -O2 -std=c99 -Wall -Wextra -static -o "$STAGE/$ID.exe" src/$ID.c \
144 || die "MinGW build failed"
145 [ "$(head -c 2 "$STAGE/$ID.exe")" = "MZ" ] || die "windows artifact is not a PE"
146 echo " built windows-x86_64 $(wc -c < "$STAGE/$ID.exe" | tr -d ' ') bytes"
147else
148 echo " skip windows-x86_64 (no MinGW cross compiler)"
149fi
150
151# macOS legs — Mach-O must be produced by Apple clang on a real Mac.
152# Both slices are cross-built from one host: -arch arm64 and -arch x86_64.
153if [ "$DO_MACOS" -eq 1 ] && [ -n "${MAC_HOST:-}" ]; then
154 MAC_USER="${MAC_USER:-$(whoami)}"
155 MAC_DIR="/tmp/${ID}-mac-$$"
156 echo " building macOS legs on $MAC_HOST"
157 SSH_MAC=(ssh -o StrictHostKeyChecking=no)
158 SCP_MAC=(scp -o StrictHostKeyChecking=no)
159 if [ -n "${MAC_PASS:-}" ]; then
160 SSH_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SSH_MAC[@]}")
161 SCP_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SCP_MAC[@]}")
162 fi
163 if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR' && mkdir -p '$MAC_DIR'"; then
164 if "${SCP_MAC[@]}" src/$ID.c "$MAC_USER@$MAC_HOST:$MAC_DIR/$ID.c"; then
165 # Build straight from the uploaded file — no src/ tree on the remote.
166 for slice in arm64 x86_64; do
167 if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" \
168 "cd '$MAC_DIR' && clang -O2 -std=c99 -Wall -Wextra -arch $slice -o $ID.$slice $ID.c" \
169 2>/dev/null; then
170 "${SCP_MAC[@]}" "$MAC_USER@$MAC_HOST:$MAC_DIR/$ID.$slice" "$STAGE/$ID.macos-$slice" 2>/dev/null \
171 && echo " built macos-$slice ($(wc -c < "$STAGE/$ID.macos-$slice" | tr -d ' ') bytes)"
172 else
173 echo " warn: macos-$slice build failed on $MAC_HOST"
174 fi
175 done
176 "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR'" 2>/dev/null || true
177 else
178 echo " warn: mac scp failed"
179 fi
180 else
181 echo " warn: mac host unreachable"
182 fi
183else
184 echo " skip macOS legs (set MAC_HOST/MAC_USER/MAC_PASS to include them)"
185fi
186
187# ---------------------------------------------------------------- 3. package
188say "3/7 package"
189
190git archive --format=tar.gz --prefix="${ID}-${VERSION}/" \
191 -o "$STAGE/source.tar.gz" HEAD 2>/dev/null \
192 || tar -czf "$STAGE/source.tar.gz" --exclude=.git .
193echo " source.tar.gz $(wc -c < "$STAGE/source.tar.gz" | tr -d ' ') bytes"
194
195target_meta() { # target_meta <key> <file> <format> <relpath> <name>
196 local key="$1" file="$2" fmt="$3" rel="$4" name="$5"
197 [ -f "$file" ] || return 0
198 local sha size
199 sha=$(sha256sum "$file" | awk '{print $1}')
200 size=$(wc -c < "$file" | tr -d ' ')
201 printf ' "%s": {"url": "%s/%s/%s/%s", "sha256": "%s", "size_bytes": %s, "format": "%s"}' \
202 "$key" "$DIST_BASE_URL" "$SLUG" "$VERSION" "$rel" "$sha" "$size" "$fmt"
203}
204
205{
206 printf '{\n'
207 printf ' "slug": "%s",\n' "$SLUG"
208 printf ' "id": "%s",\n' "$ID"
209 printf ' "name": "crontab",\n'
210 printf ' "desc": "Cron jobs",\n'
211 printf ' "title": "Local Cron Schedule Inspector",\n'
212 printf ' "summary": "Zero-dependency local cron inspector: lists the scheduled jobs on your node, explains each expression in plain English, and computes the exact next five firing times in UTC and local time. Includes an interactive cron expression playground that works even with no crontab installed.",\n'
213 printf ' "category": "devtools",\n'
214 printf ' "version": "%s",\n' "$VERSION"
215 printf ' "license": "MIT",\n'
216 printf ' "trust_notice": "Executes native C code on your node. Strictly read-only: reads crontab listings and Windows task lists to describe schedules, never writes a schedule and never executes a scheduled command. Makes zero outbound network requests.",\n'
217 printf ' "capabilities": ["crontab_read", "cron_parse", "cron_next_run"],\n'
218 printf ' "git_repo_url": "https://plugingit.scorpiox.net/scorpiox-bot-plugin-%s.git",\n' "$SLUG"
219 printf ' "git_web_url": "https://plugingit.scorpiox.net/repo?name=scorpiox-bot-plugin-%s",\n' "$SLUG"
220 printf ' "git_commit": "%s",\n' "$COMMIT"
221 printf ' "source_tarball_url": "%s/%s/%s/source.tar.gz",\n' "$DIST_BASE_URL" "$SLUG" "$VERSION"
222 printf ' "source_tarball_sha256": "%s",\n' "$(sha256sum "$STAGE/source.tar.gz" | awk '{print $1}')"
223 printf ' "build_cmd_posix": "cc -O2 -std=c99 -Wall -Wextra -o %s src/%s.c",\n' "$ID" "$ID"
224 printf ' "build_cmd_win64": "x86_64-w64-mingw32-gcc -O2 -std=c99 -static -o %s.exe src/%s.c",\n' "$ID" "$ID"
225 printf ' "published_at": "%s",\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
226 printf ' "targets": {\n'
227 first=1
228 emit() { if [ -f "$2" ]; then [ $first -eq 1 ] || printf ',\n'; first=0; target_meta "$@"; fi; }
229 emit "linux-x86_64" "$STAGE/$ID" "elf-x86_64" "linux-x86_64/$ID" "$ID"
230 emit "linux-x86_64-musl" "$STAGE/$ID.musl" "elf-x86_64-static" "linux-x86_64-musl/$ID" "$ID"
231 emit "macos-arm64" "$STAGE/$ID.macos-arm64" "macho-arm64" "macos-arm64/$ID" "$ID"
232 emit "macos-x86_64" "$STAGE/$ID.macos-x86_64" "macho-x86_64" "macos-x86_64/$ID" "$ID"
233 emit "windows-x86_64" "$STAGE/$ID.exe" "pe-x86_64" "windows-x86_64/$ID.exe" "$ID"
234 printf '\n }\n}\n'
235} > "$STAGE/manifest.json"
236
237python3 -c 'import json,sys; json.load(open(sys.argv[1]))' "$STAGE/manifest.json" \
238 || die "generated manifest.json is not valid JSON"
239echo " manifest.json ok"
240
241# SHA256SUMS — relative paths, exactly as published
242( cd "$STAGE"
243 for f in source.tar.gz; do [ -f "$f" ] && sha256sum "$f"; done
244 [ -f "$ID" ] && sha256sum "$ID" | sed "s| $ID\$| linux-x86_64/$ID|"
245 [ -f "$ID.musl" ] && sha256sum "$ID.musl" | sed "s| $ID.musl\$| linux-x86_64-musl/$ID|"
246 [ -f "$ID.macos-arm64" ]&& sha256sum "$ID.macos-arm64"| sed "s| $ID.macos-arm64\$| macos-arm64/$ID|"
247 [ -f "$ID.macos-x86_64" ]&& sha256sum "$ID.macos-x86_64"| sed "s| $ID.macos-x86_64\$| macos-x86_64/$ID|"
248 [ -f "$ID.exe" ] && sha256sum "$ID.exe" | sed "s| $ID.exe\$| windows-x86_64/$ID.exe|"
249 true ) > "$STAGE/SHA256SUMS"
250echo " SHA256SUMS $(wc -l < "$STAGE/SHA256SUMS" | tr -d ' ') entries"
251
252# ---------------------------------------------------------------- 4. stage
253say "4/7 stage tree"
254PKG="$STAGE/pkg"
255mkdir -p "$PKG/linux-x86_64" "$PKG/linux-x86_64-musl" "$PKG/macos-arm64" \
256 "$PKG/macos-x86_64" "$PKG/windows-x86_64"
257cp -f "$STAGE/$ID" "$PKG/linux-x86_64/$ID"
258[ -f "$STAGE/$ID.musl" ] && cp -f "$STAGE/$ID.musl" "$PKG/linux-x86_64-musl/$ID"
259[ -f "$STAGE/$ID.macos-arm64" ]&& cp -f "$STAGE/$ID.macos-arm64" "$PKG/macos-arm64/$ID"
260[ -f "$STAGE/$ID.macos-x86_64" ]&&cp -f "$STAGE/$ID.macos-x86_64" "$PKG/macos-x86_64/$ID"
261[ -f "$STAGE/$ID.exe" ] && cp -f "$STAGE/$ID.exe" "$PKG/windows-x86_64/$ID.exe"
262cp -f "$STAGE/manifest.json" "$STAGE/SHA256SUMS" "$STAGE/source.tar.gz" "$PKG/"
263chmod +x "$PKG"/*/$ID 2>/dev/null || true
264find "$PKG" -type f | sed 's/^/ /'
265
266if [ "$DRY_RUN" -eq 1 ]; then
267 say "dry run — staged at $STAGE/pkg (kept for inspection)"
268 trap - EXIT
269 exit 0
270fi
271
272# ---------------------------------------------------------------- 5. upload
273say "5/7 upload"
274
275[ -n "$DIST_HOST" ] || die "DIST_HOST is not set — cannot upload"
276SSH=(ssh -o StrictHostKeyChecking=no)
277SCP=(scp -o StrictHostKeyChecking=no)
278if [ -n "$DIST_PASS" ]; then
279 SSH=(scorpiox-sshpass -p "$DIST_PASS" "${SSH[@]}")
280 SCP=(scorpiox-sshpass -p "$DIST_PASS" "${SCP[@]}")
281fi
282REMOTE_DIR="$DIST_PATH/bot-plugins/$SLUG/$VERSION"
283
284"${SSH[@]}" "$DIST_HOST" "mkdir -p '$REMOTE_DIR' '$DIST_PATH/bot-plugins/$SLUG'" \
285 || die "cannot create $REMOTE_DIR on $DIST_HOST"
286# Stage to one tarball, transfer it, extract remotely. Streaming tar through
287# ssh corrupts on some OpenSSH builds (remote gzip dies with SIGPIPE), and
288# OpenSSH 9 refuses to scp a bare "." source.
289tar -czf "$STAGE/pkg.tar.gz" -C "$PKG" . || die "packaging failed"
290"${SCP[@]}" -q "$STAGE/pkg.tar.gz" "$DIST_HOST:$REMOTE_DIR/pkg.tar.gz" || die "upload failed"
291"${SSH[@]}" "$DIST_HOST" "cd '$REMOTE_DIR' && tar -xzf pkg.tar.gz && rm -f pkg.tar.gz" \
292 || die "remote extract failed"
293echo " uploaded to $DIST_HOST:$REMOTE_DIR"
294
295# latest.json = copy of this version's manifest
296"${SSH[@]}" "$DIST_HOST" "cp -f '$REMOTE_DIR/manifest.json' '$DIST_PATH/bot-plugins/$SLUG/latest.json'" \
297 || die "latest.json update failed"
298echo " updated bot-plugins/$SLUG/latest.json"
299
300# index.json — merge, preserving every sibling <slug>/latest.json
301say "index.json merge"
302"${SSH[@]}" "$DIST_HOST" "cd '$DIST_PATH/bot-plugins' && python3 - '$SLUG' <<'PY'
303import json, os, sys, glob
304slug = sys.argv[1]
305entries = {}
306for p in sorted(glob.glob('*/latest.json')):
307 s = os.path.dirname(p)
308 try:
309 entries[s] = json.load(open(p))
310 except Exception:
311 pass
312index = {
313 'ok': True,
314 'service': 'bot-plugins-dist',
315 'dist_base_url': 'https://dist.scorpiox.net/bot-plugins',
316 'count': len(entries),
317 'slugs': sorted(entries),
318 'plugins': [entries[k] for k in sorted(entries)],
319}
320open('index.json.tmp', 'w').write(json.dumps(index, indent=2) + chr(10))
321os.replace('index.json.tmp', 'index.json')
322print(' index.json now has %d plugin(s): %s' % (len(entries), ', '.join(sorted(entries))))
323PY" || die "index.json merge failed"
324
325# ---------------------------------------------------------------- 6. mirror
326say "6/7 plugingit mirror"
327if [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then
328 TOKEN=$(tr -d ' \r\n' < "$PLUGINGIT_TOKEN_FILE")
329 AUTH_REMOTE=$(printf '%s' "$PLUGINGIT_REMOTE" | sed "s|https://|https://x-access-token:${TOKEN}@|")
330 git push --all "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || die "mirror push failed"
331 git push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true
332 echo " mirrored to $PLUGINGIT_REMOTE"
333else
334 echo " skip (no $PLUGINGIT_TOKEN_FILE or --no-mirror)"
335fi
336
337# ---------------------------------------------------------------- 7. verify
338say "7/7 verify"
339CODE=$(curl -fsS -o "$STAGE/latest.check" -w '%{http_code}' \
340 "$DIST_BASE_URL/$SLUG/latest.json" || echo 000)
341if [ "$CODE" = "200" ]; then
342 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"])))' \
343 "$STAGE/latest.check" || echo " warn: latest.json is not valid JSON"
344else
345 echo " warn: $DIST_BASE_URL/$SLUG/latest.json returned HTTP $CODE"
346fi
347
348printf '\n\033[1mrelease complete\033[0m\n'
349printf ' registry %s/%s/%s/\n' "$DIST_BASE_URL" "$SLUG" "$VERSION"
350printf ' manifest %s/%s/%s/manifest.json\n' "$DIST_BASE_URL" "$SLUG" "$VERSION"