File size: 6,587 Bytes
b73c9ca
3fdc4d4
cdf49d2
b73c9ca
6b8e0fb
cdf49d2
 
b73c9ca
 
cdf49d2
 
b73c9ca
 
 
 
 
 
 
 
 
 
ae6afde
 
 
1bb0571
194e319
1bb0571
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194e319
 
 
e88676b
ae6afde
8619e57
b73c9ca
 
 
c294590
194e319
b73c9ca
 
ae6afde
5b17630
 
ae6afde
 
5b17630
ae6afde
b73c9ca
 
 
 
 
 
 
 
 
 
 
 
6b8e0fb
 
 
 
 
 
 
 
 
 
 
 
 
935f3fa
 
ae6afde
6b8e0fb
 
 
 
 
 
 
 
 
 
 
 
b73c9ca
 
6b8e0fb
b73c9ca
 
 
 
6b8e0fb
 
 
b73c9ca
 
 
 
 
 
8619e57
6b8e0fb
b73c9ca
6b8e0fb
b73c9ca
 
 
 
 
 
 
 
ae6afde
 
 
b73c9ca
cdf49d2
 
 
 
 
 
b73c9ca
6b8e0fb
 
b73c9ca
 
 
 
 
 
 
ae6afde
b73c9ca
 
ae6afde
 
b73c9ca
 
8619e57
b73c9ca
 
8619e57
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
FROM node:22-slim

# ========= 1. 安装系统依赖 + Chromium 浏览器 =========
RUN apt-get update && apt-get install -y --no-install-recommends \
    git ca-certificates build-essential python3 python3-pip curl \
    chromium \
    libnss3 libatk-bridge2.0-0 libdrm2 libxkbcommon0 libgbm1 libasound2 \
    && rm -rf /var/lib/apt/lists/*

ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium

RUN pip3 install --no-cache-dir huggingface_hub --break-system-packages

RUN npm install -g openclaw@latest --unsafe-perm

RUN npx -y @tencent-weixin/openclaw-weixin-cli@latest install

ENV PORT=7860 \
    OPENCLAW_GATEWAY_MODE=local \
    HOME=/root

# 设置环境变量,防止 doctor 自动启用 Telegram
ENV OPENCLAW_DISABLE_TELEGRAM=1

# ========= 2. 完整的 sync.py 备份恢复脚本 =========
RUN cat > /usr/local/bin/sync.py << 'SYNC_EOF'
import os, sys, tarfile
from huggingface_hub import HfApi, hf_hub_download
from datetime import datetime, timedelta
import subprocess
import time

api = HfApi()
repo_id = os.getenv("HF_DATASET")
token = os.getenv("HF_TOKEN")

def restore():
    try:
        print(f"--- [SYNC] 启动恢复流程, 目标仓库: {repo_id} ---")
        if not repo_id or not token:
            print("--- [SYNC] 跳过恢复: 未配置 HF_DATASET 或 HF_TOKEN ---")
            return False
        files = api.list_repo_files(repo_id=repo_id, repo_type="dataset", token=token)
        now = datetime.now()
        for i in range(5):
            day = (now - timedelta(days=i)).strftime("%Y-%m-%d")
            name = f"backup_{day}.tar.gz"
            if name in files:
                print(f"--- [SYNC] 发现备份文件: {name}, 正在下载... ---")
                path = hf_hub_download(repo_id=repo_id, filename=name, repo_type="dataset", token=token)
                with tarfile.open(path, "r:gz") as tar:
                    tar.extractall(path="/root/.openclaw/")
                print(f"--- [SYNC] 恢复成功! 数据已覆盖至 /root/.openclaw/ ---")
                return True
        print("--- [SYNC] 未找到最近 5 天的备份包 ---")
    except Exception as e:
        print(f"--- [SYNC] 恢复异常: {e} ---")

def backup():
    try:
        day = datetime.now().strftime("%Y-%m-%d")
        name = f"backup_{day}.tar.gz"
        print(f"--- [SYNC] 正在执行全量备份: {name} ---")
        with tarfile.open(name, "w:gz") as tar:
            for target in ["sessions", "workspace", "agents", "memory", "openclaw.json", "wechat-data"]:
                full_path = f"/root/.openclaw/{target}"
                if os.path.exists(full_path):
                    tar.add(full_path, arcname=target)
        api.upload_file(path_or_fileobj=name, path_in_repo=name, repo_id=repo_id, repo_type="dataset", token=token)
        print(f"--- [SYNC] 备份上传成功! ---")
        for _ in range(3):
            try:
                result = subprocess.run(["openclaw", "wechat", "send", "--to=me", f"备份成功: {name}"], capture_output=True, timeout=10)
                if result.returncode == 0:
                    break
            except Exception:
                pass
            time.sleep(5)
    except Exception as e:
        print(f"--- [SYNC] 备份失败: {e} ---")

if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "backup":
        backup()
    else:
        restore()
SYNC_EOF

RUN chmod +x /usr/local/bin/sync.py

# ========= 3. 启动脚本 start-openclaw(禁用 Telegram + 前台 exec) =========
RUN cat > /usr/local/bin/start-openclaw << 'EOF'
#!/bin/bash
set -e

echo "Starting OpenClaw gateway..."

python3 /usr/local/bin/sync.py restore

# 彻底删除 Telegram 所有残留文件
rm -rf /root/.openclaw/agents/main/agent/channels/telegram
rm -rf /root/.openclaw/credentials/telegram
rm -rf /root/.openclaw/agents/main/agent/telegram*
rm -rf /root/.openclaw/agents/main/agent/channels/telegram*
rm -f /root/.openclaw/agents/main/agent/auth-profiles.json
find /root/.openclaw -name "*telegram*" -exec rm -rf {} + 2>/dev/null || true

mkdir -p /root/.openclaw/sessions
mkdir -p /root/.openclaw/workspace
mkdir -p /root/.openclaw/workspace/memory
mkdir -p /root/.openclaw/wechat-data
touch /root/.openclaw/workspace/MEMORY.md
DATE=$(date +%Y-%m-%d)
touch /root/.openclaw/workspace/memory/$DATE.md

CLEAN_BASE=$(echo "$OPENAI_API_BASE" | sed "s|/chat/completions||g" | sed "s|/v1/|/v1|g" | sed "s|/v1$|/v1|g")

if [ -n "$GEMINI_API_KEY" ]; then
  PROVIDER="google"
  API_KEY_VAR="$GEMINI_API_KEY"
  BASE_URL_VAR="https://generativelanguage.googleapis.com/v1beta"
  MODEL_VAR="$MODEL"
  PRIMARY_MODEL="google/$MODEL_VAR"
  API_TYPE="google-generative-ai"
else
  PROVIDER="openai"
  API_KEY_VAR="$OPENAI_API_KEY"
  BASE_URL_VAR="$CLEAN_BASE"
  MODEL_VAR="$MODEL"
  PRIMARY_MODEL="openai/$MODEL_VAR"
  API_TYPE="openai-completions"
fi

# 写入 openclaw.json(显式禁用 telegram)
cat > /root/.openclaw/openclaw.json <<EOF2
{
  "models": {
    "providers": {
      "$PROVIDER": {
        "baseUrl": "$BASE_URL_VAR",
        "apiKey": "$API_KEY_VAR",
        "api": "$API_TYPE",
        "models": [{ "id": "$MODEL_VAR", "name": "$MODEL_VAR", "contextWindow": 128000 }]
      }
    }
  },
  "agents": {
    "defaults": {
      "model": { "primary": "$PRIMARY_MODEL" },
      "imageModel": { "primary": "openai/gpt-4o-mini" },
      "tools": {
        "elevated": {
          "enabled": true,
          "allowFrom": {
            "openclaw-weixin": true
          }
        }
      }
    }
  },
  "gateway": {
    "mode": "local",
    "bind": "lan",
    "port": $PORT,
    "trustedProxies": ["0.0.0.0/0", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"],
    "auth": { "mode": "token", "token": "$OPENCLAW_GATEWAY_PASSWORD" },
    "controlUi": { "allowInsecureAuth": true, "allowedOrigins": ["*"] }
  },
  "plugins": { "allow": ["openclaw-weixin"] },
  "channels": {
    "openclaw-weixin": {
      "enabled": true,
      "dataDir": "/root/.openclaw/wechat-data",
      "autoLogin": true
    },
    "telegram": {
      "enabled": false
    }
  },
  "browser": {
    "enabled": true,
    "headless": true,
    "noSandbox": true,
    "defaultProfile": "openclaw"
  }
}
EOF2

echo "=== openclaw.json content ==="
cat /root/.openclaw/openclaw.json
echo "=== end ==="

openclaw doctor --fix

# 启动定时备份(后台运行)
(while true; do sleep 1800; python3 /usr/local/bin/sync.py backup; done) &

# ✅ 前台运行 gateway(容器永不退出)
exec openclaw gateway run --port $PORT
EOF

RUN chmod +x /usr/local/bin/start-openclaw

EXPOSE 7860
CMD ["/usr/local/bin/start-openclaw"]