Skip to content

Commit e41efdd

Browse files
committed
buildx: localstate refs
Signed-off-by: CrazyMax <[email protected]>
1 parent 33688c4 commit e41efdd

15 files changed

+117
-1
lines changed

__tests__/buildx/buildx.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {Exec} from '../../src/exec';
2626

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

29+
const fixturesDir = path.join(__dirname, '..', 'fixtures');
2930
// prettier-ignore
3031
const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-jest');
3132
const tmpName = path.join(tmpDir, '.tmpname-jest');
@@ -252,3 +253,38 @@ describe('resolveCertsDriverOpts', () => {
252253
}
253254
});
254255
});
256+
257+
describe('refs', () => {
258+
it('returns all refs', async () => {
259+
const refs = Buildx.refs({
260+
dir: path.join(fixturesDir, 'buildx-refs')
261+
});
262+
expect(Object.keys(refs).length).toEqual(11);
263+
});
264+
it('returns default builder refs', async () => {
265+
const refs = Buildx.refs({
266+
dir: path.join(fixturesDir, 'buildx-refs'),
267+
builderName: 'default'
268+
});
269+
expect(Object.keys(refs).length).toEqual(8);
270+
});
271+
it('returns foo builder refs', async () => {
272+
const refs = Buildx.refs({
273+
dir: path.join(fixturesDir, 'buildx-refs'),
274+
builderName: 'foo'
275+
});
276+
expect(Object.keys(refs).length).toEqual(3);
277+
});
278+
it('returns default builder refs since', async () => {
279+
const mdate = new Date('2023-09-05T00:00:00Z');
280+
fs.utimesSync(path.join(fixturesDir, 'buildx-refs', 'default', 'default', '36dix0eiv9evr61vrwzn32w7q'), mdate, mdate);
281+
fs.utimesSync(path.join(fixturesDir, 'buildx-refs', 'default', 'default', '49p5r8und2konke5pmlyzqp3n'), mdate, mdate);
282+
fs.utimesSync(path.join(fixturesDir, 'buildx-refs', 'default', 'default', 'a8zqzhhv5yiazm396jobsgdw2'), mdate, mdate);
283+
const refs = Buildx.refs({
284+
dir: path.join(fixturesDir, 'buildx-refs'),
285+
builderName: 'default',
286+
since: new Date('2024-01-10T00:00:00Z')
287+
});
288+
expect(Object.keys(refs).length).toEqual(5);
289+
});
290+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Definition":"eyJncm91cCI6eyJkZWZhdWx0Ijp7InRhcmdldHMiOlsiZGV2Il19fSwidGFyZ2V0Ijp7ImRldiI6eyJjb250ZXh0IjoiLiIsImRvY2tlcmZpbGUiOiJEb2NrZXJmaWxlIiwiYXJncyI6eyJCVUlMREtJVF9DT05URVhUX0tFRVBfR0lUX0RJUiI6IjEiLCJERUZBVUxUX1BST0RVQ1RfTElDRU5TRSI6IiIsIkRPQ0tFUl9CVUlMRFRBR1MiOiIiLCJET0NLRVJfREVCVUciOiIiLCJET0NLRVJfTERGTEFHUyI6IiIsIkRPQ0tFUl9TVEFUSUMiOiIxIiwiUEFDS0FHRVJfTkFNRSI6IiIsIlBMQVRGT1JNIjoiIiwiUFJPRFVDVCI6IiIsIlNZU1RFTUQiOiJmYWxzZSIsIlZFUlNJT04iOiIifSwidGFncyI6WyJkb2NrZXItZGV2Il0sInRhcmdldCI6ImRldiIsIm91dHB1dCI6WyJ0eXBlPWRvY2tlciJdfX19","Targets":["dev"],"Refs":["vzxn0jxr44khtq7hc8drtzwjv"]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"LocalPath":"/home/crazymax/github/docker/docker-alpine-s6","DockerfilePath":""}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":""}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":"/home/crazymax/github/docker_org/buildx/Dockerfile"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":"/home/crazymax/github/docker_org/buildx/Dockerfile"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":""}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":"/home/crazymax/github/docker_org/buildx/Dockerfile"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":""}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Target":"dev","LocalPath":"/home/crazymax/github/docker_org/docker","DockerfilePath":"/home/crazymax/github/docker_org/docker/Dockerfile","GroupRef":"1fugf958r4peyg86h6scim5t5"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Target":"default","LocalPath":"/home/crazymax/github/docker_org/compose/.dev/47231","DockerfilePath":"/home/crazymax/github/docker_org/compose/.dev/47231/bad/Dockerfile"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Target":"default","LocalPath":"/home/crazymax/github/docker_org/compose/.dev/47231","DockerfilePath":"/home/crazymax/github/docker_org/compose/.dev/47231/bad/Dockerfile"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Target":"default","LocalPath":"/home/crazymax/github/docker_org/compose/.dev/47231","DockerfilePath":"/home/crazymax/github/docker_org/compose/.dev/47231/bad/Dockerfile"}

src/buildx/buildx.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import * as semver from 'semver';
2222
import {Docker} from '../docker/docker';
2323
import {Exec} from '../exec';
2424

25-
import {Cert} from '../types/buildx';
25+
import {Cert, LocalRefsOpts, LocalRefsResponse, LocalState} from '../types/buildx';
2626

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

48+
static get refsDir(): string {
49+
return path.join(Buildx.configDir, 'refs');
50+
}
51+
52+
static get refsGroupDir(): string {
53+
return path.join(Buildx.refsDir, '__group__');
54+
}
55+
4856
static get certsDir(): string {
4957
return path.join(Buildx.configDir, 'certs');
5058
}
@@ -168,4 +176,46 @@ export class Buildx {
168176
}
169177
return driverOpts;
170178
}
179+
180+
public static refs(opts: LocalRefsOpts, refs: LocalRefsResponse = {}): LocalRefsResponse {
181+
const {dir, builderName, nodeName, since} = opts;
182+
183+
let dirpath = path.resolve(dir);
184+
if (opts.builderName) {
185+
dirpath = path.join(dirpath, opts.builderName);
186+
}
187+
if (opts.nodeName) {
188+
dirpath = path.join(dirpath, opts.nodeName);
189+
}
190+
if (!fs.existsSync(dirpath)) {
191+
return refs;
192+
}
193+
194+
const files = fs.readdirSync(dirpath);
195+
for (const file of files) {
196+
const filePath = path.join(dirpath, file);
197+
const stat = fs.statSync(filePath);
198+
if (stat.isDirectory()) {
199+
const nopts: LocalRefsOpts = {...opts};
200+
if (!builderName) {
201+
if (file === '__group__') {
202+
continue;
203+
}
204+
nopts.builderName = file;
205+
} else if (!nodeName) {
206+
nopts.nodeName = file;
207+
}
208+
Buildx.refs(nopts, refs);
209+
} else {
210+
if (since && stat.mtime < since) {
211+
continue;
212+
}
213+
const localState = <LocalState>JSON.parse(fs.readFileSync(filePath, 'utf8'));
214+
const ref = `${builderName}/${nodeName}/${file}`;
215+
refs[ref] = localState;
216+
}
217+
}
218+
219+
return refs;
220+
}
171221
}

src/types/buildx.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,21 @@ export interface DownloadVersion {
2626
downloadURL: string;
2727
releasesURL: string;
2828
}
29+
30+
export interface LocalRefsOpts {
31+
dir: string;
32+
builderName?: string;
33+
nodeName?: string;
34+
since?: Date;
35+
}
36+
37+
export interface LocalRefsResponse {
38+
[ref: string]: LocalState;
39+
}
40+
41+
export interface LocalState {
42+
Target: string;
43+
LocalPath: string;
44+
DockerfilePath: string;
45+
GroupRef?: string;
46+
}

0 commit comments

Comments
 (0)