buildx: localstate refs

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2024-04-25 09:51:14 +02:00
parent 33688c4ac8
commit e41efdd2aa
No known key found for this signature in database
GPG Key ID: ADE44D8C9D44FBE4
15 changed files with 117 additions and 1 deletions

View File

@ -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');
@ -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);
});
});

View File

@ -0,0 +1 @@
{"Definition":"eyJncm91cCI6eyJkZWZhdWx0Ijp7InRhcmdldHMiOlsiZGV2Il19fSwidGFyZ2V0Ijp7ImRldiI6eyJjb250ZXh0IjoiLiIsImRvY2tlcmZpbGUiOiJEb2NrZXJmaWxlIiwiYXJncyI6eyJCVUlMREtJVF9DT05URVhUX0tFRVBfR0lUX0RJUiI6IjEiLCJERUZBVUxUX1BST0RVQ1RfTElDRU5TRSI6IiIsIkRPQ0tFUl9CVUlMRFRBR1MiOiIiLCJET0NLRVJfREVCVUciOiIiLCJET0NLRVJfTERGTEFHUyI6IiIsIkRPQ0tFUl9TVEFUSUMiOiIxIiwiUEFDS0FHRVJfTkFNRSI6IiIsIlBMQVRGT1JNIjoiIiwiUFJPRFVDVCI6IiIsIlNZU1RFTUQiOiJmYWxzZSIsIlZFUlNJT04iOiIifSwidGFncyI6WyJkb2NrZXItZGV2Il0sInRhcmdldCI6ImRldiIsIm91dHB1dCI6WyJ0eXBlPWRvY2tlciJdfX19","Targets":["dev"],"Refs":["vzxn0jxr44khtq7hc8drtzwjv"]}

View File

@ -0,0 +1 @@
{"LocalPath":"/home/crazymax/github/docker/docker-alpine-s6","DockerfilePath":""}

View File

@ -0,0 +1 @@
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":""}

View File

@ -0,0 +1 @@
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":"/home/crazymax/github/docker_org/buildx/Dockerfile"}

View File

@ -0,0 +1 @@
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":"/home/crazymax/github/docker_org/buildx/Dockerfile"}

View File

@ -0,0 +1 @@
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":""}

View File

@ -0,0 +1 @@
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":"/home/crazymax/github/docker_org/buildx/Dockerfile"}

View File

@ -0,0 +1 @@
{"LocalPath":"/home/crazymax/github/docker_org/buildx","DockerfilePath":""}

View File

@ -0,0 +1 @@
{"Target":"dev","LocalPath":"/home/crazymax/github/docker_org/docker","DockerfilePath":"/home/crazymax/github/docker_org/docker/Dockerfile","GroupRef":"1fugf958r4peyg86h6scim5t5"}

View File

@ -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"}

View File

@ -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"}

View File

@ -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"}

View File

@ -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;
@ -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');
}
@ -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;
}
}

View File

@ -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;
}