Files
OpenMesh/install-windows.ps1

283 lines
8.3 KiB
PowerShell
Raw Permalink Normal View History

#Requires -Version 5.1
<#
.SYNOPSIS
OpenWorker Windows 单机版安装脚本
.DESCRIPTION
自动完成以下任务:
1. 检查 Docker Desktop 是否安装
2. 创建必要的目录结构
3. 生成配置文件 (包含自定义模型 smesh-smartops)
4. 构建并启动 Docker 容器
.PARAMETER ApiKey
模型 API Key (默认: your_api_key_here)
.PARAMETER ModelEndpoint
模型服务地址 (默认: http://114.242.42.23:9998/v1)
.PARAMETER ModelName
模型名称 (默认: smesh-smartops)
.PARAMETER Port
服务端口 (默认: 8765)
.PARAMETER WorkspacePath
工作目录路径 (默认: C:\openworker\workspace)
.EXAMPLE
.\install-windows.ps1
.\install-windows.ps1 -ApiKey "sk-xxxx" -ModelName "my-model"
#>
[CmdletBinding()]
param(
[string]$ApiKey = "your_api_key_here",
[string]$ModelEndpoint = "http://114.242.42.23:9998/v1",
[string]$ModelName = "smesh-smartops",
[int]$Port = 8765,
[string]$WorkspacePath = "C:\openworker\workspace"
)
$ErrorActionPreference = 'Stop'
$InstallDir = $PSScriptRoot
# 颜色定义
function Write-Step($message) {
Write-Host "[步骤] $message" -ForegroundColor Cyan
}
function Write-Success($message) {
Write-Host "[成功] $message" -ForegroundColor Green
}
function Write-Error($message) {
Write-Host "[错误] $message" -ForegroundColor Red
}
function Write-Info($message) {
Write-Host "[信息] $message" -ForegroundColor Yellow
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Magenta
Write-Host " OpenWorker Windows 单机版安装程序" -ForegroundColor Magenta
Write-Host "========================================" -ForegroundColor Magenta
Write-Host ""
# ---------------------------------------------------------------------------
# 1. 检查 Docker Desktop
# ---------------------------------------------------------------------------
Write-Step "检查 Docker Desktop 是否已安装..."
$docker = Get-Command docker -ErrorAction SilentlyContinue
if (-not $docker) {
Write-Error "Docker Desktop 未安装或未在 PATH 中"
Write-Host ""
Write-Host "请先安装 Docker Desktop: https://www.docker.com/products/docker-desktop/"
Write-Host "安装完成后,请重启终端并重新运行此脚本"
exit 1
}
Write-Success "Docker 已安装"
# 检查 Docker 是否运行
Write-Step "检查 Docker 服务状态..."
try {
$dockerInfo = docker info 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Error "Docker 服务未运行"
Write-Host "请启动 Docker Desktop 后重新运行此脚本"
exit 1
}
} catch {
Write-Error "无法连接到 Docker 服务"
Write-Host "请启动 Docker Desktop 后重新运行此脚本"
exit 1
}
Write-Success "Docker 服务运行正常"
# ---------------------------------------------------------------------------
# 2. 创建目录结构
# ---------------------------------------------------------------------------
Write-Step "创建目录结构..."
$dirs = @(
$WorkspacePath,
(Join-Path $InstallDir "config"),
(Join-Path $InstallDir "data")
)
foreach ($dir in $dirs) {
if (-not (Test-Path $dir)) {
New-Item -ItemType Directory -Force -Path $dir | Out-Null
Write-Host " 创建: $dir" -ForegroundColor Gray
}
}
Write-Success "目录结构创建完成"
# ---------------------------------------------------------------------------
# 3. 生成配置文件
# ---------------------------------------------------------------------------
Write-Step "生成配置文件..."
$configContent = @"
# OpenWorker 配置文件
# 此文件由 install-windows.ps1 自动生成
# 默认模型
model = `"$ModelName`"
# 运行模式: interactive | plan | auto | custom
mode = `"interactive`"
# 最大迭代次数
max_iterations = 12
# 允许的命令 (无需审批)
allowed_commands = [
`"ls`", `"cat`", `"pwd`", `"grep`", `"find`",
`"git status`", `"git diff`", `"git log`",
`"python`", `"python3`", `"node`", `"npm`",
]
# 自动允许的工具 (在 custom 模式下)
auto_allow = [`"write_file`", `"replace_in_file`", `"apply_patch`"]
# 服务器配置
host = `"0.0.0.0`"
port = $Port
[provider.smesh]
# 自定义模型配置 (使用 smesh-smartops)
api_key = `"$ApiKey`"
base_url = `"$ModelEndpoint`"
"@
$configPath = Join-Path $InstallDir "config\config.toml"
Set-Content -Path $configPath -Value $configContent -Encoding UTF8
Write-Host " 配置文件: $configPath" -ForegroundColor Gray
Write-Success "配置文件生成完成"
# ---------------------------------------------------------------------------
# 4. 创建 .env 文件
# ---------------------------------------------------------------------------
Write-Step "创建环境变量文件..."
$envContent = @"
# OpenWorker 环境变量
# 此文件由 install-windows.ps1 自动生成
# 模型配置
OPENWORKER_MODEL=$ModelName
OPENWORKER_MODE=interactive
# 自定义模型 API
SMESH_API_BASE=$ModelEndpoint
SMESH_API_KEY=$ApiKey
# 服务端口
OPENWORKER_PORT=$Port
"@
$envPath = Join-Path $InstallDir ".env"
Set-Content -Path $envPath -Value $envContent -Encoding UTF8
Write-Host " 环境变量文件: $envPath" -ForegroundColor Gray
Write-Success "环境变量文件创建完成"
# ---------------------------------------------------------------------------
# 5. 构建 Docker 镜像
# ---------------------------------------------------------------------------
Write-Step "构建 Docker 镜像 (首次安装可能需要几分钟)..."
Push-Location $InstallDir
try {
docker build -f Dockerfile.windows -t openworker/server:windows-latest .
if ($LASTEXITCODE -ne 0) {
throw "Docker 构建失败"
}
} finally {
Pop-Location
}
Write-Success "Docker 镜像构建完成"
# ---------------------------------------------------------------------------
# 6. 启动服务
# ---------------------------------------------------------------------------
Write-Step "启动 OpenWorker 服务..."
# 先停止已存在的容器
$existingContainer = docker ps -a --filter "name=openworker-server" --format "{{.Names}}"
if ($existingContainer) {
Write-Info "停止已存在的容器..."
docker stop openworker-server 2>$null
docker rm openworker-server 2>$null
}
# 启动新容器
docker run -d `
--name openworker-server `
--restart unless-stopped `
-p ${Port}:8765 `
-v `"$((Join-Path $InstallDir 'config').Replace('\', '/')):/app/config:ro`" `
-v `"$($WorkspacePath.Replace('\', '/')):/workspace`" `
-v openworker-data:/app/data `
openworker/server:windows-latest
if ($LASTEXITCODE -ne 0) {
Write-Error "服务启动失败"
exit 1
}
Write-Success "服务启动完成"
# ---------------------------------------------------------------------------
# 7. 等待服务就绪
# ---------------------------------------------------------------------------
Write-Step "等待服务就绪 (最多等待 30 秒)..."
$maxWait = 30
$waited = 0
$ready = $false
while ($waited -lt $maxWait) {
Start-Sleep -Seconds 2
$waited += 2
try {
$response = Invoke-WebRequest -Uri "http://localhost:$Port/health" -UseBasicParsing -TimeoutSec 5 -ErrorAction SilentlyContinue
if ($response.StatusCode -eq 200) {
$ready = $true
break
}
} catch {
Write-Host "." -NoNewline -ForegroundColor Gray
}
}
Write-Host ""
if ($ready) {
Write-Success "服务已就绪!"
} else {
Write-Info "服务启动中 (健康检查可能需要更长时间)"
}
# ---------------------------------------------------------------------------
# 完成
# ---------------------------------------------------------------------------
Write-Host ""
Write-Host "========================================" -ForegroundColor Magenta
Write-Host " 安装完成!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Magenta
Write-Host ""
Write-Host "访问地址: http://localhost:$Port" -ForegroundColor White
Write-Host ""
Write-Host "常用命令:" -ForegroundColor White
Write-Host " 查看日志: docker logs -f openworker-server" -ForegroundColor Gray
Write-Host " 停止服务: docker stop openworker-server" -ForegroundColor Gray
Write-Host " 启动服务: docker start openworker-server" -ForegroundColor Gray
Write-Host " 重新安装: .\install-windows.ps1" -ForegroundColor Gray
Write-Host ""
Write-Host "配置文件: $configPath" -ForegroundColor Gray
Write-Host "工作目录: $WorkspacePath" -ForegroundColor Gray
Write-Host ""