anbaric-cloud-hosting 1.31.0 → 1.32.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anbaric-cloud-hosting",
3
- "version": "1.31.0",
3
+ "version": "1.32.0",
4
4
  "description": "Anbaric Cloud hosting service: Postgres-backed job persistence and queuing exposed over an HTTP API",
5
5
  "license": "MIT",
6
6
  "author": "chris@anbaric.ai",
@@ -18,18 +18,18 @@
18
18
  "@aws-sdk/client-s3": "^3.1110.0",
19
19
  "@aws-sdk/client-secrets-manager": "^3.700.0",
20
20
  "@aws-sdk/client-servicediscovery": "^3.1110.0",
21
- "anbaric-data-store": "^1.31.0",
22
- "anbaric-plugins": "^1.31.0",
23
- "anbaric-tsapi": "^1.31.0",
24
- "anbaric-web": "^1.31.0",
21
+ "anbaric-data-store": "^1.32.0",
22
+ "anbaric-plugins": "^1.32.0",
23
+ "anbaric-tsapi": "^1.32.0",
24
+ "anbaric-web": "^1.32.0",
25
25
  "esbuild": "^0.28.2",
26
26
  "pg": "^8.16.0"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/node": "^26.2.0",
30
30
  "@types/pg": "^8.15.0",
31
- "anbaric-impl-cloud": "^1.31.0",
32
- "anbaric-state-machine": "^1.31.0",
31
+ "anbaric-impl-cloud": "^1.32.0",
32
+ "anbaric-state-machine": "^1.32.0",
33
33
  "tsx": "^4.20.0",
34
34
  "typescript": "^7.0.2"
35
35
  },
@@ -114,10 +114,28 @@ abstract class BaseBuildLayer implements BuildLayer {
114
114
  }));
115
115
  }
116
116
 
117
+ // Regenerates an app's docs from its uploaded source without redeploying it:
118
+ // the source is materialised (from S3 for Fargate, from disk for Docker) and
119
+ // the same generator that runs on deploy is invoked against it.
120
+ async regenerateDocs(appName : string) : Promise<number> {
121
+ if (!this.deployments.has(appName)) throw new Error(`No app named "${appName}"`);
122
+ if (!this.docGenerator) throw new Error("Documentation generation is not configured on this platform");
123
+
124
+ const { dir, cleanup } = await this.sourceDir(appName);
125
+ try {
126
+ return await this.docGenerator.generate(appName, dir, { redeploy: true });
127
+ } finally {
128
+ await cleanup();
129
+ }
130
+ }
131
+
117
132
  protected abstract appHostFor(appName : string) : string;
118
133
  protected abstract start(deployment : Deployment, appDir : string, entryPoint : string) : Promise<void>;
119
134
  protected abstract stop(deployment : Deployment) : Promise<void>;
120
135
  protected abstract streamLogs(deployment : Deployment, signal : AbortSignal) : AsyncIterable<string>;
136
+ // Materialises the app's source into a directory for regeneration; the
137
+ // returned cleanup removes anything temporary the implementation created.
138
+ protected abstract sourceDir(appName : string) : Promise<{ dir : string, cleanup : () => Promise<void> }>;
121
139
 
122
140
  private async buildAndStart(deployment : Deployment, tarball : Buffer) : Promise<void> {
123
141
  const appDir = join(this.appsDir, deployment.appName);
@@ -16,6 +16,9 @@ interface BuildLayer {
16
16
  ping(appName : string) : Promise<boolean>;
17
17
  logs(appName : string, signal : AbortSignal) : AsyncIterable<string>;
18
18
  teardown(appName : string) : Promise<boolean>;
19
+ // Re-runs documentation generation for an already-deployed app from its
20
+ // uploaded source, without redeploying it. Returns the number of docs written.
21
+ regenerateDocs(appName : string) : Promise<number>;
19
22
  cleanUp() : Promise<void>;
20
23
 
21
24
  }
@@ -120,6 +120,12 @@ class DockerBuildLayer extends BaseBuildLayer {
120
120
  await this.removeContainer(deployment, this.appHostFor(deployment.appName));
121
121
  }
122
122
 
123
+ // Locally the extracted source from the last deploy is still on disk, so
124
+ // regeneration reads it in place with nothing to clean up.
125
+ protected async sourceDir(appName : string) : Promise<{ dir : string, cleanup : () => Promise<void> }> {
126
+ return { dir: join(this.appsDir, appName), cleanup: async () => {} };
127
+ }
128
+
123
129
  private async removeContainer(deployment : Deployment, container : string) : Promise<void> {
124
130
  await this.docker(deployment, ["rm", "--force", container]).catch(() => {});
125
131
  }
@@ -1,10 +1,12 @@
1
- import {readFile, writeFile} from "node:fs/promises";
1
+ import {spawn} from "node:child_process";
2
+ import {mkdir, mkdtemp, readFile, rm, writeFile} from "node:fs/promises";
3
+ import {tmpdir} from "node:os";
2
4
  import {join} from "node:path";
3
5
  import {CodeBuildClient, BatchGetBuildsCommand, StartBuildCommand} from "@aws-sdk/client-codebuild";
4
6
  import {ECSClient, CreateServiceCommand, DeleteServiceCommand, DescribeServicesCommand,
5
7
  DescribeTaskDefinitionCommand, ListServicesCommand as ListEcsServicesCommand,
6
8
  RegisterTaskDefinitionCommand, UpdateServiceCommand} from "@aws-sdk/client-ecs";
7
- import {S3Client, PutObjectCommand} from "@aws-sdk/client-s3";
9
+ import {S3Client, GetObjectCommand, PutObjectCommand} from "@aws-sdk/client-s3";
8
10
  import {ServiceDiscoveryClient, CreateServiceCommand as CreateDiscoveryServiceCommand,
9
11
  ListServicesCommand} from "@aws-sdk/client-servicediscovery";
10
12
  import {CloudWatchLogsClient, FilterLogEventsCommand, StartLiveTailCommand} from "@aws-sdk/client-cloudwatch-logs";
@@ -344,7 +346,38 @@ class FargateBuildLayer extends BaseBuildLayer {
344
346
  return `builds/${appName}.tar.gz`;
345
347
  }
346
348
 
349
+ protected async sourceDir(appName : string) : Promise<{ dir : string, cleanup : () => Promise<void> }> {
350
+ const work = await mkdtemp(join(tmpdir(), `anbaric-docs-${appName}-`));
351
+ const cleanup = () => rm(work, { recursive: true, force: true });
352
+
353
+ try {
354
+ const object = await this.aws.s3.send(new GetObjectCommand({
355
+ Bucket: this.options.buildBucket,
356
+ Key: this.buildKeyFor(appName),
357
+ }));
358
+ const bytes = await object.Body.transformToByteArray();
359
+
360
+ const tarballPath = join(work, "source.tar.gz");
361
+ await writeFile(tarballPath, Buffer.from(bytes));
362
+ const dir = join(work, "src");
363
+ await mkdir(dir, { recursive: true });
364
+ await extractTarball(tarballPath, dir);
365
+
366
+ return { dir, cleanup };
367
+ } catch (error) {
368
+ await cleanup();
369
+ throw error;
370
+ }
371
+ }
372
+
347
373
  }
348
374
 
375
+ const extractTarball = (tarballPath : string, dir : string) : Promise<void> =>
376
+ new Promise((resolve, reject) => {
377
+ const tar = spawn("tar", ["-xzf", tarballPath, "-C", dir]);
378
+ tar.on("error", reject);
379
+ tar.on("close", code => code === 0 ? resolve() : reject(new Error(`tar exited with code ${code}`)));
380
+ });
381
+
349
382
  export { FargateBuildLayer };
350
383
  export type { AwsClients, FargateBuildLayerOptions };
@@ -75,20 +75,22 @@ class DocGenerator {
75
75
 
76
76
  constructor(private model : DocModel, private store : AppDocsStore) {}
77
77
 
78
- async generate(appName : string, appDir : string, options : { redeploy : boolean }) : Promise<void> {
78
+ async generate(appName : string, appDir : string, options : { redeploy : boolean }) : Promise<number> {
79
79
  try {
80
80
  const { files, truncated } = await gatherSources(appDir);
81
- if (files.length === 0) return;
81
+ if (files.length === 0) return 0;
82
82
 
83
83
  const drafts = await this.model.writeDocs({ appName, files, truncated });
84
84
  const readme = files.find(file => README.test(file.path))?.content;
85
85
  const docs = buildDocTree(drafts, readme);
86
- if (docs.length === 0) return;
86
+ if (docs.length === 0) return 0;
87
87
 
88
88
  await this.store.replaceForApp(appName, docs);
89
89
  console.log(`[docs] ${options.redeploy ? "regenerated" : "generated"} ${docs.length} doc(s) for "${appName}"`);
90
+ return docs.length;
90
91
  } catch (error) {
91
92
  console.error(`[docs] could not generate documentation for "${appName}": ${error instanceof Error ? error.message : error}`);
93
+ return 0;
92
94
  }
93
95
  }
94
96
 
@@ -15,6 +15,9 @@ class AppsHandler implements RequestHandler {
15
15
  case "logs":
16
16
  if (request.id) return this.handleLogs(request, request.id);
17
17
  break;
18
+ case "docs":
19
+ if (request.id) return this.handleDocs(request, request.id);
20
+ break;
18
21
  case undefined:
19
22
  if (request.id) return this.handleApp(request, request.id);
20
23
  return this.handleCollection(request);
@@ -22,6 +25,17 @@ class AppsHandler implements RequestHandler {
22
25
  request.notFound();
23
26
  }
24
27
 
28
+ private async handleDocs(request : Request, appName : string) : Promise<void> {
29
+ if (request.method !== "POST") return request.notFound();
30
+ try {
31
+ const docs = await this.buildLayer.regenerateDocs(appName);
32
+ return request.reply(200, { appName, docs });
33
+ } catch (error) {
34
+ const message = error instanceof Error ? error.message : "Could not regenerate documentation";
35
+ return request.reply(message.startsWith("No app") ? 404 : 400, { error: message });
36
+ }
37
+ }
38
+
25
39
  private async handleDeploy(request : Request, appName : string) : Promise<void> {
26
40
  switch (request.method) {
27
41
  case "POST": {