8900 commited on
Update entrypoint.sh
Browse files- entrypoint.sh +79 -69
entrypoint.sh
CHANGED
|
@@ -1,101 +1,111 @@
|
|
| 1 |
#!/bin/sh
|
| 2 |
|
| 3 |
# OpenClaw HF Spaces - Production Entrypoint
|
| 4 |
-
#
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# –– resolve writable home ———————————
|
| 7 |
if mkdir -p /data/.openclaw 2>/dev/null; then
|
| 8 |
-
|
| 9 |
-
echo "[entrypoint] ✅ 使用持久存储 Storage Buckets: OPENCLAW_HOME=/data"
|
| 10 |
else
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
echo "[entrypoint] ⚠️ 未找到 Storage Buckets,使用临时存储"
|
| 14 |
fi
|
| 15 |
echo "[entrypoint] OPENCLAW_HOME=$OPENCLAW_HOME"
|
| 16 |
|
| 17 |
-
#
|
|
|
|
| 18 |
for VAR in $(env | cut -d= -f1); do
|
| 19 |
-
case "$VAR" in
|
| 20 |
-
OPENCLAW_*|SPACE_*|SYSTEM_*|HF_*|NODE_*|npm_*) continue ;;
|
| 21 |
-
esac
|
| 22 |
-
case "$VAR" in
|
| 23 |
-
*_API_KEY|*_SECRET_KEY|*_ACCESS_TOKEN|*_BOT_TOKEN|*_AUTH_TOKEN|*_APP_KEY)
|
| 24 |
-
VAL=$(printenv "$VAR" 2>/dev/null || true)
|
| 25 |
-
if [ -n "$VAL" ]; then
|
| 26 |
-
export "$VAR"
|
| 27 |
-
echo "[entrypoint] exported: $VAR"
|
| 28 |
-
fi
|
| 29 |
-
;;
|
| 30 |
-
esac
|
| 31 |
done
|
| 32 |
|
| 33 |
export HF_TOKEN="${HF_TOKEN:-}"
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
|
|
|
| 37 |
node /app/spaces/huggingface/setup-hf-config.mjs || true
|
| 38 |
echo "[entrypoint] Setup done."
|
| 39 |
|
| 40 |
-
#
|
|
|
|
| 41 |
if [ -f /app/security-check.sh ]; then
|
| 42 |
-
sh /app/security-check.sh || true
|
| 43 |
fi
|
| 44 |
|
| 45 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
(
|
| 47 |
-
DEVICES_DIR="$OPENCLAW_HOME/.openclaw/devices"
|
| 48 |
-
mkdir -p "$DEVICES_DIR"
|
| 49 |
-
echo "[auto-pair] Device auto-approval started"
|
| 50 |
-
while true; do
|
| 51 |
-
sleep 8
|
| 52 |
-
PENDING="$DEVICES_DIR/pending.json"
|
| 53 |
-
PAIRED="$DEVICES_DIR/paired.json"
|
| 54 |
-
if [ -f "$PENDING" ]; then
|
| 55 |
-
node - << 'JSEOF'
|
| 56 |
const fs = require('fs');
|
| 57 |
const devDir = process.env.OPENCLAW_HOME + '/.openclaw/devices';
|
| 58 |
const pendingPath = devDir + '/pending.json';
|
| 59 |
const pairedPath = devDir + '/paired.json';
|
| 60 |
try {
|
| 61 |
-
const raw = fs.readFileSync(pendingPath, 'utf-8').trim();
|
| 62 |
-
if (!raw || raw === '[]' || raw === '{}') process.exit(0);
|
| 63 |
-
let pending = JSON.parse(raw);
|
| 64 |
-
if (!Array.isArray(pending)) pending = Object.values(pending);
|
| 65 |
-
if (pending.length === 0) process.exit(0);
|
| 66 |
-
let paired = [];
|
| 67 |
-
if (fs.existsSync(pairedPath)) {
|
| 68 |
-
try { paired = JSON.parse(fs.readFileSync(pairedPath, 'utf-8')); } catch(e) {}
|
| 69 |
-
}
|
| 70 |
-
if (!Array.isArray(paired)) paired = [];
|
| 71 |
-
const pairedIds = new Set(paired.map(function(d) { return d.id || d.deviceId || d.name; }));
|
| 72 |
-
const toApprove = pending.filter(function(d) {
|
| 73 |
-
return !pairedIds.has(d.id || d.deviceId || d.name);
|
| 74 |
-
});
|
| 75 |
-
if (toApprove.length > 0) {
|
| 76 |
-
const approved = toApprove.map(function(d) {
|
| 77 |
-
return Object.assign({}, d, { approved: true, approvedAt: new Date().toISOString() });
|
| 78 |
-
});
|
| 79 |
-
paired = paired.concat(approved);
|
| 80 |
-
fs.writeFileSync(pairedPath, JSON.stringify(paired, null, 2));
|
| 81 |
-
fs.writeFileSync(pendingPath, '[]');
|
| 82 |
-
console.log('[auto-pair] Approved ' + toApprove.length + ' device(s)');
|
| 83 |
-
}
|
| 84 |
} catch(e) { /* ignore */ }
|
| 85 |
JSEOF
|
| 86 |
-
fi
|
| 87 |
-
done
|
| 88 |
) &
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
while true; do
|
| 93 |
-
echo "[entrypoint] Gateway starting
|
| 94 |
-
node /app/openclaw.mjs gateway \
|
| 95 |
-
--allow-unconfigured \
|
| 96 |
-
--bind lan \
|
| 97 |
-
--port 7860
|
| 98 |
-
EXIT_CODE=$?
|
| 99 |
-
echo "[entrypoint] Gateway exited (code $EXIT_CODE) - restarting in 2s
|
| 100 |
-
sleep 2
|
| 101 |
done
|
|
|
|
| 1 |
#!/bin/sh
|
| 2 |
|
| 3 |
# OpenClaw HF Spaces - Production Entrypoint
|
| 4 |
+
# Storage: HF Storage Bucket mounted at /data (100 GB, persists across restarts)
|
| 5 |
+
|
| 6 |
+
# -- resolve writable home ----------------------------------------
|
| 7 |
|
|
|
|
| 8 |
if mkdir -p /data/.openclaw 2>/dev/null; then
|
| 9 |
+
export OPENCLAW_HOME=/data
|
|
|
|
| 10 |
else
|
| 11 |
+
export OPENCLAW_HOME=/home/user
|
| 12 |
+
mkdir -p /home/user/.openclaw
|
|
|
|
| 13 |
fi
|
| 14 |
echo "[entrypoint] OPENCLAW_HOME=$OPENCLAW_HOME"
|
| 15 |
|
| 16 |
+
# -- dynamic provider key export ----------------------------------
|
| 17 |
+
|
| 18 |
for VAR in $(env | cut -d= -f1); do
|
| 19 |
+
case "$VAR" in
|
| 20 |
+
OPENCLAW_*|SPACE_*|SYSTEM_*|HF_*|NODE_*|npm_*) continue ;;
|
| 21 |
+
esac
|
| 22 |
+
case "$VAR" in
|
| 23 |
+
*_API_KEY|*_SECRET_KEY|*_ACCESS_TOKEN|*_BOT_TOKEN|*_AUTH_TOKEN|*_APP_KEY)
|
| 24 |
+
VAL=$(printenv "$VAR" 2>/dev/null || true)
|
| 25 |
+
if [ -n "$VAL" ]; then
|
| 26 |
+
export "$VAR"
|
| 27 |
+
echo "[entrypoint] exported: $VAR"
|
| 28 |
+
fi
|
| 29 |
+
;;
|
| 30 |
+
esac
|
| 31 |
done
|
| 32 |
|
| 33 |
export HF_TOKEN="${HF_TOKEN:-}"
|
| 34 |
|
| 35 |
+
# -- run setup (patch mode: preserves user settings) --------------
|
| 36 |
+
|
| 37 |
+
echo "[entrypoint] Running setup..."
|
| 38 |
node /app/spaces/huggingface/setup-hf-config.mjs || true
|
| 39 |
echo "[entrypoint] Setup done."
|
| 40 |
|
| 41 |
+
# -- security audit -----------------------------------------------
|
| 42 |
+
|
| 43 |
if [ -f /app/security-check.sh ]; then
|
| 44 |
+
sh /app/security-check.sh || true
|
| 45 |
fi
|
| 46 |
|
| 47 |
+
# -- auto-approve pending device pairings -------------------------
|
| 48 |
+
|
| 49 |
+
# Sub-agents connecting as nodes require device pairing.
|
| 50 |
+
# This loop detects pending pairing requests and approves them automatically.
|
| 51 |
+
|
| 52 |
(
|
| 53 |
+
DEVICES_DIR="$OPENCLAW_HOME/.openclaw/devices"
|
| 54 |
+
mkdir -p "$DEVICES_DIR"
|
| 55 |
+
echo "[auto-pair] Device auto-approval started"
|
| 56 |
+
while true; do
|
| 57 |
+
sleep 8
|
| 58 |
+
PENDING="$DEVICES_DIR/pending.json"
|
| 59 |
+
PAIRED="$DEVICES_DIR/paired.json"
|
| 60 |
+
if [ -f "$PENDING" ]; then
|
| 61 |
+
node - << 'JSEOF'
|
| 62 |
const fs = require('fs');
|
| 63 |
const devDir = process.env.OPENCLAW_HOME + '/.openclaw/devices';
|
| 64 |
const pendingPath = devDir + '/pending.json';
|
| 65 |
const pairedPath = devDir + '/paired.json';
|
| 66 |
try {
|
| 67 |
+
const raw = fs.readFileSync(pendingPath, 'utf-8').trim();
|
| 68 |
+
if (!raw || raw === '[]' || raw === '{}') process.exit(0);
|
| 69 |
+
let pending = JSON.parse(raw);
|
| 70 |
+
if (!Array.isArray(pending)) pending = Object.values(pending);
|
| 71 |
+
if (pending.length === 0) process.exit(0);
|
| 72 |
+
let paired = [];
|
| 73 |
+
if (fs.existsSync(pairedPath)) {
|
| 74 |
+
try { paired = JSON.parse(fs.readFileSync(pairedPath, 'utf-8')); } catch(e) {}
|
| 75 |
+
}
|
| 76 |
+
if (!Array.isArray(paired)) paired = [];
|
| 77 |
+
const pairedIds = new Set(paired.map(function(d) { return d.id || d.deviceId || d.name; }));
|
| 78 |
+
const toApprove = pending.filter(function(d) {
|
| 79 |
+
return !pairedIds.has(d.id || d.deviceId || d.name);
|
| 80 |
+
});
|
| 81 |
+
if (toApprove.length > 0) {
|
| 82 |
+
const approved = toApprove.map(function(d) {
|
| 83 |
+
return Object.assign({}, d, { approved: true, approvedAt: new Date().toISOString() });
|
| 84 |
+
});
|
| 85 |
+
paired = paired.concat(approved);
|
| 86 |
+
fs.writeFileSync(pairedPath, JSON.stringify(paired, null, 2));
|
| 87 |
+
fs.writeFileSync(pendingPath, '[]');
|
| 88 |
+
console.log('[auto-pair] Approved ' + toApprove.length + ' device(s)');
|
| 89 |
+
}
|
| 90 |
} catch(e) { /* ignore */ }
|
| 91 |
JSEOF
|
| 92 |
+
fi
|
| 93 |
+
done
|
| 94 |
) &
|
| 95 |
|
| 96 |
+
# -- gateway loop -------------------------------------------------
|
| 97 |
+
|
| 98 |
+
# Gateway exits on config save (OpenClaw full process restart).
|
| 99 |
+
# Loop keeps container alive and restarts gateway in 2s.
|
| 100 |
+
|
| 101 |
+
echo "[entrypoint] Starting gateway loop..."
|
| 102 |
while true; do
|
| 103 |
+
echo "[entrypoint] Gateway starting..."
|
| 104 |
+
node /app/openclaw.mjs gateway \
|
| 105 |
+
--allow-unconfigured \
|
| 106 |
+
--bind lan \
|
| 107 |
+
--port 7860
|
| 108 |
+
EXIT_CODE=$?
|
| 109 |
+
echo "[entrypoint] Gateway exited (code $EXIT_CODE) - restarting in 2s..."
|
| 110 |
+
sleep 2
|
| 111 |
done
|