open-webui / start_with_sync.sh
wwforonce's picture
fix storage permission error
2509e3a
raw
history blame
2.69 kB
#!/bin/bash
set -e
# Use /tmp for all writable data
export DATA_DIR="/tmp/open-webui-data"
export HF_STORAGE_REPO="${HF_STORAGE_REPO:-nxdev-org/open-webui-storage}"
export SYNC_INTERVAL="${SYNC_INTERVAL:-300}"
# Set all HuggingFace and cache directories to /tmp
export HF_HOME="/tmp/hf_cache"
export HUGGINGFACE_HUB_CACHE="/tmp/hf_cache"
export TRANSFORMERS_CACHE="/tmp/hf_cache"
export SENTENCE_TRANSFORMERS_HOME="/tmp/hf_cache"
# Override Open WebUI environment variables
export STATIC_DIR="/tmp/static"
export UPLOAD_DIR="/tmp/uploads"
echo "Starting Open WebUI with HF Dataset persistence..."
echo "Data directory: $DATA_DIR"
echo "HF Repository: $HF_STORAGE_REPO"
echo "HF Cache: $HF_HOME"
# Create all necessary directories
mkdir -p "$DATA_DIR" "$HF_HOME" "$STATIC_DIR" "$UPLOAD_DIR"
# Copy static files to writable location
if [ -d "/app/backend/open_webui/static" ]; then
echo "Copying static files to writable location..."
cp -r /app/backend/open_webui/static/* "$STATIC_DIR/" 2>/dev/null || true
fi
# Test write permissions
if touch "$DATA_DIR/test" 2>/dev/null; then
rm "$DATA_DIR/test"
echo "Data directory is writable"
else
echo "Warning: Data directory may not be writable"
fi
# Check if HF_TOKEN is set
if [ -z "$HF_TOKEN" ]; then
echo "Warning: HF_TOKEN not set. Sync functionality will be limited."
else
echo "HF_TOKEN is set, proceeding with sync..."
fi
# Download existing data on startup
echo "Syncing data from Hugging Face..."
python3 /app/sync_storage.py download
# Function to handle graceful shutdown
cleanup() {
echo "Shutting down gracefully..."
# Upload final data state
if [ -n "$HF_TOKEN" ]; then
echo "Uploading final data state..."
python3 /app/sync_storage.py upload
fi
# Kill background processes
kill $SYNC_PID 2>/dev/null || true
kill $WEBUI_PID 2>/dev/null || true
exit 0
}
# Set up signal handlers
trap cleanup SIGTERM SIGINT
# Background sync function
background_sync() {
if [ -n "$HF_TOKEN" ]; then
while true; do
sleep $SYNC_INTERVAL
echo "Periodic sync to Hugging Face..."
python3 /app/sync_storage.py upload
done
else
echo "Skipping background sync - no HF_TOKEN"
while true; do
sleep 3600
done
fi
}
# Start background sync
background_sync &
SYNC_PID=$!
# Start Open WebUI
echo "Starting Open WebUI..."
# Set environment variables for Open WebUI
export WEBUI_SECRET_KEY="${WEBUI_SECRET_KEY:-$(openssl rand -hex 32)}"
# Start Open WebUI in background
/app/backend/start.sh &
WEBUI_PID=$!
# Wait for Open WebUI process
wait $WEBUI_PID