31 lines
2.0 KiB
PowerShell
31 lines
2.0 KiB
PowerShell
# Creates local secrets and .env (Windows PowerShell 5.1 / PowerShell 7).
|
|
# Existing files are NEVER overwritten: the password of an already initialised database volume
|
|
# must keep matching secrets/postgres_password.
|
|
$ErrorActionPreference = 'Stop'
|
|
Set-Location (Join-Path $PSScriptRoot '..')
|
|
$db = if ($env:POSTGRES_DB) { $env:POSTGRES_DB } else { 'messenger' }
|
|
$user = if ($env:POSTGRES_USER) { $env:POSTGRES_USER } else { 'messenger' }
|
|
New-Item -ItemType Directory -Force -Path 'secrets' | Out-Null
|
|
# UTF-8 WITHOUT BOM and without a trailing newline: readable by the PostgreSQL image and by the app.
|
|
$utf8 = New-Object System.Text.UTF8Encoding($false)
|
|
$pwFile = Join-Path (Get-Location) 'secrets/postgres_password'
|
|
$urlFile = Join-Path (Get-Location) 'secrets/database_url'
|
|
if (-not (Test-Path $pwFile)) {
|
|
$bytes = New-Object byte[] 24
|
|
[System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)
|
|
$password = ($bytes | ForEach-Object { $_.ToString('x2') }) -join ''
|
|
[System.IO.File]::WriteAllText($pwFile, $password, $utf8)
|
|
Write-Host 'created secrets/postgres_password'
|
|
}
|
|
if (-not (Test-Path $urlFile)) {
|
|
$password = [System.IO.File]::ReadAllText($pwFile).Trim()
|
|
[System.IO.File]::WriteAllText($urlFile, "postgres://${user}:${password}@postgres:5432/${db}?sslmode=disable", $utf8)
|
|
Write-Host 'created secrets/database_url'
|
|
}
|
|
$akFile = Join-Path (Get-Location) 'secrets/s3_access_key_id'
|
|
$skFile = Join-Path (Get-Location) 'secrets/s3_secret_access_key'
|
|
function New-Hex($n) { $b = New-Object byte[] $n; [System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($b); ($b | ForEach-Object { $_.ToString('x2') }) -join '' }
|
|
if (-not (Test-Path $akFile)) { [System.IO.File]::WriteAllText($akFile, ('s3' + (New-Hex 8)), $utf8); Write-Host 'created secrets/s3_access_key_id' }
|
|
if (-not (Test-Path $skFile)) { [System.IO.File]::WriteAllText($skFile, (New-Hex 24), $utf8); Write-Host 'created secrets/s3_secret_access_key' }
|
|
if (-not (Test-Path '.env')) { Copy-Item '.env.example' '.env'; Write-Host 'created .env' }
|