# ============================================================================ # scripts/deploy-nas.ps1 # D3RO Voice — Automated NAS Docker Deployment & Packaging Script (PowerShell) # ============================================================================ param ( [string]$NasHost = "", [string]$NasUser = "", [int]$NasSshPort = 0, [string]$NasPath = "", [int]$Port = 0, [switch]$SkipBuild = $false, [switch]$DeploySsh = $false ) $ErrorActionPreference = "Stop" # Load variables from root .env if present $rootEnvFile = Join-Path $PSScriptRoot "..\.env" $envDict = @{} if (Test-Path $rootEnvFile) { Get-Content $rootEnvFile | ForEach-Object { $line = $_.Trim() if ($line -and -not $line.StartsWith("#") -and $line.Contains("=")) { $parts = $line.Split("=", 2) $envDict[$parts[0].Trim()] = $parts[1].Trim() } } } # Fallback to .env values if parameters are not provided if (-not $NasHost -and $envDict.ContainsKey("NAS_HOST")) { $NasHost = $envDict["NAS_HOST"] } if (-not $NasUser -and $envDict.ContainsKey("NAS_USER")) { $NasUser = $envDict["NAS_USER"] } if ($NasSshPort -eq 0 -and $envDict.ContainsKey("NAS_SSH_PORT")) { [int]::TryParse($envDict["NAS_SSH_PORT"], [ref]$NasSshPort) | Out-Null } if (-not $NasPath -and $envDict.ContainsKey("NAS_DEPLOY_PATH")) { $NasPath = $envDict["NAS_DEPLOY_PATH"] } if ($Port -eq 0 -and $envDict.ContainsKey("PORT")) { [int]::TryParse($envDict["PORT"], [ref]$Port) | Out-Null } # Defaults if still empty if (-not $NasUser) { $NasUser = "yunchan" } if ($NasSshPort -eq 0) { $NasSshPort = 22 } if (-not $NasPath) { $NasPath = "/volume1/docker/d3ro" } if ($Port -eq 0) { $Port = 5000 } Write-Host "========================================================" -ForegroundColor Cyan Write-Host " D3RO Voice — NAS Docker Deployment & Packaging Tool " -ForegroundColor Cyan Write-Host "========================================================" -ForegroundColor Cyan if ($NasHost) { Write-Host " [Target NAS] $NasUser@$($NasHost):$NasSshPort (Path: $NasPath)" -ForegroundColor Gray } # 1. Output directory setup $outDir = Join-Path $PSScriptRoot "..\out" $nasPkgDir = Join-Path $outDir "nas-package" if (-not (Test-Path $nasPkgDir)) { New-Item -ItemType Directory -Path $nasPkgDir -Force | Out-Null } # 2. Build Docker Image if (-not $SkipBuild) { Write-Host "`n[1/4] Building D3RO Voice API & Promotion Site Image..." -ForegroundColor Yellow docker build -t d3ro-voice-api:latest ./apps/api-server if ($LASTEXITCODE -ne 0) { Write-Error "API Docker image build failed!" exit 1 } Write-Host " Docker image built: d3ro-voice-api:latest" -ForegroundColor Green Write-Host "`n[1.5/4] Building D3RO Voice Next.js Admin CRM Image..." -ForegroundColor Yellow docker build -t d3ro-voice-admin:latest -f apps/admin/Dockerfile . if ($LASTEXITCODE -ne 0) { Write-Error "Admin Docker image build failed!" exit 1 } Write-Host " Docker image built: d3ro-voice-admin:latest" -ForegroundColor Green } else { Write-Host "`n[1/4] Skipping Docker build (-SkipBuild specified)" -ForegroundColor Gray } # 3. Export Docker Image Archives $tarPath = Join-Path $nasPkgDir "d3ro-voice-api.tar" $adminTarPath = Join-Path $nasPkgDir "d3ro-voice-admin.tar" Write-Host "`n[2/4] Exporting Docker Images to Archives..." -ForegroundColor Yellow docker save -o $tarPath d3ro-voice-api:latest docker save -o $adminTarPath d3ro-voice-admin:latest Write-Host " Image archives exported: $tarPath, $adminTarPath" -ForegroundColor Green # 4. Prepare Standalone NAS Package Write-Host "`n[3/4] Packaging NAS deployment files..." -ForegroundColor Yellow # Copy docker-compose.nas.yml as docker-compose.yml in package Copy-Item (Join-Path $PSScriptRoot "..\docker-compose.nas.yml") (Join-Path $nasPkgDir "docker-compose.yml") -Force # Copy or generate .env in package if (Test-Path $rootEnvFile) { Copy-Item $rootEnvFile (Join-Path $nasPkgDir ".env") -Force } else { $envContent = @" PORT=$Port DATA_PATH=./data JWT_SECRET=D3ROVoice_Super_Secure_Secret_Key_2026_Key! TZ=Asia/Seoul "@ Set-Content -Path (Join-Path $nasPkgDir ".env") -Value $envContent -Encoding UTF8 } # Copy control script Copy-Item (Join-Path $PSScriptRoot "nas-control.sh") (Join-Path $nasPkgDir "nas-control.sh") -Force -ErrorAction SilentlyContinue # Create Quick Instructions $readmeContent = @" ======================================================================== D3RO Voice — Standalone NAS Deployment Package ======================================================================== [Synology / QNAP / Linux NAS 배포 방법] 1. 이 폴더의 모든 파일 (d3ro-voice-api.tar, docker-compose.yml, .env, nas-control.sh)을 NAS의 Docker 작업 폴더 ($NasPath)에 업로드합니다. 2. NAS SSH 터미널에 접속하여 해당 폴더로 이동합니다: cd $NasPath 3. Docker 이미지를 로드합니다: docker load < d3ro-voice-api.tar 4. 서비스를 시작합니다: docker compose up -d 5. 브라우저에서 접속 확인: - 포털 메인: http://:$Port/ - 관리자 백오피스: http://:$Port/admin - API Swagger: http://:$Port/swagger - 헬스체크: http://:$Port/health "@ Set-Content -Path (Join-Path $nasPkgDir "README.txt") -Value $readmeContent -Encoding UTF8 Write-Host " NAS Deployment Package ready at: $nasPkgDir" -ForegroundColor Green # 5. Remote SSH Deployment (Optional or if -DeploySsh is specified) if ($DeploySsh -and $NasHost) { Write-Host "`n[4/4] Deploying to Remote NAS ($NasUser@$($NasHost):$NasPath on SSH Port $NasSshPort)..." -ForegroundColor Yellow $sshCmd = "ssh -p $NasSshPort" $scpCmd = "scp -O -P $NasSshPort" Write-Host " [1/3] Creating remote directory on NAS ($NasPath/data)..." -ForegroundColor Gray ssh -p $NasSshPort "$NasUser@$NasHost" "mkdir -p $NasPath/data" Write-Host " [2/3] Uploading deployment package to NAS (SCP)..." -ForegroundColor Gray scp -O -P $NasSshPort "$tarPath" "$adminTarPath" (Join-Path $nasPkgDir "docker-compose.yml") (Join-Path $nasPkgDir ".env") (Join-Path $nasPkgDir "nas-control.sh") "$NasUser@$($NasHost):$NasPath/" Write-Host " [3/3] Loading Docker images and launching services on NAS..." -ForegroundColor Gray ssh -p $NasSshPort "$NasUser@$NasHost" "cd $NasPath && chmod +x nas-control.sh 2>/dev/null; docker load < d3ro-voice-api.tar && docker load < d3ro-voice-admin.tar && docker compose down 2>/dev/null; docker compose up -d" Write-Host "`n========================================================" -ForegroundColor Green Write-Host " NAS Deployment Completed Successfully!" -ForegroundColor Green Write-Host " Access your D3RO Cloud Services at:" -ForegroundColor Cyan Write-Host " - Official Promotion Site: http://$($NasHost):$Port/" -ForegroundColor White Write-Host " - Next.js Admin CRM: http://$($NasHost):3001/" -ForegroundColor White Write-Host " - Swagger API Docs: http://$($NasHost):$Port/swagger" -ForegroundColor White Write-Host " - Health Check: http://$($NasHost):$Port/health" -ForegroundColor White Write-Host "========================================================" -ForegroundColor Green } else { Write-Host "`n========================================================" -ForegroundColor Green Write-Host " NAS Package Ready!" -ForegroundColor Green Write-Host " Output Folder: $nasPkgDir" -ForegroundColor White Write-Host " Files generated:" -ForegroundColor White Write-Host " - d3ro-voice-api.tar (Docker image archive)" -ForegroundColor Gray Write-Host " - docker-compose.yml (NAS Compose configuration)" -ForegroundColor Gray Write-Host " - .env (Environment variables)" -ForegroundColor Gray Write-Host " - nas-control.sh (Container management script)" -ForegroundColor Gray Write-Host " - README.txt (Deployment guide)" -ForegroundColor Gray Write-Host "`n To deploy automatically via SSH, run:" -ForegroundColor Cyan Write-Host " .\scripts\deploy-nas.ps1 -DeploySsh" -ForegroundColor Yellow Write-Host "========================================================" -ForegroundColor Green }