buildx: split module

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-01-31 21:13:39 +01:00
parent 388280c282
commit cc48ecede1
11 changed files with 428 additions and 309 deletions

View File

@@ -20,8 +20,10 @@ import * as exec from '@actions/exec';
import * as semver from 'semver';
import {Context} from './context';
import {Buildx} from './buildx';
import {Builder, BuilderInfo} from './builder';
import {Buildx} from './buildx/buildx';
import {Builder} from './buildx/builder';
import {BuilderInfo} from './types/builder';
export interface BuildKitOpts {
context: Context;
@@ -42,14 +44,6 @@ export class BuildKit {
});
}
private async getBuilderInfo(name: string): Promise<BuilderInfo> {
const builder = new Builder({
context: this.context,
buildx: this.buildx
});
return builder.inspect(name);
}
public async getVersion(builderName: string): Promise<string | undefined> {
const builderInfo = await this.getBuilderInfo(builderName);
if (builderInfo.nodes.length == 0) {
@@ -137,4 +131,12 @@ export class BuildKit {
fs.writeFileSync(configFile, s);
return configFile;
}
private async getBuilderInfo(name: string): Promise<BuilderInfo> {
const builder = new Builder({
context: this.context,
buildx: this.buildx
});
return builder.inspect(name);
}
}

View File

@@ -17,24 +17,9 @@
import * as exec from '@actions/exec';
import {Buildx} from './buildx';
import {Context} from './context';
import {Context} from '../context';
export interface BuilderInfo {
name?: string;
driver?: string;
lastActivity?: Date;
nodes: NodeInfo[];
}
export interface NodeInfo {
name?: string;
endpoint?: string;
driverOpts?: Array<string>;
status?: string;
buildkitdFlags?: string;
buildkitVersion?: string;
platforms?: string;
}
import {BuilderInfo, NodeInfo} from '../types/builder';
export interface BuilderOpts {
context: Context;

137
src/buildx/buildx.ts Normal file
View File

@@ -0,0 +1,137 @@
/**
* Copyright 2023 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as exec from '@actions/exec';
import * as httpm from '@actions/http-client';
import * as semver from 'semver';
import {Docker} from '../docker';
import {Context} from '../context';
import {Inputs} from './inputs';
import {GitHubRelease} from '../types/github';
export interface BuildxOpts {
context: Context;
standalone?: boolean;
}
export class Buildx {
private readonly context: Context;
private _version: string | undefined;
public readonly inputs: Inputs;
public readonly standalone: boolean;
constructor(opts: BuildxOpts) {
this.context = opts.context;
this.inputs = new Inputs(this.context);
this.standalone = opts?.standalone ?? !Docker.isAvailable();
}
public static async getRelease(version: string): Promise<GitHubRelease> {
// FIXME: Use https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-releases.json when repo public
const url = `https://raw.githubusercontent.com/docker/buildx/master/.github/releases.json`;
const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit');
const resp: httpm.HttpClientResponse = await http.get(url);
const body = await resp.readBody();
const statusCode = resp.message.statusCode || 500;
if (statusCode >= 400) {
throw new Error(`Failed to get Buildx release ${version} from ${url} with status code ${statusCode}: ${body}`);
}
const releases = <Record<string, GitHubRelease>>JSON.parse(body);
if (!releases[version]) {
throw new Error(`Cannot find Buildx release ${version} in ${url}`);
}
return releases[version];
}
public getCommand(args: Array<string>) {
return {
command: this.standalone ? 'buildx' : 'docker',
args: this.standalone ? args : ['buildx', ...args]
};
}
public async isAvailable(): Promise<boolean> {
const cmd = this.getCommand([]);
return await exec
.getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
return false;
}
return res.exitCode == 0;
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch(error => {
return false;
});
}
public async printInspect(name: string): Promise<void> {
const cmd = this.getCommand(['inspect', name]);
await exec.exec(cmd.command, cmd.args, {
failOnStdErr: false
});
}
get version() {
return (async () => {
if (!this._version) {
const cmd = this.getCommand(['version']);
this._version = await exec
.getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
return Buildx.parseVersion(res.stdout.trim());
});
}
return this._version;
})();
}
public async printVersion() {
const cmd = this.getCommand(['version']);
await exec.exec(cmd.command, cmd.args, {
failOnStdErr: false
});
}
public static parseVersion(stdout: string): string {
const matches = /\sv?([0-9a-f]{7}|[0-9.]+)/.exec(stdout);
if (!matches) {
throw new Error(`Cannot parse buildx version`);
}
return matches[1];
}
public async versionSatisfies(range: string, version?: string): Promise<boolean> {
const ver = version ?? (await this.version);
if (!ver) {
return false;
}
return semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null;
}
}

View File

@@ -17,111 +17,15 @@
import fs from 'fs';
import path from 'path';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as httpm from '@actions/http-client';
import {parse} from 'csv-parse/sync';
import * as semver from 'semver';
import {Docker} from './docker';
import {Context} from './context';
import {Context} from '../context';
export interface GitHubRelease {
id: number;
tag_name: string;
html_url: string;
assets: Array<string>;
}
export interface BuildxOpts {
context: Context;
standalone?: boolean;
}
export class Buildx {
export class Inputs {
private readonly context: Context;
private _version: string | undefined;
public standalone: boolean;
constructor(opts: BuildxOpts) {
this.context = opts.context;
this.standalone = opts?.standalone ?? !Docker.isAvailable();
}
public getCommand(args: Array<string>) {
return {
command: this.standalone ? 'buildx' : 'docker',
args: this.standalone ? args : ['buildx', ...args]
};
}
public async isAvailable(): Promise<boolean> {
const cmd = this.getCommand([]);
return await exec
.getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
return false;
}
return res.exitCode == 0;
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch(error => {
return false;
});
}
public async printInspect(name: string): Promise<void> {
const cmd = this.getCommand(['inspect', name]);
await exec.exec(cmd.command, cmd.args, {
failOnStdErr: false
});
}
get version() {
return (async () => {
if (!this._version) {
const cmd = this.getCommand(['version']);
this._version = await exec
.getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
return Buildx.parseVersion(res.stdout.trim());
});
}
return this._version;
})();
}
public async printVersion() {
const cmd = this.getCommand(['version']);
await exec.exec(cmd.command, cmd.args, {
failOnStdErr: false
});
}
public static parseVersion(stdout: string): string {
const matches = /\sv?([0-9a-f]{7}|[0-9.]+)/.exec(stdout);
if (!matches) {
throw new Error(`Cannot parse buildx version`);
}
return matches[1];
}
public async versionSatisfies(range: string, version?: string): Promise<boolean> {
const ver = version ?? (await this.version);
if (!ver) {
return false;
}
return semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null;
constructor(context: Context) {
this.context = context;
}
public getBuildImageIDFilePath(): string {
@@ -228,33 +132,16 @@ export class Buildx {
return `${input},builder-id=${this.context.provenanceBuilderID}`;
}
public static async getRelease(version: string): Promise<GitHubRelease> {
// FIXME: Use https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-releases.json when repo public
const url = `https://raw.githubusercontent.com/docker/buildx/master/.github/releases.json`;
const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit');
const resp: httpm.HttpClientResponse = await http.get(url);
const body = await resp.readBody();
const statusCode = resp.message.statusCode || 500;
if (statusCode >= 400) {
throw new Error(`Failed to get Buildx release ${version} from ${url} with status code ${statusCode}: ${body}`);
}
const releases = <Record<string, GitHubRelease>>JSON.parse(body);
if (!releases[version]) {
throw new Error(`Cannot find Buildx release ${version} in ${url}`);
}
return releases[version];
}
public static hasLocalExporter(exporters: string[]): boolean {
return Buildx.hasExporterType('local', exporters);
return Inputs.hasExporterType('local', exporters);
}
public static hasTarExporter(exporters: string[]): boolean {
return Buildx.hasExporterType('tar', exporters);
return Inputs.hasExporterType('tar', exporters);
}
public static hasDockerExporter(exporters: string[], load?: boolean): boolean {
return load ?? Buildx.hasExporterType('docker', exporters);
return load ?? Inputs.hasExporterType('docker', exporters);
}
public static hasExporterType(name: string, exporters: string[]): boolean {

View File

@@ -15,19 +15,21 @@
*/
import {Context} from './context';
import {Buildx} from './buildx';
import {Buildx} from './buildx/buildx';
import {BuildKit} from './buildkit';
import {GitHub} from './github';
export {Builder, BuilderOpts, BuilderInfo, NodeInfo} from './builder';
export {Builder, BuilderOpts} from './buildx/builder';
export {BuildKit, BuildKitOpts} from './buildkit';
export {Buildx, BuildxOpts} from './buildx';
export {Buildx, BuildxOpts} from './buildx/buildx';
export {Context} from './context';
export {Docker} from './docker';
export {Git} from './git';
export {GitHub, GitHubRepo, GitHubActionsRuntimeToken} from './github';
export {Util} from './util';
export {BuilderInfo, NodeInfo} from './types/builder';
export interface ToolkitOpts {
/**
* GitHub token to use for authentication.

32
src/types/builder.ts Normal file
View File

@@ -0,0 +1,32 @@
/**
* Copyright 2023 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface BuilderInfo {
name?: string;
driver?: string;
lastActivity?: Date;
nodes: NodeInfo[];
}
export interface NodeInfo {
name?: string;
endpoint?: string;
driverOpts?: Array<string>;
status?: string;
buildkitdFlags?: string;
buildkitVersion?: string;
platforms?: string;
}

22
src/types/github.ts Normal file
View File

@@ -0,0 +1,22 @@
/**
* Copyright 2023 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface GitHubRelease {
id: number;
tag_name: string;
html_url: string;
assets: Array<string>;
}