my-clawdbot / run.sh
mdhosainp414's picture
Update run.sh
520ee63 verified
#!/bin/bash
set -euo pipefail
echo "===== Application Startup at $(date) ====="
git config --global safe.directory '*'
mkdir -p /root/.openclaw
# --------------------------
# REQUIRED SECRETS CHECK
# --------------------------
# This prevents token mismatch by forcing only ONE source of truth.
if [[ -z "${GATEWAY_TOKEN:-}" ]]; then
echo "ERROR: Missing GATEWAY_TOKEN secret in Space Settings → Secrets."
echo "Add secret: GATEWAY_TOKEN = <Md12002#>"
exit 1
fi
# Dataset persistence is optional, but you use it, so we warn if missing.
if [[ -z "${HF_TOKEN:-}" || -z "${DATASET_ID:-}" ]]; then
echo "WARNING: HF_TOKEN or DATASET_ID missing; dataset restore/backup will be skipped."
fi
# --------------------------
# 1) RESTORE FROM HF DATASET
# --------------------------
if [[ -n "${HF_TOKEN:-}" && -n "${DATASET_ID:-}" ]]; then
echo "Restoring from HF dataset: ${DATASET_ID}"
rm -rf /app/data-storage || true
if git clone "https://x-access-token:${HF_TOKEN}@huggingface.co/datasets/${DATASET_ID}" /app/data-storage; then
echo "Dataset clone OK."
else
echo "Dataset clone failed (permissions / first run)."
fi
fi
# --------------------------
# 2) WRITE CLEAN CONFIG (HOOKS DISABLED)
# --------------------------
# IMPORTANT:
# - bind must be a MODE, not an IP.
# - hooks disabled so no hooks.token needed.
# - port 7860 so HF App tab works.
echo "Writing clean OpenClaw config (hooks disabled)"
cat > /root/.openclaw/openclaw.json << 'EOF'
{
"gateway": {
"mode": "local",
"port": 7860,
"bind": "lan",
"controlUi": { "enabled": true, "allowInsecureAuth": true }
},
"hooks": { "enabled": false }
}
EOF
# --------------------------
# 3) START OPENCLAW GATEWAY
# --------------------------
echo "--- Starting OpenClaw Gateway on :7860 ---"
openclaw gateway --port 7860 --bind lan --token "$GATEWAY_TOKEN" --allow-unconfigured &
GATEWAY_PID=$!
# --------------------------
# 4) SAVE FIXED CONFIG BACK TO DATASET (ONCE)
# --------------------------
if [[ -d "/app/data-storage/.git" ]]; then
echo "Saving fixed config to dataset"
git config --global user.name "${GIT_USER_NAME:-openclaw-bot}"
git config --global user.email "${GIT_USER_EMAIL:-openclaw-bot@users.noreply.huggingface.co}"
mkdir -p /app/data-storage/config
cp /root/.openclaw/openclaw.json /app/data-storage/config/openclaw.json
cd /app/data-storage
git add .
if git diff --cached --quiet; then
echo "No dataset changes to commit."
else
git commit -m "stable config (hooks off) $(date -u +'%Y-%m-%dT%H:%M:%SZ')" || true
git push origin main || echo "Push failed"
fi
fi
# --------------------------
# 5) KEEP RUNNING
# --------------------------
wait "$GATEWAY_PID"