magic-nix-cache-action/src/index.ts

320 lines
8.8 KiB
TypeScript
Raw Normal View History

2024-04-12 01:21:15 +02:00
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import { spawn, exec, SpawnOptions } from "node:child_process";
import { openSync, readFileSync } from "node:fs";
import { inspect, promisify } from "node:util";
import * as http from "http";
import * as core from "@actions/core";
import { Tail } from "tail";
import got from "got";
2024-04-12 01:21:15 +02:00
import { IdsToolbox } from "detsys-ts";
2024-04-12 01:21:15 +02:00
const ENV_CACHE_DAEMONDIR = "MAGIC_NIX_CACHE_DAEMONDIR";
const gotClient = got.extend({
retry: {
2023-12-05 02:12:12 +01:00
limit: 1,
2024-04-12 01:21:15 +02:00
methods: ["POST", "GET", "PUT", "HEAD", "DELETE", "OPTIONS", "TRACE"],
},
hooks: {
beforeRetry: [
(error, retryCount) => {
core.info(`Retrying after error ${error.code}, retry #: ${retryCount}`);
2024-04-12 01:21:15 +02:00
},
],
},
});
2024-04-12 01:21:15 +02:00
async function fetchAutoCacher(toolbox: IdsToolbox): Promise<string> {
const closurePath = await toolbox.fetch();
toolbox.recordEvent("load_closure");
const { stdout } = await promisify(exec)(
`cat "${closurePath}" | xz -d | nix-store --import`,
);
const paths = stdout.split(os.EOL);
2024-01-09 18:51:52 +01:00
// Since the export is in reverse topologically sorted order, magic-nix-cache is always the penultimate entry in the list (the empty string left by split being the last).
const last_path = paths.at(-2);
return `${last_path}/bin/magic-nix-cache`;
}
2024-04-12 01:21:15 +02:00
function tailLog(daemonDir: string): Tail {
const log = new Tail(path.join(daemonDir, "daemon.log"));
2024-01-11 16:55:51 +01:00
core.debug(`tailing daemon.log...`);
2024-04-12 01:21:15 +02:00
log.on("line", (line) => {
2024-01-11 16:55:51 +01:00
core.info(line);
});
return log;
}
2024-04-12 01:21:15 +02:00
async function setUpAutoCache(toolbox: IdsToolbox): Promise<void> {
const tmpdir = process.env["RUNNER_TEMP"] || os.tmpdir();
const required_env = [
"ACTIONS_CACHE_URL",
"ACTIONS_RUNTIME_URL",
"ACTIONS_RUNTIME_TOKEN",
];
2024-04-12 01:21:15 +02:00
let anyMissing = false;
for (const n of required_env) {
if (!process.env.hasOwnProperty(n)) {
anyMissing = true;
2024-04-12 01:21:15 +02:00
core.warning(
`Disabling automatic caching since required environment ${n} isn't available`,
);
}
}
if (anyMissing) {
return;
}
2024-04-12 01:21:15 +02:00
core.debug(`GitHub Action Cache URL: ${process.env["ACTIONS_CACHE_URL"]}`);
2024-04-12 01:21:15 +02:00
const daemonDir = await fs.mkdtemp(path.join(tmpdir, "magic-nix-cache-"));
2024-04-12 01:21:15 +02:00
let daemonBin: string;
if (core.getInput("source-binary")) {
daemonBin = core.getInput("source-binary");
} else {
2024-04-12 01:21:15 +02:00
daemonBin = await fetchAutoCacher(toolbox);
}
2024-04-12 01:21:15 +02:00
let runEnv;
if (core.isDebug()) {
runEnv = {
2023-06-26 18:27:45 +02:00
RUST_LOG: "trace,magic_nix_cache=debug,gha_cache=debug",
RUST_BACKTRACE: "full",
2024-04-12 01:21:15 +02:00
...process.env,
};
} else {
runEnv = process.env;
}
2024-04-12 01:21:15 +02:00
const notifyPort = core.getInput("startup-notification-port");
const notifyPromise = new Promise<Promise<void>>((resolveListening) => {
const promise = new Promise<void>(async (resolveQuit) => {
const notifyServer = http.createServer((req, res) => {
2024-04-12 01:21:15 +02:00
if (req.method === "POST" && req.url === "/") {
core.debug(`Notify server shutting down.`);
2024-04-12 01:21:15 +02:00
res.writeHead(200, { "Content-Type": "application/json" });
res.end("{}");
notifyServer.close(() => {
resolveQuit();
});
}
});
notifyServer.listen(notifyPort, () => {
core.debug(`Notify server running.`);
resolveListening(promise);
});
});
});
2024-02-23 19:51:28 +01:00
// Start tailing the daemon log.
const outputPath = `${daemonDir}/daemon.log`;
2024-04-12 01:21:15 +02:00
const output = openSync(outputPath, "a");
2024-01-11 16:55:51 +01:00
const log = tailLog(daemonDir);
const netrc = await netrcPath();
const nixConfPath = `${process.env["HOME"]}/.config/nix/nix.conf`;
const daemonCliFlags: string[] = [
2024-04-12 01:21:15 +02:00
"--startup-notification-url",
`http://127.0.0.1:${notifyPort}`,
"--listen",
core.getInput("listen"),
"--upstream",
core.getInput("upstream-cache"),
"--diagnostic-endpoint",
core.getInput("diagnostic-endpoint"),
"--nix-conf",
nixConfPath,
]
.concat(
core.getBooleanInput("use-flakehub")
? [
"--use-flakehub",
"--flakehub-cache-server",
core.getInput("flakehub-cache-server"),
"--flakehub-api-server",
core.getInput("flakehub-api-server"),
"--flakehub-api-server-netrc",
netrc,
"--flakehub-flake-name",
core.getInput("flakehub-flake-name"),
]
: [],
)
.concat(core.getBooleanInput("use-gha-cache") ? ["--use-gha-cache"] : []);
const opts: SpawnOptions = {
2024-04-12 01:21:15 +02:00
stdio: ["ignore", output, output],
env: runEnv,
2024-04-12 01:21:15 +02:00
detached: true,
};
// Display the final command for debugging purposes
core.debug("Full daemon start command:");
core.debug(`${daemonBin} ${daemonCliFlags.join(" ")}`);
2024-02-23 19:51:28 +01:00
// Start the server. Once it is ready, it will notify us via the notification server.
const daemon = spawn(daemonBin, daemonCliFlags, opts);
2024-04-12 01:21:15 +02:00
const pidFile = path.join(daemonDir, "daemon.pid");
await fs.writeFile(pidFile, `${daemon.pid}`);
core.info("Waiting for magic-nix-cache to start...");
await new Promise<void>((resolve, reject) => {
2024-04-12 01:21:15 +02:00
notifyPromise
// eslint-disable-next-line github/no-then
.then((_value) => {
resolve();
})
// eslint-disable-next-line github/no-then
.catch((err) => {
reject(new Error(`error in notifyPromise: ${err}`));
});
daemon.on("exit", async (code, signal) => {
if (signal) {
2024-01-11 16:55:51 +01:00
reject(new Error(`Daemon was killed by signal ${signal}`));
} else if (code) {
2024-01-11 16:55:51 +01:00
reject(new Error(`Daemon exited with code ${code}`));
} else {
2024-01-11 16:55:51 +01:00
reject(new Error(`Daemon unexpectedly exited`));
}
});
});
daemon.unref();
2024-04-12 01:21:15 +02:00
core.info("Launched Magic Nix Cache");
core.exportVariable(ENV_CACHE_DAEMONDIR, daemonDir);
2024-01-11 16:55:51 +01:00
log.unwatch();
}
2024-04-12 01:21:15 +02:00
async function notifyAutoCache(): Promise<void> {
const daemonDir = process.env[ENV_CACHE_DAEMONDIR];
if (!daemonDir) {
return;
}
try {
core.debug(`Indicating workflow start`);
2024-04-12 01:21:15 +02:00
const res: Response = await gotClient
.post(`http://${core.getInput("listen")}/api/workflow-start`)
.json();
core.debug(`back from post: ${res}`);
} catch (e) {
core.info(`Error marking the workflow as started:`);
core.info(inspect(e));
core.info(`Magic Nix Cache may not be running for this workflow.`);
}
}
2024-04-12 01:21:15 +02:00
async function netrcPath(): Promise<string> {
const expectedNetrcPath = path.join(
process.env["RUNNER_TEMP"] || os.tmpdir(),
"determinate-nix-installer-netrc",
);
try {
2024-04-12 01:21:15 +02:00
await fs.access(expectedNetrcPath);
return expectedNetrcPath;
} catch {
// `nix-installer` was not used, the user may be registered with FlakeHub though.
2024-04-12 01:21:15 +02:00
const destinedNetrcPath = path.join(
process.env["RUNNER_TEMP"] || os.tmpdir(),
"magic-nix-cache-netrc",
);
try {
await flakehub_login(destinedNetrcPath);
2024-02-13 20:40:27 +01:00
} catch (e) {
core.info("FlakeHub cache disabled.");
2024-04-12 01:21:15 +02:00
core.debug(`Error while logging into FlakeHub: ${e}`);
}
return destinedNetrcPath;
}
}
2024-04-12 01:21:15 +02:00
async function flakehub_login(netrc: string): Promise<void> {
const jwt = await core.getIDToken("api.flakehub.com");
await fs.writeFile(
netrc,
[
`machine api.flakehub.com login flakehub password ${jwt}`,
`machine flakehub.com login flakehub password ${jwt}`,
2024-02-27 18:42:14 +01:00
`machine cache.flakehub.com login flakehub password ${jwt}`,
].join("\n"),
);
2024-02-13 20:40:27 +01:00
core.info("Logged in to FlakeHub.");
}
2024-04-12 01:21:15 +02:00
async function tearDownAutoCache(): Promise<void> {
const daemonDir = process.env[ENV_CACHE_DAEMONDIR];
if (!daemonDir) {
2024-04-12 01:21:15 +02:00
core.debug("magic-nix-cache not started - Skipping");
return;
}
2024-04-12 01:21:15 +02:00
const pidFile = path.join(daemonDir, "daemon.pid");
const pid = parseInt(await fs.readFile(pidFile, { encoding: "ascii" }));
core.debug(`found daemon pid: ${pid}`);
if (!pid) {
throw new Error("magic-nix-cache did not start successfully");
}
2024-01-11 16:55:51 +01:00
const log = tailLog(daemonDir);
try {
core.debug(`about to post to localhost`);
2024-04-12 01:21:15 +02:00
const res: Response = await gotClient
.post(`http://${core.getInput("listen")}/api/workflow-finish`)
.json();
core.debug(`back from post: ${res}`);
} finally {
core.debug(`unwatching the daemon log`);
log.unwatch();
}
core.debug(`killing`);
try {
2024-04-12 01:21:15 +02:00
process.kill(pid, "SIGTERM");
} catch (e) {
2024-04-12 01:21:15 +02:00
if (typeof e === "object" && e && "code" in e && e.code !== "ESRCH") {
throw e;
}
2023-12-05 02:30:23 +01:00
} finally {
if (core.isDebug()) {
core.info("Entire log:");
2024-04-12 01:21:15 +02:00
const entireLog = readFileSync(path.join(daemonDir, "daemon.log"));
core.info(entireLog.toString());
2023-12-05 02:30:23 +01:00
}
}
}
2024-04-12 01:21:15 +02:00
const idslib = new IdsToolbox({
name: "magic-nix-cache",
fetchStyle: "gh-env-style",
2024-04-12 01:25:26 +02:00
idsProjectName: "magic-nix-cache-closure",
2024-04-18 20:14:44 +02:00
requireNix: "warn",
2024-04-12 01:21:15 +02:00
});
2024-04-12 01:21:15 +02:00
idslib.onMain(async () => {
await setUpAutoCache(idslib);
await notifyAutoCache();
});
idslib.onPost(async () => {
await tearDownAutoCache();
});
2024-04-12 01:21:15 +02:00
idslib.execute();