Files
mtusi2026lab4/scripts/setup.ps1
T
2026-11-11 18:55:53 +03:00

37 lines
1.3 KiB
PowerShell

$ErrorActionPreference = 'Stop'
Set-Location (Join-Path $PSScriptRoot '..')
if (!(Test-Path -LiteralPath '.env')) {
Copy-Item -LiteralPath '.env.example' -Destination '.env'
}
New-Item -ItemType Directory -Path 'secrets' -Force | Out-Null
function New-SecretFile([string]$Name, [int]$Length) {
$path = Join-Path (Get-Location) "secrets/$Name"
if (!(Test-Path -LiteralPath $path)) {
$bytes = [byte[]]::new($Length)
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try {
$rng.GetBytes($bytes)
} finally {
$rng.Dispose()
}
$value = [BitConverter]::ToString($bytes).Replace('-', '').ToLowerInvariant()
[IO.File]::WriteAllText($path, $value, [Text.Encoding]::ASCII)
}
}
New-SecretFile 'db_password' 24
New-SecretFile 's3_root_access' 10
New-SecretFile 's3_root_secret' 20
New-SecretFile 's3_access_key_id' 10
New-SecretFile 's3_secret_access_key' 20
if (!(Test-Path -LiteralPath 'secrets/database_url')) {
$password = (Get-Content -LiteralPath 'secrets/db_password' -Raw).TrimEnd([char]10)
$encoded = [Uri]::EscapeDataString($password)
$url = "postgresql://messenger:${encoded}@db:5432/messenger"
[IO.File]::WriteAllText((Join-Path (Get-Location) 'secrets/database_url'), $url, [Text.Encoding]::ASCII)
}
Write-Host 'Local secrets are ready.'