mirror of
https://github.com/DeterminateSystems/magic-nix-cache-action.git
synced 2024-12-26 01:12:17 +01:00
Extract out the daemonDir into a class member
This commit is contained in:
parent
6e8580ecdc
commit
4ae13c51d8
5 changed files with 110 additions and 29 deletions
87
dist/index.js
generated
vendored
87
dist/index.js
generated
vendored
|
@ -93750,7 +93750,9 @@ const got = source_create(defaults);
|
|||
|
||||
;// CONCATENATED MODULE: external "node:stream/promises"
|
||||
const external_node_stream_promises_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:stream/promises");
|
||||
;// CONCATENATED MODULE: ./node_modules/.pnpm/github.com+DeterminateSystems+detsys-ts@5abcb239472d24b114a53f70800f0e42fc30819c_ey4bwtjkop43mcem42nicbf3we/node_modules/detsys-ts/dist/index.js
|
||||
;// CONCATENATED MODULE: external "node:zlib"
|
||||
const external_node_zlib_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:zlib");
|
||||
;// CONCATENATED MODULE: ./node_modules/.pnpm/github.com+DeterminateSystems+detsys-ts@3baa96b315ee9f6b26033191a123eb9015522791_2fnsfzpup2itrpjvndt6evj3qu/node_modules/detsys-ts/dist/index.js
|
||||
var __defProp = Object.defineProperty;
|
||||
var __export = (target, all) => {
|
||||
for (var name in all)
|
||||
|
@ -94096,17 +94098,27 @@ function getNixPlatform(archOs) {
|
|||
// src/inputs.ts
|
||||
var inputs_exports = {};
|
||||
__export(inputs_exports, {
|
||||
getArrayOfStrings: () => getArrayOfStrings,
|
||||
getBool: () => getBool,
|
||||
getMultilineStringOrNull: () => getMultilineStringOrNull,
|
||||
getNumberOrNull: () => getNumberOrNull,
|
||||
getString: () => getString,
|
||||
getStringOrNull: () => getStringOrNull,
|
||||
getStringOrUndefined: () => getStringOrUndefined
|
||||
getStringOrUndefined: () => getStringOrUndefined,
|
||||
handleString: () => handleString
|
||||
});
|
||||
|
||||
var getBool = (name) => {
|
||||
return core.getBooleanInput(name);
|
||||
};
|
||||
var getArrayOfStrings = (name, separator) => {
|
||||
const original = getString(name);
|
||||
return handleString(original, separator);
|
||||
};
|
||||
var handleString = (input, separator) => {
|
||||
const sepChar = separator === "comma" ? "," : /\s+/;
|
||||
return input.trim().split(sepChar).map((s) => s.trim());
|
||||
};
|
||||
var getMultilineStringOrNull = (name) => {
|
||||
const value = core.getMultilineInput(name);
|
||||
if (value.length === 0) {
|
||||
|
@ -94186,18 +94198,24 @@ function constructSourceParameters(legacyPrefix) {
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
var DEFAULT_IDS_HOST = "https://install.determinate.systems";
|
||||
var IDS_HOST = process.env["IDS_HOST"] ?? DEFAULT_IDS_HOST;
|
||||
var EVENT_EXCEPTION = "exception";
|
||||
var EVENT_ARTIFACT_CACHE_HIT = "artifact_cache_hit";
|
||||
var EVENT_ARTIFACT_CACHE_MISS = "artifact_cache_miss";
|
||||
var EVENT_ARTIFACT_CACHE_PERSIST = "artifact_cache_persist";
|
||||
var FACT_ENDED_WITH_EXCEPTION = "ended_with_exception";
|
||||
var FACT_FINAL_EXCEPTION = "final_exception";
|
||||
var FACT_SOURCE_URL = "source_url";
|
||||
var FACT_SOURCE_URL_ETAG = "source_url_etag";
|
||||
var IdsToolbox = class {
|
||||
constructor(actionOptions) {
|
||||
this.actionOptions = makeOptionsConfident(actionOptions);
|
||||
this.hookMain = void 0;
|
||||
this.hookPost = void 0;
|
||||
this.exceptionAttachments = /* @__PURE__ */ new Map();
|
||||
this.events = [];
|
||||
this.client = got_dist_source.extend({
|
||||
retry: {
|
||||
|
@ -94276,6 +94294,10 @@ var IdsToolbox = class {
|
|||
);
|
||||
this.recordEvent(`begin_${this.executionPhase}`);
|
||||
}
|
||||
// Attach a file to the diagnostics data in error conditions.
|
||||
stapleFile(name, location) {
|
||||
this.exceptionAttachments.set(name, location);
|
||||
}
|
||||
onMain(callback) {
|
||||
this.hookMain = callback;
|
||||
}
|
||||
|
@ -94288,6 +94310,9 @@ var IdsToolbox = class {
|
|||
process.exitCode = 1;
|
||||
});
|
||||
}
|
||||
stringifyError(error2) {
|
||||
return error2 instanceof Error || typeof error2 == "string" ? error2.toString() : JSON.stringify(error2);
|
||||
}
|
||||
async executeAsync() {
|
||||
try {
|
||||
process.env.DETSYS_CORRELATION = JSON.stringify(
|
||||
|
@ -94305,14 +94330,29 @@ var IdsToolbox = class {
|
|||
this.addFact(FACT_ENDED_WITH_EXCEPTION, false);
|
||||
} catch (error2) {
|
||||
this.addFact(FACT_ENDED_WITH_EXCEPTION, true);
|
||||
const reportable = error2 instanceof Error || typeof error2 == "string" ? error2.toString() : JSON.stringify(error2);
|
||||
const reportable = this.stringifyError(error2);
|
||||
this.addFact(FACT_FINAL_EXCEPTION, reportable);
|
||||
if (this.executionPhase === "post") {
|
||||
core.warning(reportable);
|
||||
} else {
|
||||
core.setFailed(reportable);
|
||||
}
|
||||
this.recordEvent(EVENT_EXCEPTION);
|
||||
const do_deflate = (0,external_node_util_.promisify)(external_node_zlib_namespaceObject.deflate);
|
||||
const exceptionContext = /* @__PURE__ */ new Map();
|
||||
for (const [attachmentLabel, filePath] of this.exceptionAttachments) {
|
||||
let logText;
|
||||
try {
|
||||
logText = (0,external_node_fs_namespaceObject.readFileSync)(filePath);
|
||||
} catch (e) {
|
||||
logText = Buffer.from(this.stringifyError(e));
|
||||
}
|
||||
const buf = await do_deflate(logText);
|
||||
exceptionContext.set(
|
||||
`staple_${attachmentLabel}`,
|
||||
buf.toString("base64")
|
||||
);
|
||||
}
|
||||
this.recordEvent(EVENT_EXCEPTION, Object.fromEntries(exceptionContext));
|
||||
} finally {
|
||||
await this.complete();
|
||||
}
|
||||
|
@ -94354,6 +94394,7 @@ var IdsToolbox = class {
|
|||
const versionCheckup = await this.client.head(correlatedUrl);
|
||||
if (versionCheckup.headers.etag) {
|
||||
const v = versionCheckup.headers.etag;
|
||||
this.addFact(FACT_SOURCE_URL_ETAG, v);
|
||||
core.debug(
|
||||
`Checking the tool cache for ${this.getUrl()} at ${v}`
|
||||
);
|
||||
|
@ -94402,6 +94443,7 @@ var IdsToolbox = class {
|
|||
getUrl() {
|
||||
const p = this.sourceParameters;
|
||||
if (p.url) {
|
||||
this.addFact(FACT_SOURCE_URL, p.url);
|
||||
return new URL(p.url);
|
||||
}
|
||||
const fetchUrl = new URL(IDS_HOST);
|
||||
|
@ -94418,6 +94460,7 @@ var IdsToolbox = class {
|
|||
fetchUrl.pathname += `/stable`;
|
||||
}
|
||||
fetchUrl.pathname += `/${this.architectureFetchSuffix}`;
|
||||
this.addFact(FACT_SOURCE_URL, fetchUrl.toString());
|
||||
return fetchUrl;
|
||||
}
|
||||
cacheKey(version2) {
|
||||
|
@ -94465,7 +94508,7 @@ var IdsToolbox = class {
|
|||
void 0,
|
||||
true
|
||||
);
|
||||
this.recordEvent(EVENT_ARTIFACT_CACHE_HIT);
|
||||
this.recordEvent(EVENT_ARTIFACT_CACHE_PERSIST);
|
||||
} finally {
|
||||
process.env.GITHUB_WORKSPACE = process.env.GITHUB_WORKSPACE_BACKUP;
|
||||
delete process.env.GITHUB_WORKSPACE_BACKUP;
|
||||
|
@ -94624,6 +94667,8 @@ function mungeDiagnosticEndpoint(inputUrl) {
|
|||
* Copyright (c) 2018-2020 [Samuel Carreira]
|
||||
*/
|
||||
//# sourceMappingURL=index.js.map
|
||||
;// CONCATENATED MODULE: external "fs/promises"
|
||||
const external_fs_promises_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("fs/promises");
|
||||
// EXTERNAL MODULE: external "http"
|
||||
var external_http_ = __nccwpck_require__(3685);
|
||||
;// CONCATENATED MODULE: external "node:child_process"
|
||||
|
@ -94689,6 +94734,7 @@ async function flakeHubLogin(netrc) {
|
|||
|
||||
|
||||
|
||||
|
||||
var ENV_CACHE_DAEMONDIR = "MAGIC_NIX_CACHE_DAEMONDIR";
|
||||
var MagicNixCacheAction = class {
|
||||
constructor() {
|
||||
|
@ -94713,9 +94759,27 @@ var MagicNixCacheAction = class {
|
|||
]
|
||||
}
|
||||
});
|
||||
if (process.env[ENV_CACHE_DAEMONDIR]) {
|
||||
this.unsafeDaemonDir = process.env[ENV_CACHE_DAEMONDIR];
|
||||
} else {
|
||||
this.unsafeDaemonDir = this.idslib.getTemporaryName();
|
||||
core.exportVariable(ENV_CACHE_DAEMONDIR, this.unsafeDaemonDir);
|
||||
}
|
||||
}
|
||||
async getDaemonDir() {
|
||||
if (this.daemonDir === void 0) {
|
||||
await (0,external_fs_promises_namespaceObject.mkdir)(this.unsafeDaemonDir, { recursive: true });
|
||||
this.daemonDir = this.unsafeDaemonDir;
|
||||
return this.unsafeDaemonDir;
|
||||
} else {
|
||||
return this.daemonDir;
|
||||
}
|
||||
}
|
||||
async daemonDirExists() {
|
||||
const statRes = await (0,external_fs_promises_namespaceObject.stat)(this.unsafeDaemonDir);
|
||||
return statRes.isDirectory();
|
||||
}
|
||||
async setUpAutoCache() {
|
||||
const tmpdir3 = process.env["RUNNER_TEMP"] || external_node_os_.tmpdir();
|
||||
const requiredEnv = [
|
||||
"ACTIONS_CACHE_URL",
|
||||
"ACTIONS_RUNTIME_URL",
|
||||
|
@ -94736,7 +94800,6 @@ var MagicNixCacheAction = class {
|
|||
core.debug(
|
||||
`GitHub Action Cache URL: ${process.env["ACTIONS_CACHE_URL"]}`
|
||||
);
|
||||
const daemonDir = await promises_namespaceObject.mkdtemp(external_node_path_namespaceObject.join(tmpdir3, "magic-nix-cache-"));
|
||||
const sourceBinary = inputs_exports.getStringOrNull("source-binary");
|
||||
const daemonBin = sourceBinary !== null ? sourceBinary : await this.fetchAutoCacher();
|
||||
let runEnv;
|
||||
|
@ -94768,6 +94831,7 @@ var MagicNixCacheAction = class {
|
|||
});
|
||||
});
|
||||
});
|
||||
const daemonDir = await this.getDaemonDir();
|
||||
const outputPath = `${daemonDir}/daemon.log`;
|
||||
const output = (0,external_node_fs_namespaceObject.openSync)(outputPath, "a");
|
||||
const log = tailLog(daemonDir);
|
||||
|
@ -94834,7 +94898,6 @@ var MagicNixCacheAction = class {
|
|||
});
|
||||
daemon.unref();
|
||||
core.info("Launched Magic Nix Cache");
|
||||
core.exportVariable(ENV_CACHE_DAEMONDIR, daemonDir);
|
||||
log.unwatch();
|
||||
}
|
||||
async fetchAutoCacher() {
|
||||
|
@ -94848,10 +94911,6 @@ var MagicNixCacheAction = class {
|
|||
return `${lastPath}/bin/magic-nix-cache`;
|
||||
}
|
||||
async notifyAutoCache() {
|
||||
const daemonDir = process.env[ENV_CACHE_DAEMONDIR];
|
||||
if (!daemonDir) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
core.debug(`Indicating workflow start`);
|
||||
const hostAndPort = inputs_exports.getString("listen");
|
||||
|
@ -94864,11 +94923,11 @@ var MagicNixCacheAction = class {
|
|||
}
|
||||
}
|
||||
async tearDownAutoCache() {
|
||||
const daemonDir = process.env[ENV_CACHE_DAEMONDIR];
|
||||
if (!daemonDir) {
|
||||
if (await this.daemonDirExists()) {
|
||||
core.debug("magic-nix-cache not started - Skipping");
|
||||
return;
|
||||
}
|
||||
const daemonDir = await this.getDaemonDir();
|
||||
const pidFile = external_node_path_namespaceObject.join(daemonDir, "daemon.pid");
|
||||
const pid = parseInt(await promises_namespaceObject.readFile(pidFile, { encoding: "ascii" }));
|
||||
core.debug(`found daemon pid: ${pid}`);
|
||||
|
|
2
dist/index.js.map
generated
vendored
2
dist/index.js.map
generated
vendored
File diff suppressed because one or more lines are too long
|
@ -27,7 +27,7 @@
|
|||
"dependencies": {
|
||||
"@actions/core": "^1.10.1",
|
||||
"@actions/exec": "^1.1.1",
|
||||
"detsys-ts": "github:DeterminateSystems/detsys-ts",
|
||||
"detsys-ts": "github:DeterminateSystems/detsys-ts#error-condition",
|
||||
"got": "^14.2.1",
|
||||
"tail": "^2.2.6"
|
||||
},
|
||||
|
|
|
@ -12,8 +12,8 @@ dependencies:
|
|||
specifier: ^1.1.1
|
||||
version: 1.1.1
|
||||
detsys-ts:
|
||||
specifier: github:DeterminateSystems/detsys-ts
|
||||
version: github.com/DeterminateSystems/detsys-ts/d872d42fb693faad3027a08c78620639f23168e1
|
||||
specifier: github:DeterminateSystems/detsys-ts#error-condition
|
||||
version: github.com/DeterminateSystems/detsys-ts/3baa96b315ee9f6b26033191a123eb9015522791
|
||||
got:
|
||||
specifier: ^14.2.1
|
||||
version: 14.2.1
|
||||
|
@ -3831,8 +3831,8 @@ packages:
|
|||
engines: {node: '>=10'}
|
||||
dev: true
|
||||
|
||||
github.com/DeterminateSystems/detsys-ts/d872d42fb693faad3027a08c78620639f23168e1:
|
||||
resolution: {tarball: https://codeload.github.com/DeterminateSystems/detsys-ts/tar.gz/d872d42fb693faad3027a08c78620639f23168e1}
|
||||
github.com/DeterminateSystems/detsys-ts/3baa96b315ee9f6b26033191a123eb9015522791:
|
||||
resolution: {tarball: https://codeload.github.com/DeterminateSystems/detsys-ts/tar.gz/3baa96b315ee9f6b26033191a123eb9015522791}
|
||||
name: detsys-ts
|
||||
version: 1.0.0
|
||||
dependencies:
|
||||
|
|
40
src/index.ts
40
src/index.ts
|
@ -1,6 +1,7 @@
|
|||
import { netrcPath, tailLog } from "./helpers.js";
|
||||
import * as actionsCore from "@actions/core";
|
||||
import { IdsToolbox, inputs } from "detsys-ts";
|
||||
import { mkdir, stat } from "fs/promises";
|
||||
import got, { Got } from "got";
|
||||
import * as http from "http";
|
||||
import { SpawnOptions, exec, spawn } from "node:child_process";
|
||||
|
@ -16,6 +17,9 @@ class MagicNixCacheAction {
|
|||
idslib: IdsToolbox;
|
||||
private client: Got;
|
||||
|
||||
private daemonDir?: string;
|
||||
private unsafeDaemonDir: string;
|
||||
|
||||
constructor() {
|
||||
this.idslib = new IdsToolbox({
|
||||
name: "magic-nix-cache",
|
||||
|
@ -39,10 +43,31 @@ class MagicNixCacheAction {
|
|||
],
|
||||
},
|
||||
});
|
||||
|
||||
if (process.env[ENV_CACHE_DAEMONDIR]) {
|
||||
this.unsafeDaemonDir = process.env[ENV_CACHE_DAEMONDIR];
|
||||
} else {
|
||||
this.unsafeDaemonDir = this.idslib.getTemporaryName();
|
||||
actionsCore.exportVariable(ENV_CACHE_DAEMONDIR, this.unsafeDaemonDir);
|
||||
}
|
||||
}
|
||||
|
||||
async getDaemonDir(): Promise<string> {
|
||||
if (this.daemonDir === undefined) {
|
||||
await mkdir(this.unsafeDaemonDir, { recursive: true });
|
||||
this.daemonDir = this.unsafeDaemonDir;
|
||||
return this.unsafeDaemonDir;
|
||||
} else {
|
||||
return this.daemonDir;
|
||||
}
|
||||
}
|
||||
|
||||
async daemonDirExists(): Promise<boolean> {
|
||||
const statRes = await stat(this.unsafeDaemonDir);
|
||||
return statRes.isDirectory();
|
||||
}
|
||||
|
||||
async setUpAutoCache(): Promise<void> {
|
||||
const tmpdir = process.env["RUNNER_TEMP"] || os.tmpdir();
|
||||
const requiredEnv = [
|
||||
"ACTIONS_CACHE_URL",
|
||||
"ACTIONS_RUNTIME_URL",
|
||||
|
@ -67,7 +92,6 @@ class MagicNixCacheAction {
|
|||
`GitHub Action Cache URL: ${process.env["ACTIONS_CACHE_URL"]}`,
|
||||
);
|
||||
|
||||
const daemonDir = await fs.mkdtemp(path.join(tmpdir, "magic-nix-cache-"));
|
||||
const sourceBinary = inputs.getStringOrNull("source-binary");
|
||||
const daemonBin =
|
||||
sourceBinary !== null ? sourceBinary : await this.fetchAutoCacher();
|
||||
|
@ -106,6 +130,7 @@ class MagicNixCacheAction {
|
|||
});
|
||||
|
||||
// Start tailing the daemon log.
|
||||
const daemonDir = await this.getDaemonDir();
|
||||
const outputPath = `${daemonDir}/daemon.log`;
|
||||
const output = openSync(outputPath, "a");
|
||||
const log = tailLog(daemonDir);
|
||||
|
@ -192,7 +217,6 @@ class MagicNixCacheAction {
|
|||
daemon.unref();
|
||||
|
||||
actionsCore.info("Launched Magic Nix Cache");
|
||||
actionsCore.exportVariable(ENV_CACHE_DAEMONDIR, daemonDir);
|
||||
|
||||
log.unwatch();
|
||||
}
|
||||
|
@ -211,9 +235,8 @@ class MagicNixCacheAction {
|
|||
}
|
||||
|
||||
async notifyAutoCache(): Promise<void> {
|
||||
const daemonDir = process.env[ENV_CACHE_DAEMONDIR];
|
||||
|
||||
if (!daemonDir) {
|
||||
if (!await this.daemonDirExists()) {
|
||||
actionsCore.debug("magic-nix-cache not started - Skipping");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -232,12 +255,11 @@ class MagicNixCacheAction {
|
|||
}
|
||||
|
||||
async tearDownAutoCache(): Promise<void> {
|
||||
const daemonDir = process.env[ENV_CACHE_DAEMONDIR];
|
||||
|
||||
if (!daemonDir) {
|
||||
if (!await this.daemonDirExists()) {
|
||||
actionsCore.debug("magic-nix-cache not started - Skipping");
|
||||
return;
|
||||
}
|
||||
const daemonDir = await this.getDaemonDir();
|
||||
|
||||
const pidFile = path.join(daemonDir, "daemon.pid");
|
||||
const pid = parseInt(await fs.readFile(pidFile, { encoding: "ascii" }));
|
||||
|
|
Loading…
Reference in a new issue