@hyperfixation/cli 0.1.5 → 0.1.7

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/dist/cli.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- export declare const COMMANDS: readonly ["new", "migrate", "bootstrap", "status-token", "check", "gen", "dev", "up", "doctor", "restore-check"];
1
+ export declare const COMMANDS: readonly ["new", "migrate", "bootstrap", "status-token", "check", "gen", "dev", "up", "deploy", "doctor", "restore-check", "version"];
2
2
  export type Command = (typeof COMMANDS)[number];
3
- export declare const USAGE = "hf \u2014 the hyperfixation CLI\n\n hf new <name> provision the app in the cloud: fetch the template, push a private\n repo, register a backup, Sentry, Langfuse and DNS, create the\n database and its roles, create the Coolify application and its\n environment, deploy, and print what is left to do by hand.\n Resumable \u2014 a rerun repeats only what did not finish\n --budget-usd <amount> the app's monthly LLM budget (required; no default)\n --email <address> the bootstrap admin's address (required)\n --from <specifier> template to fetch (default: gh:grahamlutz/hyperfixation-template)\n --into <dir> where to create <name> (default: the working directory)\n\n hf new <name> --local copy the template into ./<name>, substitute its placeholders, and\n prompt for the bootstrap admin's email\n --from <dir> template checkout (default: the sibling hyperfixation-template)\n --into <dir> where to create <name> (default: the working directory)\n --email <address> the bootstrap admin's address; skips the prompt\n\n hf up install, infra, migrate, bootstrap, status tokens, then hf dev \u2014\n the whole local loop after hf new, safe to rerun; seeds a $10\n budget unless HF_BOOTSTRAP_BUDGET_USD is set in .env\n\n hf migrate create the application role, then run the app's migrate.ts\n --skip-roles the cloud path, where the roles already exist\n\n hf bootstrap grant the app its one bootstrap admin, and seed hf_app_state\n --email <address> the address to promote; otherwise HF_BOOTSTRAP_EMAIL\n --budget-usd <amount> the app's starting budget; otherwise HF_BOOTSTRAP_BUDGET_USD\n\n hf status-token provision /api/status's read and write tokens\n --read only the read token; refuses if it is already set\n --write only the write token; refuses if it is already set\n --rotate replace a token that is already set\n (no flags: fills in whichever of the two is unset)\n\n hf check declared env, pending migrations, and E001-E006\n\n hf doctor [name] every deployed app in the state cache, or one: /api/status under its\n read token, the deployed version against main, E006 as the app role,\n the last restore check, and open core-bump PRs. Exits 1 on any finding\n\n hf gen [generator] the app's turbo generators\n\n hf dev docker compose up, then pnpm dev under HF_BUILD_SHA=dev-<timestamp>\n --no-compose leave the dev infrastructure alone\n --compose-only bring the infrastructure up and stop\n\n hf restore-check <name> restore the newest hf_<name> dump beside the live database and\n compare row counts; exits 1 on any mismatch\n --backup-dir <dir> where the dumps are (default: Coolify's on the box)\n --from-s3 read the dump from object storage (not implemented)\n\nEvery command but `new`, `doctor` and `restore-check` runs against the app at or above the working directory, or --dir.\n";
3
+ export declare const USAGE = "hf \u2014 the hyperfixation CLI\n\n hf new <name> provision the app in the cloud: fetch the template, push a private\n repo, register a backup, Sentry, Langfuse and DNS, create the\n database and its roles, create the Coolify application and its\n environment, deploy, and print what is left to do by hand.\n Resumable \u2014 a rerun repeats only what did not finish\n --budget-usd <amount> the app's monthly LLM budget (required; no default)\n --email <address> the bootstrap admin's address (required)\n --from <specifier> template to fetch (default: gh:grahamlutz/hyperfixation-template)\n --into <dir> where to create <name> (default: the working directory)\n\n hf new <name> --local copy the template into ./<name>, substitute its placeholders, and\n write both bootstrap answers into its .env\n --budget-usd <amount> the app's monthly LLM budget (required; no default)\n --email <address> the bootstrap admin's address (required)\n --from <dir> template checkout (default: the sibling hyperfixation-template)\n --into <dir> where to create <name> (default: the working directory)\n\n hf up install, infra, migrate, bootstrap, status tokens, then hf dev \u2014\n the whole local loop after hf new, safe to rerun; seeds a $10\n budget unless HF_BOOTSTRAP_BUDGET_USD is set in .env\n\n hf migrate create the application role, then run the app's migrate.ts\n --skip-roles the cloud path, where the roles already exist\n\n hf bootstrap grant the app its one bootstrap admin, and seed hf_app_state\n --email <address> the address to promote; otherwise HF_BOOTSTRAP_EMAIL\n --budget-usd <amount> the app's starting budget; otherwise HF_BOOTSTRAP_BUDGET_USD\n\n hf status-token provision /api/status's read and write tokens\n --read only the read token; refuses if it is already set\n --write only the write token; refuses if it is already set\n --rotate replace a token that is already set\n (no flags: fills in whichever of the two is unset)\n\n hf check declared env, pending migrations, and E001-E006\n\n hf deploy <name> set SOURCE_COMMIT to a commit, deploy it, and wait until /api/status\n reports it. Coolify's push auto-deploy is off, so this is what\n publishes a merge to main\n --sha <sha> the commit to deploy (default: main's, via git ls-remote)\n\n hf doctor [name] every deployed app in the state cache, or one: /api/status under its\n read token, the deployed version against main, E006 as the app role,\n the box's connection slots, the worker's advisory lock, the last\n restore check, and open core-bump PRs. Exits 1 on any finding\n\n hf gen [generator] the app's turbo generators\n\n hf dev docker compose up, then pnpm dev under HF_BUILD_SHA=dev-<timestamp>\n --no-compose leave the dev infrastructure alone\n --compose-only bring the infrastructure up and stop\n\n hf restore-check <name> restore the newest hf_<name> dump beside the live database and\n compare row counts. An append-only table the live side has moved\n on from reads ok (drift +N); exits 1 on any mismatch, and on a\n dump older than 24 h\n --backup-dir <dir> where the dumps are (default: Coolify's on the box)\n --from-s3 read the dump from object storage (not implemented)\n --strict compare every table exactly; no table may drift\n\n hf version the @hyperfixation/cli version behind this hf, also as --version, -v\n\nEvery command but `new`, `deploy`, `doctor` and `restore-check` runs against the app at or above the working directory, or --dir.\n";
4
4
  export interface Io {
5
5
  out(line: string): void;
6
6
  err(line: string): void;
package/dist/cli.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { parseArgs } from "node:util";
2
2
  import { bootstrapApp } from "./bootstrap.js";
3
3
  import { checkApp } from "./check.js";
4
+ import { deployApp } from "./deploy-app.js";
4
5
  import { dev, devBuildSha } from "./dev.js";
5
6
  import { doctor, doctorLines } from "./doctor.js";
6
7
  import { generate } from "./gen.js";
@@ -11,6 +12,7 @@ import { formatRestoreCheck, restoreCheckApp } from "./restore-check.js";
11
12
  import { statusTokenApp } from "./status-token.js";
12
13
  import { requireTemplateSource } from "./template-source.js";
13
14
  import { DEV_BUDGET_USD, upApp } from "./up.js";
15
+ import { cliVersion } from "./version.js";
14
16
  export const COMMANDS = [
15
17
  "new",
16
18
  "migrate",
@@ -20,8 +22,10 @@ export const COMMANDS = [
20
22
  "gen",
21
23
  "dev",
22
24
  "up",
25
+ "deploy",
23
26
  "doctor",
24
27
  "restore-check",
28
+ "version",
25
29
  ];
26
30
  export const USAGE = `hf — the hyperfixation CLI
27
31
 
@@ -36,10 +40,11 @@ export const USAGE = `hf — the hyperfixation CLI
36
40
  --into <dir> where to create <name> (default: the working directory)
37
41
 
38
42
  hf new <name> --local copy the template into ./<name>, substitute its placeholders, and
39
- prompt for the bootstrap admin's email
43
+ write both bootstrap answers into its .env
44
+ --budget-usd <amount> the app's monthly LLM budget (required; no default)
45
+ --email <address> the bootstrap admin's address (required)
40
46
  --from <dir> template checkout (default: the sibling hyperfixation-template)
41
47
  --into <dir> where to create <name> (default: the working directory)
42
- --email <address> the bootstrap admin's address; skips the prompt
43
48
 
44
49
  hf up install, infra, migrate, bootstrap, status tokens, then hf dev —
45
50
  the whole local loop after hf new, safe to rerun; seeds a $10
@@ -60,9 +65,15 @@ export const USAGE = `hf — the hyperfixation CLI
60
65
 
61
66
  hf check declared env, pending migrations, and E001-E006
62
67
 
68
+ hf deploy <name> set SOURCE_COMMIT to a commit, deploy it, and wait until /api/status
69
+ reports it. Coolify's push auto-deploy is off, so this is what
70
+ publishes a merge to main
71
+ --sha <sha> the commit to deploy (default: main's, via git ls-remote)
72
+
63
73
  hf doctor [name] every deployed app in the state cache, or one: /api/status under its
64
74
  read token, the deployed version against main, E006 as the app role,
65
- the last restore check, and open core-bump PRs. Exits 1 on any finding
75
+ the box's connection slots, the worker's advisory lock, the last
76
+ restore check, and open core-bump PRs. Exits 1 on any finding
66
77
 
67
78
  hf gen [generator] the app's turbo generators
68
79
 
@@ -71,11 +82,16 @@ export const USAGE = `hf — the hyperfixation CLI
71
82
  --compose-only bring the infrastructure up and stop
72
83
 
73
84
  hf restore-check <name> restore the newest hf_<name> dump beside the live database and
74
- compare row counts; exits 1 on any mismatch
85
+ compare row counts. An append-only table the live side has moved
86
+ on from reads ok (drift +N); exits 1 on any mismatch, and on a
87
+ dump older than 24 h
75
88
  --backup-dir <dir> where the dumps are (default: Coolify's on the box)
76
89
  --from-s3 read the dump from object storage (not implemented)
90
+ --strict compare every table exactly; no table may drift
77
91
 
78
- Every command but \`new\`, \`doctor\` and \`restore-check\` runs against the app at or above the working directory, or --dir.
92
+ hf version the @hyperfixation/cli version behind this hf, also as --version, -v
93
+
94
+ Every command but \`new\`, \`deploy\`, \`doctor\` and \`restore-check\` runs against the app at or above the working directory, or --dir.
79
95
  `;
80
96
  const consoleIo = {
81
97
  out: (line) => console.log(line),
@@ -94,6 +110,10 @@ export async function main(argv, io = consoleIo) {
94
110
  io.out(USAGE);
95
111
  return command === undefined ? 1 : 0;
96
112
  }
113
+ if (command === "--version" || command === "-v") {
114
+ io.out(await cliVersion());
115
+ return 0;
116
+ }
97
117
  if (!COMMANDS.includes(command)) {
98
118
  io.err(`unknown command ${JSON.stringify(command)}`);
99
119
  io.err(USAGE);
@@ -125,10 +145,15 @@ async function dispatch(command, argv, io) {
125
145
  return await commandDev(argv, io);
126
146
  case "up":
127
147
  return await commandUp(argv, io);
148
+ case "deploy":
149
+ return await commandDeploy(argv, io);
128
150
  case "doctor":
129
151
  return await commandDoctor(argv, io);
130
152
  case "restore-check":
131
153
  return await commandRestoreCheck(argv, io);
154
+ case "version":
155
+ io.out(await cliVersion());
156
+ return 0;
132
157
  }
133
158
  }
134
159
  async function commandNew(argv, io) {
@@ -151,6 +176,15 @@ async function commandNew(argv, io) {
151
176
  if (!values.local) {
152
177
  return await commandNewCloud(name, values, io);
153
178
  }
179
+ // Refused here rather than prompted for: `hf up` reads both out of the app's `.env`, so a
180
+ // local app started without them is the same half-answered app the cloud path has refused
181
+ // since Phase 3 — one that either cannot spend or cannot stop.
182
+ const missing = missingBootstrapFlags(values);
183
+ if (missing.length > 0) {
184
+ io.err(`hf new ${name} --local needs ${missing.join(" and ")}: hf up reads both from the app's ` +
185
+ ".env and neither has a default.");
186
+ return 1;
187
+ }
154
188
  const from = values.from ?? (await requireTemplateSource());
155
189
  const result = await newApp({
156
190
  name,
@@ -158,29 +192,35 @@ async function commandNew(argv, io) {
158
192
  into: values.into,
159
193
  local: values.local,
160
194
  email: values.email,
195
+ budgetUsd: values["budget-usd"],
161
196
  });
162
197
  io.out(`created ${result.dir} from ${from}`);
163
198
  io.out(` app ${result.appName}, database ${result.databaseName}`);
164
199
  io.out(` ${result.substituted.length} file(s) substituted` +
165
200
  (result.wroteEnv ? ", .env written from .env.example" : "") +
166
- (result.wroteBootstrapEmail ? ", HF_BOOTSTRAP_EMAIL set" : ""));
201
+ (result.wroteBootstrapEmail ? ", HF_BOOTSTRAP_EMAIL set" : "") +
202
+ (result.wroteBootstrapBudget ? ", HF_BOOTSTRAP_BUDGET_USD set" : ""));
167
203
  io.out("");
168
204
  io.out(`next: cd ${result.given} && hf up`);
169
205
  return 0;
170
206
  }
171
207
  /**
172
- * The cloud half: both inputs it cannot invent are refused up front.
208
+ * The two answers `hf new` cannot invent, named in the order the usage lists them.
173
209
  *
174
- * Neither has a default. `--email` designates the one admin an app is ever granted without an
175
- * admin behind it, and a budget nobody chose is a deployed app that either cannot spend or
176
- * cannot stop — `hf bootstrap` has refused an unset one since Phase 1, and this is the same rule
177
- * one command earlier, where the answer costs nothing yet.
210
+ * `--email` designates the one admin an app is ever granted without an admin behind it, and a
211
+ * budget nobody chose is an app that either cannot spend or cannot stop — `hf bootstrap` has
212
+ * refused an unset one since Phase 1, and this is the same rule one command earlier, where the
213
+ * answer costs nothing yet.
178
214
  */
179
- async function commandNewCloud(name, values, io) {
180
- const missing = [
215
+ function missingBootstrapFlags(values) {
216
+ return [
181
217
  ...(values["budget-usd"] === undefined ? ["--budget-usd <amount>"] : []),
182
218
  ...(values.email === undefined ? ["--email <address>"] : []),
183
219
  ];
220
+ }
221
+ /** The cloud half: both inputs are refused up front, with nowhere else to carry them. */
222
+ async function commandNewCloud(name, values, io) {
223
+ const missing = missingBootstrapFlags(values);
184
224
  if (missing.length > 0) {
185
225
  io.err(`hf new ${name} needs ${missing.join(" and ")}: a cloud app has no prompt and no .env to ` +
186
226
  "carry either. Pass --local for Phase 1's local copy.");
@@ -275,6 +315,21 @@ async function commandCheck(argv, io) {
275
315
  io.err(`${finding.code}: ${finding.message}`);
276
316
  return 1;
277
317
  }
318
+ async function commandDeploy(argv, io) {
319
+ const { values, positionals } = parseArgs({
320
+ args: [...argv],
321
+ options: { sha: { type: "string" } },
322
+ allowPositionals: true,
323
+ });
324
+ const name = positionals[0];
325
+ if (name === undefined) {
326
+ io.err("hf deploy needs a name: hf deploy <name> [--sha <sha>]");
327
+ return 1;
328
+ }
329
+ // Every progress line the deploy prints — including the last one — is the command's output.
330
+ await deployApp({ app: name, sha: values.sha, io });
331
+ return 0;
332
+ }
278
333
  async function commandDoctor(argv, io) {
279
334
  const { positionals } = parseArgs({ args: [...argv], allowPositionals: true });
280
335
  const result = await doctor({ name: positionals[0] });
@@ -318,6 +373,7 @@ async function commandRestoreCheck(argv, io) {
318
373
  options: {
319
374
  "backup-dir": { type: "string" },
320
375
  "from-s3": { type: "boolean", default: false },
376
+ strict: { type: "boolean", default: false },
321
377
  },
322
378
  allowPositionals: true,
323
379
  });
@@ -330,6 +386,7 @@ async function commandRestoreCheck(argv, io) {
330
386
  app: name,
331
387
  backupDir: values["backup-dir"],
332
388
  fromS3: values["from-s3"],
389
+ strict: values.strict,
333
390
  });
334
391
  for (const line of formatRestoreCheck(result))
335
392
  io.out(line);
@@ -19,9 +19,12 @@ export declare const COMPOSE_DOMAIN_SERVICE = "web";
19
19
  /** The compose file the drift assertion reads, under the app's directory. */
20
20
  export declare const PROD_COMPOSE_FILE = "docker-compose.prod.yml";
21
21
  /**
22
- * Set by compose itself rather than by us: `DOCKER_IMAGE` has a default in the `x-app` anchor and
23
- * `SOURCE_COMMIT` is what Coolify's builder exports for the image tag and the build arg. Sending
24
- * either as an application environment variable would override the deploy's own.
22
+ * Interpolated by the compose file but never part of this step's bulk PATCH.
23
+ *
24
+ * `DOCKER_IMAGE` has a default in the `x-app` anchor. `SOURCE_COMMIT` is written by the `deploy`
25
+ * step instead, once per deployment and against the commit being deployed — it is not one of the
26
+ * app's secrets, so it belongs neither in `secretsHash` nor in a PATCH that a rotation replays.
27
+ * Both are excused here rather than counted as drift.
25
28
  */
26
29
  export declare const COMPOSE_PROVIDED_ENV: readonly ["DOCKER_IMAGE", "SOURCE_COMMIT"];
27
30
  /** Written literally into every `environment:` block, so the deploy never carries them. */
@@ -22,9 +22,12 @@ export const COMPOSE_DOMAIN_SERVICE = "web";
22
22
  /** The compose file the drift assertion reads, under the app's directory. */
23
23
  export const PROD_COMPOSE_FILE = "docker-compose.prod.yml";
24
24
  /**
25
- * Set by compose itself rather than by us: `DOCKER_IMAGE` has a default in the `x-app` anchor and
26
- * `SOURCE_COMMIT` is what Coolify's builder exports for the image tag and the build arg. Sending
27
- * either as an application environment variable would override the deploy's own.
25
+ * Interpolated by the compose file but never part of this step's bulk PATCH.
26
+ *
27
+ * `DOCKER_IMAGE` has a default in the `x-app` anchor. `SOURCE_COMMIT` is written by the `deploy`
28
+ * step instead, once per deployment and against the commit being deployed — it is not one of the
29
+ * app's secrets, so it belongs neither in `secretsHash` nor in a PATCH that a rotation replays.
30
+ * Both are excused here rather than counted as drift.
28
31
  */
29
32
  export const COMPOSE_PROVIDED_ENV = ["DOCKER_IMAGE", "SOURCE_COMMIT"];
30
33
  /** Written literally into every `environment:` block, so the deploy never carries them. */
@@ -267,6 +270,10 @@ async function findOrCreateApplication(context, coolify, projectUuid, environmen
267
270
  docker_compose_domains: [{ name: COMPOSE_DOMAIN_SERVICE, domain: `https://${fqdn}` }],
268
271
  // The `deploy` step is what deploys, once the environment is set and the database migrated.
269
272
  instant_deploy: false,
273
+ // Coolify's default is true: a push to main would then deploy whatever arrived against
274
+ // whatever `SOURCE_COMMIT` happened to hold, and the app would report a version it is not
275
+ // running. Every deploy goes through `hf` — `hf new`'s tenth step, or `hf deploy <name>`.
276
+ is_auto_deploy_enabled: false,
270
277
  });
271
278
  await context.state.patch({ coolify: { appUuid: created.uuid } });
272
279
  context.io.out(`${name}: created the Coolify application at https://${fqdn}`);
@@ -1,5 +1,7 @@
1
1
  import type { Step } from "../new-cloud.js";
2
- import { type CloudStepContext } from "./context.js";
2
+ import { CoolifyClient } from "../providers/coolify.js";
3
+ import type { FetchLike } from "../providers/http.js";
4
+ import { type CloudStepContext, type StepOut } from "./context.js";
3
5
  /**
4
6
  * How long the step waits for Coolify to build and for the app to report the sha it built.
5
7
  *
@@ -7,6 +9,41 @@ import { type CloudStepContext } from "./context.js";
7
9
  * the right version, and a build that took fourteen minutes has not left time for anything else.
8
10
  */
9
11
  export declare const DEPLOY_TIMEOUT_MS: number;
12
+ /**
13
+ * The compose variable that carries the deployed commit, and the one channel there is for it.
14
+ *
15
+ * Coolify's docker-compose build pack passes the commit into nothing: no `--build-arg`, nothing
16
+ * in the compose environment, and no `.git` in the build context even with
17
+ * `is_preserve_repository_enabled` — so the Dockerfile's `git rev-parse HEAD` fallback cannot run
18
+ * there either, and the worker dies at startup on an unresolved `HF_BUILD_SHA` (verified on the
19
+ * box, 4.3.21). What Coolify *does* do is materialise an application environment entry for every
20
+ * `${VAR}` the compose file interpolates, which is what this fills in before every deploy.
21
+ */
22
+ export declare const SOURCE_COMMIT_ENV = "SOURCE_COMMIT";
23
+ /** One deployment of one commit: everything `deployCommit` needs that a step context also has. */
24
+ export interface DeployTarget {
25
+ /** The app as the state cache names it — the prefix on every line this prints. */
26
+ name: string;
27
+ appUuid: string;
28
+ fqdn: string;
29
+ /** The exact commit being deployed; `/api/status` has to report this one. */
30
+ sha: string;
31
+ /** `/api/status`'s read token, which is the only way to ask what the app is running. */
32
+ readToken: string;
33
+ coolify: CoolifyClient;
34
+ io: StepOut;
35
+ now(): number;
36
+ sleep(ms: number): Promise<void>;
37
+ fetch?: FetchLike;
38
+ }
39
+ /**
40
+ * `SOURCE_COMMIT`, then the deploy, then the wait — in that order and never another.
41
+ *
42
+ * The environment write is what the build reads, so a deploy triggered first would build the
43
+ * previous commit's tag and the wait would then time out against a version the app is right to
44
+ * report. Both `hf new`'s tenth step and `hf deploy` come through here for that reason.
45
+ */
46
+ export declare function deployCommit(target: DeployTarget): Promise<void>;
10
47
  /**
11
48
  * Deploy, wait for the build, then wait for the app to say it is running that commit.
12
49
  *
@@ -12,6 +12,37 @@ const FIRST_POLL_MS = 2_000;
12
12
  const MAX_POLL_MS = 15_000;
13
13
  /** Coolify's one terminal success; `failed` and `cancelled-*` are the terminal failures. */
14
14
  const FINISHED = "finished";
15
+ /**
16
+ * The compose variable that carries the deployed commit, and the one channel there is for it.
17
+ *
18
+ * Coolify's docker-compose build pack passes the commit into nothing: no `--build-arg`, nothing
19
+ * in the compose environment, and no `.git` in the build context even with
20
+ * `is_preserve_repository_enabled` — so the Dockerfile's `git rev-parse HEAD` fallback cannot run
21
+ * there either, and the worker dies at startup on an unresolved `HF_BUILD_SHA` (verified on the
22
+ * box, 4.3.21). What Coolify *does* do is materialise an application environment entry for every
23
+ * `${VAR}` the compose file interpolates, which is what this fills in before every deploy.
24
+ */
25
+ export const SOURCE_COMMIT_ENV = "SOURCE_COMMIT";
26
+ /**
27
+ * `SOURCE_COMMIT`, then the deploy, then the wait — in that order and never another.
28
+ *
29
+ * The environment write is what the build reads, so a deploy triggered first would build the
30
+ * previous commit's tag and the wait would then time out against a version the app is right to
31
+ * report. Both `hf new`'s tenth step and `hf deploy` come through here for that reason.
32
+ */
33
+ export async function deployCommit(target) {
34
+ const deadline = target.now() + DEPLOY_TIMEOUT_MS;
35
+ await setSourceCommit(target);
36
+ const { deployments } = await target.coolify.deploy(target.appUuid, { force: true });
37
+ const deploymentUuid = deployments[0]?.deployment_uuid;
38
+ if (deploymentUuid === undefined) {
39
+ throw new StepFailed(`Coolify accepted the deploy of ${target.name} but named no deployment`);
40
+ }
41
+ target.io.out(`${target.name}: deployment ${deploymentUuid} queued`);
42
+ await waitForBuild(target, deploymentUuid, deadline);
43
+ await waitForVersion(target, deadline);
44
+ target.io.out(`${target.name}: serving ${short(target.sha)} at https://${target.fqdn}`);
45
+ }
15
46
  /**
16
47
  * Deploy, wait for the build, then wait for the app to say it is running that commit.
17
48
  *
@@ -32,59 +63,92 @@ export const deployStep = {
32
63
  if (sha === undefined) {
33
64
  throw new StepFailed(`${context.dir} has no commit to deploy: the install step has not run`);
34
65
  }
66
+ const fqdn = appFqdn(context);
67
+ const readToken = context.state.state.statusTokens?.read;
68
+ if (readToken === undefined) {
69
+ throw new StepFailed(`no read status token in the state cache: nothing can ask https://${fqdn}/api/status ` +
70
+ "what it is running");
71
+ }
35
72
  const required = requireOperatorConfig(context.config, ["HF_COOLIFY_URL", "HF_COOLIFY_TOKEN"], { env: context.env });
36
- const coolify = new CoolifyClient({
37
- url: required.HF_COOLIFY_URL,
38
- token: required.HF_COOLIFY_TOKEN,
73
+ await deployCommit({
74
+ name: names.given,
75
+ appUuid,
76
+ fqdn,
77
+ sha,
78
+ readToken,
79
+ coolify: new CoolifyClient({
80
+ url: required.HF_COOLIFY_URL,
81
+ token: required.HF_COOLIFY_TOKEN,
82
+ fetch: context.fetch,
83
+ }),
84
+ io: context.io,
85
+ now: context.now,
86
+ sleep: context.sleep,
39
87
  fetch: context.fetch,
40
88
  });
41
- const deadline = context.now() + DEPLOY_TIMEOUT_MS;
42
- const { deployments } = await coolify.deploy(appUuid, { force: true });
43
- const deploymentUuid = deployments[0]?.deployment_uuid;
44
- if (deploymentUuid === undefined) {
45
- throw new StepFailed(`Coolify accepted the deploy of ${names.given} but named no deployment`);
46
- }
47
- context.io.out(`${names.given}: deployment ${deploymentUuid} queued`);
48
- await waitForBuild(context, coolify, deploymentUuid, deadline);
49
- await waitForVersion(context, sha, deadline);
50
89
  await context.state.patch({ lastDeployedSha: sha });
51
- context.io.out(`${names.given}: serving ${short(sha)} at https://${appFqdn(context)}`);
52
90
  },
53
91
  };
54
- async function waitForBuild(context, coolify, deploymentUuid, deadline) {
92
+ /**
93
+ * Points `SOURCE_COMMIT` at the commit about to be deployed, creating the entry if it is absent.
94
+ *
95
+ * Idempotent, and deliberately outside the `coolify` step's bulk PATCH: this is a value `hf` sets
96
+ * per deploy rather than one of the app's secrets, so it is neither in `secretsHash` nor in what
97
+ * `assertEnvsMatchCompose` compares. Every entry Coolify lists under the name is written, because
98
+ * a compose parse materialises a preview entry beside the non-preview one and the deploy reads
99
+ * its own.
100
+ */
101
+ async function setSourceCommit(target) {
102
+ const existing = (await target.coolify.listEnvs(target.appUuid)).filter((entry) => entry.key === SOURCE_COMMIT_ENV);
103
+ const variable = {
104
+ key: SOURCE_COMMIT_ENV,
105
+ value: target.sha,
106
+ is_buildtime: true,
107
+ is_runtime: true,
108
+ };
109
+ if (existing.length === 0) {
110
+ await target.coolify.createEnv(target.appUuid, variable);
111
+ }
112
+ else {
113
+ for (const entry of existing) {
114
+ await target.coolify.updateEnv(target.appUuid, {
115
+ ...variable,
116
+ is_preview: entry.is_preview ?? false,
117
+ });
118
+ }
119
+ }
120
+ target.io.out(`${target.name}: ${SOURCE_COMMIT_ENV}=${short(target.sha)} set in Coolify`);
121
+ }
122
+ async function waitForBuild(target, deploymentUuid, deadline) {
55
123
  for (let wait = FIRST_POLL_MS;; wait = Math.min(wait * 2, MAX_POLL_MS)) {
56
- const deployment = await coolify.getDeployment(deploymentUuid);
124
+ const deployment = await target.coolify.getDeployment(deploymentUuid);
57
125
  if (deployment.status === FINISHED)
58
126
  return;
59
127
  if (deployment.status.startsWith("failed") || deployment.status.startsWith("cancelled")) {
60
128
  throw new StepFailed(`Coolify deployment ${deploymentUuid} ended ${deployment.status}: read the build log in ` +
61
129
  "Coolify, fix it, and re-run hf new");
62
130
  }
63
- if (context.now() >= deadline) {
131
+ if (target.now() >= deadline) {
64
132
  throw new StepFailed(`Coolify deployment ${deploymentUuid} was still ${deployment.status} after ` +
65
133
  `${String(DEPLOY_TIMEOUT_MS / 60_000)} minutes`);
66
134
  }
67
- await context.sleep(wait);
135
+ await target.sleep(wait);
68
136
  }
69
137
  }
70
138
  /**
71
- * Polls `/api/status` under the read token until it reports `sha`.
139
+ * Polls `/api/status` under the read token until it reports `target.sha`.
72
140
  *
73
141
  * A refusal or an unparseable answer is not a failure here — the containers are restarting, and the
74
142
  * old ones answer until the new ones are healthy — so only the deadline ends this loop.
75
143
  */
76
- async function waitForVersion(context, sha, deadline) {
77
- const url = `https://${appFqdn(context)}/api/status`;
78
- const token = context.state.state.statusTokens?.read;
79
- if (token === undefined) {
80
- throw new StepFailed(`no read status token in the state cache: nothing can ask ${url} what it is running`);
81
- }
82
- const doFetch = context.fetch ?? ((input, init) => globalThis.fetch(input, init));
144
+ async function waitForVersion(target, deadline) {
145
+ const url = `https://${target.fqdn}/api/status`;
146
+ const doFetch = target.fetch ?? ((input, init) => globalThis.fetch(input, init));
83
147
  let last = "nothing yet";
84
148
  for (let wait = FIRST_POLL_MS;; wait = Math.min(wait * 2, MAX_POLL_MS)) {
85
149
  try {
86
150
  const response = await doFetch(url, {
87
- headers: { authorization: `Bearer ${token}`, accept: "application/json" },
151
+ headers: { authorization: `Bearer ${target.readToken}`, accept: "application/json" },
88
152
  });
89
153
  if (!response.ok) {
90
154
  last = `HTTP ${String(response.status)}`;
@@ -92,7 +156,7 @@ async function waitForVersion(context, sha, deadline) {
92
156
  else {
93
157
  const report = (await response.json());
94
158
  const version = report.applicationVersion;
95
- if (version === sha)
159
+ if (version === target.sha)
96
160
  return;
97
161
  last = version === null ? "no applicationVersion" : `applicationVersion ${short(version)}`;
98
162
  }
@@ -100,11 +164,11 @@ async function waitForVersion(context, sha, deadline) {
100
164
  catch (error) {
101
165
  last = error.message;
102
166
  }
103
- if (context.now() >= deadline) {
104
- throw new StepFailed(`${url} never reported ${short(sha)} within ` +
167
+ if (target.now() >= deadline) {
168
+ throw new StepFailed(`${url} never reported ${short(target.sha)} within ` +
105
169
  `${String(DEPLOY_TIMEOUT_MS / 60_000)} minutes (last: ${last}). The build finished, so ` +
106
- "check that SOURCE_COMMIT reached the image — hf doctor reports the same mismatch.");
170
+ `check that ${SOURCE_COMMIT_ENV} reached the image — hf doctor reports the same mismatch.`);
107
171
  }
108
- await context.sleep(wait);
172
+ await target.sleep(wait);
109
173
  }
110
174
  }
@@ -1,12 +1,7 @@
1
1
  import type { Step } from "../new-cloud.js";
2
+ import { templateTempDir } from "../new.js";
2
3
  import { type CloudStepContext } from "./context.js";
3
- /**
4
- * Where the fetch lands before it becomes the app.
5
- *
6
- * Beside the target rather than under `os.tmpdir()`, so the rename is a rename and not a second
7
- * copy across filesystems, and dot-prefixed so a half-fetched tree does not look like an app.
8
- */
9
- export declare function templateTempDir(dir: string): string;
4
+ export { templateTempDir };
10
5
  /**
11
6
  * The app's files: giget's fetch of the template, substituted, renamed into place.
12
7
  *
@@ -1,16 +1,8 @@
1
1
  import { readFile, rename, rm } from "node:fs/promises";
2
2
  import path from "node:path";
3
- import { substituteTree, TEMPLATE_MARKER, TemplateError } from "../new.js";
3
+ import { scratchInTheWay, substituteTree, targetInTheWay, templateTempDir, TEMPLATE_MARKER, TemplateError, } from "../new.js";
4
4
  import { exists } from "./context.js";
5
- /**
6
- * Where the fetch lands before it becomes the app.
7
- *
8
- * Beside the target rather than under `os.tmpdir()`, so the rename is a rename and not a second
9
- * copy across filesystems, and dot-prefixed so a half-fetched tree does not look like an app.
10
- */
11
- export function templateTempDir(dir) {
12
- return path.join(path.dirname(dir), `.${path.basename(dir)}.hf-new`);
13
- }
5
+ export { templateTempDir };
14
6
  /**
15
7
  * The app's files: giget's fetch of the template, substituted, renamed into place.
16
8
  *
@@ -32,7 +24,15 @@ export const templateStep = {
32
24
  return;
33
25
  }
34
26
  const temp = templateTempDir(dir);
35
- await rm(temp, { recursive: true, force: true });
27
+ if (await exists(temp)) {
28
+ // Cleared only on a resume, by the same rule the target directory gets: `fetchStartedAt`
29
+ // says a run of *this* app fetched into that path, and without it the directory is someone
30
+ // else's however much its name looks like ours.
31
+ if (context.state.state.templateFetchStartedAt === undefined)
32
+ throw scratchInTheWay(temp);
33
+ await rm(temp, { recursive: true, force: true });
34
+ }
35
+ await context.state.patch({ templateFetchStartedAt: new Date(context.now()).toISOString() });
36
36
  const fetched = await context.fetchTemplate(context.from, temp);
37
37
  await substituteTree(fetched, names);
38
38
  // The marker is what `assertTemplateSource` looks for: an app is never a template twice.
@@ -66,7 +66,7 @@ async function adoptOrRefuse(context) {
66
66
  }
67
67
  const substituted = !(await exists(path.join(dir, TEMPLATE_MARKER))) && (await packageName(dir)) === names.appName;
68
68
  if (!substituted) {
69
- throw new TemplateError(`${dir} already exists; hf new will not write into it`);
69
+ throw targetInTheWay(dir);
70
70
  }
71
71
  context.io.out(`${names.given}: adopting the app directory already at ${dir}`);
72
72
  }
@@ -0,0 +1,32 @@
1
+ import { type StepExec, type StepOut } from "./cloud-steps/index.js";
2
+ import { type OperatorConfig } from "./config.js";
3
+ import type { FetchLike } from "./providers/http.js";
4
+ export interface DeployAppOptions {
5
+ /** The app as `hf new` named it, which is also its state file's name. */
6
+ app: string;
7
+ /** The commit to deploy. Defaults to `main`'s on the app's own repository. */
8
+ sha?: string;
9
+ io: StepOut;
10
+ config?: OperatorConfig;
11
+ /** Where the per-app state files are. Defaults to `stateDir()`. */
12
+ stateDir?: string;
13
+ env?: NodeJS.ProcessEnv;
14
+ fetch?: FetchLike;
15
+ exec?: StepExec;
16
+ now?: () => number;
17
+ sleep?: (ms: number) => Promise<void>;
18
+ }
19
+ export interface DeployAppResult {
20
+ app: string;
21
+ /** The commit the app answered `/api/status` with before this returned. */
22
+ sha: string;
23
+ url: string;
24
+ }
25
+ /**
26
+ * `hf deploy <name>` — point the app at a commit and wait until it says it is serving it.
27
+ *
28
+ * The same path as `hf new`'s tenth step, and the reason there is a command at all: auto-deploy
29
+ * is off, so a merge to main changes nothing on the box until this runs. Everything it needs is
30
+ * in the state cache the provisioning run wrote; nothing here is interactive.
31
+ */
32
+ export declare function deployApp(options: DeployAppOptions): Promise<DeployAppResult>;