|
| 1 | +param( |
| 2 | + [string]$Image = "", |
| 3 | + [switch]$ForceSecret |
| 4 | +) |
| 5 | + |
| 6 | +Set-StrictMode -Version Latest |
| 7 | +$ErrorActionPreference = "Stop" |
| 8 | + |
| 9 | +$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 10 | +$RepoRoot = Split-Path -Parent $ScriptDir |
| 11 | +$Utf8NoBom = New-Object System.Text.UTF8Encoding -ArgumentList $false |
| 12 | + |
| 13 | +function Write-Utf8File { |
| 14 | + param( |
| 15 | + [Parameter(Mandatory = $true)][string]$Path, |
| 16 | + [Parameter(Mandatory = $true)][string]$Content |
| 17 | + ) |
| 18 | + [IO.File]::WriteAllText($Path, $Content, $Utf8NoBom) |
| 19 | +} |
| 20 | + |
| 21 | +function Set-DotEnvValue { |
| 22 | + param( |
| 23 | + [Parameter(Mandatory = $true)][string]$Path, |
| 24 | + [Parameter(Mandatory = $true)][string]$Name, |
| 25 | + [Parameter(Mandatory = $true)][string]$Value |
| 26 | + ) |
| 27 | + |
| 28 | + $escapedName = [regex]::Escape($Name) |
| 29 | + if (Test-Path -LiteralPath $Path) { |
| 30 | + $lines = New-Object "System.Collections.Generic.List[string]" |
| 31 | + foreach ($line in [IO.File]::ReadAllLines($Path)) { |
| 32 | + $lines.Add($line) |
| 33 | + } |
| 34 | + } else { |
| 35 | + $lines = New-Object "System.Collections.Generic.List[string]" |
| 36 | + } |
| 37 | + |
| 38 | + $updated = $false |
| 39 | + for ($i = 0; $i -lt $lines.Count; $i++) { |
| 40 | + if ($lines[$i] -match "^\s*#?\s*$escapedName=") { |
| 41 | + $lines[$i] = "$Name=$Value" |
| 42 | + $updated = $true |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (-not $updated) { |
| 47 | + $lines.Add("$Name=$Value") |
| 48 | + } |
| 49 | + |
| 50 | + [IO.File]::WriteAllLines($Path, $lines, $Utf8NoBom) |
| 51 | +} |
| 52 | + |
| 53 | +Set-Location $RepoRoot |
| 54 | + |
| 55 | +$envExamplePath = Join-Path $RepoRoot ".env.example" |
| 56 | +$envPath = Join-Path $RepoRoot ".env" |
| 57 | +$codexHomePath = Join-Path $RepoRoot "data/codex-home" |
| 58 | +$codexWorkPath = Join-Path $RepoRoot "data/codex-work" |
| 59 | +$secretsPath = Join-Path $RepoRoot "data/secrets" |
| 60 | +$proxySecretPath = Join-Path $secretsPath "proxy_api_key" |
| 61 | + |
| 62 | +if (-not (Test-Path -LiteralPath $envExamplePath)) { |
| 63 | + throw "Missing .env.example in $RepoRoot" |
| 64 | +} |
| 65 | + |
| 66 | +if (-not (Test-Path -LiteralPath $envPath)) { |
| 67 | + Copy-Item -LiteralPath $envExamplePath -Destination $envPath |
| 68 | + Write-Host "Created .env from .env.example" |
| 69 | +} else { |
| 70 | + Write-Host "Keeping existing .env" |
| 71 | +} |
| 72 | + |
| 73 | +if ($Image.Trim()) { |
| 74 | + Set-DotEnvValue -Path $envPath -Name "CODEX_CLI_PROVIDER_IMAGE" -Value $Image.Trim() |
| 75 | + Write-Host "Set CODEX_CLI_PROVIDER_IMAGE in .env" |
| 76 | +} |
| 77 | + |
| 78 | +New-Item -ItemType Directory -Force -Path $codexHomePath, $codexWorkPath, $secretsPath | Out-Null |
| 79 | +Write-Host "Ensured data/codex-home, data/codex-work, and data/secrets exist" |
| 80 | + |
| 81 | +if ((Test-Path -LiteralPath $proxySecretPath) -and (-not $ForceSecret)) { |
| 82 | + Write-Host "Keeping existing data/secrets/proxy_api_key" |
| 83 | +} else { |
| 84 | + $tokenBytes = New-Object byte[] 48 |
| 85 | + $rng = [Security.Cryptography.RandomNumberGenerator]::Create() |
| 86 | + try { |
| 87 | + $rng.GetBytes($tokenBytes) |
| 88 | + } finally { |
| 89 | + $rng.Dispose() |
| 90 | + } |
| 91 | + $token = [Convert]::ToBase64String($tokenBytes) |
| 92 | + Write-Utf8File -Path $proxySecretPath -Content "$token`n" |
| 93 | + Write-Host "Wrote new UTF-8 wrapper bearer token to data/secrets/proxy_api_key" |
| 94 | +} |
| 95 | + |
| 96 | +Write-Host "" |
| 97 | +Write-Host "Next steps:" |
| 98 | +if ($Image.Trim()) { |
| 99 | + Write-Host " docker compose -f docker-compose.image.yml pull" |
| 100 | + Write-Host " docker compose -f docker-compose.image.yml up -d" |
| 101 | +} else { |
| 102 | + Write-Host " docker compose up --build -d" |
| 103 | +} |
| 104 | +Write-Host " docker exec -it codex-cli-provider codex login --device-auth -c 'forced_login_method=`"chatgpt`"' -c 'cli_auth_credentials_store=`"file`"'" |
| 105 | +Write-Host "" |
| 106 | +Write-Host "Use http://127.0.0.1:8320/v1 as the client base URL." |
0 commit comments