Skip to content

Commit

Permalink
Merge pull request #87 from useblacksmith/vm-tailnet
Browse files Browse the repository at this point in the history
src: join and leave tailnet on start and cleanup of builder
  • Loading branch information
adityamaru authored Dec 31, 2024
2 parents 8b07a60 + aa6b213 commit 8e71971
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
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.

1 change: 0 additions & 1 deletion src/__tests__/blacksmith-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ describe('startBlacksmithBuilder', () => {
exposeId: mockExposeId
});
expect(setupBuilder.startAndConfigureBuildkitd).toHaveBeenCalledWith(mockParallelism);
expect(core.warning).not.toHaveBeenCalled();
expect(reporter.reportBuildPushActionFailure).not.toHaveBeenCalled();
});

Expand Down
35 changes: 34 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,36 @@ import {promisify} from 'util';
import {exec} from 'child_process';
import * as reporter from './reporter';
import {setupStickyDisk, startAndConfigureBuildkitd, getNumCPUs} from './setup_builder';
import { Metric, Metric_MetricType } from "@buf/blacksmith_vm-agent.bufbuild_es/stickydisk/v1/stickydisk_pb";
import { Metric_MetricType } from "@buf/blacksmith_vm-agent.bufbuild_es/stickydisk/v1/stickydisk_pb";

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

async function joinTailnet(): Promise<void> {
const token = process.env.BLACKSMITH_TAILSCALE_TOKEN;
if (!token) {
core.warning('BLACKSMITH_TAILSCALE_TOKEN environment variable not set, skipping tailnet join');
return;
}

try {
await execAsync(`sudo tailscale up --authkey=${token} --hostname=${process.env.VM_ID}`);

core.info('Successfully joined tailnet');
} catch (error) {
throw new Error(`Failed to join tailnet: ${error.message}`);
}
}

async function leaveTailnet(): Promise<void> {
try {
await execAsync('sudo tailscale down');
} catch (error) {
core.warning(`Error leaving tailnet: ${error.message}`);
}
}

async function setupBuildx(version: string, toolkit: Toolkit): Promise<void> {
let toolPath;
const standalone = await toolkit.buildx.isStandalone();
Expand Down Expand Up @@ -70,6 +94,8 @@ async function setupBuildx(version: string, toolkit: Toolkit): Promise<void> {
*/
export async function startBlacksmithBuilder(inputs: context.Inputs): Promise<{addr: string | null; buildId: string | null; exposeId: string}> {
try {
await joinTailnet();

const dockerfilePath = context.getDockerfilePath(inputs);
if (!dockerfilePath) {
throw new Error('Failed to resolve dockerfile path');
Expand Down Expand Up @@ -323,6 +349,9 @@ actionsToolkit.run(
refs: ref ? [ref] : []
});
}

await leaveTailnet();

try {
const {stdout} = await execAsync('pgrep buildkitd');
if (stdout.trim()) {
Expand Down Expand Up @@ -403,6 +432,10 @@ actionsToolkit.run(
fs.rmSync(stateHelper.tmpDir, {recursive: true});
});
}

// Ensure we've left the tailnet.
await leaveTailnet();

// Check for any lingering buildkitd processes and try to clean up mounts
try {
// Check for buildkitd processes first
Expand Down

0 comments on commit 8e71971

Please sign in to comment.