Skip to content

Commit

Permalink
feat(seed): Always include docker logs (#4784)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney authored Oct 1, 2024
1 parent 854efd5 commit 5e82e60
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"dependencies": {
"@fern-api/fs-utils": "workspace:*",
"@fern-api/logger": "workspace:*",
"dockerode": "^4.0.2",
"tmp-promise": "^3.0.3"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AbsoluteFilePath, doesPathExist, join, RelativeFilePath } from "@fern-api/fs-utils";
import { CONSOLE_LOGGER } from "@fern-api/logger";
import { exec } from "child_process";
import { mkdir, rm } from "fs/promises";
import path from "path";
Expand Down Expand Up @@ -33,6 +34,7 @@ describe("runDocker", () => {
const expectedOutputFilePath = "my-file.txt";

await runDocker({
logger: CONSOLE_LOGGER,
imageName: BASIC_WRITER_IMAGE_NAME,
args: [expectedOutputFilePath],
binds: [`${HOST_OUTPUT_DIR}:${IMAGE_OUTPUT_DIR}`]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Logger } from "@fern-api/logger";
import Docker from "dockerode";
import { writeFile } from "fs/promises";
import { Writable } from "stream";
import tmp from "tmp-promise";

export declare namespace runDocker {
export interface Args {
logger: Logger;
imageName: string;
args?: string[];
binds?: string[];
Expand All @@ -18,14 +20,16 @@ export declare namespace runDocker {
}

export async function runDocker({
logger,
imageName,
args,
binds,
writeLogsToFile = true,
removeAfterCompletion = false
}: runDocker.Args): Promise<void> {
const docker = new Docker();
const tryRun = () => tryRunDocker({ docker, imageName, args, binds, removeAfterCompletion, writeLogsToFile });
const tryRun = () =>
tryRunDocker({ logger, docker, imageName, args, binds, removeAfterCompletion, writeLogsToFile });
try {
await tryRun();
} catch (e) {
Expand All @@ -41,13 +45,15 @@ export async function runDocker({

// may throw a 404 if the image hasn't been downloaded
async function tryRunDocker({
logger,
docker,
imageName,
args,
binds,
removeAfterCompletion,
writeLogsToFile
}: {
logger: Logger;
docker: Docker;
imageName: string;
args?: string[];
Expand Down Expand Up @@ -81,14 +87,14 @@ async function tryRunDocker({
throw status.Error;
}

if (writeLogsToFile) {
const tmpFile = await tmp.file();
await writeFile(tmpFile.path, logs);
logger.info(`Generator logs here: ${tmpFile.path}`);
}

if (status.StatusCode !== 0) {
if (writeLogsToFile) {
const tmpFile = await tmp.file();
await writeFile(tmpFile.path, logs);
throw new Error(`Docker exited with a non-zero exit code. Logs here: ${tmpFile.path}`);
} else {
throw new Error("Docker exited with a non-zero exit code.");
}
throw new Error("Docker exited with a non-zero exit code.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export async function runGenerator({
absolutePathToIr,
absolutePathToWriteConfigJson,
keepDocker,
context,
generatorInvocation,
writeUnitTests,
generateOauthClients,
Expand Down Expand Up @@ -238,6 +239,7 @@ export async function runGenerator({
}

await runDocker({
logger: context.logger,
imageName,
args: [DOCKER_GENERATOR_CONFIG_PATH],
binds,
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5e82e60

Please sign in to comment.