Skip to content

Commit

Permalink
fixup! feat(cli): build based on configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
tuler committed Oct 9, 2024
1 parent 237299c commit 29c293c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
6 changes: 3 additions & 3 deletions apps/cli/src/builder/docker.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { execa } from "execa";
import fs from "fs-extra";
import { spawnSync } from "node:child_process";
import path from "path";
import tmp from "tmp";
import { DockerDriveConfig } from "../config.js";
import { execaDockerFallback } from "../exec.js";
import { execaDockerFallback, spawnSyncDockerFallback } from "../exec.js";
import { tarToExt } from "./index.js";

type ImageBuildOptions = Pick<
Expand Down Expand Up @@ -102,12 +101,13 @@ export const build = async (
});

// create rootfs tar from OCI tar
spawnSync("crane", ["export", "-", "-"], {
await spawnSyncDockerFallback("crane", ["export", "-", "-"], {
stdio: [
fs.openSync(path.join(destination, ocitar), "r"),
fs.openSync(path.join(destination, tar), "w"),
"inherit",
],
image: sdkImage,
});

switch (format) {
Expand Down
4 changes: 2 additions & 2 deletions apps/cli/src/builder/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { execaDockerFallback, OptionsDockerFallback } from "../exec.js";
import { execaDockerFallback, ExecaOptionsDockerFallback } from "../exec.js";

export { build as buildDirectory } from "./directory.js";
export { build as buildDocker } from "./docker.js";
Expand All @@ -11,7 +11,7 @@ export const tarToExt = async (
output: string,
format: "ext2",
extraSize: number,
options: OptionsDockerFallback,
options: ExecaOptionsDockerFallback,
) => {
const blockSize = 4096; // fixed at 4k
const extraBlocks = Math.ceil(extraSize / blockSize);
Expand Down
57 changes: 55 additions & 2 deletions apps/cli/src/exec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { spawnSync, SpawnSyncOptions } from "child_process";
import { execa, ExecaError, Options } from "execa";

/**
Expand All @@ -7,11 +8,11 @@ import { execa, ExecaError, Options } from "execa";
* @param options execution options
* @returns return of execa
*/
export type OptionsDockerFallback = Options & { image?: string };
export type ExecaOptionsDockerFallback = Options & { image?: string };
export const execaDockerFallback = async (
command: string,
args: readonly string[],
options: OptionsDockerFallback,
options: ExecaOptionsDockerFallback,
) => {
try {
return await execa(command, args, options);
Expand Down Expand Up @@ -42,3 +43,55 @@ export const execaDockerFallback = async (
throw error;
}
};

/**
* Calls spawnSync and falls back to docker run if command (on the host) fails
* @param command command to be executed
* @param args arguments to be passed to the command
* @param options execution options
* @returns return of execa
*/
export type SpawnOptionsDockerFallback = SpawnSyncOptions & { image?: string };
export const spawnSyncDockerFallback = async (
command: string,
args: readonly string[],
options: SpawnOptionsDockerFallback,
) => {
const result = spawnSync(command, args, options);
if (result.error) {
const code = (result.error as any).code;

Check warning on line 62 in apps/cli/src/exec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (code === "ENOENT" && options.image) {
console.warn(
`error executing '${command}', falling back to docker execution using image '${options.image}'`,
);
const dockerOpts = [
"--volume",
`${options.cwd}:/work`,
"--workdir",
"/work",
"--interactive",
];
const dockerArgs = [
"run",
...dockerOpts,
options.image,
command,
...args,
];
const dockerResult = spawnSync("docker", dockerArgs, options);
if (dockerResult.error) {
console.error(
`error executing '${command}'`,
dockerResult.error,
);
throw dockerResult.error;
}
return dockerResult;
} else {
console.error(`error executing '${command}'`, result.error);
throw result.error;
}
}
console.log(result);
return result;
};

0 comments on commit 29c293c

Please sign in to comment.