Skip to content

Commit

Permalink
refactor(std/image): use pre-sdk support tools
Browse files Browse the repository at this point in the history
  • Loading branch information
deciduously committed Dec 4, 2024
1 parent f533c75 commit 5ff8eff
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
10 changes: 5 additions & 5 deletions packages/std/image.tg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,24 @@ export const testBasicRootfs = tg.target(async () => {
return imageFile;
});

export const testOciBasicEnv = tg.target(async () => {
export const testBasicEnv = tg.target(async () => {
const detectedHost = await std.triple.host();
const host = bootstrap.toolchainTriple(detectedHost);
const utils = await std.utils.env({ host, sdk: bootstrap.sdk.arg() });
const basicEnv = await std.env(utils, { NAME: "Tangram" }, { utils: true });
const utils = await std.utils.env({ host, sdk: false, env: bootstrap.sdk() });
const basicEnv = await std.env(utils, { NAME: "Tangram" }, { utils: false });
return basicEnv;
});

export const testBasicEnvImageDocker = tg.target(async () => {
const basicEnv = await testOciBasicEnv();
const basicEnv = await testBasicEnv();
const imageFile = await image(basicEnv, {
cmd: ["bash"],
});
return imageFile;
});

export const testBasicEnvImageOci = tg.target(async () => {
const basicEnv = await testOciBasicEnv();
const basicEnv = await testBasicEnv();
const imageFile = await image(basicEnv, {
cmd: ["bash"],
format: "oci",
Expand Down
53 changes: 40 additions & 13 deletions packages/std/image/container.tg.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as std from "../tangram.ts";
import { $ } from "../tangram.ts";
import * as bootstrap from "../bootstrap.tg.ts";
import zstd from "../sdk/dependencies/zstd.tg.ts";

/*
Expand Down Expand Up @@ -222,15 +222,15 @@ export const dockerImageFromLayers = async (
// Add each layer file to the image directory.
const layerFilenames = await Promise.all(
layers.map(async (layer) => {
const bytes = await layer.tar.bytes();
const bytes = await layer.tarball.bytes();
const size = bytes.length;
const checksum = await tg.checksum(bytes, "sha256");
const checksumValue = checksum.slice("sha256:".length);

// Add the layer to the image directory, along with the legacy metadata used by older versions of the Docker image spec.
image = tg.directory(image, {
[checksumValue]: {
"layer.tar": layer.tar,
"layer.tar": layer.tarball,
json: tg.encoding.json.encode({
// Use the checksum as a unique layer ID.
id: checksumValue,
Expand Down Expand Up @@ -269,7 +269,7 @@ export const dockerImageFromLayers = async (
});

// Create a `.tar` file of the Docker image. This is the format that `docker load` expects.
return await $`tar -cf $OUTPUT -C ${image} .`.then(tg.File.expect);
return await createTarball(image);
};

export const ociImageFromLayers = async (
Expand Down Expand Up @@ -310,11 +310,24 @@ export const ociImageFromLayers = async (
layerCompression === "gzip"
? MediaTypeV1.imageLayerTarGzip
: MediaTypeV1.imageLayerTarZstd;
const additionalEnv = layerCompression === "gzip" ? [] : [zstd()];
const additionalEnv: tg.Unresolved<Array<std.env.Arg>> = [
std.utils.env({ sdk: false, env: bootstrap.sdk() }),
];
if (layerCompression === "zstd") {
additionalEnv.push(
zstd({
sdk: false,
env: await std.env.arg(additionalEnv, bootstrap.sdk()),
}),
);
}
const layerDescriptors = await Promise.all(
layers.map(async (layer) => {
const file = await $`${compressionCmd} $(realpath ${layer.tar}) > $OUTPUT`
.env(additionalEnv)
const file = await tg
.target(tg`${compressionCmd} $(realpath ${layer.tarball}) > $OUTPUT`, {
env: std.env.arg(additionalEnv),
})
.then((target) => target.output())
.then(tg.File.expect);
const descriptor: ImageDescriptor<typeof mediaType> = {
mediaType,
Expand Down Expand Up @@ -359,7 +372,7 @@ export const ociImageFromLayers = async (
});

// Tar the result and return it.
return await $`tar -cf $OUTPUT -C ${directory} .`.then(tg.File.expect);
return await createTarball(directory);
};

/**
Expand All @@ -386,17 +399,31 @@ type ImageDescriptor<MediaType = string> = {
};

export type Layer = {
tar: tg.File;
tarball: tg.File;
diffId: string;
};

export const layer = tg.target(
async (directory: tg.Directory): Promise<Layer> => {
const bundle = tg.Artifact.bundle(directory);
const tar = await $`tar -cf $OUTPUT -C ${bundle} .`.then(tg.File.expect);
const bytes = await tar.bytes();
const bundle = tg.Artifact.bundle(directory).then(tg.Directory.expect);
const tarball = await createTarball(bundle);
const bytes = await tarball.bytes();
const diffId = await tg.checksum(bytes, "sha256");
return { tar, diffId };
return { tarball, diffId };
},
);

export const createTarball = tg.target(
async (directory: tg.Directory): Promise<tg.File> => {
const tarArtifact = await std.utils.tar.build({
sdk: false,
env: bootstrap.sdk(),
});
const script = tg`tar -cf $OUTPUT -C ${directory} .`;
return await tg
.target(script, { env: await std.env.arg(tarArtifact) })
.then((target) => target.output())
.then(tg.File.expect);
},
);

Expand Down
2 changes: 1 addition & 1 deletion packages/std/tangram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ const testActions = (): Record<string, () => Promise<any>> => {
crossWorkspace: workspace.testCross,
imageWrappedEntrypoint: image.testWrappedEntrypoint,
imageBasicRootfs: image.testBasicRootfs,
imageBasicEnv: image.testOciBasicEnv,
imageBasicEnv: image.testBasicEnv,
imageBasicEnvImageDocker: image.testBasicEnvImageDocker,
imageBasicEnvImageOci: image.testBasicEnvImageOci,
image: image.test,
Expand Down
2 changes: 1 addition & 1 deletion packages/std/utils/patch_cmds.tg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const macOsPatchCmds = tg.target(async (arg?: Arg) => {
const result = await tg
.target(script, {
host: std.triple.archAndOs(build),
env: std.env.arg(arg.env ?? {}, bootstrap.sdk.env()),
env: std.env.arg(arg?.env ?? {}, bootstrap.sdk.env()),
})
.then((target) => target.output())
.then(tg.Directory.expect);
Expand Down

0 comments on commit 5ff8eff

Please sign in to comment.