-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstart.ps1
More file actions
164 lines (138 loc) · 7.1 KB
/
Copy pathstart.ps1
File metadata and controls
164 lines (138 loc) · 7.1 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# start.ps1 - Ultimate Environment Setup Wizard & Starter (v15.0 - Configurable)
# NOTE: Increased resource limits to support Node.js, LibreOffice, and Playwright
# TODO: [OPTIMIZATION] Consider adding -Lightweight switch for minimal deployments
[CmdletBinding()]
param(
# 自定义 API Key (留空则自动生成)
[string]$ApiKey = "",
# 最小空闲工作实例数 (reduced from 15 to 10 due to higher per-worker resource requirements)
[int]$MinIdleWorkers = 10,
# 允许创建的最大工作实例总数
[int]$MaxTotalWorkers = 50,
# 每个工作实例的CPU核心数限制 (increased from 1.0 to 1.5 for LibreOffice/Playwright)
[double]$WorkerCPU = 1.5,
# 每个工作实例的内存限制 (increased from 1024 to 1536 for Node.js/LibreOffice/Playwright)
[int]$WorkerRAM_MB = 1536,
# 每个工作实例的虚拟磁盘大小 (单位: MB)
[int]$WorkerDisk_MB = 500,
# 启用 Worker 联网访问 (安全风险!)
[switch]$EnableInternet = $false
)
# --- 自我提升为管理员权限 ---
$currentUser = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Host "This script needs Administrator privileges to manage Docker." -ForegroundColor Yellow
Start-Process powershell.exe -Verb RunAs -ArgumentList "-NoExit", "-File", "`"$PSCommandPath`""
exit
}
$ErrorActionPreference = "Stop"
# ==============================================================================
# 将脚本参数设置为环境变量,以便 Docker Compose 可以使用它们
# ==============================================================================
$env:MIN_IDLE_WORKERS = $MinIdleWorkers
$env:MAX_TOTAL_WORKERS = $MaxTotalWorkers
$env:WORKER_CPU = $WorkerCPU
$env:WORKER_RAM_MB = $WorkerRAM_MB
$env:WORKER_MAX_DISK_SIZE_MB = $WorkerDisk_MB
$env:WORKER_INTERNET_ACCESS = if ($EnableInternet) { "true" } else { "false" }
# 设置自定义 API Key(如果提供)
if (-not [string]::IsNullOrWhiteSpace($ApiKey)) {
$env:AUTH_TOKEN = $ApiKey
}
Write-Host "`n⚙️ Applying Configuration:" -ForegroundColor Cyan
if (-not [string]::IsNullOrWhiteSpace($ApiKey)) {
Write-Host " - API Key : (custom)" -ForegroundColor Yellow
} else {
Write-Host " - API Key : (auto-generated)"
}
Write-Host " - Min Idle Workers : $env:MIN_IDLE_WORKERS"
Write-Host " - Max Total Workers : $env:MAX_TOTAL_WORKERS"
Write-Host " - Worker CPU Limit : $env:WORKER_CPU cores"
Write-Host " - Worker RAM Limit : $env:WORKER_RAM_MB MB"
Write-Host " - Worker Disk Size : $env:WORKER_MAX_DISK_SIZE_MB MB"
Write-Host " - Worker Internet Access: $env:WORKER_INTERNET_ACCESS"
# Security warning for internet access
if ($EnableInternet) {
Write-Host ""
Write-Host "WARNING: Internet access is ENABLED for workers!" -ForegroundColor Red
Write-Host " Security risks include:" -ForegroundColor Yellow
Write-Host " - Attackers may download malicious tools" -ForegroundColor Yellow
Write-Host " - Attackers may establish reverse shells" -ForegroundColor Yellow
Write-Host " - Attackers may abuse network for attacks (DDoS, scanning, etc.)" -ForegroundColor Yellow
Write-Host " - Private IPs are blocked, but public internet is fully accessible" -ForegroundColor Yellow
Write-Host " By continuing, you accept all security consequences." -ForegroundColor Yellow
Write-Host ""
}
# ==============================================================================
# PHASE 1: 验证环境
# ==============================================================================
Write-Host "`n🔎 [Phase 1/3] Validating Your Environment..." -ForegroundColor Cyan
function Test-Docker-Environment {
try { wsl.exe --status >$null 2>$null; if ($LASTEXITCODE -ne 0) { throw "WSL is not installed." } } catch { return "WslNotInstalled" }
docker version >$null 2>$null
if ($LASTEXITCODE -ne 0) {
if (Get-Command docker -ErrorAction SilentlyContinue) { return "DockerNotRunning" }
else { return "DockerNotInstalled" }
}
return "OK"
}
while ($true) {
$envStatus = Test-Docker-Environment
if ($envStatus -eq "OK") { Write-Host " -> ✅ Environment is ready!"; break }
Write-Host "`n⚠️ ACTION REQUIRED:" -ForegroundColor Yellow
switch ($envStatus) {
"WslNotInstalled" { Write-Host " Please enable WSL: Open Admin PowerShell, run 'wsl --install', then REBOOT." }
"DockerNotInstalled" { Write-Host " Please install Docker Desktop from https://www.docker.com" }
"DockerNotRunning" { Write-Host " Please start Docker Desktop and wait for it to initialize." }
}
Read-Host -Prompt "Press Enter to re-check"
}
# ==============================================================================
# PHASE 2: 启动应用
# ==============================================================================
Write-Host "`n🚀 [Phase 2/3] Starting application via Docker Compose..." -ForegroundColor Cyan
try {
$containerName = "code-interpreter_gateway"
$token = ""
$gatewayId = docker ps -q --filter "name=^$containerName$"
if (-not [string]::IsNullOrWhiteSpace($gatewayId)) {
Write-Host "✅ The gateway is already running."
$token = (docker exec $containerName cat /gateway/auth_token.txt).Trim()
} else {
Write-Host " -> Container is not running. Building and starting services..."
docker-compose up --build -d
$builderId = docker ps -a -q --filter "name=code-interpreter_worker_builder"
if (-not [string]::IsNullOrWhiteSpace($builderId)) {
Write-Host "🧹 Cleaning up the temporary builder container..."
docker rm $builderId > $null
}
Write-Host -n "🔑 Waiting for Gateway to generate the Auth Token..."
$i = 1
while ($i -le 30) {
try {
$tokenOutput = docker exec $containerName cat /gateway/auth_token.txt 2>$null
if ($LASTEXITCODE -eq 0 -and -not [string]::IsNullOrWhiteSpace($tokenOutput)) {
$token = $tokenOutput.Trim(); Write-Host ""; break
}
} catch {}
Write-Host -n "."; Start-Sleep -Seconds 1; $i++
}
if ([string]::IsNullOrWhiteSpace($token)) {
throw "Timed out waiting for the Auth Token."
}
}
Write-Host "`n🎉 Startup complete. The system is ready." -ForegroundColor Green
Write-Host " Your Auth Token is: $token"
}
catch {
Write-Host "`n❌ An error occurred during startup: $($_.Exception.Message)" -ForegroundColor Red
Write-Host " Showing last 50 lines of gateway logs:" -ForegroundColor Yellow
try { docker-compose logs --tail=50 code-interpreter_gateway } catch {}
Read-Host -Prompt "Press Enter to exit"
exit 1
}
# ==============================================================================
# PHASE 3: 完成
# ==============================================================================
Write-Host "`n✅ [Phase 3/3] The application has been started successfully." -ForegroundColor Green