Skip to content

Commit

Permalink
Changes [build image]
Browse files Browse the repository at this point in the history
  • Loading branch information
Electroid committed Nov 13, 2024
1 parent 6ed8300 commit c986967
Showing 1 changed file with 38 additions and 48 deletions.
86 changes: 38 additions & 48 deletions scripts/machine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { inspect, parseArgs } from "node:util";
import {
$,
getArch,
getBuildNumber,
getSecret,
isCI,
isMacOS,
readFile,
spawn,
spawnSafe,
spawnSyncSafe,
startGroup,
tmpdir,
waitForPort,
which,
Expand Down Expand Up @@ -115,11 +117,7 @@ export const aws = {
async spawn(args) {
const aws = which("aws");
if (!aws) {
if (isMacOS) {
await spawnSafe(["brew", "install", "awscli"]);
} else {
throw new Error("AWS CLI is not installed, please install it");
}
throw new Error("AWS CLI is not installed, please install it");
}

let env;
Expand Down Expand Up @@ -471,6 +469,7 @@ export const aws = {
["name"]: name || `${InstanceId}-snapshot-${Date.now()}`,
});
await aws.waitImage("image-available", imageId);
return imageId;
};

const terminate = async () => {
Expand Down Expand Up @@ -926,7 +925,7 @@ async function spawnScp(options) {
* @property {(command: string[]) => Promise<SpawnResult>} exec
* @property {(source: string, destination: string) => Promise<void>} upload
* @property {() => Promise<void>} attach
* @property {() => Promise<void>} snapshot
* @property {() => Promise<string>} snapshot
* @property {() => Promise<void>} close
*/

Expand Down Expand Up @@ -1003,56 +1002,47 @@ async function main() {
};
}

const machine = await cloud.createMachine({ ...options, sshKeys, metadata });
console.log("Created machine:", machine);
const machine = await startGroup("Creating machine", async () => {
const result = await cloud.createMachine({ ...options, sshKeys, metadata });
console.log("Created machine:", result);
return result;
});

process.on("SIGINT", () => {
machine.close().finally(() => process.exit(1));
});

const doTest = async () => {
await machine.exec(["uname", "-a"], { stdio: "inherit" });
};

const doBootstrap = async (ci = true) => {
const localPath = resolve(import.meta.dirname, "bootstrap.sh");
const remotePath = "/tmp/bootstrap.sh";
await machine.upload(localPath, remotePath);
await machine.exec(["sh", remotePath, ci ? "--ci" : "--no-ci"], { stdio: "inherit" });
};
try {
await startGroup("Connecting to machine", async () => {
await machine.exec(["uname", "-a"], { stdio: "inherit" });
});

const doAgent = async (action = "install") => {
const templatePath = resolve(import.meta.dirname, "agent.mjs");
const tmpPath = mkdtempSync(join(tmpdir(), "agent-"));
const localPath = join(tmpPath, "agent.mjs");
const remotePath = "/tmp/agent.mjs";
const npx = which("bunx") || which("npx");
await spawnSafe($`${npx} esbuild ${templatePath} --bundle --platform=node --format=esm --outfile=${localPath}`);
await machine.upload(localPath, remotePath);
await machine.exec(["node", remotePath, action], { stdio: "inherit" });
};
await startGroup("Running bootstrap script", async () => {
const localPath = resolve(import.meta.dirname, "bootstrap.sh");
const remotePath = "/tmp/bootstrap.sh";
await machine.upload(localPath, remotePath);
await machine.exec(["sh", remotePath, "--ci"], { stdio: "inherit" });
});

const doSnapshot = async () => {
const { os, arch, distro, release } = options;
const name = `${os}-${arch}-${distro}-${release}`;
await machine.snapshot(name);
};
await startGroup("Installing buildkite-agent", async () => {
const templatePath = resolve(import.meta.dirname, "agent.mjs");
const tmpPath = mkdtempSync(join(tmpdir(), "agent-"));
const localPath = join(tmpPath, "agent.mjs");
const remotePath = "/tmp/agent.mjs";
const npx = which("bunx") || which("npx");
await spawnSafe($`${npx} esbuild ${templatePath} --bundle --platform=node --format=esm --outfile=${localPath}`);
await machine.upload(localPath, remotePath);
await machine.exec(["node", remotePath, "install"], { stdio: "inherit" });
});

try {
await doTest();
await doBootstrap();
await doAgent();
} catch (error) {
if (isCI) {
throw error;
} else {
console.error(error);
try {
await machine.attach();
} catch (error) {
console.error(error);
}
}
const imageId = await startGroup("Creating snapshot", async () => {
const { os, arch, distro, release } = options;
const suffix = isCI ? `build-${getBuildNumber()}` : `draft-${Date.now()}`;
const name = `${os}-${arch}-${distro}-${release}-${suffix}`;
const result = await machine.snapshot(name);
console.log("Created snapshot:", result);
return result;
});
} finally {
await machine.close();
}
Expand Down

0 comments on commit c986967

Please sign in to comment.