param( [Parameter(Mandatory = $true)] [string]$NasUser, [string]$NasHost = "comtropy.synology.me", [int]$NasPort = 50022, [string]$RemoteDir = "/volume1/docker/telegram-codex-bot", [string]$SshKeyPath = "$env:USERPROFILE\.ssh\nas_codex_ed25519" ) $ErrorActionPreference = "Stop" $root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) $remote = "${NasUser}@${NasHost}" $remotePath = "/usr/local/bin:/var/packages/ContainerManager/target/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin" $sshAuthArgs = @() if ($SshKeyPath -and (Test-Path $SshKeyPath)) { $sshAuthArgs = @("-i", $SshKeyPath, "-o", "IdentitiesOnly=yes") } if (-not (Test-Path (Join-Path $root ".env"))) { Write-Host "Missing .env. Copy .env.example to .env and fill secrets first." -ForegroundColor Yellow exit 1 } function Invoke-Checked { param( [Parameter(Mandatory = $true)] [scriptblock]$Script, [Parameter(Mandatory = $true)] [string]$Description ) & $Script if ($LASTEXITCODE -ne 0) { throw "$Description failed with exit code $LASTEXITCODE" } } Invoke-Checked -Description "Creating remote directory" -Script { ssh @sshAuthArgs -p $NasPort $remote "export PATH='$remotePath'; mkdir -p '$RemoteDir'" } Invoke-Checked -Description "Cleaning old uploaded source files" -Script { ssh @sshAuthArgs -p $NasPort $remote "export PATH='$remotePath'; rm -rf '$RemoteDir/app' '$RemoteDir/scripts'" } $uploadPaths = @( (Join-Path $root "app"), (Join-Path $root "scripts"), (Join-Path $root ".dockerignore"), (Join-Path $root ".env"), (Join-Path $root "Dockerfile"), (Join-Path $root "README.md"), (Join-Path $root "docker-compose.yml"), (Join-Path $root "requirements.txt") ) Invoke-Checked -Description "Uploading project files" -Script { $scpArgs = @("-O") + $sshAuthArgs + @("-P", "$NasPort", "-r") + $uploadPaths + @("${remote}:$RemoteDir/") & scp @scpArgs } Invoke-Checked -Description "Building and starting container" -Script { ssh @sshAuthArgs -tt -p $NasPort $remote "export PATH='$remotePath'; cd '$RemoteDir' && mkdir -p data workspaces codex-home && (sudo /usr/local/bin/docker-compose up -d --build || sudo /var/packages/ContainerManager/target/usr/bin/docker-compose up -d --build || sudo docker compose up -d --build)" } Write-Host "Deployment finished: $RemoteDir"