forked from ownpilot/OwnPilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ps1
More file actions
324 lines (273 loc) · 11.4 KB
/
setup.ps1
File metadata and controls
324 lines (273 loc) · 11.4 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#Requires -Version 5.1
<#
.SYNOPSIS
OwnPilot Interactive Setup Wizard for Windows
.DESCRIPTION
Walks through the full setup interactively: prerequisites, config, .env generation,
Docker PostgreSQL, dependency install, and build.
#>
$ErrorActionPreference = "Stop"
# ─── Helpers ──────────────────────────────────────────────────────────────────
function Write-Info($msg) { Write-Host " i $msg" -ForegroundColor Blue }
function Write-Ok($msg) { Write-Host " + $msg" -ForegroundColor Green }
function Write-Warn($msg) { Write-Host " ! $msg" -ForegroundColor Yellow }
function Write-Fail($msg) { Write-Host " x $msg" -ForegroundColor Red }
function Write-Section($title) {
Write-Host ""
Write-Host "--- $title ---" -ForegroundColor Blue -NoNewline:$false
Write-Host ""
}
function Ask {
param(
[string]$Prompt,
[string]$Default
)
$display = if ($Default) { "[$Default]" } else { "[]" }
$input = Read-Host " ? $Prompt $display"
if ([string]::IsNullOrWhiteSpace($input)) { return $Default }
return $input
}
function Ask-Secret {
param(
[string]$Prompt,
[string]$Default
)
$display = if ($Default) { "[$Default]" } else { "[]" }
$secure = Read-Host " ? $Prompt $display" -AsSecureString
$plain = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
[Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
)
if ([string]::IsNullOrWhiteSpace($plain)) { return $Default }
return $plain
}
function Ask-Choice {
param(
[string]$Prompt,
[string]$Default,
[string[]]$Options
)
Write-Host " ? $Prompt" -ForegroundColor Cyan
foreach ($opt in $Options) {
$marker = if ($opt -eq $Default) { " -> " } else { " " }
Write-Host "$marker$opt"
}
$input = Read-Host " Choose [$Default]"
if ([string]::IsNullOrWhiteSpace($input)) { return $Default }
return $input
}
function Generate-RandomHex([int]$Length = 32) {
$bytes = New-Object byte[] ($Length / 2)
[Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)
return ($bytes | ForEach-Object { $_.ToString("x2") }) -join ''
}
# ─── Banner ───────────────────────────────────────────────────────────────────
Clear-Host
Write-Host @"
___ ____ _ _ _
/ _ \__ ___ __ | _ \(_) | ___ | |_
| | | \ \ /\ / / '_ \| |_) | | |/ _ \| __|
| |_| |\ V V /| | | | __/| | | (_) | |_
\___/ \_/\_/ |_| |_|_| |_|_|\___/ \__|
"@ -ForegroundColor Cyan
Write-Host " Privacy-first personal AI assistant" -ForegroundColor White
Write-Host " Interactive Setup Wizard"
Write-Host ""
# ─── Idempotency check ───────────────────────────────────────────────────────
if (Test-Path ".env") {
Write-Warn "An existing .env file was found."
$overwrite = Read-Host " ? Overwrite it? (y/N)"
if ($overwrite -notmatch '^[Yy]$') {
Write-Info "Setup cancelled. Your .env was not modified."
exit 0
}
Write-Host ""
}
# ─── Prerequisites ────────────────────────────────────────────────────────────
Write-Section "Checking Prerequisites"
$prereqsOk = $true
# Node.js
if (Get-Command node -ErrorAction SilentlyContinue) {
$nodeVersion = (node -v).TrimStart('v')
$nodeMajor = [int]($nodeVersion.Split('.')[0])
if ($nodeMajor -ge 22) {
Write-Ok "Node.js v$nodeVersion"
} else {
Write-Fail "Node.js v$nodeVersion found - v22+ required"
$prereqsOk = $false
}
} else {
Write-Fail "Node.js not found - install v22+ from https://nodejs.org"
$prereqsOk = $false
}
# pnpm
if (Get-Command pnpm -ErrorAction SilentlyContinue) {
$pnpmVersion = pnpm -v
$pnpmMajor = [int]($pnpmVersion.Split('.')[0])
if ($pnpmMajor -ge 10) {
Write-Ok "pnpm v$pnpmVersion"
} else {
Write-Fail "pnpm v$pnpmVersion found - v10+ required"
$prereqsOk = $false
}
} else {
Write-Fail "pnpm not found - install with: npm install -g pnpm"
$prereqsOk = $false
}
# Docker
$hasDocker = $false
if (Get-Command docker -ErrorAction SilentlyContinue) {
$dockerVersion = (docker --version) -replace '.*?(\d+\.\d+\.\d+).*', '$1'
Write-Ok "Docker v$dockerVersion"
$hasDocker = $true
} else {
Write-Warn "Docker not found - needed if you want to run PostgreSQL in a container"
}
# Docker Compose
$hasCompose = $false
if ($hasDocker) {
try {
$composeOut = docker compose version 2>&1
if ($LASTEXITCODE -eq 0) {
$composeVersion = ($composeOut -replace '.*?(\d+\.\d+\.\d+).*', '$1')
Write-Ok "Docker Compose v$composeVersion"
$hasCompose = $true
}
} catch {
Write-Warn "Docker Compose not found - needed for containerized PostgreSQL"
}
} else {
Write-Warn "Docker Compose not found - needed for containerized PostgreSQL"
}
if (-not $prereqsOk) {
Write-Host ""
Write-Fail "Please install the missing prerequisites and re-run this script."
exit 1
}
# ─── Server Configuration ────────────────────────────────────────────────────
Write-Section "Server Configuration"
$port = Ask -Prompt "Gateway API port" -Default "8080"
$uiPort = Ask -Prompt "UI dev server port" -Default "5173"
$host_ = Ask -Prompt "Bind address" -Default "127.0.0.1"
$nodeEnv = Ask-Choice -Prompt "Environment" -Default "development" -Options @("development", "production")
$corsOrigins = Ask -Prompt "Extra CORS origins (comma-separated, empty = auto)" -Default ""
# ─── Authentication ──────────────────────────────────────────────────────────
Write-Section "Authentication"
$authType = Ask-Choice -Prompt "Auth type" -Default "none" -Options @("none", "api-key", "jwt")
$apiKeys = ""
$jwtSecret = ""
switch ($authType) {
"api-key" {
$defaultKey = "change-me-$(Generate-RandomHex 8)"
$apiKeys = Ask -Prompt "API keys (comma-separated)" -Default $defaultKey
}
"jwt" {
$jwtSecret = Generate-RandomHex 32
Write-Ok "Generated JWT secret: $($jwtSecret.Substring(0, 8))..."
}
}
# ─── Database ─────────────────────────────────────────────────────────────────
Write-Section "Database (PostgreSQL)"
$pgHost = Ask -Prompt "PostgreSQL host" -Default "localhost"
$pgPort = Ask -Prompt "PostgreSQL port" -Default "25432"
$pgUser = Ask -Prompt "PostgreSQL user" -Default "ownpilot"
$pgPassword = Ask-Secret -Prompt "PostgreSQL password" -Default "ownpilot_secret"
$pgDb = Ask -Prompt "PostgreSQL database name" -Default "ownpilot"
$useDockerPg = "n"
if ($hasDocker -and $hasCompose) {
$useDockerPg = Read-Host " ? Start PostgreSQL with Docker? [Y/n]"
if ([string]::IsNullOrWhiteSpace($useDockerPg)) { $useDockerPg = "y" }
}
# ─── Generate .env ────────────────────────────────────────────────────────────
Write-Section "Generating .env"
$timestamp = (Get-Date).ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss 'UTC'")
$envContent = @"
# OwnPilot Configuration
# Generated by setup.ps1 on $timestamp
# API keys and tokens are configured via Config Center UI.
# ===========================================
# Server
# ===========================================
PORT=$port
UI_PORT=$uiPort
HOST=$host_
NODE_ENV=$nodeEnv
CORS_ORIGINS=$corsOrigins
# ===========================================
# Database (PostgreSQL)
# ===========================================
POSTGRES_HOST=$pgHost
POSTGRES_PORT=$pgPort
POSTGRES_USER=$pgUser
POSTGRES_PASSWORD=$pgPassword
POSTGRES_DB=$pgDb
# ===========================================
# Authentication (DB is primary, env is fallback)
# ===========================================
AUTH_TYPE=$authType
API_KEYS=$apiKeys
JWT_SECRET=$jwtSecret
# ===========================================
# Logging
# ===========================================
LOG_LEVEL=info
"@
$envContent | Set-Content -Path ".env" -Encoding UTF8
Write-Ok ".env file created"
# ─── Start PostgreSQL with Docker ─────────────────────────────────────────────
if ($useDockerPg -match '^[Yy]$') {
Write-Section "Starting PostgreSQL"
Write-Info "Running: docker compose -f docker-compose.db.yml up -d"
docker compose -f docker-compose.db.yml up -d
Write-Info "Waiting for PostgreSQL to be ready..."
$retries = 0
$maxRetries = 30
$ready = $false
while (-not $ready -and $retries -lt $maxRetries) {
try {
$result = docker compose -f docker-compose.db.yml exec -T postgres pg_isready -U $pgUser -d $pgDb 2>&1
if ($LASTEXITCODE -eq 0) {
$ready = $true
}
} catch {}
if (-not $ready) {
$retries++
Start-Sleep -Seconds 1
Write-Host "." -NoNewline
}
}
Write-Host ""
if ($ready) {
Write-Ok "PostgreSQL is ready"
} else {
Write-Fail "PostgreSQL did not become ready in time. Check: docker compose -f docker-compose.db.yml logs"
exit 1
}
}
# ─── Install Dependencies ────────────────────────────────────────────────────
Write-Section "Installing Dependencies"
Write-Info "Running: pnpm install"
pnpm install
Write-Ok "Dependencies installed"
# ─── Build ────────────────────────────────────────────────────────────────────
Write-Section "Building Project"
Write-Info "Running: pnpm run build"
pnpm run build
Write-Ok "Build complete"
# ─── Summary ──────────────────────────────────────────────────────────────────
Write-Section "Setup Complete!"
Write-Host " OwnPilot is ready to go." -ForegroundColor Green
Write-Host ""
Write-Host " Configuration:"
Write-Host " Gateway: http://${host_}:${port}"
Write-Host " UI: http://localhost:${uiPort}"
Write-Host " Database: PostgreSQL @ ${pgHost}:${pgPort}/${pgDb}"
Write-Host " Auth: $authType"
Write-Host ""
Write-Host " Next steps:"
Write-Host " 1. " -NoNewline
Write-Host "pnpm run dev" -ForegroundColor Cyan -NoNewline
Write-Host " Start in development mode"
Write-Host " 2. Open " -NoNewline
Write-Host "http://localhost:${uiPort}" -ForegroundColor Cyan -NoNewline
Write-Host " Configure API keys in Config Center"
Write-Host ""