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

backend: use device returned in ExposeVolume response #61

Merged
merged 1 commit into from
Dec 7, 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
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import * as TOML from '@iarna/toml';

const buildxVersion = 'v0.17.0';
const mountPoint = '/var/lib/buildkit';
const device = '/dev/vdb';
const execAsync = promisify(exec);

// Returns a client for the sticky disk manager on the agent on this host
Expand Down Expand Up @@ -210,7 +209,7 @@ async function getWithRetry(client: AxiosInstance, url: string, formData: FormDa
throw new Error('Max retries reached');
}

async function getStickyDisk(retryCondition: (error: AxiosError) => boolean, options?: {signal?: AbortSignal}): Promise<{expose_id: string}> {
async function getStickyDisk(retryCondition: (error: AxiosError) => boolean, options?: {signal?: AbortSignal}): Promise<{expose_id: string; device: string}> {
const client = await getBlacksmithAgentClient();
const formData = new FormData();
// TODO(adityamaru): Support a stickydisk-per-build flag that will namespace the stickydisks by Dockerfile.
Expand All @@ -230,10 +229,10 @@ async function getStickyDisk(retryCondition: (error: AxiosError) => boolean, opt
}
const response = await getWithRetry(client, '/stickydisks', formData, retryCondition, options);
// For backward compatibility, if expose_id is set, return it
if (response.data?.expose_id) {
return {expose_id: response.data.expose_id};
if (response.data?.expose_id && response.data?.disk_identifier) {
return {expose_id: response.data.expose_id, device: response.data.disk_identifier};
}
return {expose_id: ''};
return {expose_id: '', device: ''};
}

async function getDiskSize(device: string): Promise<number> {
Expand All @@ -250,7 +249,7 @@ async function getDiskSize(device: string): Promise<number> {
}
}

async function writeBuildkitdTomlFile(parallelism: number): Promise<void> {
async function writeBuildkitdTomlFile(parallelism: number, device: string): Promise<void> {
const diskSize = await getDiskSize(device);
const jsonConfig: TOML.JsonMap = {
root: '/var/lib/buildkit',
Expand Down Expand Up @@ -303,9 +302,9 @@ async function writeBuildkitdTomlFile(parallelism: number): Promise<void> {
}
}

async function startBuildkitd(parallelism: number): Promise<string> {
async function startBuildkitd(parallelism: number, device: string): Promise<string> {
adityamaru marked this conversation as resolved.
Show resolved Hide resolved
try {
await writeBuildkitdTomlFile(parallelism);
await writeBuildkitdTomlFile(parallelism, device);
await execAsync('sudo mkdir -p /run/buildkit');
await execAsync('sudo chmod 755 /run/buildkit');
const addr = 'unix:///run/buildkit/buildkitd.sock';
Expand Down Expand Up @@ -435,9 +434,14 @@ async function getBuilderAddr(inputs: context.Inputs, dockerfilePath: string): P

let buildResponse: {docker_build_id: string} | null = null;
let exposeId: string = '';
let device: string = '';
try {
const stickyDiskResponse = await getStickyDisk(retryCondition, {signal: controller.signal});
exposeId = stickyDiskResponse.expose_id;
device = stickyDiskResponse.device;
if (device === '') {
throw new Error('No device returned from ExportVolume request');
}
clearTimeout(timeoutId);
await maybeFormatBlockDevice(device);
buildResponse = await reportBuild(dockerfilePath);
Expand All @@ -455,7 +459,7 @@ async function getBuilderAddr(inputs: context.Inputs, dockerfilePath: string): P

// Start buildkitd.
const parallelism = await getNumCPUs();
const buildkitdAddr = await startBuildkitd(parallelism);
const buildkitdAddr = await startBuildkitd(parallelism, device);
core.debug(`buildkitd daemon started at addr ${buildkitdAddr}`);
// Change permissions on the buildkitd socket to allow non-root access
const startTime = Date.now();
Expand Down Expand Up @@ -761,7 +765,7 @@ actionsToolkit.run(
for (let attempt = 1; attempt <= 3; attempt++) {
try {
await execAsync(`sudo umount ${mountPoint}`);
core.debug(`${device} has been unmounted`);
core.debug(`${mountPoint} has been unmounted`);
break;
} catch (error) {
if (attempt === 3) {
Expand Down
Loading