hazo_env 0.10.4 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGE_LOG.md +69 -0
- package/config/hazo_env_config.ini.sample +61 -0
- package/dist/cli.js +315 -0
- package/dist/deploy/build.d.ts +90 -0
- package/dist/deploy/build.d.ts.map +1 -0
- package/dist/deploy/build.js +299 -0
- package/dist/deploy/clean.d.ts +63 -0
- package/dist/deploy/clean.d.ts.map +1 -0
- package/dist/deploy/clean.js +135 -0
- package/dist/deploy/index.d.ts +6 -0
- package/dist/deploy/index.d.ts.map +1 -0
- package/dist/deploy/index.js +6 -0
- package/dist/deploy/init.d.ts +20 -0
- package/dist/deploy/init.d.ts.map +1 -0
- package/dist/deploy/init.js +80 -0
- package/dist/deploy/peer.d.ts +18 -0
- package/dist/deploy/peer.d.ts.map +1 -0
- package/dist/deploy/peer.js +48 -0
- package/dist/deploy/pm2-start.d.ts +88 -0
- package/dist/deploy/pm2-start.d.ts.map +1 -0
- package/dist/deploy/pm2-start.js +196 -0
- package/dist/deploy/rollback.d.ts +18 -0
- package/dist/deploy/rollback.d.ts.map +1 -0
- package/dist/deploy/rollback.js +167 -0
- package/dist/deploy/runner.d.ts +8 -0
- package/dist/deploy/runner.d.ts.map +1 -0
- package/dist/deploy/runner.js +35 -0
- package/dist/deploy/state.d.ts +19 -0
- package/dist/deploy/state.d.ts.map +1 -0
- package/dist/deploy/state.js +52 -0
- package/dist/deploy/status.d.ts +54 -0
- package/dist/deploy/status.d.ts.map +1 -0
- package/dist/deploy/status.js +182 -0
- package/dist/deploy/switch.d.ts +27 -0
- package/dist/deploy/switch.d.ts.map +1 -0
- package/dist/deploy/switch.js +152 -0
- package/dist/deploy/types.d.ts +96 -0
- package/dist/deploy/types.d.ts.map +1 -0
- package/dist/deploy/types.js +16 -0
- package/dist/deploy/verify.d.ts +41 -0
- package/dist/deploy/verify.d.ts.map +1 -0
- package/dist/deploy/verify.js +214 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/resolve/connect.d.ts.map +1 -1
- package/dist/resolve/connect.js +15 -1
- package/dist/resolve/deploy.d.ts +8 -0
- package/dist/resolve/deploy.d.ts.map +1 -0
- package/dist/resolve/deploy.js +121 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +3 -3
package/CHANGE_LOG.md
CHANGED
|
@@ -1,5 +1,74 @@
|
|
|
1
1
|
# hazo_env — Change Log
|
|
2
2
|
|
|
3
|
+
## 0.12.0 — 2026-10-24
|
|
4
|
+
|
|
5
|
+
New: `hazo-env deploy` — a full blue-green deployment CLI for single-VM sites
|
|
6
|
+
that maintain two sibling directories differing only by a blue/green suffix
|
|
7
|
+
(e.g. `gotimer-blue/`, `gotimer-green/`), orchestrated entirely through a new
|
|
8
|
+
`[deploy]` ini section and a state file at `~/.hazo-deploy/<app_name>.json`:
|
|
9
|
+
|
|
10
|
+
- `deploy init` — creates the peer directory, clones the repo into it, and
|
|
11
|
+
writes the initial state file.
|
|
12
|
+
- `deploy build` — runs the full standby build pipeline (pre-build/install/
|
|
13
|
+
build, DB provisioning via `db_strategy`, an optional `migrate_cmd`) and
|
|
14
|
+
starts the result as `<app_name>-staging` on `staging_port` for
|
|
15
|
+
health-checking before switch.
|
|
16
|
+
- `deploy verify` — re-runs health/typecheck/migration checks against the
|
|
17
|
+
standby build without switching.
|
|
18
|
+
- `deploy switch` — promotes standby to live and demotes live to standby.
|
|
19
|
+
- `deploy rollback` — reverses the last `deploy switch` in one command, using
|
|
20
|
+
`previous_live_dir` recorded by switch; aborts with a clear message if
|
|
21
|
+
there's no switch left to reverse (either none has run, or a prior
|
|
22
|
+
rollback already consumed it). `previous_live_dir` is deliberately
|
|
23
|
+
cleared (not repopulated) by rollback itself, so a rollback is a one-shot
|
|
24
|
+
recovery action rather than a live/standby toggle — see `src/deploy/
|
|
25
|
+
rollback.ts`'s header for the full reasoning.
|
|
26
|
+
- `deploy status` — reports live/standby dirs, ports, PM2 process state, and
|
|
27
|
+
disk/memory headroom.
|
|
28
|
+
- `deploy clean` — reclaims disk space from old builds/caches, with
|
|
29
|
+
`--aggressive` system-level cleanup suggestions.
|
|
30
|
+
|
|
31
|
+
Both `deploy build`'s staging start and `deploy switch`/`deploy rollback`'s
|
|
32
|
+
live-promotion start share one hardened primitive, `startNamedPm2Process`
|
|
33
|
+
(`src/deploy/pm2-start.ts`): `pm2 start ecosystem.config.js --name X` does
|
|
34
|
+
NOT rename the process when starting from a config file — pm2 reads the
|
|
35
|
+
name baked into the file itself and ignores the CLI override — so a direct
|
|
36
|
+
call silently restarts (or no-ops against) whatever was already registered
|
|
37
|
+
under the ecosystem file's real name instead of creating a distinct
|
|
38
|
+
process. The shared helper clones the target dir's ecosystem.config.js with
|
|
39
|
+
name/cwd/port overridden into a per-call temp file and starts from THAT
|
|
40
|
+
file instead. Every step that replaces a PM2 registration uses `pm2
|
|
41
|
+
delete`, never `stop` — `stop` leaves the old registration's `pm_cwd`
|
|
42
|
+
pointing at the old directory, so the next start under the same name would
|
|
43
|
+
just restart it in place rather than registering fresh against the new
|
|
44
|
+
directory.
|
|
45
|
+
|
|
46
|
+
## 0.11.0 — 2026-10-21
|
|
47
|
+
|
|
48
|
+
New: `[db.<env>]`'s `type = sqlite` section now accepts an optional `driver` key
|
|
49
|
+
(`sql.js` or `better-sqlite3`), passed straight through to hazo_connect as
|
|
50
|
+
`sqlite.driver`. `resolveConnectConfig()` previously only ever forwarded
|
|
51
|
+
`database_path`, so hazo_connect's own `better-sqlite3` driver — the one built for
|
|
52
|
+
safe multi-connection access to one file — was unreachable from the ini; every
|
|
53
|
+
`resolveConnectConfig()`-driven app was silently stuck on the `sql.js` default
|
|
54
|
+
regardless of what it configured.
|
|
55
|
+
|
|
56
|
+
Found while root-causing a Quizaboo dev bug: Next.js dev compiles `middleware.ts`
|
|
57
|
+
separately from route handlers, so a `db` singleton built from
|
|
58
|
+
`resolveConnectConfig()` and imported in both ends up as two independent processes'
|
|
59
|
+
worth of `sql.js` adapter, each holding its own in-memory copy of the same
|
|
60
|
+
`.sqlite` file and overwriting the whole file on every write — a torn read during
|
|
61
|
+
that overwrite loaded a corrupted buffer in one of them, producing an intermittent
|
|
62
|
+
`database disk image is malformed` on every request in that bundle until restart.
|
|
63
|
+
hazo_connect's `better-sqlite3` driver (real OS-level WAL locking, safe for
|
|
64
|
+
concurrent connections) already exists for exactly this; this fix just makes it
|
|
65
|
+
reachable via the ini an app actually configures. An unrecognized `driver` value
|
|
66
|
+
throws `HAZO_ENV_CONFIG_UNKNOWN_SQLITE_DRIVER` at config-resolve time rather than
|
|
67
|
+
surfacing later as hazo_connect's generic "Unsupported connection type".
|
|
68
|
+
|
|
69
|
+
Backward compatible: omitting `driver` keeps the existing `sql.js` default
|
|
70
|
+
behavior unchanged.
|
|
71
|
+
|
|
3
72
|
## 0.10.4 — 2026-10-11
|
|
4
73
|
|
|
5
74
|
Bug fix: `hazo-env doctor --probe`'s SQLite DB-probe check crashed with
|
|
@@ -35,6 +35,17 @@ root = app_data
|
|
|
35
35
|
; SQLite database for local development
|
|
36
36
|
type = sqlite
|
|
37
37
|
database_path = ${DATA_ROOT}/dev.sqlite
|
|
38
|
+
; Optional: which SQLite engine hazo_connect opens the file with.
|
|
39
|
+
; sql.js (default) — zero native deps, but keeps one in-memory copy per
|
|
40
|
+
; process and overwrites the whole file on every write. Safe only when
|
|
41
|
+
; exactly one process ever touches database_path. A Next.js dev server
|
|
42
|
+
; already breaks that assumption on its own (middleware.ts compiles as a
|
|
43
|
+
; separate bundle from route handlers, each importing its own copy), and
|
|
44
|
+
; it WILL corrupt the file under concurrent access.
|
|
45
|
+
; better-sqlite3 — opens the file natively with real OS-level WAL locking;
|
|
46
|
+
; safe for multiple connections/processes against the same file. Needs
|
|
47
|
+
; the optional `better-sqlite3` package installed.
|
|
48
|
+
; driver = better-sqlite3
|
|
38
49
|
|
|
39
50
|
[db.test]
|
|
40
51
|
; SQLite database for automated tests
|
|
@@ -122,3 +133,53 @@ path = /home/pubs/kinstripe/var/uploads
|
|
|
122
133
|
; strict and pinned — no trust-on-first-use. When omitted, the first connection
|
|
123
134
|
; is accepted (accept-new) and persisted to config/hazo_env_known_hosts.
|
|
124
135
|
; host_key = ${SSH_HOST_KEY_PROD}
|
|
136
|
+
|
|
137
|
+
; ─── deploy: blue-green deployment orchestration ─────────────────────────────
|
|
138
|
+
; Drives `hazo-env deploy ...` (init/build/verify/switch/rollback/status/clean).
|
|
139
|
+
; Sites maintain two sibling directories differing only by a blue/green
|
|
140
|
+
; suffix (e.g. gotimer-blue/, gotimer-green/). Only app_name and dir_suffix
|
|
141
|
+
; are required — every other field has a sane default.
|
|
142
|
+
|
|
143
|
+
[deploy]
|
|
144
|
+
; Logical app name. Also names the state file: ~/.hazo-deploy/<app_name>.json
|
|
145
|
+
app_name = myapp
|
|
146
|
+
; Which color suffix THIS directory is (the peer is resolved by swapping
|
|
147
|
+
; blue<->green on the current directory's basename). Required, no default.
|
|
148
|
+
dir_suffix = green
|
|
149
|
+
|
|
150
|
+
; Production PM2 port. Default: 3000
|
|
151
|
+
prod_port = 3000
|
|
152
|
+
; Staging port used by `deploy build`/`deploy verify` before switching. Default: 3001
|
|
153
|
+
staging_port = 3001
|
|
154
|
+
; Path hit for health checks during build/verify/status. Default: /api/health
|
|
155
|
+
health_url = /api/health
|
|
156
|
+
|
|
157
|
+
; Minimum free disk (MB) required before a build proceeds. Default: 1024
|
|
158
|
+
min_disk_mb = 1024
|
|
159
|
+
; Minimum free memory (MB) required before a build proceeds. Default: 512
|
|
160
|
+
min_memory_mb = 512
|
|
161
|
+
|
|
162
|
+
; Git branch pulled by `deploy build` (overridable with --branch). Default: main
|
|
163
|
+
branch = main
|
|
164
|
+
; How the standby DB is provisioned: auto (infer from [db.*] driver), copy
|
|
165
|
+
; (sqlite file copy from peer), or shared (point at the same Postgres DB).
|
|
166
|
+
; Default: auto
|
|
167
|
+
db_strategy = auto
|
|
168
|
+
|
|
169
|
+
; Shell commands for the build pipeline. Defaults shown.
|
|
170
|
+
pre_build_cmd = rm -rf .next
|
|
171
|
+
install_cmd = npm ci
|
|
172
|
+
build_cmd = npm run build
|
|
173
|
+
|
|
174
|
+
; Optional shell command run after DB setup during `deploy build`. Migrations
|
|
175
|
+
; are project-specific (no generic cross-project migration runner lives in
|
|
176
|
+
; this package), so the default is empty — "deploy build" then logs that it's
|
|
177
|
+
; assuming the consuming app's own schema bootstrap runs automatically at
|
|
178
|
+
; startup and moves on. Set this when the app needs an explicit migration step.
|
|
179
|
+
; migrate_cmd = npm run db:migrate
|
|
180
|
+
|
|
181
|
+
; Optional shell commands run after a successful `deploy switch` (indexnow
|
|
182
|
+
; submission, CDN cache purge, ...). Numbered suffixes (_2, _3, ...) run in
|
|
183
|
+
; order after the unsuffixed post_switch_cmd. Omit entirely to run none.
|
|
184
|
+
; post_switch_cmd = npm run indexnow
|
|
185
|
+
; post_switch_cmd_2 = bash scripts/cf-purge.sh
|
package/dist/cli.js
CHANGED
|
@@ -201,6 +201,316 @@ async function runServe() {
|
|
|
201
201
|
process.exit(1);
|
|
202
202
|
}
|
|
203
203
|
}
|
|
204
|
+
const DEPLOY_SUBCOMMANDS = ['init', 'build', 'verify', 'switch', 'rollback', 'status', 'clean'];
|
|
205
|
+
function printDeployHelp() {
|
|
206
|
+
console.log(`
|
|
207
|
+
${pc.bold('hazo-env deploy')} — blue-green deployment orchestration
|
|
208
|
+
|
|
209
|
+
Usage:
|
|
210
|
+
hazo-env deploy init Create peer dir, clone repo, generate config, write state
|
|
211
|
+
hazo-env deploy build Run the full build pipeline on the standby dir
|
|
212
|
+
--stop-prod --branch=<b> --db=<env> --peer-dir=<path>
|
|
213
|
+
hazo-env deploy verify Re-run health/typecheck/migration checks on standby
|
|
214
|
+
hazo-env deploy switch Stop prod, start standby on the prod port, run post-switch hooks
|
|
215
|
+
hazo-env deploy rollback Reverse the last switch
|
|
216
|
+
hazo-env deploy status Show live/standby dirs, ports, PM2 state, disk/memory
|
|
217
|
+
hazo-env deploy clean Reclaim disk space from old builds and caches
|
|
218
|
+
--aggressive Also suggest system-level cleanup commands
|
|
219
|
+
`);
|
|
220
|
+
}
|
|
221
|
+
function formatDirStatus(info) {
|
|
222
|
+
const indicator = info.running ? pc.green(`✓ running (pid ${info.pid})`) : pc.dim('✗ not running');
|
|
223
|
+
return ` ${pc.bold(info.role.padEnd(8))} ${info.dir} ${pc.dim(`port ${info.port}`)} ${indicator}`;
|
|
224
|
+
}
|
|
225
|
+
async function runDeployStatus() {
|
|
226
|
+
const { resolveDeployConfig } = await import('./resolve/deploy.js');
|
|
227
|
+
const { runStatus } = await import('./deploy/status.js');
|
|
228
|
+
const { nodeCommandRunner } = await import('./deploy/runner.js');
|
|
229
|
+
const config = resolveDeployConfig();
|
|
230
|
+
const result = runStatus(config, nodeCommandRunner);
|
|
231
|
+
console.log(`\n${pc.bold(`hazo-env deploy status`)} ${pc.dim(result.appName)}\n`);
|
|
232
|
+
if (!result.stateFound) {
|
|
233
|
+
console.log(` ${pc.yellow('⚠')} No deploy state found yet — run "hazo-env deploy init" first.\n`);
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
if (result.live)
|
|
237
|
+
console.log(formatDirStatus(result.live));
|
|
238
|
+
if (result.standby)
|
|
239
|
+
console.log(formatDirStatus(result.standby));
|
|
240
|
+
console.log('');
|
|
241
|
+
console.log(` last switch: ${result.lastSwitch ?? pc.dim('(none)')}`);
|
|
242
|
+
console.log(` last build: ${result.lastBuild ?? pc.dim('(none)')}${result.lastBuildStatus ? ` ${result.lastBuildStatus === 'success' ? pc.green(result.lastBuildStatus) : pc.red(result.lastBuildStatus)}` : ''}`);
|
|
243
|
+
console.log('');
|
|
244
|
+
}
|
|
245
|
+
const diskLine = result.diskFreeMb !== undefined ? `${result.diskFreeMb} MB free` : pc.dim('(unknown)');
|
|
246
|
+
console.log(` disk: ${diskLine}`);
|
|
247
|
+
const memValue = result.memoryFreeMb !== undefined ? `${result.memoryFreeMb} MB free` : pc.dim('(unknown)');
|
|
248
|
+
const memLine = result.memoryLow ? `${pc.yellow(memValue)} ${pc.yellow(`⚠ below min_memory_mb (${result.minMemoryMb})`)}` : memValue;
|
|
249
|
+
console.log(` memory: ${memLine}`);
|
|
250
|
+
if (result.warnings.length) {
|
|
251
|
+
console.log('');
|
|
252
|
+
for (const warning of result.warnings) {
|
|
253
|
+
console.log(` ${pc.yellow('⚠')} ${warning}`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
console.log('');
|
|
257
|
+
}
|
|
258
|
+
async function runDeployInit() {
|
|
259
|
+
const { resolveDeployConfig } = await import('./resolve/deploy.js');
|
|
260
|
+
const { runInit } = await import('./deploy/init.js');
|
|
261
|
+
const { nodeCommandRunner } = await import('./deploy/runner.js');
|
|
262
|
+
const config = resolveDeployConfig();
|
|
263
|
+
console.log(`\n${pc.bold('hazo-env deploy init')} ${pc.dim(config.app_name)}\n`);
|
|
264
|
+
try {
|
|
265
|
+
const result = runInit(config, nodeCommandRunner);
|
|
266
|
+
console.log(` ${pc.green('✓')} Peer directory created: ${result.peerDir}`);
|
|
267
|
+
console.log(` ${pc.dim(`cloned from ${result.originUrl}`)}`);
|
|
268
|
+
console.log('');
|
|
269
|
+
console.log(` Next steps:`);
|
|
270
|
+
console.log(` cd ${result.peerDir}`);
|
|
271
|
+
console.log(` hazo-env deploy build`);
|
|
272
|
+
console.log('');
|
|
273
|
+
}
|
|
274
|
+
catch (e) {
|
|
275
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
276
|
+
console.error(`\n ${pc.red('✗')} ${msg}\n`);
|
|
277
|
+
process.exit(1);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
function formatBytes(bytes) {
|
|
281
|
+
if (bytes >= 1024 * 1024 * 1024)
|
|
282
|
+
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
|
|
283
|
+
if (bytes >= 1024 * 1024)
|
|
284
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
285
|
+
if (bytes >= 1024)
|
|
286
|
+
return `${(bytes / 1024).toFixed(1)} KB`;
|
|
287
|
+
return `${bytes} B`;
|
|
288
|
+
}
|
|
289
|
+
async function runDeployClean() {
|
|
290
|
+
const { resolveDeployConfig } = await import('./resolve/deploy.js');
|
|
291
|
+
const { runClean } = await import('./deploy/clean.js');
|
|
292
|
+
const { nodeCommandRunner } = await import('./deploy/runner.js');
|
|
293
|
+
const aggressive = args.includes('--aggressive');
|
|
294
|
+
const config = resolveDeployConfig();
|
|
295
|
+
console.log(`\n${pc.bold('hazo-env deploy clean')} ${pc.dim(config.app_name)}\n`);
|
|
296
|
+
const result = runClean(config, nodeCommandRunner, { aggressive });
|
|
297
|
+
for (const target of result.targets) {
|
|
298
|
+
if (target.skipped) {
|
|
299
|
+
console.log(` ${pc.dim('·')} ${target.target.padEnd(20)} ${pc.dim('(not present)')}`);
|
|
300
|
+
}
|
|
301
|
+
else if (target.error) {
|
|
302
|
+
console.log(` ${pc.red('✗')} ${target.target.padEnd(20)} ${pc.red(target.error)}`);
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
console.log(` ${pc.green('✓')} ${target.target.padEnd(20)} ${pc.dim(`reclaimed ${formatBytes(target.bytesReclaimed)}`)}`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
console.log('');
|
|
309
|
+
console.log(` ${pc.bold('total reclaimed:')} ${formatBytes(result.totalBytesReclaimed)}`);
|
|
310
|
+
console.log('');
|
|
311
|
+
if (result.warnings.length) {
|
|
312
|
+
for (const warning of result.warnings) {
|
|
313
|
+
console.log(` ${pc.yellow('⚠')} ${warning}`);
|
|
314
|
+
}
|
|
315
|
+
console.log('');
|
|
316
|
+
}
|
|
317
|
+
if (result.swapSuggestions.length) {
|
|
318
|
+
const memValue = result.memoryFreeMb !== undefined ? `${result.memoryFreeMb} MB free` : '(unknown)';
|
|
319
|
+
console.log(` ${pc.yellow('⚠')} Free memory (${memValue}) is below min_memory_mb (${result.minMemoryMb}).`);
|
|
320
|
+
console.log(` ${pc.dim('Consider increasing swap (not executed — requires sudo):')}`);
|
|
321
|
+
for (const cmd of result.swapSuggestions) {
|
|
322
|
+
console.log(` ${pc.dim('$')} ${cmd}`);
|
|
323
|
+
}
|
|
324
|
+
console.log('');
|
|
325
|
+
}
|
|
326
|
+
if (aggressive) {
|
|
327
|
+
console.log(` ${pc.dim('--aggressive: suggested system-level cleanup (not executed — requires sudo):')}`);
|
|
328
|
+
for (const cmd of result.systemSuggestions) {
|
|
329
|
+
console.log(` ${pc.dim('$')} ${cmd}`);
|
|
330
|
+
}
|
|
331
|
+
console.log('');
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
function flagValue(flagArgs, name) {
|
|
335
|
+
const idx = flagArgs.findIndex((a) => a === `--${name}`);
|
|
336
|
+
if (idx >= 0)
|
|
337
|
+
return flagArgs[idx + 1];
|
|
338
|
+
const inline = flagArgs.find((a) => a.startsWith(`--${name}=`));
|
|
339
|
+
return inline?.split('=').slice(1).join('=');
|
|
340
|
+
}
|
|
341
|
+
async function runDeployBuild() {
|
|
342
|
+
const { resolveDeployConfig } = await import('./resolve/deploy.js');
|
|
343
|
+
const { runBuild, BuildAbortError } = await import('./deploy/build.js');
|
|
344
|
+
const { nodeCommandRunner } = await import('./deploy/runner.js');
|
|
345
|
+
const stopProd = args.includes('--stop-prod');
|
|
346
|
+
const branch = flagValue(args, 'branch');
|
|
347
|
+
const db = flagValue(args, 'db');
|
|
348
|
+
const peerDir = flagValue(args, 'peer-dir');
|
|
349
|
+
const config = resolveDeployConfig();
|
|
350
|
+
console.log(`\n${pc.bold('hazo-env deploy build')} ${pc.dim(config.app_name)}\n`);
|
|
351
|
+
function printSteps(steps) {
|
|
352
|
+
for (const step of steps) {
|
|
353
|
+
const icon = step.status === 'pass' ? pc.green('✓') : pc.red('✗');
|
|
354
|
+
const detail = step.detail ? ` ${pc.dim(step.detail)}` : '';
|
|
355
|
+
console.log(` ${icon} ${step.name.padEnd(16)}${detail}`);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
try {
|
|
359
|
+
const result = runBuild(config, nodeCommandRunner, { stopProd, branch, db, peerDir });
|
|
360
|
+
console.log('');
|
|
361
|
+
printSteps(result.steps);
|
|
362
|
+
console.log('');
|
|
363
|
+
console.log(` ${pc.green('Build succeeded.')} staging: ${pc.bold(result.stagingUrl)} (db strategy: ${result.dbStrategyResolved})`);
|
|
364
|
+
console.log('');
|
|
365
|
+
console.log(` Next steps:`);
|
|
366
|
+
console.log(` hazo-env deploy verify`);
|
|
367
|
+
console.log(` hazo-env deploy switch`);
|
|
368
|
+
console.log('');
|
|
369
|
+
}
|
|
370
|
+
catch (e) {
|
|
371
|
+
if (e instanceof BuildAbortError) {
|
|
372
|
+
console.log('');
|
|
373
|
+
printSteps(e.steps);
|
|
374
|
+
console.log('');
|
|
375
|
+
console.error(` ${pc.red('✗ Build aborted:')} ${e.message}\n`);
|
|
376
|
+
process.exit(1);
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
380
|
+
console.error(`\n ${pc.red('✗')} ${msg}\n`);
|
|
381
|
+
process.exit(1);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
function verifyStatusIcon(status) {
|
|
385
|
+
if (status === 'pass')
|
|
386
|
+
return pc.green('✓');
|
|
387
|
+
if (status === 'skipped')
|
|
388
|
+
return pc.yellow('–');
|
|
389
|
+
return pc.red('✗');
|
|
390
|
+
}
|
|
391
|
+
async function runDeployVerify() {
|
|
392
|
+
const { resolveDeployConfig } = await import('./resolve/deploy.js');
|
|
393
|
+
const { runVerify } = await import('./deploy/verify.js');
|
|
394
|
+
const { nodeCommandRunner } = await import('./deploy/runner.js');
|
|
395
|
+
const config = resolveDeployConfig();
|
|
396
|
+
console.log(`\n${pc.bold('hazo-env deploy verify')} ${pc.dim(config.app_name)}\n`);
|
|
397
|
+
const result = runVerify(config, nodeCommandRunner);
|
|
398
|
+
const nameWidth = Math.max(...result.checks.map((c) => c.name.length)) + 2;
|
|
399
|
+
for (const check of result.checks) {
|
|
400
|
+
const icon = verifyStatusIcon(check.status);
|
|
401
|
+
console.log(` ${icon} ${check.name.padEnd(nameWidth)}${pc.dim(check.detail)}`);
|
|
402
|
+
}
|
|
403
|
+
console.log('');
|
|
404
|
+
if (result.allPassed) {
|
|
405
|
+
console.log(` ${pc.green('All checks passed.')} Next: ${pc.bold('hazo-env deploy switch')}\n`);
|
|
406
|
+
}
|
|
407
|
+
else {
|
|
408
|
+
console.log(` ${pc.red('Some checks failed.')} Fix the issue(s) above, then re-run ${pc.bold('hazo-env deploy verify')}.\n`);
|
|
409
|
+
process.exit(1);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
async function runDeploySwitch() {
|
|
413
|
+
const { resolveDeployConfig } = await import('./resolve/deploy.js');
|
|
414
|
+
const { runSwitch, SwitchAbortError } = await import('./deploy/switch.js');
|
|
415
|
+
const { nodeCommandRunner } = await import('./deploy/runner.js');
|
|
416
|
+
const config = resolveDeployConfig();
|
|
417
|
+
console.log(`\n${pc.bold('hazo-env deploy switch')} ${pc.dim(config.app_name)}\n`);
|
|
418
|
+
try {
|
|
419
|
+
const result = runSwitch(config, nodeCommandRunner);
|
|
420
|
+
console.log(` ${pc.green('✓')} deleted live PM2 registration "${config.app_name}" in ${result.previousLiveDir}`);
|
|
421
|
+
console.log(` ${result.stagingStopped ? pc.green('✓') : pc.dim('·')} staging process "${config.app_name}-staging"` +
|
|
422
|
+
`${result.stagingStopped ? ' stopped' : ' was not running (skipped)'}`);
|
|
423
|
+
console.log(` ${pc.green('✓')} started "${config.app_name}" on port ${result.prodPort} in ${result.newLiveDir}`);
|
|
424
|
+
console.log('');
|
|
425
|
+
if (result.postSwitchHooks.length) {
|
|
426
|
+
console.log(` post-switch hooks:`);
|
|
427
|
+
for (const hook of result.postSwitchHooks) {
|
|
428
|
+
const icon = hook.status === 'pass' ? pc.green('✓') : pc.red('✗');
|
|
429
|
+
console.log(` ${icon} ${hook.cmd}${hook.detail ? ` ${pc.dim(hook.detail)}` : ''}`);
|
|
430
|
+
}
|
|
431
|
+
console.log('');
|
|
432
|
+
}
|
|
433
|
+
console.log(` ${pc.green('Switch complete.')} live: ${pc.bold(result.newLiveDir)} (port ${result.prodPort})`);
|
|
434
|
+
console.log(` standby: ${result.newStandbyDir}`);
|
|
435
|
+
console.log('');
|
|
436
|
+
}
|
|
437
|
+
catch (e) {
|
|
438
|
+
if (e instanceof SwitchAbortError) {
|
|
439
|
+
console.error(`\n ${pc.red('✗')} ${e.message}\n`);
|
|
440
|
+
process.exit(1);
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
444
|
+
console.error(`\n ${pc.red('✗')} ${msg}\n`);
|
|
445
|
+
process.exit(1);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
async function runDeployRollback() {
|
|
449
|
+
const { resolveDeployConfig } = await import('./resolve/deploy.js');
|
|
450
|
+
const { runRollback, RollbackAbortError } = await import('./deploy/rollback.js');
|
|
451
|
+
const { nodeCommandRunner } = await import('./deploy/runner.js');
|
|
452
|
+
const config = resolveDeployConfig();
|
|
453
|
+
console.log(`\n${pc.bold('hazo-env deploy rollback')} ${pc.dim(config.app_name)}\n`);
|
|
454
|
+
try {
|
|
455
|
+
const result = runRollback(config, nodeCommandRunner);
|
|
456
|
+
console.log(` ${pc.green('✓')} started "${config.app_name}" on port ${result.prodPort} in ${result.restoredLiveDir}`);
|
|
457
|
+
console.log('');
|
|
458
|
+
console.log(` ${pc.green('rolled back')} to ${pc.bold(result.restoredLiveDir)}, running on port ${result.prodPort}.`);
|
|
459
|
+
console.log(` standby: ${result.newStandbyDir}`);
|
|
460
|
+
console.log('');
|
|
461
|
+
}
|
|
462
|
+
catch (e) {
|
|
463
|
+
if (e instanceof RollbackAbortError) {
|
|
464
|
+
console.error(`\n ${pc.red('✗')} ${e.message}\n`);
|
|
465
|
+
process.exit(1);
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
469
|
+
console.error(`\n ${pc.red('✗')} ${msg}\n`);
|
|
470
|
+
process.exit(1);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
async function runDeploy() {
|
|
474
|
+
const subCmd = args[0];
|
|
475
|
+
if (!subCmd || subCmd === '--help' || subCmd === '-h') {
|
|
476
|
+
printDeployHelp();
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
if (!DEPLOY_SUBCOMMANDS.includes(subCmd)) {
|
|
480
|
+
console.error(pc.red(`Error: unknown deploy subcommand "${subCmd}"`));
|
|
481
|
+
printDeployHelp();
|
|
482
|
+
process.exit(1);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
if (subCmd === 'init') {
|
|
486
|
+
await runDeployInit();
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
if (subCmd === 'status') {
|
|
490
|
+
await runDeployStatus();
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
if (subCmd === 'clean') {
|
|
494
|
+
await runDeployClean();
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
if (subCmd === 'build') {
|
|
498
|
+
await runDeployBuild();
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
if (subCmd === 'verify') {
|
|
502
|
+
await runDeployVerify();
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
if (subCmd === 'switch') {
|
|
506
|
+
await runDeploySwitch();
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
if (subCmd === 'rollback') {
|
|
510
|
+
await runDeployRollback();
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
204
514
|
function printHelp() {
|
|
205
515
|
console.log(`
|
|
206
516
|
${pc.bold('hazo-env')} — environment resolver CLI
|
|
@@ -222,6 +532,8 @@ Usage:
|
|
|
222
532
|
--show-values Print env values in the diff (hidden by default)
|
|
223
533
|
hazo-env serve Start the envsync HTTP service (HAZO_ENVSYNC_TOKEN required)
|
|
224
534
|
--port <n> --bind <host> Override HAZO_ENVSYNC_PORT / HAZO_ENVSYNC_BIND
|
|
535
|
+
hazo-env deploy [subcommand] Blue-green deployment orchestration (see "deploy --help")
|
|
536
|
+
init | build | verify | switch | rollback | status | clean
|
|
225
537
|
`);
|
|
226
538
|
}
|
|
227
539
|
(async () => {
|
|
@@ -238,6 +550,9 @@ Usage:
|
|
|
238
550
|
else if (command === 'serve') {
|
|
239
551
|
await runServe();
|
|
240
552
|
}
|
|
553
|
+
else if (command === 'deploy') {
|
|
554
|
+
await runDeploy();
|
|
555
|
+
}
|
|
241
556
|
else {
|
|
242
557
|
printHelp();
|
|
243
558
|
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { CommandRunner, DeployConfig } from './types.js';
|
|
2
|
+
export interface RunBuildOptions {
|
|
3
|
+
/** Directory to treat as "current" (the standby dir being built). Defaults to process.cwd(). */
|
|
4
|
+
cwd?: string;
|
|
5
|
+
/** Stop the live PM2 process to free memory if the memory check fails. */
|
|
6
|
+
stopProd?: boolean;
|
|
7
|
+
/** Git branch to pull. Defaults to config.branch. */
|
|
8
|
+
branch?: string;
|
|
9
|
+
/** Postgres env name to resolve against [db.<env>] (shared db_strategy only). */
|
|
10
|
+
db?: string;
|
|
11
|
+
/** Explicit peer dir override. Takes precedence over the state file's live_dir. */
|
|
12
|
+
peerDir?: string;
|
|
13
|
+
/** Health check retry count. Default: 8. */
|
|
14
|
+
healthCheckAttempts?: number;
|
|
15
|
+
/** Delay between health check retries, in ms. Default: 1500. */
|
|
16
|
+
healthCheckDelayMs?: number;
|
|
17
|
+
}
|
|
18
|
+
export type BuildStepStatus = 'pass' | 'fail';
|
|
19
|
+
export interface BuildStepResult {
|
|
20
|
+
name: string;
|
|
21
|
+
status: BuildStepStatus;
|
|
22
|
+
detail?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface RunBuildResult {
|
|
25
|
+
steps: BuildStepResult[];
|
|
26
|
+
stagingPort: number;
|
|
27
|
+
stagingUrl: string;
|
|
28
|
+
dbStrategyResolved: 'copy' | 'shared';
|
|
29
|
+
warnings: string[];
|
|
30
|
+
}
|
|
31
|
+
/** Thrown when any pipeline step fails. Carries the steps completed so far for reporting. */
|
|
32
|
+
export declare class BuildAbortError extends Error {
|
|
33
|
+
readonly steps: BuildStepResult[];
|
|
34
|
+
constructor(message: string, steps: BuildStepResult[]);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Re-root `targetPath` (known to live under `fromDir`) onto `toDir`: compute
|
|
38
|
+
* `targetPath`'s path relative to `fromDir` and rejoin it onto `toDir`.
|
|
39
|
+
*
|
|
40
|
+
* Falls back to `targetPath` unchanged when it isn't actually inside
|
|
41
|
+
* `fromDir` — e.g. an absolute `[data] root` (or an absolute db path)
|
|
42
|
+
* configured identically for both live and standby legitimately points at
|
|
43
|
+
* one shared location rather than a per-directory one, and in that case
|
|
44
|
+
* there's nothing to re-root; callers treat an unchanged result as "shared,
|
|
45
|
+
* skip the copy" rather than attempting a same-path no-op command.
|
|
46
|
+
*
|
|
47
|
+
* Exported so `deploy verify` (src/deploy/verify.ts) can re-root the standby
|
|
48
|
+
* sqlite db path onto the live dir for its migration-parity check, using the
|
|
49
|
+
* exact same live/standby path mapping this file's own db-setup step relies
|
|
50
|
+
* on, instead of duplicating it.
|
|
51
|
+
*/
|
|
52
|
+
export declare function reroot(fromDir: string, toDir: string, targetPath: string): string;
|
|
53
|
+
/**
|
|
54
|
+
* Resolve a possibly-relative path against `baseDir` without touching an
|
|
55
|
+
* already-absolute one. Exported for the same reason as `reroot` above.
|
|
56
|
+
*/
|
|
57
|
+
export declare function toAbsolute(baseDir: string, maybePath: string): string;
|
|
58
|
+
export interface HealthCheckResult {
|
|
59
|
+
ok: boolean;
|
|
60
|
+
lastCode: string;
|
|
61
|
+
attempts: number;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Poll `url` with curl up to `attempts` times, `delayMs` apart, succeeding on
|
|
65
|
+
* the first 200. `pm2 start` returns before the app has actually finished
|
|
66
|
+
* booting, so a single immediate check would almost always see
|
|
67
|
+
* connection-refused on a real deploy. Deliberately omits curl's `-f` (which
|
|
68
|
+
* makes curl itself exit non-zero — and `exec` throw — on a non-2xx
|
|
69
|
+
* response): `-w '%{http_code}'` needs the real status code in stdout so
|
|
70
|
+
* this function can judge pass/fail itself rather than only ever seeing
|
|
71
|
+
* "did curl's own exit code say ok".
|
|
72
|
+
*
|
|
73
|
+
* Exported so `deploy verify` (src/deploy/verify.ts) can reuse the exact same
|
|
74
|
+
* poll/retry logic for its own health check against the already-running
|
|
75
|
+
* staging app, instead of reimplementing it with a second curl-parsing
|
|
76
|
+
* helper. `deploy verify` passes a smaller `attempts`/`delayMs` budget by
|
|
77
|
+
* default — see verify.ts's doc comment for why.
|
|
78
|
+
*/
|
|
79
|
+
export declare function checkHealth(runner: CommandRunner, url: string, attempts: number, delayMs: number): HealthCheckResult;
|
|
80
|
+
/**
|
|
81
|
+
* Run the full build pipeline against `cwd` (the standby directory). Steps
|
|
82
|
+
* run in the order documented in the task-5 brief (with db-strategy
|
|
83
|
+
* resolution/validation moved earlier, before git pull, so a bad `--db`
|
|
84
|
+
* fails fast); any failure throws `BuildAbortError` immediately. The state
|
|
85
|
+
* file's `last_build`/`last_build_status` is updated regardless of outcome
|
|
86
|
+
* (success path included), via a `finally` so a thrown error is never
|
|
87
|
+
* swallowed.
|
|
88
|
+
*/
|
|
89
|
+
export declare function runBuild(config: DeployConfig, runner: CommandRunner, opts?: RunBuildOptions): RunBuildResult;
|
|
90
|
+
//# sourceMappingURL=build.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../../src/deploy/build.ts"],"names":[],"mappings":"AAuBA,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAI9D,MAAM,WAAW,eAAe;IAC9B,gGAAgG;IAChG,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,0EAA0E;IAC1E,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,qDAAqD;IACrD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,iFAAiF;IACjF,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,mFAAmF;IACnF,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,4CAA4C;IAC5C,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,gEAAgE;IAChE,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,MAAM,CAAC;AAE9C,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,eAAe,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,eAAe,EAAE,CAAC;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,kBAAkB,EAAE,MAAM,GAAG,QAAQ,CAAC;IACtC,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,6FAA6F;AAC7F,qBAAa,eAAgB,SAAQ,KAAK;IACxC,QAAQ,CAAC,KAAK,EAAE,eAAe,EAAE,CAAC;gBAEtB,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,eAAe,EAAE;CAKtD;AAKD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAMjF;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAErE;AAiBD,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,OAAO,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,iBAAiB,CAQpH;AAED;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,aAAa,EAAE,IAAI,GAAE,eAAoB,GAAG,cAAc,CA+NhH"}
|