Spaces:
Paused
Paused
Upload 2 files
Browse files- Dockerfile +51 -0
- entrypoint +79 -0
Dockerfile
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1. Node.js stable version
|
| 2 |
+
FROM node:18-slim
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
# 2. Install required system tools & HF CLI
|
| 6 |
+
RUN apt-get update && apt-get install -y \
|
| 7 |
+
curl \
|
| 8 |
+
bash \
|
| 9 |
+
git \
|
| 10 |
+
python3 \
|
| 11 |
+
python3-pip \
|
| 12 |
+
python3.11-venv \
|
| 13 |
+
build-essential \
|
| 14 |
+
gcc \
|
| 15 |
+
g++ \
|
| 16 |
+
make \
|
| 17 |
+
nano \
|
| 18 |
+
zip \
|
| 19 |
+
unzip \
|
| 20 |
+
procps \
|
| 21 |
+
inotify-tools \
|
| 22 |
+
&& curl -LsSf https://hf.co/cli/install.sh | bash \
|
| 23 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# 3. Install OpenCode
|
| 27 |
+
RUN curl -fsSL https://opencode.ai/install | bash
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# 4. Setup folders
|
| 31 |
+
RUN mkdir -p /home && chmod 777 /home
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# 5. Copy entrypoint script
|
| 35 |
+
COPY entrypoint.sh /entrypoint.sh
|
| 36 |
+
RUN chmod +x /entrypoint.sh
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# 6. Environment settings with RAM limit
|
| 40 |
+
ENV OPENCODE_DATA_DIR=/home
|
| 41 |
+
ENV HOME=/home
|
| 42 |
+
ENV PATH="/root/.local/bin:/home/node/.local/bin:/home/.local/bin:$PATH"
|
| 43 |
+
ENV NODE_OPTIONS="--max-old-space-size=14336"
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# 7. Expose port 7860
|
| 47 |
+
EXPOSE 7860
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# 8. Run entrypoint script
|
| 51 |
+
CMD ["/entrypoint.sh"]
|
entrypoint
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
# Ensure HF CLI is in PATH
|
| 4 |
+
export PATH="$HOME/.local/bin:$PATH"
|
| 5 |
+
|
| 6 |
+
# Set HF Token from Space Secrets
|
| 7 |
+
export HF_TOKEN="${HF_TOKEN}"
|
| 8 |
+
|
| 9 |
+
# Variables
|
| 10 |
+
BUCKET='hf://buckets/jerecom/Opencod-storage'
|
| 11 |
+
SOURCE='/home'
|
| 12 |
+
|
| 13 |
+
# Configure Git
|
| 14 |
+
git config --global user.email 'badal@example.com'
|
| 15 |
+
git config --global user.name 'Badal'
|
| 16 |
+
|
| 17 |
+
# Set OpenCode path
|
| 18 |
+
OP_PATH=$(find / -name opencode -type f -printf '%h' -quit 2>/dev/null)
|
| 19 |
+
export PATH="$OP_PATH:$PATH"
|
| 20 |
+
|
| 21 |
+
# ============================================
|
| 22 |
+
# STEP 0: BUCKET CLEANUP (Delete .cache & .npm from bucket)
|
| 23 |
+
# ============================================
|
| 24 |
+
echo '=== [STEP 0] CLEANING BUCKET (Removing .cache and .npm) ==='
|
| 25 |
+
hf buckets rm "$BUCKET/.cache" --recursive -y 2>/dev/null && echo '=== [STEP 0] .cache removed from bucket ===' || echo '=== [STEP 0] .cache not found in bucket (skipping) ==='
|
| 26 |
+
hf buckets rm "$BUCKET/.npm" --recursive -y 2>/dev/null && echo '=== [STEP 0] .npm removed from bucket ===' || echo '=== [STEP 0] .npm not found in bucket (skipping) ==='
|
| 27 |
+
echo '=== [STEP 0] BUCKET CLEANUP DONE ==='
|
| 28 |
+
|
| 29 |
+
# ============================================
|
| 30 |
+
# STEP 1: RESTORE (Try, but continue if bucket empty)
|
| 31 |
+
# ============================================
|
| 32 |
+
echo '=== [STEP 1] RESTORING DATA FROM BUCKET ==='
|
| 33 |
+
if hf sync "$BUCKET" "$SOURCE" --delete 2>/dev/null; then
|
| 34 |
+
echo '=== [STEP 1] RESTORE COMPLETE ==='
|
| 35 |
+
else
|
| 36 |
+
echo '=== [STEP 1] Restore skipped (bucket empty or failed - continuing anyway) ==='
|
| 37 |
+
fi
|
| 38 |
+
|
| 39 |
+
# Auto-create /home/user if not exists
|
| 40 |
+
if [ ! -d "/home/user" ]; then
|
| 41 |
+
echo '=== Creating /home/user folder ==='
|
| 42 |
+
mkdir -p /home/user
|
| 43 |
+
fi
|
| 44 |
+
|
| 45 |
+
# ============================================
|
| 46 |
+
# STEP 2: START OPENCODE
|
| 47 |
+
# ============================================
|
| 48 |
+
echo '=== [STEP 2] STARTING OPENCODE ==='
|
| 49 |
+
export OPENCODE_SERVER_USERNAME=${OPENCODE_SERVER_USERNAME}
|
| 50 |
+
export OPENCODE_SERVER_PASSWORD=${OPENCODE_SERVER_PASSWORD}
|
| 51 |
+
opencode web --port 7860 --hostname 0.0.0.0 > /tmp/opencode.log 2>&1 &
|
| 52 |
+
sleep 10
|
| 53 |
+
echo '=== [STEP 2] OPENCODE STARTED ==='
|
| 54 |
+
|
| 55 |
+
# ============================================
|
| 56 |
+
# STEP 3: INOTIFY-BASED SMART SYNC
|
| 57 |
+
# ============================================
|
| 58 |
+
echo '=== [STEP 3] STARTING SMART SYNC (inotifywait) ==='
|
| 59 |
+
while true; do
|
| 60 |
+
# Check if OpenCode is still running
|
| 61 |
+
if ! pgrep -f 'opencode' > /dev/null; then
|
| 62 |
+
echo 'CRITICAL: OpenCode process died! Exiting container...'
|
| 63 |
+
exit 1
|
| 64 |
+
fi
|
| 65 |
+
|
| 66 |
+
# Wait for any file change in /home (blocks until change detected)
|
| 67 |
+
# Excludes .mdb files, .cache folder, .npm folder from triggering sync
|
| 68 |
+
inotifywait -r -e modify,create,delete,move \
|
| 69 |
+
--exclude '.*\.mdb$' \
|
| 70 |
+
--exclude '.*\.log$' \
|
| 71 |
+
--exclude '.*/\.cache(/.*)?$' \
|
| 72 |
+
--exclude '.*/\.npm(/.*)?$' \
|
| 73 |
+
-q "$SOURCE"
|
| 74 |
+
|
| 75 |
+
# Change detected! Sync to bucket (excluding .mdb, .cache, .npm)
|
| 76 |
+
echo "=== [STEP 3] Change detected! Syncing to bucket... ==="
|
| 77 |
+
hf sync "$SOURCE" "$BUCKET" --delete --exclude "*.mdb" --exclude "*.log"
|
| 78 |
+
echo "=== [STEP 3] Sync done! Waiting for next change... ==="
|
| 79 |
+
done
|