hazo_env 0.11.0 → 0.14.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 +163 -0
- package/README.md +1 -1
- package/config/hazo_env_config.ini.sample +19 -0
- package/dist/cli.js +44 -1
- package/dist/deploy/build.d.ts +4 -3
- package/dist/deploy/build.d.ts.map +1 -1
- package/dist/deploy/build.js +105 -15
- package/dist/deploy/index.d.ts +8 -0
- package/dist/deploy/index.d.ts.map +1 -1
- package/dist/deploy/index.js +17 -0
- package/dist/deploy/init.d.ts.map +1 -1
- package/dist/deploy/init.js +51 -0
- package/dist/deploy/pm2-start.d.ts +36 -24
- package/dist/deploy/pm2-start.d.ts.map +1 -1
- package/dist/deploy/pm2-start.js +116 -36
- package/dist/deploy/rollback.d.ts +11 -0
- package/dist/deploy/rollback.d.ts.map +1 -1
- package/dist/deploy/rollback.js +42 -7
- package/dist/deploy/sidecar.d.ts +74 -0
- package/dist/deploy/sidecar.d.ts.map +1 -0
- package/dist/deploy/sidecar.js +156 -0
- package/dist/deploy/status.d.ts +18 -0
- package/dist/deploy/status.d.ts.map +1 -1
- package/dist/deploy/status.js +29 -0
- package/dist/deploy/switch.d.ts +23 -1
- package/dist/deploy/switch.d.ts.map +1 -1
- package/dist/deploy/switch.js +40 -1
- package/dist/deploy/types.d.ts +6 -0
- package/dist/deploy/types.d.ts.map +1 -1
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +20 -2
- package/dist/resolve/connect.d.ts.map +1 -1
- package/dist/resolve/connect.js +36 -1
- package/dist/resolve/deploy.d.ts.map +1 -1
- package/dist/resolve/deploy.js +44 -7
- package/dist/types/index.d.ts +12 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +10 -10
package/CHANGE_LOG.md
CHANGED
|
@@ -1,5 +1,168 @@
|
|
|
1
1
|
# hazo_env — Change Log
|
|
2
2
|
|
|
3
|
+
## 0.14.0 — 2026-11-09
|
|
4
|
+
|
|
5
|
+
New: `resolveConnectConfig()` (`src/resolve/connect.ts`) and `doctor()`'s
|
|
6
|
+
live-probe path now support a third `[db.<env>]` `type`: `pg` — a direct
|
|
7
|
+
Postgres connection via `hazo_connect`'s `pg` dialect (`PgAdapter`), as
|
|
8
|
+
opposed to `postgrest`, which fronts Postgres through a PostgREST HTTP
|
|
9
|
+
layer. Found while migrating the blogsketch platform app onto hazo_env:
|
|
10
|
+
its prod DB is plain Postgres via `pg` (not PostgREST) specifically because
|
|
11
|
+
only the `pg`/`PgAdapter` dialect exposes a real `.raw()` escape hatch,
|
|
12
|
+
which both hazo_blog's native-tsvector full-text search and hazo_jobs' own
|
|
13
|
+
`SELECT ... FOR UPDATE SKIP LOCKED` job-claim queries depend on — and
|
|
14
|
+
`resolveConnectConfig()` had no type value for that at all, a real gap,
|
|
15
|
+
not a style choice.
|
|
16
|
+
|
|
17
|
+
- **`DbEnvConfig` (`src/types/index.ts`)**: `type` gains `'pg'`; new
|
|
18
|
+
optional `pg?: { connectionString: string; ssl?: boolean | 'require' }`
|
|
19
|
+
field, mirroring the existing `sqlite`/`postgrest` shapes.
|
|
20
|
+
- **`resolveConnectConfig()`**: new `type = pg` branch. Reads
|
|
21
|
+
`connection_string` (through the same `${VAR}` placeholder substitution
|
|
22
|
+
as every other secret-shaped ini value) and optional `ssl` (`require` or
|
|
23
|
+
`false`; omitted means "let hazo_connect/PgAdapter use its own default,
|
|
24
|
+
which is no TLS"). Throws `HAZO_ENV_CONFIG_MISSING_DB_CONNECTION_STRING`
|
|
25
|
+
when `connection_string` is absent, and `HAZO_ENV_CONFIG_UNKNOWN_SSL_VALUE`
|
|
26
|
+
for any `ssl` value other than `require`/`false` — same "fail at
|
|
27
|
+
config-resolve time with a named error" precedent as the existing sqlite
|
|
28
|
+
`driver` validation. The final unknown-type error message now lists
|
|
29
|
+
`sqlite`, `postgrest`, and `pg`.
|
|
30
|
+
- **`doctor()`'s `--probe` path (`src/doctor.ts`)**: a `pg`-typed env is now
|
|
31
|
+
probed the same way a `sqlite`-typed one is — `createHazoConnect({ type:
|
|
32
|
+
'pg', connectionString, ssl })` then `SELECT 1` via `rawQuery()` — behind
|
|
33
|
+
the same existing prod-without-`--all` refusal guard. PostgREST envs are
|
|
34
|
+
still not probed (no behavior change there).
|
|
35
|
+
- `config/hazo_env_config.ini.sample` and `README.md`'s config reference
|
|
36
|
+
gain a commented `pg` example / the updated type list.
|
|
37
|
+
- Added `resolveConnectConfig — pg shape` tests in `__tests__/resolver.test.ts`
|
|
38
|
+
(resolve, missing-driver-default, missing `connection_string`, bad `ssl`
|
|
39
|
+
value).
|
|
40
|
+
|
|
41
|
+
No changes to the `postgrest`/`sqlite` branches — purely additive, hence a
|
|
42
|
+
minor bump.
|
|
43
|
+
|
|
44
|
+
## 0.13.1 — 2026-11-09
|
|
45
|
+
|
|
46
|
+
Fix: `startNamedPm2Process` (`src/deploy/pm2-start.ts`) hard-coded the
|
|
47
|
+
ecosystem file lookup to the literal name `ecosystem.config.js`. 0.13.0's own
|
|
48
|
+
multi-app/sidecar support was built explicitly for kinstripe's
|
|
49
|
+
`ecosystem.config.cjs` (see that release's own changelog entry and
|
|
50
|
+
`sidecar.ts`'s header, both of which name it) — but the lookup itself was
|
|
51
|
+
never updated, so every `startNamedPm2Process` call (`deploy build`'s
|
|
52
|
+
staging start, `deploy switch`/`rollback`'s promotion, and
|
|
53
|
+
`syncSidecar`'s recreate path) against a `.cjs`-named app would throw `No
|
|
54
|
+
ecosystem.config.js found` even though a perfectly valid ecosystem file
|
|
55
|
+
existed one extension over. Found while wiring kinstripe's own
|
|
56
|
+
`config/hazo_env_config.ini` `[deploy]` section onto 0.13.0 — a real
|
|
57
|
+
blocker for the exact app this feature was built for.
|
|
58
|
+
|
|
59
|
+
- New `ECOSYSTEM_CONFIG_FILENAMES` list + `resolveEcosystemConfigPath(dir)`
|
|
60
|
+
helper in `pm2-start.ts`: tries `ecosystem.config.cjs` first, then
|
|
61
|
+
`ecosystem.config.js`, returning the first that exists; throws a clear
|
|
62
|
+
error naming both candidates when neither is found. `.cjs` is checked
|
|
63
|
+
first since that's the extension a `"type": "module"` package (kinstripe's
|
|
64
|
+
own `package.json`) must use to keep an `module.exports = {...}` file
|
|
65
|
+
parsed as CommonJS rather than failing as ESM.
|
|
66
|
+
- No behavior change for single-`.js` consumers (gotimer, blogsketch) —
|
|
67
|
+
`.js` is still found exactly as before when no `.cjs` sibling exists.
|
|
68
|
+
- Added regression tests in `__tests__/deploy-pm2-start.test.ts` and
|
|
69
|
+
`__tests__/deploy-sidecar.test.ts` pinning `.cjs`-only resolution and
|
|
70
|
+
`.cjs`-over-`.js` precedence when both exist.
|
|
71
|
+
|
|
72
|
+
## 0.13.0 — 2026-11-09
|
|
73
|
+
|
|
74
|
+
New: multi-app ecosystem file support for `hazo-env deploy`, driven by two
|
|
75
|
+
real consuming apps — kinstripe's `ecosystem.config.cjs` and this monorepo's
|
|
76
|
+
own blogsketch `ecosystem.config.js` — each of which declares one critical
|
|
77
|
+
web app (cluster mode) PLUS one or more non-web sidecar processes (a
|
|
78
|
+
cron/scheduler/jobs worker in both apps' cases; kinstripe's also runs a
|
|
79
|
+
websocket server and a PDF-render pool) in the SAME ecosystem file. Until
|
|
80
|
+
now, `startNamedPm2Process` (`src/deploy/pm2-start.ts`) hard-threw on any
|
|
81
|
+
ecosystem file with more than one `apps` entry — a real blocker for both
|
|
82
|
+
apps' migration onto this module.
|
|
83
|
+
|
|
84
|
+
- **`startNamedPm2Process` (`src/deploy/pm2-start.ts`)**: `StartNamedPm2ProcessOptions`
|
|
85
|
+
gains an optional `selectAppName?: string`. When given, the template app is
|
|
86
|
+
picked from `ecosystem.apps.find(a => a.name === selectAppName)` instead of
|
|
87
|
+
assuming/requiring a single-app file, throwing a clear error if nothing
|
|
88
|
+
matches. When omitted, behavior is byte-for-byte unchanged — still throws
|
|
89
|
+
if `apps.length !== 1` — so `build.ts`/`switch.ts`/`rollback.ts`, none of
|
|
90
|
+
which pass it, see no behavior change. `port` is now also optional: when
|
|
91
|
+
omitted, NO `PORT` key is injected into either `env` or `env_production` on
|
|
92
|
+
the cloned app config at all, leaving whatever the template already sets
|
|
93
|
+
(or doesn't) completely untouched — sidecars (a cron/jobs worker, say)
|
|
94
|
+
typically don't listen on a port the way the web app does.
|
|
95
|
+
|
|
96
|
+
- **New `src/deploy/sidecar.ts`**: a parallel, independent primitive for
|
|
97
|
+
non-web-facing pm2 processes that move with whichever directory is
|
|
98
|
+
currently live but are NEVER themselves blue-green-swapped — no
|
|
99
|
+
live/standby pair, no state-file tracking, no `-staging` clone, no health
|
|
100
|
+
check. Duplicating a scheduler/jobs-worker into a standby dir the way the
|
|
101
|
+
web app is duplicated would double-claim and double-execute background
|
|
102
|
+
jobs against the same shared production DB, a real correctness bug, not
|
|
103
|
+
just wasted work — this module exists specifically to prevent that.
|
|
104
|
+
Exports `syncSidecar(runner, { dir, name })`, which NEVER throws (every
|
|
105
|
+
failure path returns `{ action: 'failed', warning: <message> }` instead,
|
|
106
|
+
since a sidecar problem must never abort the caller's web-app promotion):
|
|
107
|
+
if the named pm2 process is found, online, and its `pm_cwd` already
|
|
108
|
+
matches `dir`, it's reloaded in place (`pm2 reload <name> --update-env`);
|
|
109
|
+
otherwise (not found, not online, pointing at the OTHER — just-demoted —
|
|
110
|
+
dir, or a failed reload) it's tolerant-deleted and freshly started against
|
|
111
|
+
`dir` via `startNamedPm2Process`'s new `selectAppName` option, with no
|
|
112
|
+
`port`. `syncSidecars(runner, dir, sidecarNames)` maps this over a list.
|
|
113
|
+
Also exports `findPm2ProcessByName`, matching by exact registered pm2
|
|
114
|
+
name rather than by cwd (unlike `status.ts`'s `findPm2ProcessForDir`) —
|
|
115
|
+
right after a switch, a sidecar is still registered against the
|
|
116
|
+
just-demoted dir, so cwd-matching would miss it entirely.
|
|
117
|
+
|
|
118
|
+
- **`DeployConfig` (`src/deploy/types.ts`) / `[deploy]` resolver
|
|
119
|
+
(`src/resolve/deploy.ts`)**: new `sidecar_apps: string[]` field — other pm2
|
|
120
|
+
app names in the same ecosystem file that move with the live dir but are
|
|
121
|
+
never blue-green-swapped themselves. Parsed from a new comma-separated INI
|
|
122
|
+
key, `sidecar_apps = name1, name2`; trimmed, empty entries dropped;
|
|
123
|
+
defaults to `[]` when absent.
|
|
124
|
+
|
|
125
|
+
- **`deploy switch` / `deploy rollback` (`src/deploy/switch.ts` /
|
|
126
|
+
`src/deploy/rollback.ts`)**: after the existing `pm2 save` step, both now
|
|
127
|
+
call `syncSidecars(runner, <the dir that just became live>,
|
|
128
|
+
config.sidecar_apps)` unconditionally, adding `sidecars:
|
|
129
|
+
SidecarSyncResult[]` to `RunSwitchResult`/`RunRollbackResult`. A sidecar
|
|
130
|
+
failure is reported in this array, never thrown — it cannot affect
|
|
131
|
+
`runSwitch`/`runRollback`'s own success/throw behavior, since by this
|
|
132
|
+
point the web app's own promotion has already succeeded.
|
|
133
|
+
|
|
134
|
+
- **`deploy status` (`src/deploy/status.ts`)**: `DeployStatusResult` gains
|
|
135
|
+
`sidecars: Array<{ name, running, pid?, pm2Status?, pm_cwd?, stale }>`,
|
|
136
|
+
populated by looking up each `config.sidecar_apps` name by pm2 process name
|
|
137
|
+
(not by cwd) in the already-fetched process list. `stale: true` means the
|
|
138
|
+
process was found but its `pm_cwd` does NOT match the current live dir — a
|
|
139
|
+
signal the sidecar hasn't been synced onto the current live dir yet.
|
|
140
|
+
Always `[]` when no state file exists yet (same guard as `live`/`standby`).
|
|
141
|
+
|
|
142
|
+
- **CLI (`src/cli.ts`)**: `deploy status` prints each sidecar's running/stale
|
|
143
|
+
state after the live/standby lines; `deploy switch`/`deploy rollback`
|
|
144
|
+
print each sidecar's name/action/detail after their existing per-result
|
|
145
|
+
output — a `failed` sidecar prints a yellow `⚠` warning line but never
|
|
146
|
+
aborts or changes the process exit code, which is controlled solely by the
|
|
147
|
+
web-app promotion's own result.
|
|
148
|
+
|
|
149
|
+
- `config/hazo_env_config.ini.sample` documents the new `sidecar_apps` key
|
|
150
|
+
under `[deploy]`.
|
|
151
|
+
|
|
152
|
+
Backward compatible: every change here is additive. `sidecar_apps` defaults
|
|
153
|
+
to `[]`, which makes `syncSidecars` a no-op (no extra pm2 commands issued at
|
|
154
|
+
all) — existing single-app deployments (gotimer) see zero behavior change.
|
|
155
|
+
|
|
156
|
+
## 0.12.1 — 2026-11-07
|
|
157
|
+
|
|
158
|
+
Fix: `deploy build` health check now retries on connection-refused. `checkHealth`
|
|
159
|
+
used `runner.exec` (throws on non-zero curl exit) instead of `runner.spawn`
|
|
160
|
+
(returns the result), so the very first attempt — which almost always hits
|
|
161
|
+
connection-refused because PM2 returns before the app finishes booting — aborted
|
|
162
|
+
the entire health check with zero retries. Switched to `runner.spawn` and
|
|
163
|
+
encoded curl's own exit code as `curl_exit_<N>` in the `lastCode` field so the
|
|
164
|
+
retry loop and error message both work correctly.
|
|
165
|
+
|
|
3
166
|
## 0.12.0 — 2026-10-24
|
|
4
167
|
|
|
5
168
|
New: `hazo-env deploy` — a full blue-green deployment CLI for single-VM sites
|
package/README.md
CHANGED
|
@@ -201,7 +201,7 @@ See `config/hazo_env_config.ini.sample` for the full annotated template.
|
|
|
201
201
|
Key sections:
|
|
202
202
|
- `[env]` — `pattern` (comma-separated env names), `app` (app identifier)
|
|
203
203
|
- `[data]` — `root` (data_root; default `app_data`)
|
|
204
|
-
- `[db.<env>]` — `type` (`sqlite` | `postgrest`), `database_path` / `base_url`
|
|
204
|
+
- `[db.<env>]` — `type` (`sqlite` | `postgrest` | `pg`), `database_path` / `base_url` + `api_key` / `connection_string` + `ssl`
|
|
205
205
|
- `[host.<env>]` — `location` (`local` | `remote`)
|
|
206
206
|
|
|
207
207
|
Secret placeholders use `${ENV_VAR_NAME}` syntax — hazo_env substitutes them from `.env.local` at runtime.
|
|
@@ -64,6 +64,19 @@ type = postgrest
|
|
|
64
64
|
base_url = ${POSTGREST_PROD_URL}
|
|
65
65
|
api_key = ${POSTGREST_API_KEY}
|
|
66
66
|
|
|
67
|
+
; Alternative to [db.prod] above: a direct Postgres connection via
|
|
68
|
+
; hazo_connect's `pg` dialect (PgAdapter), instead of fronting Postgres
|
|
69
|
+
; through PostgREST. Pick this when the app needs `pg`'s `.raw()` escape
|
|
70
|
+
; hatch (native-tsvector full-text search, `SELECT ... FOR UPDATE SKIP
|
|
71
|
+
; LOCKED` job-claim queries, ...) — postgrest/supabase adapters have no
|
|
72
|
+
; `raw()` at all.
|
|
73
|
+
; [db.prod]
|
|
74
|
+
; type = pg
|
|
75
|
+
; connection_string = ${DATABASE_URL}
|
|
76
|
+
; ; Optional. "require" for a remote/managed Postgres over TLS. Omit (or
|
|
77
|
+
; ; "false") for a same-host loopback connection with no TLS.
|
|
78
|
+
; ssl = require
|
|
79
|
+
|
|
67
80
|
[host.dev]
|
|
68
81
|
; Hint about where the dev environment runs (local/remote)
|
|
69
82
|
location = local
|
|
@@ -183,3 +196,9 @@ build_cmd = npm run build
|
|
|
183
196
|
; order after the unsuffixed post_switch_cmd. Omit entirely to run none.
|
|
184
197
|
; post_switch_cmd = npm run indexnow
|
|
185
198
|
; post_switch_cmd_2 = bash scripts/cf-purge.sh
|
|
199
|
+
|
|
200
|
+
; Other pm2 app names in this SAME ecosystem file that move with the live dir
|
|
201
|
+
; but are never blue-green-swapped themselves (a cron/scheduler/jobs worker,
|
|
202
|
+
; websocket server, PDF-render pool, ...) — see src/deploy/sidecar.ts.
|
|
203
|
+
; Comma-separated. Default: none.
|
|
204
|
+
; sidecar_apps = myapp-scheduler, myapp-pdf-pool
|
package/dist/cli.js
CHANGED
|
@@ -212,6 +212,7 @@ Usage:
|
|
|
212
212
|
--stop-prod --branch=<b> --db=<env> --peer-dir=<path>
|
|
213
213
|
hazo-env deploy verify Re-run health/typecheck/migration checks on standby
|
|
214
214
|
hazo-env deploy switch Stop prod, start standby on the prod port, run post-switch hooks
|
|
215
|
+
--force Bypass the "standby must have a successful recent build" check
|
|
215
216
|
hazo-env deploy rollback Reverse the last switch
|
|
216
217
|
hazo-env deploy status Show live/standby dirs, ports, PM2 state, disk/memory
|
|
217
218
|
hazo-env deploy clean Reclaim disk space from old builds and caches
|
|
@@ -222,6 +223,12 @@ function formatDirStatus(info) {
|
|
|
222
223
|
const indicator = info.running ? pc.green(`✓ running (pid ${info.pid})`) : pc.dim('✗ not running');
|
|
223
224
|
return ` ${pc.bold(info.role.padEnd(8))} ${info.dir} ${pc.dim(`port ${info.port}`)} ${indicator}`;
|
|
224
225
|
}
|
|
226
|
+
/** Sidecars have no port (see sidecar.ts) — a short variant of formatDirStatus, flagging staleness instead. */
|
|
227
|
+
function formatSidecarStatus(info) {
|
|
228
|
+
const indicator = info.running ? pc.green(`✓ running (pid ${info.pid})`) : pc.dim('✗ not running');
|
|
229
|
+
const staleNote = info.stale ? ` ${pc.yellow('⚠ stale: pm_cwd mismatch')}` : '';
|
|
230
|
+
return ` ${pc.bold('sidecar'.padEnd(8))} ${info.name} ${indicator}${staleNote}`;
|
|
231
|
+
}
|
|
225
232
|
async function runDeployStatus() {
|
|
226
233
|
const { resolveDeployConfig } = await import('./resolve/deploy.js');
|
|
227
234
|
const { runStatus } = await import('./deploy/status.js');
|
|
@@ -237,6 +244,9 @@ async function runDeployStatus() {
|
|
|
237
244
|
console.log(formatDirStatus(result.live));
|
|
238
245
|
if (result.standby)
|
|
239
246
|
console.log(formatDirStatus(result.standby));
|
|
247
|
+
for (const sidecar of result.sidecars) {
|
|
248
|
+
console.log(formatSidecarStatus(sidecar));
|
|
249
|
+
}
|
|
240
250
|
console.log('');
|
|
241
251
|
console.log(` last switch: ${result.lastSwitch ?? pc.dim('(none)')}`);
|
|
242
252
|
console.log(` last build: ${result.lastBuild ?? pc.dim('(none)')}${result.lastBuildStatus ? ` ${result.lastBuildStatus === 'success' ? pc.green(result.lastBuildStatus) : pc.red(result.lastBuildStatus)}` : ''}`);
|
|
@@ -413,14 +423,21 @@ async function runDeploySwitch() {
|
|
|
413
423
|
const { resolveDeployConfig } = await import('./resolve/deploy.js');
|
|
414
424
|
const { runSwitch, SwitchAbortError } = await import('./deploy/switch.js');
|
|
415
425
|
const { nodeCommandRunner } = await import('./deploy/runner.js');
|
|
426
|
+
const force = args.includes('--force');
|
|
416
427
|
const config = resolveDeployConfig();
|
|
417
428
|
console.log(`\n${pc.bold('hazo-env deploy switch')} ${pc.dim(config.app_name)}\n`);
|
|
418
429
|
try {
|
|
419
|
-
const result = runSwitch(config, nodeCommandRunner);
|
|
430
|
+
const result = runSwitch(config, nodeCommandRunner, { force });
|
|
420
431
|
console.log(` ${pc.green('✓')} deleted live PM2 registration "${config.app_name}" in ${result.previousLiveDir}`);
|
|
421
432
|
console.log(` ${result.stagingStopped ? pc.green('✓') : pc.dim('·')} staging process "${config.app_name}-staging"` +
|
|
422
433
|
`${result.stagingStopped ? ' stopped' : ' was not running (skipped)'}`);
|
|
434
|
+
if (result.stagingDeleteWarning) {
|
|
435
|
+
console.log(` ${pc.yellow('⚠')} ${result.stagingDeleteWarning}`);
|
|
436
|
+
}
|
|
423
437
|
console.log(` ${pc.green('✓')} started "${config.app_name}" on port ${result.prodPort} in ${result.newLiveDir}`);
|
|
438
|
+
if (result.pm2SaveWarning) {
|
|
439
|
+
console.log(` ${pc.yellow('⚠')} ${result.pm2SaveWarning}`);
|
|
440
|
+
}
|
|
424
441
|
console.log('');
|
|
425
442
|
if (result.postSwitchHooks.length) {
|
|
426
443
|
console.log(` post-switch hooks:`);
|
|
@@ -433,6 +450,16 @@ async function runDeploySwitch() {
|
|
|
433
450
|
console.log(` ${pc.green('Switch complete.')} live: ${pc.bold(result.newLiveDir)} (port ${result.prodPort})`);
|
|
434
451
|
console.log(` standby: ${result.newStandbyDir}`);
|
|
435
452
|
console.log('');
|
|
453
|
+
if (result.sidecars.length) {
|
|
454
|
+
console.log(` sidecars:`);
|
|
455
|
+
for (const sidecar of result.sidecars) {
|
|
456
|
+
const icon = sidecar.action === 'failed' ? pc.yellow('⚠') : pc.green('✓');
|
|
457
|
+
console.log(` ${icon} ${sidecar.name} ${sidecar.action} ${pc.dim(sidecar.detail)}`);
|
|
458
|
+
if (sidecar.warning)
|
|
459
|
+
console.log(` ${pc.dim(sidecar.warning)}`);
|
|
460
|
+
}
|
|
461
|
+
console.log('');
|
|
462
|
+
}
|
|
436
463
|
}
|
|
437
464
|
catch (e) {
|
|
438
465
|
if (e instanceof SwitchAbortError) {
|
|
@@ -454,10 +481,26 @@ async function runDeployRollback() {
|
|
|
454
481
|
try {
|
|
455
482
|
const result = runRollback(config, nodeCommandRunner);
|
|
456
483
|
console.log(` ${pc.green('✓')} started "${config.app_name}" on port ${result.prodPort} in ${result.restoredLiveDir}`);
|
|
484
|
+
if (result.stagingDeleteWarning) {
|
|
485
|
+
console.log(` ${pc.yellow('⚠')} ${result.stagingDeleteWarning}`);
|
|
486
|
+
}
|
|
487
|
+
if (result.pm2SaveWarning) {
|
|
488
|
+
console.log(` ${pc.yellow('⚠')} ${result.pm2SaveWarning}`);
|
|
489
|
+
}
|
|
457
490
|
console.log('');
|
|
458
491
|
console.log(` ${pc.green('rolled back')} to ${pc.bold(result.restoredLiveDir)}, running on port ${result.prodPort}.`);
|
|
459
492
|
console.log(` standby: ${result.newStandbyDir}`);
|
|
460
493
|
console.log('');
|
|
494
|
+
if (result.sidecars.length) {
|
|
495
|
+
console.log(` sidecars:`);
|
|
496
|
+
for (const sidecar of result.sidecars) {
|
|
497
|
+
const icon = sidecar.action === 'failed' ? pc.yellow('⚠') : pc.green('✓');
|
|
498
|
+
console.log(` ${icon} ${sidecar.name} ${sidecar.action} ${pc.dim(sidecar.detail)}`);
|
|
499
|
+
if (sidecar.warning)
|
|
500
|
+
console.log(` ${pc.dim(sidecar.warning)}`);
|
|
501
|
+
}
|
|
502
|
+
console.log('');
|
|
503
|
+
}
|
|
461
504
|
}
|
|
462
505
|
catch (e) {
|
|
463
506
|
if (e instanceof RollbackAbortError) {
|
package/dist/deploy/build.d.ts
CHANGED
|
@@ -82,9 +82,10 @@ export declare function checkHealth(runner: CommandRunner, url: string, attempts
|
|
|
82
82
|
* run in the order documented in the task-5 brief (with db-strategy
|
|
83
83
|
* resolution/validation moved earlier, before git pull, so a bad `--db`
|
|
84
84
|
* fails fast); any failure throws `BuildAbortError` immediately. The state
|
|
85
|
-
* file's `last_build`/`last_build_status` is
|
|
86
|
-
*
|
|
87
|
-
*
|
|
85
|
+
* file's `last_build`/`last_build_status` is written once the pipeline
|
|
86
|
+
* reaches its first destructive step (git pull), and again with the final
|
|
87
|
+
* outcome if the pipeline completes (success or failure) — see this file's
|
|
88
|
+
* header (Fix 3) for the full rationale; an early abort never touches it.
|
|
88
89
|
*/
|
|
89
90
|
export declare function runBuild(config: DeployConfig, runner: CommandRunner, opts?: RunBuildOptions): RunBuildResult;
|
|
90
91
|
//# sourceMappingURL=build.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../../src/deploy/build.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../../src/deploy/build.ts"],"names":[],"mappings":"AAgCA,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,CASpH;AAED;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,aAAa,EAAE,IAAI,GAAE,eAAoB,GAAG,cAAc,CAiThH"}
|
package/dist/deploy/build.js
CHANGED
|
@@ -12,14 +12,23 @@
|
|
|
12
12
|
// Any step failure aborts immediately (throws BuildAbortError) and leaves
|
|
13
13
|
// whatever partial work happened on disk as-is — re-running `deploy build`
|
|
14
14
|
// is the recovery path, not an automatic rollback. The state file's
|
|
15
|
-
// last_build/last_build_status is
|
|
15
|
+
// last_build/last_build_status is updated once the pipeline reaches its
|
|
16
|
+
// first DESTRUCTIVE step (git pull) — never before (an early abort, e.g.
|
|
17
|
+
// the standby-dir guard or a disk/memory/db-resolve failure, leaves
|
|
18
|
+
// last_build untouched, since nothing destructive happened) — and is
|
|
19
|
+
// overwritten again with the final outcome if the pipeline completes
|
|
20
|
+
// normally (success or a later failure). See the "Fix 3" comments inline
|
|
21
|
+
// around the git-pull step and the closing `finally` block for the full
|
|
22
|
+
// rationale (this two-write scheme fixes both a false "stale build" block
|
|
23
|
+
// on deploy rollback from an early abort, and a missed one from a Ctrl+C
|
|
24
|
+
// after `pre_build_cmd` has already run).
|
|
16
25
|
import path from 'node:path';
|
|
17
26
|
import { autoClean } from './clean.js';
|
|
18
27
|
import { resolvePeerDir } from './peer.js';
|
|
19
28
|
import { queryDiskFreeMb, queryMemoryFreeMb } from './status.js';
|
|
20
29
|
import { readState, updateState } from './state.js';
|
|
21
30
|
import { SWAP_INCREASE_SUGGESTIONS } from './clean.js';
|
|
22
|
-
import { startNamedPm2Process } from './pm2-start.js';
|
|
31
|
+
import { startNamedPm2Process, isPm2ProcessNotFound } from './pm2-start.js';
|
|
23
32
|
import { resolveConnectConfig } from '../resolve/connect.js';
|
|
24
33
|
/** Thrown when any pipeline step fails. Carries the steps completed so far for reporting. */
|
|
25
34
|
export class BuildAbortError extends Error {
|
|
@@ -94,7 +103,8 @@ function sleepSync(ms) {
|
|
|
94
103
|
export function checkHealth(runner, url, attempts, delayMs) {
|
|
95
104
|
let lastCode = '';
|
|
96
105
|
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
97
|
-
|
|
106
|
+
const result = runner.spawn(`curl -s -o /dev/null -w '%{http_code}' ${JSON.stringify(url)}`);
|
|
107
|
+
lastCode = result.code === 0 ? result.stdout.trim() : `curl_exit_${result.code}`;
|
|
98
108
|
if (lastCode === '200')
|
|
99
109
|
return { ok: true, lastCode, attempts: attempt };
|
|
100
110
|
if (attempt < attempts)
|
|
@@ -107,9 +117,10 @@ export function checkHealth(runner, url, attempts, delayMs) {
|
|
|
107
117
|
* run in the order documented in the task-5 brief (with db-strategy
|
|
108
118
|
* resolution/validation moved earlier, before git pull, so a bad `--db`
|
|
109
119
|
* fails fast); any failure throws `BuildAbortError` immediately. The state
|
|
110
|
-
* file's `last_build`/`last_build_status` is
|
|
111
|
-
*
|
|
112
|
-
*
|
|
120
|
+
* file's `last_build`/`last_build_status` is written once the pipeline
|
|
121
|
+
* reaches its first destructive step (git pull), and again with the final
|
|
122
|
+
* outcome if the pipeline completes (success or failure) — see this file's
|
|
123
|
+
* header (Fix 3) for the full rationale; an early abort never touches it.
|
|
113
124
|
*/
|
|
114
125
|
export function runBuild(config, runner, opts = {}) {
|
|
115
126
|
const cwd = path.resolve(opts.cwd ?? process.cwd());
|
|
@@ -118,12 +129,33 @@ export function runBuild(config, runner, opts = {}) {
|
|
|
118
129
|
const healthCheckDelayMs = opts.healthCheckDelayMs ?? DEFAULT_HEALTH_CHECK_DELAY_MS;
|
|
119
130
|
const warnings = [];
|
|
120
131
|
const steps = [];
|
|
132
|
+
// Fix 3: whether the pipeline has reached its first DESTRUCTIVE step (git
|
|
133
|
+
// pull — see the `finally` block at the bottom of this function for the
|
|
134
|
+
// full rationale). Early aborts (standby-dir guard, disk/memory checks,
|
|
135
|
+
// db-resolve validation) never flip this, so they never touch
|
|
136
|
+
// last_build/last_build_status at all — nothing destructive happened, so
|
|
137
|
+
// there's nothing stale to warn rollback.ts about.
|
|
138
|
+
let destructiveStepReached = false;
|
|
139
|
+
// Fix 6: true once `--stop-prod` has actually stopped the live PM2
|
|
140
|
+
// process. If the build subsequently aborts for ANY reason, `abort()`
|
|
141
|
+
// below makes sure that fact — production is currently DOWN — is
|
|
142
|
+
// unmissable in the thrown error, rather than buried behind an unrelated
|
|
143
|
+
// "build failed" message.
|
|
144
|
+
let productionStopped = false;
|
|
145
|
+
let liveDirForAbortMessage = '';
|
|
121
146
|
function pass(name, detail) {
|
|
122
147
|
steps.push({ name, status: 'pass', detail });
|
|
123
148
|
}
|
|
124
149
|
function abort(name, message) {
|
|
125
|
-
|
|
126
|
-
|
|
150
|
+
const finalMessage = productionStopped
|
|
151
|
+
? `${message}\n\n` +
|
|
152
|
+
`⚠ PRODUCTION IS DOWN: --stop-prod stopped "${config.app_name}" in ${liveDirForAbortMessage} to free ` +
|
|
153
|
+
`memory, and the build subsequently aborted — production was never restarted. Bring it back up with:\n` +
|
|
154
|
+
` cd ${liveDirForAbortMessage} && pm2 start ecosystem.config.js --env production\n` +
|
|
155
|
+
`or run "deploy rollback" / investigate manually.`
|
|
156
|
+
: message;
|
|
157
|
+
steps.push({ name, status: 'fail', detail: finalMessage });
|
|
158
|
+
throw new BuildAbortError(finalMessage, steps);
|
|
127
159
|
}
|
|
128
160
|
/**
|
|
129
161
|
* Run a risky operation (anything touching CommandRunner or an external
|
|
@@ -181,6 +213,8 @@ export function runBuild(config, runner, opts = {}) {
|
|
|
181
213
|
if (opts.stopProd) {
|
|
182
214
|
runStep('stop-prod', () => runner.exec(`pm2 stop ${config.app_name}`, { cwd: liveDir }));
|
|
183
215
|
pass('stop-prod', `stopped "${config.app_name}" in ${liveDir}`);
|
|
216
|
+
productionStopped = true;
|
|
217
|
+
liveDirForAbortMessage = liveDir;
|
|
184
218
|
memoryFreeMb = queryMemoryFreeMb(runner, warnings);
|
|
185
219
|
memoryLow = memoryFreeMb !== undefined && memoryFreeMb < config.min_memory_mb;
|
|
186
220
|
}
|
|
@@ -203,6 +237,27 @@ export function runBuild(config, runner, opts = {}) {
|
|
|
203
237
|
abort('db-resolve', `db_strategy resolved to "copy" but the resolved db config is type "${dbConfig.type}", not sqlite.`);
|
|
204
238
|
}
|
|
205
239
|
pass('db-resolve', `strategy=${dbStrategyResolved}${opts.db ? ` db=${opts.db}` : ''}`);
|
|
240
|
+
// Fix 3: write a provisional/pessimistic `last_build` marker right
|
|
241
|
+
// before the first DESTRUCTIVE step (git pull) — everything above this
|
|
242
|
+
// point (guard, disk/memory checks, db-resolve) is read-only, so an
|
|
243
|
+
// abort before here must NEVER touch last_build at all (an early abort
|
|
244
|
+
// leaves the previous live build fully intact; rollback.ts's
|
|
245
|
+
// stale-build guard must stay unblocked). From here on, the standby
|
|
246
|
+
// dir's on-disk state is no longer guaranteed clean: if the operator
|
|
247
|
+
// Ctrl+C's the process after `pre_build_cmd` (typically `rm -rf .next`)
|
|
248
|
+
// has already run, the `finally` block below never executes (process
|
|
249
|
+
// killed) — this provisional write is what's left behind for
|
|
250
|
+
// rollback.ts to see, correctly blocking it (the standby dir may now be
|
|
251
|
+
// a half-destroyed mess that is no longer safe to promote). A normal
|
|
252
|
+
// completion (success or a later, cleaner failure) overwrites this via
|
|
253
|
+
// the `finally` block's own write.
|
|
254
|
+
destructiveStepReached = true;
|
|
255
|
+
try {
|
|
256
|
+
updateState(config.app_name, { last_build: new Date().toISOString(), last_build_status: 'failed' });
|
|
257
|
+
}
|
|
258
|
+
catch {
|
|
259
|
+
// No state file yet — best-effort, same as the finally block below.
|
|
260
|
+
}
|
|
206
261
|
// Step 4: git pull
|
|
207
262
|
runStep('git-pull', () => runner.exec(`git pull origin ${branch}`, { cwd }));
|
|
208
263
|
pass('git-pull', `origin ${branch}`);
|
|
@@ -272,6 +327,32 @@ export function runBuild(config, runner, opts = {}) {
|
|
|
272
327
|
// in and starting pm2 from THAT clone instead — see pm2-start.ts's
|
|
273
328
|
// header for the full why.
|
|
274
329
|
const stagingProcessName = `${config.app_name}-staging`;
|
|
330
|
+
// Fix 4a: tolerant-delete any stale "<app>-staging" registration before
|
|
331
|
+
// starting it fresh — defense in depth. `start-staging` normally finds
|
|
332
|
+
// nothing here because a prior `deploy switch`/`deploy rollback` already
|
|
333
|
+
// deleted it (via `promoteDirFresh`'s own staging-delete step). If that
|
|
334
|
+
// delete ever silently failed, the NEXT `deploy build` would otherwise
|
|
335
|
+
// hit the stale registration and `startNamedPm2Process` would restart
|
|
336
|
+
// IT IN PLACE (pm2 only recomputes cwd for a name it's never registered
|
|
337
|
+
// before — see pm2-start.ts's header) — meaning the health check below
|
|
338
|
+
// would test the OLD directory's code, not this build's, and a
|
|
339
|
+
// subsequent `deploy switch` would silently promote untested code. Never
|
|
340
|
+
// fatal: "process not found" is the expected common case, and any other
|
|
341
|
+
// failure here is only a warning — the fresh start below is what
|
|
342
|
+
// actually matters.
|
|
343
|
+
const stagingCleanupResult = runner.spawn(`pm2 delete ${stagingProcessName}`, { cwd });
|
|
344
|
+
if (stagingCleanupResult.code === 0) {
|
|
345
|
+
pass('clean-stale-staging', `deleted a stale "${stagingProcessName}" PM2 registration before starting fresh`);
|
|
346
|
+
}
|
|
347
|
+
else if (isPm2ProcessNotFound(stagingCleanupResult)) {
|
|
348
|
+
pass('clean-stale-staging', `no stale "${stagingProcessName}" registration found`);
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
const detail = `failed to delete a stale "${stagingProcessName}" registration (continuing anyway): ` +
|
|
352
|
+
(stagingCleanupResult.stderr || stagingCleanupResult.stdout || `exit code ${stagingCleanupResult.code}`);
|
|
353
|
+
warnings.push(detail);
|
|
354
|
+
pass('clean-stale-staging', detail);
|
|
355
|
+
}
|
|
275
356
|
runStep('start-staging', () => startNamedPm2Process(runner, { dir: cwd, name: stagingProcessName, port: config.staging_port }));
|
|
276
357
|
pass('start-staging', `name=${stagingProcessName} PORT=${config.staging_port}, cwd=${cwd}`);
|
|
277
358
|
// Step 10: health check, retried — pm2 start returns before the app has
|
|
@@ -287,13 +368,22 @@ export function runBuild(config, runner, opts = {}) {
|
|
|
287
368
|
return { steps, stagingPort: config.staging_port, stagingUrl, dbStrategyResolved, warnings };
|
|
288
369
|
}
|
|
289
370
|
finally {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
371
|
+
// Fix 3: only overwrite last_build/last_build_status here if the
|
|
372
|
+
// pipeline actually reached the first destructive step (the provisional
|
|
373
|
+
// marker written just before git pull, above). An early abort (guard/
|
|
374
|
+
// disk/memory/db-resolve) never set that flag, so this block correctly
|
|
375
|
+
// no-ops for it — nothing destructive happened, so last_build should be
|
|
376
|
+
// left exactly as it was (possibly undefined) rather than stamped
|
|
377
|
+
// "failed" and falsely blocking deploy rollback's stale-build guard.
|
|
378
|
+
if (destructiveStepReached) {
|
|
379
|
+
try {
|
|
380
|
+
updateState(config.app_name, { last_build: new Date().toISOString(), last_build_status: buildStatus });
|
|
381
|
+
}
|
|
382
|
+
catch {
|
|
383
|
+
// No state file yet (deploy init was never run) — build still ran;
|
|
384
|
+
// state bookkeeping is best-effort and must never mask the real
|
|
385
|
+
// pipeline error (or success) above.
|
|
386
|
+
}
|
|
297
387
|
}
|
|
298
388
|
}
|
|
299
389
|
}
|
package/dist/deploy/index.d.ts
CHANGED
|
@@ -3,4 +3,12 @@ export * from './state.js';
|
|
|
3
3
|
export * from './runner.js';
|
|
4
4
|
export * from './peer.js';
|
|
5
5
|
export * from './pm2-start.js';
|
|
6
|
+
export * from './sidecar.js';
|
|
7
|
+
export * from './clean.js';
|
|
8
|
+
export * from './status.js';
|
|
9
|
+
export * from './init.js';
|
|
10
|
+
export * from './build.js';
|
|
11
|
+
export * from './verify.js';
|
|
12
|
+
export * from './switch.js';
|
|
13
|
+
export * from './rollback.js';
|
|
6
14
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/deploy/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/deploy/index.ts"],"names":[],"mappings":"AAUA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
|
package/dist/deploy/index.js
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
// hazo_env/src/deploy/index.ts — Deploy module barrel export
|
|
2
|
+
//
|
|
3
|
+
// Fix 5 (final review wave): the command functions (runInit/runBuild/
|
|
4
|
+
// runVerify/runSwitch/runRollback/runStatus/runClean) and their abort error
|
|
5
|
+
// classes were missing from this barrel — only the supporting
|
|
6
|
+
// types/state/runner/peer/pm2-start modules were exported, so programmatic
|
|
7
|
+
// callers of `hazo_env` (per the spec's "Package export" section) couldn't
|
|
8
|
+
// actually import the deploy commands themselves. `src/index.ts`'s own
|
|
9
|
+
// `export * from './deploy/index.js'` picks up everything added here
|
|
10
|
+
// automatically — no change needed there.
|
|
2
11
|
export * from './types.js';
|
|
3
12
|
export * from './state.js';
|
|
4
13
|
export * from './runner.js';
|
|
5
14
|
export * from './peer.js';
|
|
6
15
|
export * from './pm2-start.js';
|
|
16
|
+
export * from './sidecar.js';
|
|
17
|
+
export * from './clean.js';
|
|
18
|
+
export * from './status.js';
|
|
19
|
+
export * from './init.js';
|
|
20
|
+
export * from './build.js';
|
|
21
|
+
export * from './verify.js';
|
|
22
|
+
export * from './switch.js';
|
|
23
|
+
export * from './rollback.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../src/deploy/init.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../src/deploy/init.ts"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAe,MAAM,YAAY,CAAC;AAgD3E,MAAM,WAAW,cAAc;IAC7B,iFAAiF;IACjF,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,oEAAoE;IACpE,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,aAAa;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AA0CD;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,aAAa,EAAE,IAAI,GAAE,cAAmB,GAAG,aAAa,CAgC7G"}
|
package/dist/deploy/init.js
CHANGED
|
@@ -14,9 +14,20 @@
|
|
|
14
14
|
// not hazo_env's own file I/O.
|
|
15
15
|
import fs from 'node:fs';
|
|
16
16
|
import path from 'node:path';
|
|
17
|
+
import { HazoConfig } from 'hazo_config/server';
|
|
17
18
|
import { resolvePeerDir } from './peer.js';
|
|
18
19
|
import { writeState } from './state.js';
|
|
20
|
+
import { getEnv } from '../index.client.js';
|
|
19
21
|
const CONFIG_RELATIVE_PATH = path.join('config', 'hazo_env_config.ini');
|
|
22
|
+
// Matches resolve/files.ts's DEFAULT_DATA_ROOT — kept as a local literal
|
|
23
|
+
// rather than importing that module, since resolveFilesConfig()/
|
|
24
|
+
// resolveFilesRoot() are both hard-coded to resolve against
|
|
25
|
+
// `process.cwd()`, which during `deploy init` is the CURRENT (live) dir,
|
|
26
|
+
// not the freshly-cloned peer dir this function needs to resolve the data
|
|
27
|
+
// root *for*. Re-reading the peer's own just-copied INI directly (see
|
|
28
|
+
// `ensurePeerDataDir` below) keeps this correct regardless of what
|
|
29
|
+
// directory `deploy init` itself happens to be run from.
|
|
30
|
+
const DEFAULT_DATA_ROOT = 'app_data';
|
|
20
31
|
/**
|
|
21
32
|
* Blue<->green swap applied to the `dir_suffix = <value>` VALUE in the INI
|
|
22
33
|
* (as opposed to peer.ts's swapColorSuffix, which swaps a "-blue"/"-green"
|
|
@@ -48,6 +59,45 @@ function swapDirSuffixInIni(iniContent) {
|
|
|
48
59
|
const swapped = swapDirSuffixValue(match[2]);
|
|
49
60
|
return iniContent.replace(pattern, `$1${swapped}$3`);
|
|
50
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Fix 9: `deploy build`'s FIRST run on a fresh peer dir aborts at the
|
|
64
|
+
* sqlite db-copy step with "destination directory does not exist" because
|
|
65
|
+
* the `[data] root` dir (typically gitignored, so a fresh `git clone`
|
|
66
|
+
* doesn't bring it along) was never created. Mirrors build.ts's own
|
|
67
|
+
* db_strategy detection (auto -> infer sqlite-ness from `[db.<env>]` type;
|
|
68
|
+
* copy -> always sqlite; shared -> never sqlite) so this only fires for
|
|
69
|
+
* sites that are actually sqlite-backed — a no-op for Postgres/"shared"
|
|
70
|
+
* sites, which have no per-dir data file to provision.
|
|
71
|
+
*
|
|
72
|
+
* Reads the PEER's own just-copied INI (not `resolveFilesConfig()`/
|
|
73
|
+
* `resolveConnectConfig()`, both of which resolve against `process.cwd()`
|
|
74
|
+
* — the CURRENT/live dir here, not the peer) so the `[data] root` and
|
|
75
|
+
* `[db.<env>] type` values reflect the peer's own config exactly.
|
|
76
|
+
*/
|
|
77
|
+
function ensurePeerDataDir(config, peerIniPath, peerDir) {
|
|
78
|
+
if (config.db_strategy === 'shared')
|
|
79
|
+
return; // never sqlite-backed by definition — nothing to provision.
|
|
80
|
+
let peerConfig;
|
|
81
|
+
try {
|
|
82
|
+
peerConfig = new HazoConfig({ filePath: peerIniPath });
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// Can't read the INI we just wrote — nothing safe to infer or create.
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
let sqliteBacked = config.db_strategy === 'copy';
|
|
89
|
+
if (config.db_strategy === 'auto') {
|
|
90
|
+
const env = getEnv();
|
|
91
|
+
const dbSection = peerConfig.getSection(`db.${env}`);
|
|
92
|
+
sqliteBacked = dbSection?.['type'] === 'sqlite';
|
|
93
|
+
}
|
|
94
|
+
if (!sqliteBacked)
|
|
95
|
+
return;
|
|
96
|
+
const dataSection = peerConfig.getSection('data');
|
|
97
|
+
const root = dataSection?.['root'] ?? DEFAULT_DATA_ROOT;
|
|
98
|
+
const dataRoot = path.isAbsolute(root) ? root : path.join(peerDir, root);
|
|
99
|
+
fs.mkdirSync(dataRoot, { recursive: true });
|
|
100
|
+
}
|
|
51
101
|
/**
|
|
52
102
|
* Create the peer (standby) directory: resolve its path, abort if it
|
|
53
103
|
* already exists, clone the repo into it, copy the INI config across with
|
|
@@ -68,6 +118,7 @@ export function runInit(config, runner, opts = {}) {
|
|
|
68
118
|
const swappedIni = swapDirSuffixInIni(iniContent);
|
|
69
119
|
fs.mkdirSync(path.dirname(peerIniPath), { recursive: true });
|
|
70
120
|
fs.writeFileSync(peerIniPath, swappedIni, 'utf-8');
|
|
121
|
+
ensurePeerDataDir(config, peerIniPath, peerDir);
|
|
71
122
|
const state = {
|
|
72
123
|
app_name: config.app_name,
|
|
73
124
|
live_dir: currentDir,
|