wd21 commited on
Commit
b73c9ca
·
verified ·
1 Parent(s): de7a938

Upload 2 files

Browse files
Files changed (2) hide show
  1. Dockerfile +185 -0
  2. README.md +5 -8
Dockerfile ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM node:22-slim
2
+
3
+ RUN apt-get update && apt-get install -y --no-install-recommends \
4
+ git ca-certificates build-essential python3 python3-pip curl \
5
+ && rm -rf /var/lib/apt/lists/*
6
+
7
+ RUN pip3 install --no-cache-dir huggingface_hub --break-system-packages
8
+
9
+ RUN npm install -g openclaw@latest --unsafe-perm
10
+
11
+ RUN npx -y @tencent-weixin/openclaw-weixin-cli@latest install
12
+
13
+ ENV PORT=7860 \
14
+ OPENCLAW_GATEWAY_MODE=local \
15
+ HOME=/root
16
+
17
+ # 备份恢复脚本 sync.py
18
+ RUN cat > /usr/local/bin/sync.py << 'EOF'
19
+ import os, sys, tarfile
20
+ from huggingface_hub import HfApi, hf_hub_download
21
+ from datetime import datetime, timedelta
22
+ import subprocess
23
+ import time
24
+
25
+ api = HfApi()
26
+ repo_id = os.getenv("HF_DATASET")
27
+ token = os.getenv("HF_TOKEN")
28
+
29
+ def restore():
30
+ try:
31
+ print(f"--- [SYNC] 启动恢复流程, 目标仓库: {repo_id} ---")
32
+ if not repo_id or not token:
33
+ print("--- [SYNC] 跳过恢复: 未配置 HF_DATASET 或 HF_TOKEN ---")
34
+ return False
35
+ files = api.list_repo_files(repo_id=repo_id, repo_type="dataset", token=token)
36
+ now = datetime.now()
37
+ for i in range(5):
38
+ day = (now - timedelta(days=i)).strftime("%Y-%m-%d")
39
+ name = f"backup_{day}.tar.gz"
40
+ if name in files:
41
+ print(f"--- [SYNC] 发现备份文件: {name}, 正在下载... ---")
42
+ path = hf_hub_download(repo_id=repo_id, filename=name, repo_type="dataset", token=token)
43
+ with tarfile.open(path, "r:gz") as tar:
44
+ tar.extractall(path="/root/.openclaw/")
45
+ print(f"--- [SYNC] 恢复成功! 数据已覆盖至 /root/.openclaw/ ---")
46
+ return True
47
+ print("--- [SYNC] 未找到最近 5 天的备份包 ---")
48
+ except Exception as e:
49
+ print(f"--- [SYNC] 恢复异常: {e} ---")
50
+
51
+ def backup():
52
+ try:
53
+ day = datetime.now().strftime("%Y-%m-%d")
54
+ name = f"backup_{day}.tar.gz"
55
+ print(f"--- [SYNC] 正在执行全量备份: {name} ---")
56
+ with tarfile.open(name, "w:gz") as tar:
57
+ for target in ["sessions", "workspace", "agents", "memory", "openclaw.json", "wechat-data"]:
58
+ full_path = f"/root/.openclaw/{target}"
59
+ if os.path.exists(full_path):
60
+ tar.add(full_path, arcname=target)
61
+ api.upload_file(path_or_fileobj=name, path_in_repo=name, repo_id=repo_id, repo_type="dataset", token=token)
62
+ print(f"--- [SYNC] 备份上传成功! ---")
63
+ for _ in range(3):
64
+ try:
65
+ result = subprocess.run(["openclaw", "wechat", "send", "--to=me", f"备份成功: {name}"], capture_output=True, timeout=10)
66
+ if result.returncode == 0:
67
+ break
68
+ except Exception:
69
+ pass
70
+ time.sleep(5)
71
+ except Exception as e:
72
+ print(f"--- [SYNC] 备份失败: {e} ---")
73
+
74
+ if __name__ == "__main__":
75
+ if len(sys.argv) > 1 and sys.argv[1] == "backup":
76
+ backup()
77
+ else:
78
+ restore()
79
+ EOF
80
+
81
+ # 启动脚本 start-openclaw
82
+ RUN cat > /usr/local/bin/start-openclaw << 'EOF'
83
+ #!/bin/bash
84
+ set -e
85
+
86
+ # 恢复备份
87
+ python3 /usr/local/bin/sync.py restore
88
+
89
+ # 强制清理所有 Telegram 残留
90
+ rm -rf /root/.openclaw/agents/main/agent/channels/telegram
91
+ rm -rf /root/.openclaw/credentials/telegram
92
+ rm -f /root/.openclaw/agents/main/agent/auth-profiles.json
93
+ find /root/.openclaw -name "*telegram*" -exec rm -rf {} + 2>/dev/null || true
94
+
95
+ mkdir -p /root/.openclaw/sessions
96
+ mkdir -p /root/.openclaw/workspace
97
+ mkdir -p /root/.openclaw/workspace/memory
98
+ mkdir -p /root/.openclaw/wechat-data
99
+ touch /root/.openclaw/workspace/MEMORY.md
100
+ DATE=$(date +%Y-%m-%d)
101
+ touch /root/.openclaw/workspace/memory/$DATE.md
102
+
103
+ CLEAN_BASE=$(echo "$OPENAI_API_BASE" | sed "s|/chat/completions||g" | sed "s|/v1/|/v1|g" | sed "s|/v1$|/v1|g")
104
+
105
+ if [ -n "$GEMINI_API_KEY" ]; then
106
+ PROVIDER="google"
107
+ API_KEY_VAR="$GEMINI_API_KEY"
108
+ BASE_URL_VAR="https://generativelanguage.googleapis.com/v1beta"
109
+ MODEL_VAR="$MODEL"
110
+ PRIMARY_MODEL="google/$MODEL_VAR"
111
+ API_TYPE="google-generative-ai"
112
+ else
113
+ PROVIDER="openai"
114
+ API_KEY_VAR="$OPENAI_API_KEY"
115
+ BASE_URL_VAR="$CLEAN_BASE"
116
+ MODEL_VAR="$MODEL"
117
+ PRIMARY_MODEL="openai/$MODEL_VAR"
118
+ API_TYPE="openai-completions"
119
+ fi
120
+
121
+ cat > /root/.openclaw/openclaw.json <<EOF2
122
+ {
123
+ "models": {
124
+ "providers": {
125
+ "$PROVIDER": {
126
+ "baseUrl": "$BASE_URL_VAR",
127
+ "apiKey": "$API_KEY_VAR",
128
+ "api": "$API_TYPE",
129
+ "models": [{ "id": "$MODEL_VAR", "name": "$MODEL_VAR", "contextWindow": 128000 }]
130
+ }
131
+ }
132
+ },
133
+ "agents": {
134
+ "defaults": {
135
+ "model": { "primary": "$PRIMARY_MODEL" },
136
+ "imageModel": { "primary": "openai/gpt-4o-mini" },
137
+ "tools": {
138
+ "elevated": {
139
+ "enabled": true,
140
+ "allowFrom": {
141
+ "openclaw-weixin": true
142
+ }
143
+ }
144
+ }
145
+ }
146
+ },
147
+ "gateway": {
148
+ "mode": "local",
149
+ "bind": "lan",
150
+ "port": $PORT,
151
+ "trustedProxies": ["0.0.0.0/0", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"],
152
+ "auth": { "mode": "token", "token": "$OPENCLAW_GATEWAY_PASSWORD" },
153
+ "controlUi": { "allowInsecureAuth": true, "allowedOrigins": ["*"] }
154
+ },
155
+ "plugins": { "allow": ["openclaw-weixin"] },
156
+ "channels": {
157
+ "openclaw-weixin": {
158
+ "enabled": true,
159
+ "dataDir": "/root/.openclaw/wechat-data",
160
+ "autoLogin": true
161
+ }
162
+ }
163
+ }
164
+ EOF2
165
+
166
+ echo "=== openclaw.json content ==="
167
+ cat /root/.openclaw/openclaw.json
168
+ echo "=== end ==="
169
+
170
+ openclaw doctor --fix
171
+
172
+ # 启动后台定时备份(每半小时)
173
+ (while true; do sleep 1800; python3 /usr/local/bin/sync.py backup; done) &
174
+
175
+ # 容器退出时执行一次备份
176
+ trap 'echo "Container stopping, running final backup..."; python3 /usr/local/bin/sync.py backup' EXIT
177
+
178
+ # 启动网关(前台运行)
179
+ exec openclaw gateway run --port $PORT
180
+ EOF
181
+
182
+ RUN chmod +x /usr/local/bin/start-openclaw
183
+
184
+ EXPOSE 7860
185
+ CMD ["/usr/local/bin/start-openclaw"]
README.md CHANGED
@@ -1,12 +1,9 @@
1
  ---
2
  title: Openclaw
3
- emoji: 🐨
4
- colorFrom: purple
5
- colorTo: blue
6
  sdk: docker
 
7
  pinned: false
8
- license: apache-2.0
9
- short_description: openclaw
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Openclaw
3
+ emoji: 🦞
4
+ colorFrom: pink
5
+ colorTo: red
6
  sdk: docker
7
+ port: 18789
8
  pinned: false
9
+ ---