Files
shop-server/scripts/register-ssh-key-for-root.ps1
T
@kirill.komarov e67d8bdc0a deploy
2026-05-10 16:49:55 +05:00

34 lines
1.3 KiB
PowerShell

# Appends local id_ed25519.pub to root authorized_keys on the server (uses scripts/deploy.env).
# Run from repo root: .\scripts\register-ssh-key-for-root.ps1
$ErrorActionPreference = "Stop"
$scriptsDir = $PSScriptRoot
$deployEnv = Join-Path $scriptsDir "deploy.env"
if (-not (Test-Path $deployEnv)) {
Write-Error "Missing scripts/deploy.env. Copy from deploy.env.example and set DEPLOY_HOST."
}
. "$PSScriptRoot\read-deploy-env.ps1"
Import-DeployDotEnv $deployEnv
$deployHost = [Environment]::GetEnvironmentVariable("DEPLOY_HOST", "Process")
$user = [Environment]::GetEnvironmentVariable("DEPLOY_USER", "Process")
if ([string]::IsNullOrWhiteSpace($user)) { $user = "root" }
if ([string]::IsNullOrWhiteSpace($deployHost)) {
Write-Error "DEPLOY_HOST is not set in scripts/deploy.env."
}
$keyPub = Join-Path $env:USERPROFILE ".ssh\id_ed25519.pub"
if (-not (Test-Path $keyPub)) {
Write-Error "Public key not found: $keyPub"
}
$remote = "${user}@${deployHost}"
Write-Host "Adding key to $remote (from $keyPub). Enter password if SSH asks."
$bashCmd = "umask 077; mkdir -p .ssh && touch .ssh/authorized_keys && chmod 700 .ssh && cat >> .ssh/authorized_keys && chmod 600 .ssh/authorized_keys"
Get-Content -Raw $keyPub | ssh -o StrictHostKeyChecking=accept-new $remote $bashCmd
Write-Host "Done. Verify: ssh $remote echo ssh-ok"