Spaces:
Sleeping
Sleeping
# 确保任何命令失败都会立即退出脚本 | |
set -e | |
# ======================= 配置区 ======================= | |
export REPO_ID="arcticaurora/mcp-settings" | |
# ====================================================== | |
# --- 定义路径 --- | |
# 只读的源码目录,由 Dockerfile 构建 | |
APP_SRC_DIR="/app" | |
# 【关键修复】在 /tmp 下创建可写的运行时目录 | |
RUN_DIR="/tmp/mcphub" | |
CONFIG_FILE_NAME="mcp_settings.json" | |
CONFIG_FILE_PATH="${RUN_DIR}/${CONFIG_FILE_NAME}" | |
export HF_HOME="/tmp/.cache/huggingface" | |
echo "--- MCPHub Persistence Wrapper (v8 - /tmp Runtime) ---" | |
# 1. 创建运行时目录并复制应用文件 | |
echo "Creating writable runtime directory at ${RUN_DIR}..." | |
mkdir -p "$RUN_DIR" | |
echo "Copying application files from ${APP_SRC_DIR}..." | |
cp -a "${APP_SRC_DIR}/." "$RUN_DIR/" | |
# 2. 登录 Hugging Face Hub | |
echo "Logging into Hugging Face Hub..." | |
huggingface-cli login --token "$HF_TOKEN" &> /dev/null | |
# 3. 下载或创建配置文件到可写目录 | |
echo "Preparing config file at ${CONFIG_FILE_PATH}..." | |
# 先下载到 /tmp,然后移动到最终位置 | |
TEMP_DOWNLOAD_PATH="/tmp/${CONFIG_FILE_NAME}" | |
python3 -c "from huggingface_hub import hf_hub_download; hf_hub_download(repo_id='$REPO_ID', filename='$CONFIG_FILE_NAME', repo_type='dataset', local_dir='/tmp', etag_timeout=10, resume_download=True)" &> /dev/null | |
if [ -f "$TEMP_DOWNLOAD_PATH" ]; then | |
mv "$TEMP_DOWNLOAD_PATH" "$CONFIG_FILE_PATH" | |
else | |
echo "{}" > "$CONFIG_FILE_PATH" | |
huggingface-cli upload "$REPO_ID" "$CONFIG_FILE_PATH" --repo-type dataset --commit-message "feat: Initial empty config" &> /dev/null | |
fi | |
echo "Config is ready." | |
# 4. 启动后台持久化脚本 | |
echo "Starting persistence sync script in background..." | |
( | |
# 初始化哈希值 | |
LAST_HASH="" | |
if [ -f "$CONFIG_FILE_PATH" ]; then | |
LAST_HASH=$(md5sum "$CONFIG_FILE_PATH" | awk '{ print $1 }') | |
fi | |
echo "Initial config hash: $LAST_HASH" | |
while true; do | |
sleep 60 | |
if [ -f "$CONFIG_FILE_PATH" ]; then | |
CURRENT_HASH=$(md5sum "$CONFIG_FILE_PATH" | awk '{ print $1 }') | |
if [ "$CURRENT_HASH" != "$LAST_HASH" ]; then | |
echo "[Sync] Config changed, uploading to Hub..." | |
huggingface-cli upload "$REPO_ID" "$CONFIG_FILE_PATH" --repo-type dataset --commit-message "chore: Auto-sync settings" | |
LAST_HASH=$CURRENT_HASH | |
fi | |
fi | |
done | |
) & | |
# 5. 【关键修复】切换到可写的运行时目录,然后启动应用 | |
echo "Changing directory to ${RUN_DIR}..." | |
cd "$RUN_DIR" | |
echo "Starting MCPHub server in foreground on port ${PORT}..." | |
npm start | |
echo "MCPHub server exited. Container will now stop." |