Spaces:
Sleeping
Sleeping
| set -e | |
| set -x # Enable debug logging | |
| # Print system info | |
| echo "System Info:" | |
| uname -a | |
| free -m || true | |
| df -h || true | |
| # Force Port 7860 for Hugging Face Spaces | |
| export PICOCLAW_GATEWAY_PORT=7860 | |
| export PICOCLAW_GATEWAY_HOST=0.0.0.0 | |
| export PORT=7860 | |
| # Try to fix DNS if possible | |
| if [ -w /etc/resolv.conf ]; then | |
| echo "nameserver 8.8.8.8" > /etc/resolv.conf || echo "Failed to write resolv.conf" | |
| fi | |
| # ================================================================================== | |
| # PERSISTENCE HANDLING (Hugging Face Spaces /data) | |
| # ================================================================================== | |
| # Check for Hugging Face persistent storage mount point | |
| if [ -d "/data" ]; then | |
| echo "Detected persistent storage at /data" | |
| # Create persistent directory structure if it doesn't exist | |
| mkdir -p /data/picoclaw | |
| # If persistent config doesn't exist, copy from image's default location | |
| if [ ! -f "/data/picoclaw/config.json" ] && [ -f "/root/.picoclaw/config.json" ]; then | |
| echo "Initializing persistent config from image..." | |
| cp /root/.picoclaw/config.json /data/picoclaw/config.json | |
| fi | |
| # If persistent workspace doesn't exist, try to copy from image | |
| if [ ! -d "/data/picoclaw/workspace" ] && [ -d "/root/.picoclaw/workspace" ]; then | |
| echo "Initializing persistent workspace from image..." | |
| cp -r /root/.picoclaw/workspace /data/picoclaw/ | |
| fi | |
| # Replace ephemeral home with symlink to persistent storage | |
| # We remove the ephemeral directory and replace it with a symlink to /data/picoclaw | |
| echo "Symlinking /root/.picoclaw to /data/picoclaw..." | |
| rm -rf /root/.picoclaw | |
| ln -s /data/picoclaw /root/.picoclaw | |
| # ================================================================================== | |
| # CUSTOM STARTUP SCRIPT (Hugging Face Spaces /data) | |
| # ================================================================================== | |
| # Check for custom startup script in persistent storage | |
| if [ -f "/data/on_startup.sh" ]; then | |
| echo "Found custom startup script at /data/on_startup.sh, executing..." | |
| chmod +x /data/on_startup.sh | |
| /data/on_startup.sh | |
| fi | |
| # ================================================================================== | |
| fi | |
| # ================================================================================== | |
| # Create user directories if needed (running as root now, so we need to ensure permissions) | |
| # But wait, if we run as root, HOME is /root usually. | |
| # Let's ensure workspace exists | |
| mkdir -p /root/.picoclaw/workspace | |
| # Copy workspace template if empty | |
| if [ -z "$(ls -A /root/.picoclaw/workspace)" ]; then | |
| cp -r /app/workspace/* /root/.picoclaw/workspace/ || true | |
| fi | |
| # ================================================================================== | |
| # AUTHENTICATION & GIT CONFIG (Private Space) | |
| # ================================================================================== | |
| # Set credentials if not provided by environment | |
| if [ -z "$GITHUB_TOKEN" ]; then | |
| export GITHUB_TOKEN="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" | |
| fi | |
| export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-personalbotai}" | |
| export GIT_AUTHOR_EMAIL="${GIT_AUTHOR_EMAIL:-arrow1328239syamsul@gmail.com}" | |
| export GIT_COMMITTER_NAME="${GIT_COMMITTER_NAME:-personalbotai}" | |
| export GIT_COMMITTER_EMAIL="${GIT_COMMITTER_EMAIL:-arrow1328239syamsul@gmail.com}" | |
| # Configure Git | |
| git config --global user.name "$GIT_AUTHOR_NAME" | |
| git config --global user.email "$GIT_AUTHOR_EMAIL" | |
| # Setup GitHub CLI credential helper | |
| if [ -n "$GITHUB_TOKEN" ]; then | |
| # Login to gh | |
| # If GITHUB_TOKEN is set in environment, we don't need to explicitly login | |
| # and trying to login might fail because the token takes precedence. | |
| # We just setup git credential helper. | |
| gh auth setup-git | |
| fi | |
| # ================================================================================== | |
| # DATASET SYNC (Free Persistence Alternative) | |
| # ================================================================================== | |
| # Set default repo if not provided | |
| export DATASET_REPO="${DATASET_REPO:-https://github.com/personalbotai/picoclaw-memory.git}" | |
| # Run the sync script in background | |
| if [ -n "$DATASET_REPO" ] && [ -f "/usr/local/bin/sync_dataset.sh" ]; then | |
| echo "Starting Dataset Sync Service..." | |
| chmod +x /usr/local/bin/sync_dataset.sh | |
| /usr/local/bin/sync_dataset.sh & | |
| fi | |
| # ================================================================================== | |
| # KEEP ALIVE (Prevent Sleep) | |
| # ================================================================================== | |
| # If KEEP_ALIVE_URL is set, ping it periodically to prevent sleep | |
| if [ -n "$KEEP_ALIVE_URL" ]; then | |
| echo "Starting keep-alive pinger for $KEEP_ALIVE_URL..." | |
| ( | |
| while true; do | |
| echo "Pinging $KEEP_ALIVE_URL to keep alive..." | |
| curl -s "$KEEP_ALIVE_URL" > /dev/null || echo "Ping failed" | |
| sleep 300 # Ping every 5 minutes | |
| done | |
| ) & | |
| fi | |
| # ================================================================================== | |
| # DEBUG HEALTH CHECK (Print status to logs) | |
| # ================================================================================== | |
| ( | |
| echo "Starting health check debugger..." | |
| sleep 10 | |
| while true; do | |
| STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:7860/health || echo "FAIL") | |
| echo "[DEBUG] Health check probe: $STATUS" | |
| sleep 10 | |
| done | |
| ) & | |
| # Execute the passed command | |
| exec "$@" | |