Update Dockerfile
Browse files- Dockerfile +37 -33
Dockerfile
CHANGED
|
@@ -13,26 +13,26 @@ RUN pip3 install --no-cache-dir huggingface_hub --break-system-packages
|
|
| 13 |
|
| 14 |
RUN npm install -g openclaw@latest --unsafe-perm
|
| 15 |
|
|
|
|
| 16 |
RUN npx -y @tencent-weixin/openclaw-weixin-cli@latest install
|
| 17 |
|
| 18 |
ENV PORT=7860 \
|
| 19 |
OPENCLAW_GATEWAY_MODE=local \
|
| 20 |
HOME=/root
|
| 21 |
|
| 22 |
-
#
|
| 23 |
ENV OPENCLAW_DISABLE_TELEGRAM=1
|
| 24 |
|
| 25 |
-
# ========= 2.
|
| 26 |
RUN cat > /usr/local/bin/sync.py << 'SYNC_EOF'
|
| 27 |
-
import os, sys, tarfile
|
| 28 |
from huggingface_hub import HfApi, hf_hub_download
|
| 29 |
from datetime import datetime, timedelta
|
| 30 |
-
import subprocess
|
| 31 |
-
import time
|
| 32 |
|
| 33 |
api = HfApi()
|
| 34 |
repo_id = os.getenv("HF_DATASET")
|
| 35 |
token = os.getenv("HF_TOKEN")
|
|
|
|
| 36 |
|
| 37 |
def restore():
|
| 38 |
try:
|
|
@@ -48,13 +48,16 @@ def restore():
|
|
| 48 |
if name in files:
|
| 49 |
print(f"--- [SYNC] 发现备份文件: {name}, 正在下载... ---")
|
| 50 |
path = hf_hub_download(repo_id=repo_id, filename=name, repo_type="dataset", token=token)
|
|
|
|
|
|
|
| 51 |
with tarfile.open(path, "r:gz") as tar:
|
| 52 |
-
tar.extractall(path=
|
| 53 |
-
print(f"--- [SYNC] 恢复成功! 数据已覆盖至
|
| 54 |
return True
|
| 55 |
print("--- [SYNC] 未找到最近 5 天的备份包 ---")
|
| 56 |
except Exception as e:
|
| 57 |
print(f"--- [SYNC] 恢复异常: {e} ---")
|
|
|
|
| 58 |
|
| 59 |
def backup():
|
| 60 |
try:
|
|
@@ -63,11 +66,12 @@ def backup():
|
|
| 63 |
print(f"--- [SYNC] 正在执行全量备份: {name} ---")
|
| 64 |
with tarfile.open(name, "w:gz") as tar:
|
| 65 |
for target in ["sessions", "workspace", "agents", "memory", "openclaw.json", "wechat-data"]:
|
| 66 |
-
full_path =
|
| 67 |
if os.path.exists(full_path):
|
| 68 |
tar.add(full_path, arcname=target)
|
| 69 |
api.upload_file(path_or_fileobj=name, path_in_repo=name, repo_id=repo_id, repo_type="dataset", token=token)
|
| 70 |
print(f"--- [SYNC] 备份上传成功! ---")
|
|
|
|
| 71 |
for _ in range(3):
|
| 72 |
try:
|
| 73 |
result = subprocess.run(["openclaw", "wechat", "send", "--to=me", f"备份成功: {name}"], capture_output=True, timeout=10)
|
|
@@ -88,23 +92,23 @@ SYNC_EOF
|
|
| 88 |
|
| 89 |
RUN chmod +x /usr/local/bin/sync.py
|
| 90 |
|
| 91 |
-
# ========= 3. 启动脚本 start-openclaw
|
| 92 |
RUN cat > /usr/local/bin/start-openclaw << 'EOF'
|
| 93 |
#!/bin/bash
|
| 94 |
set -e
|
| 95 |
|
| 96 |
echo "Starting OpenClaw gateway..."
|
| 97 |
|
|
|
|
| 98 |
python3 /usr/local/bin/sync.py restore
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
rm -rf /root/.openclaw/agents/main/agent/channels/telegram
|
| 102 |
-
rm -rf /root/.openclaw/credentials/telegram
|
| 103 |
-
rm -rf /root/.openclaw/agents/main/agent/telegram*
|
| 104 |
rm -rf /root/.openclaw/agents/main/agent/channels/telegram*
|
|
|
|
| 105 |
rm -f /root/.openclaw/agents/main/agent/auth-profiles.json
|
| 106 |
find /root/.openclaw -name "*telegram*" -exec rm -rf {} + 2>/dev/null || true
|
| 107 |
|
|
|
|
| 108 |
mkdir -p /root/.openclaw/sessions
|
| 109 |
mkdir -p /root/.openclaw/workspace
|
| 110 |
mkdir -p /root/.openclaw/workspace/memory
|
|
@@ -113,24 +117,12 @@ touch /root/.openclaw/workspace/MEMORY.md
|
|
| 113 |
DATE=$(date +%Y-%m-%d)
|
| 114 |
touch /root/.openclaw/workspace/memory/$DATE.md
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
MODEL_VAR="$MODEL"
|
| 123 |
-
PRIMARY_MODEL="google/$MODEL_VAR"
|
| 124 |
-
API_TYPE="google-generative-ai"
|
| 125 |
-
else
|
| 126 |
-
PROVIDER="openai"
|
| 127 |
-
API_KEY_VAR="$OPENAI_API_KEY"
|
| 128 |
-
BASE_URL_VAR="$CLEAN_BASE"
|
| 129 |
-
MODEL_VAR="$MODEL"
|
| 130 |
-
PRIMARY_MODEL="openai/$MODEL_VAR"
|
| 131 |
-
API_TYPE="openai-completions"
|
| 132 |
-
fi
|
| 133 |
-
|
| 134 |
|
| 135 |
echo "=== openclaw.json content ==="
|
| 136 |
cat /root/.openclaw/openclaw.json
|
|
@@ -138,14 +130,26 @@ echo "=== end ==="
|
|
| 138 |
|
| 139 |
openclaw doctor --fix
|
| 140 |
|
| 141 |
-
# 启动定时备
|
| 142 |
(while true; do sleep 1800; python3 /usr/local/bin/sync.py backup; done) &
|
| 143 |
|
| 144 |
-
#
|
| 145 |
exec openclaw gateway run --port $PORT
|
| 146 |
EOF
|
| 147 |
|
| 148 |
RUN chmod +x /usr/local/bin/start-openclaw
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
EXPOSE 7860
|
| 151 |
CMD ["/usr/local/bin/start-openclaw"]
|
|
|
|
| 13 |
|
| 14 |
RUN npm install -g openclaw@latest --unsafe-perm
|
| 15 |
|
| 16 |
+
# 安装微信插件(若不需要可注释)
|
| 17 |
RUN npx -y @tencent-weixin/openclaw-weixin-cli@latest install
|
| 18 |
|
| 19 |
ENV PORT=7860 \
|
| 20 |
OPENCLAW_GATEWAY_MODE=local \
|
| 21 |
HOME=/root
|
| 22 |
|
| 23 |
+
# 防止自动启用 Telegram
|
| 24 |
ENV OPENCLAW_DISABLE_TELEGRAM=1
|
| 25 |
|
| 26 |
+
# ========= 2. 改进的 sync.py 备份恢复脚本 =========
|
| 27 |
RUN cat > /usr/local/bin/sync.py << 'SYNC_EOF'
|
| 28 |
+
import os, sys, tarfile, subprocess, time
|
| 29 |
from huggingface_hub import HfApi, hf_hub_download
|
| 30 |
from datetime import datetime, timedelta
|
|
|
|
|
|
|
| 31 |
|
| 32 |
api = HfApi()
|
| 33 |
repo_id = os.getenv("HF_DATASET")
|
| 34 |
token = os.getenv("HF_TOKEN")
|
| 35 |
+
DATA_DIR = "/root/.openclaw"
|
| 36 |
|
| 37 |
def restore():
|
| 38 |
try:
|
|
|
|
| 48 |
if name in files:
|
| 49 |
print(f"--- [SYNC] 发现备份文件: {name}, 正在下载... ---")
|
| 50 |
path = hf_hub_download(repo_id=repo_id, filename=name, repo_type="dataset", token=token)
|
| 51 |
+
# 使用 extractall 前确保目标目录存在
|
| 52 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 53 |
with tarfile.open(path, "r:gz") as tar:
|
| 54 |
+
tar.extractall(path=DATA_DIR)
|
| 55 |
+
print(f"--- [SYNC] 恢复成功! 数据已覆盖至 {DATA_DIR} ---")
|
| 56 |
return True
|
| 57 |
print("--- [SYNC] 未找到最近 5 天的备份包 ---")
|
| 58 |
except Exception as e:
|
| 59 |
print(f"--- [SYNC] 恢复异常: {e} ---")
|
| 60 |
+
return False
|
| 61 |
|
| 62 |
def backup():
|
| 63 |
try:
|
|
|
|
| 66 |
print(f"--- [SYNC] 正在执行全量备份: {name} ---")
|
| 67 |
with tarfile.open(name, "w:gz") as tar:
|
| 68 |
for target in ["sessions", "workspace", "agents", "memory", "openclaw.json", "wechat-data"]:
|
| 69 |
+
full_path = os.path.join(DATA_DIR, target)
|
| 70 |
if os.path.exists(full_path):
|
| 71 |
tar.add(full_path, arcname=target)
|
| 72 |
api.upload_file(path_or_fileobj=name, path_in_repo=name, repo_id=repo_id, repo_type="dataset", token=token)
|
| 73 |
print(f"--- [SYNC] 备份上传成功! ---")
|
| 74 |
+
# 可选:发送微信通知(需确保微信通道可用)
|
| 75 |
for _ in range(3):
|
| 76 |
try:
|
| 77 |
result = subprocess.run(["openclaw", "wechat", "send", "--to=me", f"备份成功: {name}"], capture_output=True, timeout=10)
|
|
|
|
| 92 |
|
| 93 |
RUN chmod +x /usr/local/bin/sync.py
|
| 94 |
|
| 95 |
+
# ========= 3. 启动脚本 start-openclaw =========
|
| 96 |
RUN cat > /usr/local/bin/start-openclaw << 'EOF'
|
| 97 |
#!/bin/bash
|
| 98 |
set -e
|
| 99 |
|
| 100 |
echo "Starting OpenClaw gateway..."
|
| 101 |
|
| 102 |
+
# 恢复备份数据(在创建新文件之前执行)
|
| 103 |
python3 /usr/local/bin/sync.py restore
|
| 104 |
|
| 105 |
+
# 清理 Telegram 残留(若需要)
|
|
|
|
|
|
|
|
|
|
| 106 |
rm -rf /root/.openclaw/agents/main/agent/channels/telegram*
|
| 107 |
+
rm -rf /root/.openclaw/credentials/telegram*
|
| 108 |
rm -f /root/.openclaw/agents/main/agent/auth-profiles.json
|
| 109 |
find /root/.openclaw -name "*telegram*" -exec rm -rf {} + 2>/dev/null || true
|
| 110 |
|
| 111 |
+
# 确保必要目录存在(不会覆盖已恢复的数据)
|
| 112 |
mkdir -p /root/.openclaw/sessions
|
| 113 |
mkdir -p /root/.openclaw/workspace
|
| 114 |
mkdir -p /root/.openclaw/workspace/memory
|
|
|
|
| 117 |
DATE=$(date +%Y-%m-%d)
|
| 118 |
touch /root/.openclaw/workspace/memory/$DATE.md
|
| 119 |
|
| 120 |
+
# 更健壮的 API Base 处理:通过环境变量直接指定,不在脚本中做字符串替换
|
| 121 |
+
# 你需要在 openclaw.json 中使用正确的 baseUrl,这里只做兜底
|
| 122 |
+
echo "当前配置的环境变量:"
|
| 123 |
+
echo "OPENAI_API_BASE=$OPENAI_API_BASE"
|
| 124 |
+
echo "GEMINI_API_KEY=$GEMINI_API_KEY"
|
| 125 |
+
echo "MODEL=$MODEL"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
echo "=== openclaw.json content ==="
|
| 128 |
cat /root/.openclaw/openclaw.json
|
|
|
|
| 130 |
|
| 131 |
openclaw doctor --fix
|
| 132 |
|
| 133 |
+
# 启动定时备���(后台,每30分钟)
|
| 134 |
(while true; do sleep 1800; python3 /usr/local/bin/sync.py backup; done) &
|
| 135 |
|
| 136 |
+
# 前台运行 gateway
|
| 137 |
exec openclaw gateway run --port $PORT
|
| 138 |
EOF
|
| 139 |
|
| 140 |
RUN chmod +x /usr/local/bin/start-openclaw
|
| 141 |
|
| 142 |
+
# ========= 4. 导入配置文件(关键步骤) =========
|
| 143 |
+
# ⚠️ 重要安全提醒:
|
| 144 |
+
# 请将你本地配置好的 openclaw.json(包含模型和通道设置,但建议使用占位符密钥)
|
| 145 |
+
# 放到与 Dockerfile 同一目录下,然后取消下面这行的注释。
|
| 146 |
+
# 切勿将包含真实 API Key 的 openclaw.json 提交到公开仓库!
|
| 147 |
+
# ------------------------------------------------
|
| 148 |
+
# RUN mkdir -p /root/.openclaw
|
| 149 |
+
# COPY openclaw.json /root/.openclaw/openclaw.json
|
| 150 |
+
# ------------------------------------------------
|
| 151 |
+
# 你可以改用环境变量动态生成配置文件,或者在 Hugging Face Spaces 的 Settings 中
|
| 152 |
+
# 通过 Repository secrets 注入完整的 openclaw.json 内容(需要额外脚本处理)。
|
| 153 |
+
|
| 154 |
EXPOSE 7860
|
| 155 |
CMD ["/usr/local/bin/start-openclaw"]
|