Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buildx: localstate refs #316

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions __tests__/buildx/buildx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {Exec} from '../../src/exec';

import {Cert} from '../../src/types/buildx';

const fixturesDir = path.join(__dirname, '..', 'fixtures');
// prettier-ignore
const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-jest');
const tmpName = path.join(tmpDir, '.tmpname-jest');
Expand Down Expand Up @@ -252,3 +253,38 @@ describe('resolveCertsDriverOpts', () => {
}
});
});

describe('refs', () => {
it('returns all refs', async () => {
const refs = Buildx.refs({
dir: path.join(fixturesDir, 'buildx-refs')
});
expect(Object.keys(refs).length).toEqual(11);
});
it('returns default builder refs', async () => {
const refs = Buildx.refs({
dir: path.join(fixturesDir, 'buildx-refs'),
builderName: 'default'
});
expect(Object.keys(refs).length).toEqual(8);
});
it('returns foo builder refs', async () => {
const refs = Buildx.refs({
dir: path.join(fixturesDir, 'buildx-refs'),
builderName: 'foo'
});
expect(Object.keys(refs).length).toEqual(3);
});
it('returns default builder refs since', async () => {
const mdate = new Date('2023-09-05T00:00:00Z');
fs.utimesSync(path.join(fixturesDir, 'buildx-refs', 'default', 'default', '36dix0eiv9evr61vrwzn32w7q'), mdate, mdate);
fs.utimesSync(path.join(fixturesDir, 'buildx-refs', 'default', 'default', '49p5r8und2konke5pmlyzqp3n'), mdate, mdate);
fs.utimesSync(path.join(fixturesDir, 'buildx-refs', 'default', 'default', 'a8zqzhhv5yiazm396jobsgdw2'), mdate, mdate);
const refs = Buildx.refs({
dir: path.join(fixturesDir, 'buildx-refs'),
builderName: 'default',
since: new Date('2024-01-10T00:00:00Z')
});
expect(Object.keys(refs).length).toEqual(5);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Definition":"eyJncm91cCI6eyJkZWZhdWx0Ijp7InRhcmdldHMiOlsiZGV2Il19fSwidGFyZ2V0Ijp7ImRldiI6eyJjb250ZXh0IjoiLiIsImRvY2tlcmZpbGUiOiJEb2NrZXJmaWxlIiwiYXJncyI6eyJCVUlMREtJVF9DT05URVhUX0tFRVBfR0lUX0RJUiI6IjEiLCJERUZBVUxUX1BST0RVQ1RfTElDRU5TRSI6IiIsIkRPQ0tFUl9CVUlMRFRBR1MiOiIiLCJET0NLRVJfREVCVUciOiIiLCJET0NLRVJfTERGTEFHUyI6IiIsIkRPQ0tFUl9TVEFUSUMiOiIxIiwiUEFDS0FHRVJfTkFNRSI6IiIsIlBMQVRGT1JNIjoiIiwiUFJPRFVDVCI6IiIsIlNZU1RFTUQiOiJmYWxzZSIsIlZFUlNJT04iOiIifSwidGFncyI6WyJkb2NrZXItZGV2Il0sInRhcmdldCI6ImRldiIsIm91dHB1dCI6WyJ0eXBlPWRvY2tlciJdfX19","Targets":["dev"],"Refs":["vzxn0jxr44khtq7hc8drtzwjv"]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"LocalPath":"/home/crazymax/github/docker/docker-alpine-s6","DockerfilePath":""}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":""}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":"/home/crazymax/github/docker_org/buildx/Dockerfile"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":"/home/crazymax/github/docker_org/buildx/Dockerfile"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":""}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":"/home/crazymax/github/docker_org/buildx/Dockerfile"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":""}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Target":"dev","LocalPath":"/home/crazymax/github/docker_org/docker","DockerfilePath":"/home/crazymax/github/docker_org/docker/Dockerfile","GroupRef":"1fugf958r4peyg86h6scim5t5"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Target":"default","LocalPath":"/home/crazymax/github/docker_org/compose/.dev/47231","DockerfilePath":"/home/crazymax/github/docker_org/compose/.dev/47231/bad/Dockerfile"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Target":"default","LocalPath":"/home/crazymax/github/docker_org/compose/.dev/47231","DockerfilePath":"/home/crazymax/github/docker_org/compose/.dev/47231/bad/Dockerfile"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Target":"default","LocalPath":"/home/crazymax/github/docker_org/compose/.dev/47231","DockerfilePath":"/home/crazymax/github/docker_org/compose/.dev/47231/bad/Dockerfile"}
52 changes: 51 additions & 1 deletion src/buildx/buildx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import * as semver from 'semver';
import {Docker} from '../docker/docker';
import {Exec} from '../exec';

import {Cert} from '../types/buildx';
import {Cert, LocalRefsOpts, LocalRefsResponse, LocalState} from '../types/buildx';

export interface BuildxOpts {
standalone?: boolean;
Expand All @@ -45,6 +45,14 @@ export class Buildx {
return process.env.BUILDX_CONFIG || path.join(Docker.configDir, 'buildx');
}

static get refsDir(): string {
return path.join(Buildx.configDir, 'refs');
}

static get refsGroupDir(): string {
return path.join(Buildx.refsDir, '__group__');
}

static get certsDir(): string {
return path.join(Buildx.configDir, 'certs');
}
Expand Down Expand Up @@ -168,4 +176,46 @@ export class Buildx {
}
return driverOpts;
}

public static refs(opts: LocalRefsOpts, refs: LocalRefsResponse = {}): LocalRefsResponse {
const {dir, builderName, nodeName, since} = opts;

let dirpath = path.resolve(dir);
if (opts.builderName) {
dirpath = path.join(dirpath, opts.builderName);
}
if (opts.nodeName) {
dirpath = path.join(dirpath, opts.nodeName);
}
if (!fs.existsSync(dirpath)) {
return refs;
}

const files = fs.readdirSync(dirpath);
for (const file of files) {
const filePath = path.join(dirpath, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
const nopts: LocalRefsOpts = {...opts};
if (!builderName) {
if (file === '__group__') {
continue;
}
nopts.builderName = file;
} else if (!nodeName) {
nopts.nodeName = file;
}
Buildx.refs(nopts, refs);
} else {
if (since && stat.mtime < since) {
continue;
}
const localState = <LocalState>JSON.parse(fs.readFileSync(filePath, 'utf8'));
const ref = `${builderName}/${nodeName}/${file}`;
refs[ref] = localState;
}
}

return refs;
}
}
18 changes: 18 additions & 0 deletions src/types/buildx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,21 @@ export interface DownloadVersion {
downloadURL: string;
releasesURL: string;
}

export interface LocalRefsOpts {
dir: string;
builderName?: string;
nodeName?: string;
since?: Date;
}

export interface LocalRefsResponse {
[ref: string]: LocalState;
}

export interface LocalState {
Target: string;
LocalPath: string;
DockerfilePath: string;
GroupRef?: string;
}
Loading