-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ps1
More file actions
158 lines (130 loc) · 4.18 KB
/
setup.ps1
File metadata and controls
158 lines (130 loc) · 4.18 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
$ErrorActionPreference = "Stop"
$ExampleEnv = ".env.example"
$TargetEnv = ".env"
Write-Host "Setting up code execution gateway configuration..."
Write-Host ""
function New-ApiSecret {
$bytes = New-Object byte[] 32
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try {
$rng.GetBytes($bytes)
}
finally {
$rng.Dispose()
}
return -join ($bytes | ForEach-Object { $_.ToString("x2") })
}
function Write-Utf8NoBomLines {
param(
[string]$Path,
[string[]]$Lines
)
$encoding = [System.Text.UTF8Encoding]::new($false)
[System.IO.File]::WriteAllText((Resolve-Path -LiteralPath $Path), (($Lines -join [Environment]::NewLine) + [Environment]::NewLine), $encoding)
}
function Sync-EnvWithExample {
param(
[string]$ExampleFile,
[string]$TargetFile
)
$lines = @(Get-Content -LiteralPath $TargetFile)
$targetKeys = @{}
foreach ($line in $lines) {
if ($line -match "^\s*(#|$)") {
continue
}
if ($line -notlike "*=*") {
continue
}
$key = (($line -split "=", 2)[0].Trim() -split "\s+", 2)[0]
if ($key) {
$targetKeys[$key] = $true
}
}
$added = 0
foreach ($line in Get-Content -LiteralPath $ExampleFile) {
if ($line -match "^\s*(#|$)") {
continue
}
if ($line -notlike "*=*") {
continue
}
$key = (($line -split "=", 2)[0].Trim() -split "\s+", 2)[0]
if (-not $key) {
continue
}
if (-not $targetKeys.ContainsKey($key)) {
$lines += $line
$targetKeys[$key] = $true
$added += 1
}
}
if ($added -gt 0) {
Write-Utf8NoBomLines -Path $TargetFile -Lines $lines
Write-Host "Added $added new key(s) from $ExampleFile into $TargetFile"
}
else {
Write-Host "$TargetFile already contains all keys from $ExampleFile"
}
}
function Ensure-ApiKeys {
param([string]$EnvFile)
$lines = @(Get-Content -LiteralPath $EnvFile)
$apiKeyIndex = -1
$currentValue = ""
for ($i = 0; $i -lt $lines.Count; $i++) {
if ($lines[$i] -match "^API_KEYS=(.*)$") {
$apiKeyIndex = $i
$currentValue = $Matches[1].Trim().Trim('"').Trim("'")
break
}
}
if ($apiKeyIndex -lt 0) {
$lines += "API_KEYS="
$apiKeyIndex = $lines.Count - 1
}
$secretPart = $currentValue
if ($secretPart.Contains(":")) {
$secretPart = ($secretPart -split ":", 2)[1]
}
$normalizedValue = $currentValue.ToLowerInvariant()
$placeholderValues = @(
"",
"changeme",
"default",
"local:changeme",
"local:default",
"replace-with-a-long-random-secret",
"local:replace-with-a-long-random-secret"
)
if (($placeholderValues -notcontains $normalizedValue) -and ($secretPart.Length -ge 32)) {
Write-Host "API_KEYS already configured"
return
}
$apiSecret = New-ApiSecret
if (-not $apiSecret) {
throw "Failed to generate API_KEYS"
}
$lines[$apiKeyIndex] = "API_KEYS=local:$apiSecret"
Write-Utf8NoBomLines -Path $EnvFile -Lines $lines
Write-Host "Generated a local API_KEYS secret in $EnvFile"
}
if (-not (Test-Path -LiteralPath $ExampleEnv -PathType Leaf)) {
throw "Missing $ExampleEnv; cannot create setup configuration."
}
if (-not (Test-Path -LiteralPath $TargetEnv -PathType Leaf)) {
Copy-Item -LiteralPath $ExampleEnv -Destination $TargetEnv
Write-Host "Created $TargetEnv from $ExampleEnv"
}
else {
Write-Host "$TargetEnv already exists; syncing new keys from $ExampleEnv"
Sync-EnvWithExample -ExampleFile $ExampleEnv -TargetFile $TargetEnv
}
Ensure-ApiKeys -EnvFile $TargetEnv
Write-Host ""
Write-Host "Setup complete."
Write-Host ""
Write-Host "Next steps:"
Write-Host " 1. Review .env if you want to adjust ports, CORS, limits, or production hardening."
Write-Host " 2. Start the gateway: docker compose --profile local-docker --profile build build; docker compose --profile local-docker up -d"
Write-Host " 3. Check status: docker compose --profile local-docker ps"