-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdeploy.ps1
More file actions
289 lines (243 loc) · 9.83 KB
/
deploy.ps1
File metadata and controls
289 lines (243 loc) · 9.83 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Deployment script for grub-crawl service
Based on gnosis-ocr deploy.ps1 pattern
.DESCRIPTION
Deploys grub-crawl to local Docker, Cloud Run, or a 2-node mesh.
Supports local development, cloud production, and mesh topologies.
.PARAMETER Target
Deployment target: 'local', 'cloudrun', or 'mesh'
.PARAMETER Tag
Docker image tag (default: 'latest')
.PARAMETER Rebuild
Force rebuild of Docker image
.PARAMETER MeshSecret
Shared HMAC secret for mesh nodes (default: auto-generated)
.PARAMETER CloudMeshPeer
URL of a cloud peer to connect to when deploying with mesh enabled
.EXAMPLE
./deploy.ps1 -Target local
Deploy single node to local Docker Compose
.EXAMPLE
./deploy.ps1 -Target mesh
Deploy 2-node mesh locally (node-a + node-b)
.EXAMPLE
./deploy.ps1 -Target cloudrun -Tag v1.0.0
Deploy to Google Cloud Run
.EXAMPLE
./deploy.ps1 -Target cloudrun -Tag v1.0.0 -CloudMeshPeer http://localhost:6792 -MeshSecret mykey
Deploy to Cloud Run with mesh enabled, peering with local node
#>
param(
[ValidateSet("local", "cloudrun", "mesh")]
[string]$Target = "local",
[string]$Tag = "latest",
[switch]$Rebuild = $false,
[string]$MeshSecret = "",
[string]$CloudMeshPeer = ""
)
# Configuration — matches grub-site / gnosis-ocr pattern
$ServiceName = "grub-crawl"
$ProjectId = $env:GOOGLE_CLOUD_PROJECT
if (-not $ProjectId) { $ProjectId = "gnosis-459403" }
$Region = "us-central1"
$ImageName = "gcr.io/${ProjectId}/${ServiceName}"
Write-Host "==> Deploying $ServiceName to $Target" -ForegroundColor Cyan
# ---------------------------------------------------------------------------
# Validate prerequisites
# ---------------------------------------------------------------------------
if ($Target -eq "cloudrun") {
# Check gcloud
try {
$null = gcloud auth list --filter="status:ACTIVE" --format="value(account)" 2>$null
if ($LASTEXITCODE -ne 0) {
Write-Error "Please authenticate with: gcloud auth login"
exit 1
}
}
catch {
Write-Error "gcloud CLI not found. Install: winget install Google.CloudSDK"
exit 1
}
gcloud config set project $ProjectId
}
# ---------------------------------------------------------------------------
# Build image
# ---------------------------------------------------------------------------
Write-Host "==> Building Docker image..." -ForegroundColor Yellow
if ($Target -eq "cloudrun") {
$FullImageName = "${ImageName}:${Tag}"
} else {
$FullImageName = "${ServiceName}:${Tag}"
}
$BuildArgs = @()
if ($Rebuild) { $BuildArgs += "--no-cache" }
try {
docker build $BuildArgs -t $FullImageName .
if ($LASTEXITCODE -ne 0) {
Write-Error "Docker build failed"
exit 1
}
Write-Host "==> Image built: $FullImageName" -ForegroundColor Green
}
catch {
Write-Error "Failed to build Docker image: $_"
exit 1
}
# ---------------------------------------------------------------------------
# Deploy
# ---------------------------------------------------------------------------
switch ($Target) {
"local" {
Write-Host "==> Deploying single node locally..." -ForegroundColor Yellow
# Stop mesh compose first (shares port 6792)
docker-compose -f docker-compose.mesh.yml down 2>$null
docker-compose down 2>$null
try {
docker-compose up -d
if ($LASTEXITCODE -ne 0) {
Write-Error "Docker Compose failed"
exit 1
}
Write-Host ""
Write-Host "==> Service deployed locally" -ForegroundColor Green
Write-Host " API: http://localhost:6792" -ForegroundColor Cyan
Write-Host " Health: http://localhost:6792/health" -ForegroundColor Cyan
Write-Host " Site: http://localhost:6792/site" -ForegroundColor Cyan
Write-Host ""
docker-compose logs --tail=10
}
catch {
Write-Error "Failed to deploy locally: $_"
exit 1
}
}
"mesh" {
Write-Host "==> Deploying 2-node mesh locally..." -ForegroundColor Yellow
# Stop single-node compose first (shares port 6792)
docker-compose down 2>$null
docker-compose -f docker-compose.mesh.yml down 2>$null
try {
docker-compose -f docker-compose.mesh.yml up -d --build
if ($LASTEXITCODE -ne 0) {
Write-Error "Mesh compose failed"
exit 1
}
Write-Host ""
Write-Host "==> Mesh deployed (2 nodes)" -ForegroundColor Green
Write-Host " Node A: http://localhost:6792 (local)" -ForegroundColor Cyan
Write-Host " Node B: http://localhost:6793 (cloud)" -ForegroundColor Cyan
Write-Host " Peers A: http://localhost:6792/mesh/peers" -ForegroundColor Cyan
Write-Host " Peers B: http://localhost:6793/mesh/peers" -ForegroundColor Cyan
Write-Host " Health: http://localhost:6792/health" -ForegroundColor Cyan
Write-Host " Site: http://localhost:6792/site" -ForegroundColor Cyan
Write-Host ""
Write-Host " Verify: curl http://localhost:6792/mesh/peers" -ForegroundColor Yellow
Write-Host ""
# Wait for startup then show peer status
Start-Sleep -Seconds 5
docker-compose -f docker-compose.mesh.yml logs --tail=15
}
catch {
Write-Error "Failed to deploy mesh: $_"
exit 1
}
}
"cloudrun" {
Write-Host "==> Deploying to Google Cloud Run..." -ForegroundColor Yellow
# Enable required APIs (one-time)
Write-Host "==> Enabling required APIs..." -ForegroundColor Yellow
gcloud services enable `
run.googleapis.com `
cloudbuild.googleapis.com `
artifactregistry.googleapis.com
# Push image
Write-Host "==> Pushing image to Container Registry..." -ForegroundColor Yellow
try {
docker push $FullImageName
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to push image"
exit 1
}
}
catch {
Write-Error "Failed to push Docker image: $_"
exit 1
}
# Build env vars
$EnvVars = @(
"RUNNING_IN_CLOUD=true",
"GCS_BUCKET_NAME=grub-crawl-storage-prod",
"GNOSIS_AUTH_URL=https://auth.nuts.services",
"GOOGLE_CLOUD_PROJECT=$ProjectId"
)
# Add mesh env vars if mesh peer specified
if ($CloudMeshPeer) {
if (-not $MeshSecret) {
$MeshSecret = -join ((48..57) + (65..90) + (97..122) | Get-Random -Count 24 | ForEach-Object { [char]$_ })
Write-Host "==> Generated mesh secret: $MeshSecret" -ForegroundColor Yellow
Write-Host " Use this secret when starting the local peer." -ForegroundColor Yellow
}
# Get the Cloud Run service URL (will be set after deploy, use placeholder)
$EnvVars += "MESH_ENABLED=true"
$EnvVars += "MESH_NODE_NAME=cloud"
$EnvVars += "MESH_SECRET=$MeshSecret"
$EnvVars += "MESH_PEERS=$CloudMeshPeer"
$EnvVars += "MESH_PREFER_LOCAL=false"
$EnvVars += "MESH_HEARTBEAT_INTERVAL_S=15"
}
# Deploy to Cloud Run
Write-Host "==> Deploying to Cloud Run..." -ForegroundColor Yellow
$EnvString = $EnvVars -join ","
try {
gcloud run deploy $ServiceName `
--image $FullImageName `
--platform managed `
--region $Region `
--allow-unauthenticated `
--port 8080 `
--memory 1Gi `
--cpu 1 `
--max-instances 10 `
--timeout 300 `
--concurrency 100 `
--set-env-vars $EnvString
if ($LASTEXITCODE -ne 0) {
Write-Error "Cloud Run deployment failed"
exit 1
}
# Get service URL
$ServiceUrl = gcloud run services describe $ServiceName --region $Region --format "value(status.url)"
# If mesh is enabled, update MESH_ADVERTISE_URL to the actual Cloud Run URL
if ($CloudMeshPeer) {
Write-Host "==> Updating mesh advertise URL..." -ForegroundColor Yellow
gcloud run services update $ServiceName `
--region $Region `
--update-env-vars "MESH_ADVERTISE_URL=$ServiceUrl"
}
Write-Host ""
Write-Host "==> Service deployed to Cloud Run" -ForegroundColor Green
Write-Host " URL: $ServiceUrl" -ForegroundColor Cyan
Write-Host " Health: $ServiceUrl/health" -ForegroundColor Cyan
Write-Host " Site: $ServiceUrl/site" -ForegroundColor Cyan
if ($CloudMeshPeer) {
Write-Host ""
Write-Host "==> Mesh enabled" -ForegroundColor Green
Write-Host " Peers: $ServiceUrl/mesh/peers" -ForegroundColor Cyan
Write-Host " Status: $ServiceUrl/mesh/status" -ForegroundColor Cyan
Write-Host ""
Write-Host " To connect your local node:" -ForegroundColor Yellow
Write-Host " MESH_ENABLED=true MESH_SECRET=$MeshSecret MESH_PEERS=$ServiceUrl \" -ForegroundColor Yellow
Write-Host " MESH_ADVERTISE_URL=http://your-local-ip:6792 \" -ForegroundColor Yellow
Write-Host " uvicorn app.main:app --port 6792" -ForegroundColor Yellow
}
}
catch {
Write-Error "Failed to deploy to Cloud Run: $_"
exit 1
}
}
}
Write-Host ""
Write-Host "==> Deployment completed." -ForegroundColor Green