File size: 2,706 Bytes
6bbb181
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
# 确保任何命令失败都会立即退出脚本
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."