Compare commits
5 Commits
v0.1.0-bet
...
v0.1.0-bet
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
abe37ab72b | ||
|
|
b3a30a9e32 | ||
|
|
31e915c017 | ||
|
|
05bbe49fe1 | ||
|
|
c80fedbbae |
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
|
||||
import {beforeEach, describe, expect, it, jest, test} from '@jest/globals';
|
||||
import * as semver from 'semver';
|
||||
|
||||
import {BuildKit} from '../../src/buildkit/buildkit';
|
||||
import {Builder} from '../../src/buildx/builder';
|
||||
@@ -48,11 +47,15 @@ jest.spyOn(Builder.prototype, 'inspect').mockImplementation(async (): Promise<Bu
|
||||
|
||||
describe('getVersion', () => {
|
||||
it('valid', async () => {
|
||||
const builder = new Builder({
|
||||
context: new Context()
|
||||
});
|
||||
const builderInfo = await builder.inspect('builder2');
|
||||
const buildkit = new BuildKit({
|
||||
context: new Context()
|
||||
});
|
||||
const version = await buildkit.getVersion('builder2');
|
||||
expect(semver.valid(version)).not.toBeNull();
|
||||
const version = await buildkit.getVersion(builderInfo.nodes[0]);
|
||||
expect(version).toBe('v0.11.0');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -48,9 +48,9 @@ describe('configDir', () => {
|
||||
});
|
||||
|
||||
describe('isAvailable', () => {
|
||||
it('cli', () => {
|
||||
it('cli', async () => {
|
||||
const execSpy = jest.spyOn(exec, 'getExecOutput');
|
||||
Docker.isAvailable;
|
||||
Docker.getInstance().available;
|
||||
// eslint-disable-next-line jest/no-standalone-expect
|
||||
expect(execSpy).toHaveBeenCalledWith(`docker`, undefined, {
|
||||
silent: true,
|
||||
|
||||
@@ -23,7 +23,7 @@ import {Buildx} from '../buildx/buildx';
|
||||
import {Builder} from '../buildx/builder';
|
||||
import {Config} from './config';
|
||||
|
||||
import {BuilderInfo} from '../types/builder';
|
||||
import {BuilderInfo, NodeInfo} from '../types/builder';
|
||||
|
||||
export interface BuildKitOpts {
|
||||
context: Context;
|
||||
@@ -46,14 +46,7 @@ export class BuildKit {
|
||||
});
|
||||
}
|
||||
|
||||
public async getVersion(builderName: string): Promise<string | undefined> {
|
||||
const builderInfo = await this.getBuilderInfo(builderName);
|
||||
if (builderInfo.nodes.length == 0) {
|
||||
// a builder always have on node, should not happen.
|
||||
return undefined;
|
||||
}
|
||||
// TODO: get version for all nodes
|
||||
const node = builderInfo.nodes[0];
|
||||
public async getVersion(node: NodeInfo): Promise<string | undefined> {
|
||||
if (!node.buildkitVersion && node.name) {
|
||||
try {
|
||||
return await this.getVersionWithinImage(node.name);
|
||||
@@ -92,8 +85,13 @@ export class BuildKit {
|
||||
});
|
||||
}
|
||||
|
||||
public async versionSatisfies(builderName: string, range: string): Promise<boolean> {
|
||||
const builderInfo = await this.getBuilderInfo(builderName);
|
||||
public async versionSatisfies(builderName: string, range: string, builderInfo?: BuilderInfo): Promise<boolean> {
|
||||
if (!builderInfo) {
|
||||
builderInfo = await new Builder({
|
||||
context: this.context,
|
||||
buildx: this.buildx
|
||||
}).inspect(builderName);
|
||||
}
|
||||
for (const node of builderInfo.nodes) {
|
||||
let bkversion = node.buildkitVersion;
|
||||
if (!bkversion) {
|
||||
@@ -113,12 +111,4 @@ export class BuildKit {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private async getBuilderInfo(name: string): Promise<BuilderInfo> {
|
||||
const builder = new Builder({
|
||||
context: this.context,
|
||||
buildx: this.buildx
|
||||
});
|
||||
return builder.inspect(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +41,8 @@ export class Buildx {
|
||||
|
||||
constructor(opts: BuildxOpts) {
|
||||
this.context = opts.context;
|
||||
this.standalone = opts?.standalone ?? !Docker.getInstance().available;
|
||||
this.inputs = new Inputs(this.context);
|
||||
this.standalone = opts?.standalone ?? !Docker.isAvailable;
|
||||
}
|
||||
|
||||
static get configDir(): string {
|
||||
|
||||
@@ -42,7 +42,7 @@ export class Install {
|
||||
|
||||
constructor(opts?: InstallOpts) {
|
||||
this.context = opts?.context || new Context();
|
||||
this.standalone = opts?.standalone ?? !Docker.isAvailable;
|
||||
this.standalone = opts?.standalone ?? !Docker.getInstance().available;
|
||||
}
|
||||
|
||||
public async download(version: string, dest?: string): Promise<string> {
|
||||
|
||||
@@ -20,36 +20,47 @@ import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
export class Docker {
|
||||
private static instance?: Docker;
|
||||
static getInstance = (): Docker => (Docker.instance = Docker.instance ?? new Docker());
|
||||
|
||||
private _available: boolean | undefined;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
private constructor() {}
|
||||
|
||||
static get configDir(): string {
|
||||
return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
|
||||
}
|
||||
|
||||
static get isAvailable(): boolean {
|
||||
let dockerAvailable = false;
|
||||
exec
|
||||
.getExecOutput('docker', undefined, {
|
||||
ignoreReturnCode: true,
|
||||
silent: true
|
||||
})
|
||||
.then(res => {
|
||||
if (res.stderr.length > 0 && res.exitCode != 0) {
|
||||
core.debug(`Docker.isAvailable error: ${res.stderr}`);
|
||||
dockerAvailable = false;
|
||||
} else {
|
||||
core.debug(`Docker.isAvailable ok`);
|
||||
dockerAvailable = res.exitCode == 0;
|
||||
}
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
.catch(error => {
|
||||
core.debug(`Docker.isAvailable failed: ${error}`);
|
||||
dockerAvailable = false;
|
||||
});
|
||||
return dockerAvailable;
|
||||
get available() {
|
||||
return (async () => {
|
||||
if (!this._available) {
|
||||
this._available = await exec
|
||||
.getExecOutput('docker', undefined, {
|
||||
ignoreReturnCode: true,
|
||||
silent: true
|
||||
})
|
||||
.then(res => {
|
||||
if (res.stderr.length > 0 && res.exitCode != 0) {
|
||||
core.debug(`Docker.isAvailable error: ${res.stderr}`);
|
||||
return false;
|
||||
} else {
|
||||
core.debug(`Docker.isAvailable ok`);
|
||||
return res.exitCode == 0;
|
||||
}
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
.catch(error => {
|
||||
core.debug(`Docker.isAvailable failed: ${error}`);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
return this._available;
|
||||
})();
|
||||
}
|
||||
|
||||
public static async printVersion(standalone?: boolean): Promise<void> {
|
||||
const noDocker = standalone ?? !Docker.isAvailable;
|
||||
const noDocker = standalone ?? !Docker.getInstance().available;
|
||||
if (noDocker) {
|
||||
core.debug('Docker.printVersion: Docker is not available, skipping.');
|
||||
return;
|
||||
@@ -60,7 +71,7 @@ export class Docker {
|
||||
}
|
||||
|
||||
public static async printInfo(standalone?: boolean): Promise<void> {
|
||||
const noDocker = standalone ?? !Docker.isAvailable;
|
||||
const noDocker = standalone ?? !Docker.getInstance().available;
|
||||
if (noDocker) {
|
||||
core.debug('Docker.printInfo: Docker is not available, skipping.');
|
||||
return;
|
||||
|
||||
@@ -20,6 +20,7 @@ import {Install} from './buildx/install';
|
||||
import {Builder} from './buildx/builder';
|
||||
import {BuildKit} from './buildkit/buildkit';
|
||||
import {GitHub} from './github';
|
||||
import {Docker} from './docker';
|
||||
|
||||
export interface ToolkitOpts {
|
||||
/**
|
||||
@@ -32,6 +33,7 @@ export interface ToolkitOpts {
|
||||
export class Toolkit {
|
||||
public context: Context;
|
||||
public github: GitHub;
|
||||
public docker: Docker;
|
||||
public buildx: Buildx;
|
||||
public buildxInstall: Install;
|
||||
public builder: Builder;
|
||||
@@ -40,7 +42,8 @@ export class Toolkit {
|
||||
constructor(opts: ToolkitOpts = {}) {
|
||||
this.context = new Context();
|
||||
this.github = new GitHub({token: opts.githubToken});
|
||||
this.buildx = new Buildx({context: this.context});
|
||||
this.docker = Docker.getInstance();
|
||||
this.buildx = new Buildx({context: this.context, standalone: !this.docker.available});
|
||||
this.buildxInstall = new Install({context: this.context, standalone: this.buildx.standalone});
|
||||
this.builder = new Builder({context: this.context, buildx: this.buildx});
|
||||
this.buildkit = new BuildKit({context: this.context, buildx: this.buildx});
|
||||
|
||||
Reference in New Issue
Block a user