-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.ps1
More file actions
139 lines (119 loc) · 5.39 KB
/
Copy pathdeploy.ps1
File metadata and controls
139 lines (119 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# ─────────────────────────────────────────────────────────
# deploy.ps1 — ALOS deploy script (Windows)
# Run from repo root: .\deploy.ps1
#
# Pi layout:
# /opt/anni/alos → built frontend (Vite dist)
# /opt/anni/alos-server → Node/Express backend
# /srv/storage/ALOS/ → alos.db (never touched by deploy)
# ─────────────────────────────────────────────────────────
param(
[switch]$ClientOnly,
[switch]$ServerOnly,
[switch]$SkipBuild,
[switch]$Help
)
$ErrorActionPreference = "Stop"
Import-Module "$PSScriptRoot\lib\AnniLog.psd1" -Force
# ── Config ──
$PI_USER = "akira"
$PI_HOST = "yme-04"
$PI_WEB = "/opt/anni/alos-www"
$PI_SERVER = "/opt/anni/alos"
$LogFile = Join-Path $PSScriptRoot "logs\deploy-$(Get-Date -Format 'yyyy-MM-dd_HHmmss').log"
function Invoke-SSH { param($cmd) ssh "${PI_USER}@${PI_HOST}" $cmd }
function Invoke-SCP { param($src, $dst) scp -r $src "${PI_USER}@${PI_HOST}:${dst}" }
if ($Help) {
Write-Host "Usage: .\deploy.ps1 [options]"
Write-Host " -ClientOnly Only deploy frontend"
Write-Host " -ServerOnly Only deploy backend + restart service"
Write-Host " -SkipBuild Skip npm build step"
exit 0
}
$DeployClient = -not $ServerOnly
$DeployServer = -not $ClientOnly
Initialize-AnniLog -LogFilePath $LogFile -LogLevel "INFO" -EnableStopwatch
Write-Host ""
Write-Host "====================================" -ForegroundColor Cyan
Write-Host " ALOS Deploy Script " -ForegroundColor Cyan
Write-Host "====================================" -ForegroundColor Cyan
Write-Host ""
if (-not (Get-Command ssh -ErrorAction SilentlyContinue)) {
Write-AnniLog -Level ERROR -Message "ssh not found in PATH"; Close-AnniLog; exit 1
}
if (-not (Get-Command scp -ErrorAction SilentlyContinue)) {
Write-AnniLog -Level ERROR -Message "scp not found in PATH"; Close-AnniLog; exit 1
}
if ($DeployClient -and -not $SkipBuild -and -not (Get-Command npm -ErrorAction SilentlyContinue)) {
Write-AnniLog -Level ERROR -Message "npm not found in PATH"; Close-AnniLog; exit 1
}
# ── SSH reachability check ──
Write-AnniLog -Level INFO -Message "Checking connection to ${PI_USER}@${PI_HOST}..."
& ssh -o BatchMode=yes -o ConnectTimeout=5 -q "${PI_USER}@${PI_HOST}" "exit"
if ($LASTEXITCODE -ne 0) {
& ssh -o ConnectTimeout=5 "${PI_USER}@${PI_HOST}" "exit"
if ($LASTEXITCODE -ne 0) {
Write-AnniLog -Level ERROR -Message "SSH check failed. Check Tailscale/network and SSH auth."
Close-AnniLog; exit 1
}
}
Write-AnniLog -Level SUCCESS -Message "Connected to ${PI_HOST}"
# ── Build frontend ──
if ($DeployClient -and -not $SkipBuild) {
Write-AnniLog -Level INFO -Message "Building frontend..."
Push-Location client
npm run build 2>&1 | Select-Object -Last 5
if ($LASTEXITCODE -ne 0) {
Pop-Location
Write-AnniLog -Level ERROR -Message "Frontend build failed"
Close-AnniLog; exit 1
}
Pop-Location
Write-AnniLog -Level SUCCESS -Message "Frontend built → dist/"
}
# ── Deploy frontend ──
if ($DeployClient) {
Write-AnniLog -Level INFO -Message "Deploying frontend to ${PI_HOST}:${PI_WEB}..."
Invoke-SSH "rm -rf ${PI_WEB}/assets ${PI_WEB}/index.html"
Get-ChildItem "dist" | ForEach-Object { Invoke-SCP $_.FullName $PI_WEB }
if ($LASTEXITCODE -ne 0) {
Write-AnniLog -Level ERROR -Message "Frontend deploy failed"
Close-AnniLog; exit 1
}
Write-AnniLog -Level SUCCESS -Message "Frontend deployed"
}
# ── Deploy backend ──
if ($DeployServer) {
Write-AnniLog -Level INFO -Message "Deploying backend to ${PI_HOST}:${PI_SERVER}..."
Invoke-SSH "mkdir -p ${PI_SERVER}/db"
$serverFiles = Get-ChildItem server | Where-Object { $_.Name -notin @('node_modules', '.env', 'db') }
foreach ($f in $serverFiles) { Invoke-SCP $f.FullName $PI_SERVER }
if ($LASTEXITCODE -ne 0) {
Write-AnniLog -Level ERROR -Message "Backend deploy failed"
Close-AnniLog; exit 1
}
Invoke-SCP "server\db\db.js" "${PI_SERVER}/db/"
Invoke-SCP "server\db\schema.sql" "${PI_SERVER}/db/"
Write-AnniLog -Level SUCCESS -Message "Backend files synced (.env and alos.db preserved)"
Write-AnniLog -Level INFO -Message "Installing backend dependencies on Pi..."
Invoke-SSH "cd ${PI_SERVER} && npm install --omit=dev 2>&1 | tail -3"
Write-AnniLog -Level SUCCESS -Message "Dependencies installed"
Write-AnniLog -Level INFO -Message "Restarting alos service..."
Invoke-SSH "sudo systemctl restart alos"
Start-Sleep 2
$status = (Invoke-SSH "systemctl is-active alos" | Out-String).Trim()
if ($status -eq "active") {
Write-AnniLog -Level SUCCESS -Message "alos service is running"
} else {
Write-AnniLog -Level ERROR -Message "Service failed to start — run: journalctl -u alos -n 20"
Close-AnniLog; exit 1
}
}
Close-AnniLog
Write-Host ""
Write-Host "====================================" -ForegroundColor Green
Write-Host " Deploy complete! " -ForegroundColor Green
Write-Host "====================================" -ForegroundColor Green
Write-Host ""
Write-Host " https://alos.yumehana.dev" -ForegroundColor Cyan
Write-Host ""