-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdocker-setup.bat
More file actions
146 lines (128 loc) · 8.25 KB
/
docker-setup.bat
File metadata and controls
146 lines (128 loc) · 8.25 KB
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
@echo off
chcp 65001 >nul
setlocal
cd /d "%~dp0"
REM 若没有 .env,从示例复制
if not exist .env (
copy .env.example .env >nul
echo [docker-setup] 已创建 .env(可从 .env.example 修改)
)
REM 若 .env 中 OPENCLAW_GATEWAY_TOKEN 为空,用 PowerShell 生成并写回
powershell -NoProfile -Command "$c = Get-Content .env -Raw -ErrorAction SilentlyContinue; if ($c -and $c -notmatch 'OPENCLAW_GATEWAY_TOKEN=.+') { $b = New-Object byte[] 24; (New-Object Security.Cryptography.RNGCryptoServiceProvider).GetBytes($b); $t = -join ($b | ForEach-Object { $_.ToString('x2') }); if ($c -match 'OPENCLAW_GATEWAY_TOKEN=') { $c = $c -replace 'OPENCLAW_GATEWAY_TOKEN=.*', \"OPENCLAW_GATEWAY_TOKEN=$t\" } else { $c = $c.TrimEnd() + \"`nOPENCLAW_GATEWAY_TOKEN=$t`n\" }; Set-Content .env $c -NoNewline; Write-Host '[docker-setup] 已生成 OPENCLAW_GATEWAY_TOKEN 并写入 .env' }"
REM 读取 Gateway token(用于后续配置)
for /f "tokens=2 delims==" %%a in ('findstr /b "OPENCLAW_GATEWAY_TOKEN=" .env 2^>nul') do set GATEWAY_TOKEN=%%a
set GATEWAY_TOKEN=%GATEWAY_TOKEN:"=%
REM 读取数据目录(支持 .env 自定义)
for /f "tokens=2 delims==" %%a in ('findstr /b "OPENCLAW_CONFIG_DIR=" .env 2^>nul') do set OPENCLAW_CONFIG_DIR=%%a
set OPENCLAW_CONFIG_DIR=%OPENCLAW_CONFIG_DIR:"=%
if not defined OPENCLAW_CONFIG_DIR set OPENCLAW_CONFIG_DIR=data\openclaw
for /f "tokens=2 delims==" %%a in ('findstr /b "OPENCLAW_WORKSPACE_DIR=" .env 2^>nul') do set OPENCLAW_WORKSPACE_DIR=%%a
set OPENCLAW_WORKSPACE_DIR=%OPENCLAW_WORKSPACE_DIR:"=%
if not defined OPENCLAW_WORKSPACE_DIR set OPENCLAW_WORKSPACE_DIR=data\workspace
REM 确保数据目录存在,包括 extensions 子目录
if not exist "%OPENCLAW_CONFIG_DIR%" mkdir "%OPENCLAW_CONFIG_DIR%"
if not exist "%OPENCLAW_CONFIG_DIR%\\extensions" mkdir "%OPENCLAW_CONFIG_DIR%\\extensions"
if not exist "%OPENCLAW_WORKSPACE_DIR%" mkdir "%OPENCLAW_WORKSPACE_DIR%"
REM 检测并转换宿主机代理地址(容器内可访问)
REM Windows/macOS 使用 host.docker.internal,Linux 也尝试使用(Docker 20.10+ 支持)
REM 检测宿主机代理(优先从环境变量,其次从 .env)
set CONTAINER_HTTP_PROXY=
if defined HTTP_PROXY (
set HOST_PROXY=%HTTP_PROXY%
) else if defined http_proxy (
set HOST_PROXY=%http_proxy%
) else (
for /f "tokens=2 delims==" %%a in ('findstr /b "HTTP_PROXY=" .env 2^>nul') do set HOST_PROXY=%%a
set HOST_PROXY=%HOST_PROXY:"=%
)
if defined HOST_PROXY (
echo %HOST_PROXY% | findstr /i "127.0.0.1 localhost" >nul
if not errorlevel 1 (
REM 将 127.0.0.1 或 localhost 替换为 host.docker.internal
for /f "delims=" %%p in ('powershell -NoProfile -Command "$p='%HOST_PROXY%'; $p -replace '127.0.0.1','host.docker.internal' -replace 'localhost','host.docker.internal'"') do set CONTAINER_HTTP_PROXY=%%p
) else (
set CONTAINER_HTTP_PROXY=%HOST_PROXY%
)
)
set CONTAINER_HTTPS_PROXY=%CONTAINER_HTTP_PROXY%
REM 自动检测并写入运行时代理配置到 .env
if defined CONTAINER_HTTP_PROXY (
echo [docker-setup] 检测到代理配置: %CONTAINER_HTTP_PROXY%
echo [docker-setup] 自动配置运行时代理(已转换为容器可访问地址)...
powershell -NoProfile -Command "$c = Get-Content .env -Raw -ErrorAction SilentlyContinue; if (-not $c) { $c = '' }; $c = $c -replace '(?m)^OPENCLAW_RUNTIME_HTTP_PROXY=.*', \"OPENCLAW_RUNTIME_HTTP_PROXY=%CONTAINER_HTTP_PROXY%\"; if ($c -notmatch '(?m)^OPENCLAW_RUNTIME_HTTP_PROXY=') { $c = $c.TrimEnd() + \"`nOPENCLAW_RUNTIME_HTTP_PROXY=%CONTAINER_HTTP_PROXY%`n\" }; $c = $c -replace '(?m)^OPENCLAW_RUNTIME_HTTPS_PROXY=.*', \"OPENCLAW_RUNTIME_HTTPS_PROXY=%CONTAINER_HTTPS_PROXY%\"; if ($c -notmatch '(?m)^OPENCLAW_RUNTIME_HTTPS_PROXY=') { $c = $c.TrimEnd() + \"`nOPENCLAW_RUNTIME_HTTPS_PROXY=%CONTAINER_HTTPS_PROXY%`n\" }; $c = $c -replace '(?m)^OPENCLAW_RUNTIME_ALL_PROXY=.*', \"OPENCLAW_RUNTIME_ALL_PROXY=%CONTAINER_HTTP_PROXY%\"; if ($c -notmatch '(?m)^OPENCLAW_RUNTIME_ALL_PROXY=') { $c = $c.TrimEnd() + \"`nOPENCLAW_RUNTIME_ALL_PROXY=%CONTAINER_HTTP_PROXY%`n\" }; Set-Content .env $c -NoNewline"
) else (
echo [docker-setup] 未检测到代理配置,将不使用代理运行
echo [docker-setup] 提示: 如需配置代理,可在 .env 中设置 OPENCLAW_RUNTIME_HTTP_PROXY 等变量
powershell -NoProfile -Command "$c = Get-Content .env -Raw -ErrorAction SilentlyContinue; if (-not $c) { $c = '' }; $c = $c -replace '(?m)^OPENCLAW_RUNTIME_HTTP_PROXY=.*', \"OPENCLAW_RUNTIME_HTTP_PROXY=\"; if ($c -notmatch '(?m)^OPENCLAW_RUNTIME_HTTP_PROXY=') { $c = $c.TrimEnd() + \"`nOPENCLAW_RUNTIME_HTTP_PROXY=`n\" }; $c = $c -replace '(?m)^OPENCLAW_RUNTIME_HTTPS_PROXY=.*', \"OPENCLAW_RUNTIME_HTTPS_PROXY=\"; if ($c -notmatch '(?m)^OPENCLAW_RUNTIME_HTTPS_PROXY=') { $c = $c.TrimEnd() + \"`nOPENCLAW_RUNTIME_HTTPS_PROXY=`n\" }; Set-Content .env $c -NoNewline"
)
REM 供 CLI 使用的代理环境
if defined CONTAINER_HTTP_PROXY (
set PROXY_ENV=-e HTTP_PROXY=%CONTAINER_HTTP_PROXY% -e HTTPS_PROXY=%CONTAINER_HTTPS_PROXY% -e http_proxy=%CONTAINER_HTTP_PROXY% -e https_proxy=%CONTAINER_HTTPS_PROXY% -e NO_PROXY=localhost,127.0.0.1,::1 -e no_proxy=localhost,127.0.0.1,::1
) else (
set PROXY_ENV=-e HTTP_PROXY= -e HTTPS_PROXY= -e http_proxy= -e https_proxy= -e NO_PROXY=* -e no_proxy=*
)
REM 使用 npm 安装 OpenClaw,无需克隆源码(镜像构建时 npm install -g openclaw)
echo [docker-setup] 构建镜像(首次较慢,将从 npm 安装 OpenClaw)...
docker compose build
if errorlevel 1 exit /b 1
echo [docker-setup] 启动 Gateway...
docker compose up -d openclaw-gateway
if errorlevel 1 exit /b 1
REM 自动执行 onboarding(若配置不存在)
if not exist "%OPENCLAW_CONFIG_DIR%\\openclaw.json" (
echo [docker-setup] 检测到首次运行,执行自动配置(onboarding)...
REM 等待 Gateway 完全就绪
echo [docker-setup] 等待 Gateway 完全就绪...
timeout /t 10 /nobreak >nul
REM 创建最小配置文件(使用 gateway.auth.token 新格式)
REM 添加 gateway.mode=local 避免 Gateway 因缺少模式而反复重启
echo [docker-setup] 创建最小配置文件...
if not exist "%OPENCLAW_CONFIG_DIR%" mkdir "%OPENCLAW_CONFIG_DIR%"
(
echo {
echo "gateway": {
echo "mode": "local",
echo "auth": {
if defined GATEWAY_TOKEN (
echo "token": "%GATEWAY_TOKEN%"
) else (
echo "token": ""
)
echo }
echo }
echo }
) > "%OPENCLAW_CONFIG_DIR%\\openclaw.json"
REM 如果有 token,通过 CLI 设置(使用新格式)
if defined GATEWAY_TOKEN (
echo [docker-setup] 设置 Gateway token...
docker compose run --rm %PROXY_ENV% openclaw-cli config set gateway.auth.token "%GATEWAY_TOKEN%" >nul 2>&1
)
REM Windows 下通常有交互终端,使用 -it 参数交互执行 onboarding
echo [docker-setup] 启动配置向导(onboarding)...
echo [docker-setup] 提示: 可按 Ctrl+C 跳过,稍后手动执行: docker compose run --rm -it openclaw-cli onboard
docker compose run --rm -it -e OPENCLAW_GATEWAY_TOKEN="%GATEWAY_TOKEN%" %PROXY_ENV% openclaw-cli onboard
if errorlevel 1 (
echo [docker-setup] Onboarding 已取消或失败
echo [docker-setup] Gateway 已使用最小配置启动,可通过 Control UI 或 CLI 完善配置
)
) else (
echo [docker-setup] 检测到已有配置文件,跳过 onboarding
REM 自动迁移旧配置格式(gateway.token -> gateway.auth.token)
echo [docker-setup] 检查并迁移旧配置格式...
powershell -NoProfile -Command "$p = '%OPENCLAW_CONFIG_DIR%\\openclaw.json'; if (Test-Path $p) { $data = Get-Content $p -Raw | ConvertFrom-Json; if ($data.gateway.token -and -not $data.gateway.auth.token) { $data.gateway.auth = @{token = $data.gateway.token}; $data.gateway.PSObject.Properties.Remove('token'); $data | ConvertTo-Json -Depth 10 | Set-Content $p -NoNewline; Write-Host '[docker-setup] 配置已迁移到 gateway.auth.token'; exit 0 } else { exit 1 } } else { exit 1 }" 2>nul
if not errorlevel 1 (
echo [docker-setup] 重启 Gateway 使新配置生效...
docker compose restart openclaw-gateway >nul 2>&1
timeout /t 3 /nobreak >nul
)
)
echo.
echo ✅ Gateway 已启动并配置完成!
echo - Control UI: http://127.0.0.1:18789/
if defined GATEWAY_TOKEN (
echo - Gateway Token: %GATEWAY_TOKEN%
)
echo.
echo 后续配置:
echo - 查看日志: docker compose logs -f openclaw-gateway
endlocal