fix(render): completely rewrite startup script for reliability

- Remove all complex permission testing
- Use HOME/.clawdbot as default (always writable)
- Add explicit checks for node and dist/index.js
- Simplify error handling
- Better logging
This commit is contained in:
Ojus Save 2026-01-26 14:25:04 -08:00
parent 0e1641f87b
commit aa0621ae38

View File

@ -3,47 +3,39 @@
set -e set -e
echo "=== Render startup script ===" echo "=== Render startup script ==="
echo "HOME=${HOME:-not set}"
echo "CLAWDBOT_STATE_DIR=${CLAWDBOT_STATE_DIR:-not set}"
echo "User: $(whoami 2>/dev/null || echo unknown)"
echo "UID: $(id -u 2>/dev/null || echo unknown)"
# Ensure HOME is set (node user's home is /home/node in node:22-bookworm) # Set HOME if not set (node user's home is /home/node)
if [ -z "${HOME}" ]; then if [ -z "${HOME}" ]; then
export HOME="/home/node" export HOME="/home/node"
if [ ! -d "${HOME}" ]; then if [ ! -d "${HOME}" ]; then
export HOME="/tmp" export HOME="/tmp"
fi fi
echo "Set HOME to: ${HOME}"
fi fi
echo "HOME=${HOME}" # Use CLAWDBOT_STATE_DIR if set and writable, otherwise use HOME/.clawdbot
echo "CLAWDBOT_STATE_DIR=${CLAWDBOT_STATE_DIR}"
echo "User: $(whoami)"
echo "UID: $(id -u)"
# Determine config directory
# Use CLAWDBOT_STATE_DIR if set and writable, otherwise try /data/.clawdbot, fallback to HOME
CONFIG_DIR="${HOME}/.clawdbot" CONFIG_DIR="${HOME}/.clawdbot"
# Try preferred locations (disable set -e temporarily for testing)
set +e
if [ -n "${CLAWDBOT_STATE_DIR}" ]; then if [ -n "${CLAWDBOT_STATE_DIR}" ]; then
mkdir -p "${CLAWDBOT_STATE_DIR}" 2>/dev/null && touch "${CLAWDBOT_STATE_DIR}/.test" 2>/dev/null && rm -f "${CLAWDBOT_STATE_DIR}/.test" 2>/dev/null # Test if we can write to it
if [ $? -eq 0 ]; then if mkdir -p "${CLAWDBOT_STATE_DIR}" 2>/dev/null && touch "${CLAWDBOT_STATE_DIR}/.test" 2>/dev/null; then
rm -f "${CLAWDBOT_STATE_DIR}/.test" 2>/dev/null
CONFIG_DIR="${CLAWDBOT_STATE_DIR}" CONFIG_DIR="${CLAWDBOT_STATE_DIR}"
echo "Using CLAWDBOT_STATE_DIR: ${CONFIG_DIR}"
else
echo "Warning: ${CLAWDBOT_STATE_DIR} not writable, using ${CONFIG_DIR}"
fi fi
fi fi
if [ "${CONFIG_DIR}" = "${HOME}/.clawdbot" ]; then
mkdir -p "/data/.clawdbot" 2>/dev/null && touch "/data/.clawdbot/.test" 2>/dev/null && rm -f "/data/.clawdbot/.test" 2>/dev/null
if [ $? -eq 0 ]; then
CONFIG_DIR="/data/.clawdbot"
fi
fi
set -e
CONFIG_FILE="${CONFIG_DIR}/clawdbot.json" CONFIG_FILE="${CONFIG_DIR}/clawdbot.json"
echo "Config dir: ${CONFIG_DIR}" echo "Config dir: ${CONFIG_DIR}"
echo "Config file: ${CONFIG_FILE}" echo "Config file: ${CONFIG_FILE}"
# Create config directory (should always succeed now) # Create config directory
mkdir -p "${CONFIG_DIR}" mkdir -p "${CONFIG_DIR}"
# Write config file # Write config file
@ -59,53 +51,34 @@ cat > "${CONFIG_FILE}" << 'EOF'
} }
EOF EOF
echo "=== Config written ===" echo "=== Config written to ${CONFIG_FILE} ==="
echo "=== ${CONFIG_FILE}: ==="
cat "${CONFIG_FILE}" cat "${CONFIG_FILE}"
echo "=== End config ==="
# Verify file exists # Set environment variables for gateway
echo "=== Verifying config file ==="
if [ -f "${CONFIG_FILE}" ]; then
echo "Config file exists: ${CONFIG_FILE}"
ls -la "${CONFIG_FILE}" || true
else
echo "ERROR: Config file not found: ${CONFIG_FILE}"
exit 1
fi
# Start the gateway with token from env var
# Explicitly set CLAWDBOT_CONFIG_PATH to ensure config is loaded from the file we wrote
# Also update CLAWDBOT_STATE_DIR to match the directory we're actually using
# Disable config cache to ensure fresh reads
echo "=== Starting gateway ==="
echo "=== Using config dir: ${CONFIG_DIR} ==="
echo "=== Setting CLAWDBOT_STATE_DIR=${CONFIG_DIR} ==="
echo "=== Setting CLAWDBOT_CONFIG_PATH=${CONFIG_FILE} ==="
echo "=== Disabling config cache ==="
export CLAWDBOT_STATE_DIR="${CONFIG_DIR}" export CLAWDBOT_STATE_DIR="${CONFIG_DIR}"
export CLAWDBOT_CONFIG_PATH="${CONFIG_FILE}" export CLAWDBOT_CONFIG_PATH="${CONFIG_FILE}"
export CLAWDBOT_CONFIG_CACHE_MS=0 export CLAWDBOT_CONFIG_CACHE_MS=0
# Verify config can be read echo "=== Starting gateway ==="
echo "=== Verifying config can be read ===" echo "CLAWDBOT_STATE_DIR=${CLAWDBOT_STATE_DIR}"
node -e " echo "CLAWDBOT_CONFIG_PATH=${CLAWDBOT_CONFIG_PATH}"
const fs = require('fs');
const path = '${CONFIG_FILE}';
if (fs.existsSync(path)) {
const content = fs.readFileSync(path, 'utf-8');
const parsed = JSON.parse(content);
console.log('Config loaded successfully:');
console.log('trustedProxies:', JSON.stringify(parsed.gateway?.trustedProxies));
} else {
console.error('Config file not found:', path);
process.exit(1);
}
"
# Verify node is available
if ! command -v node >/dev/null 2>&1; then
echo "ERROR: node command not found"
exit 1
fi
# Verify dist/index.js exists
if [ ! -f "dist/index.js" ]; then
echo "ERROR: dist/index.js not found"
exit 1
fi
# Start gateway
exec node dist/index.js gateway \ exec node dist/index.js gateway \
--port 8080 \ --port 8080 \
--bind lan \ --bind lan \
--auth token \ --auth token \
--token "$CLAWDBOT_GATEWAY_TOKEN" \ --token "${CLAWDBOT_GATEWAY_TOKEN}" \
--allow-unconfigured --allow-unconfigured