Skip to content

Commit

Permalink
Create cache destination manually.
Browse files Browse the repository at this point in the history
We have to do it with sudo (to create dirs in root) and make sure that the resulting dirs are owned by the runner user.

In order to do that, implement `mkdir -p` manually.
  • Loading branch information
nichtverstehen committed Feb 12, 2024
1 parent a2c3f02 commit 1e6bbb0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
26 changes: 23 additions & 3 deletions dist/index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27147,6 +27147,27 @@ function resolveHome(filepath) {
}
return filepath;
}
async function sudoMkdirP(path, userColonGroup) {
const anc = ancestors(path);
for (const p of anc) {
if (external_fs_.existsSync(p))
continue;
await lib_exec.exec("sudo", ["mkdir", p]);
await lib_exec.exec("sudo", ["chown", userColonGroup, p]);
}
}
function ancestors(filepath) {
const res = [];
let norm = external_path_.normalize(filepath);
while (norm !== "." && norm !== "/") {
res.unshift(norm);
const next = external_path_.dirname(norm);
if (next === norm)
break;
norm = next;
}
return res;
}
async function getCacheUtil(cachePath) {
const { stdout } = await exec.getExecOutput(`/bin/sh -c "du -sb ${cachePath} | cut -f1"`, [], {
silent: true,
Expand Down Expand Up @@ -27245,9 +27266,8 @@ async function restoreLocalCache(cachePaths) {
}
const expandedFilePath = resolveHome(p.mountTarget);
await io.mkdirP(p.pathInCache);
// Sudo to be able to create dirs in root (e.g. /nix).
// Use `install` instead of `mkdir -p` to easily set owners.
await lib_exec.exec(`sudo install -d -o runner -g docker ${expandedFilePath}`);
// Sudo to be able to create dirs in root (e.g. /nix), but set the runner as owner.
await sudoMkdirP(expandedFilePath, "runner:docker");
await lib_exec.exec(`sudo mount --bind ${p.pathInCache} ${expandedFilePath}`);
}
return cacheMisses;
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ export async function restoreLocalCache(

const expandedFilePath = utils.resolveHome(p.mountTarget);
await io.mkdirP(p.pathInCache);
// Sudo to be able to create dirs in root (e.g. /nix).
// Use `install` instead of `mkdir -p` to easily set owners.
await exec.exec(`sudo install -d -o runner -g docker ${expandedFilePath}`);
// Sudo to be able to create dirs in root (e.g. /nix), but set the runner as owner.
await utils.sudoMkdirP(expandedFilePath, "runner:docker");
await exec.exec(`sudo mount --bind ${p.pathInCache} ${expandedFilePath}`);
}

Expand Down
21 changes: 21 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ export function resolveHome(filepath: string): string {
return filepath;
}

export async function sudoMkdirP(path: string, userColonGroup: string) {
const anc = ancestors(path);
for (const p of anc) {
if (fs.existsSync(p)) continue;
await exec.exec("sudo", ["mkdir", p]);
await exec.exec("sudo", ["chown", userColonGroup, p]);
}
}

function ancestors(filepath: string) {
const res: string[] = [];
let norm = path.normalize(filepath);
while (norm !== "." && norm !== "/") {
res.unshift(norm);
const next = path.dirname(norm);
if (next === norm) break;
norm = next;
}
return res;
}

export async function getCacheUtil(cachePath: string): Promise<number> {
const { stdout } = await exec.getExecOutput(
`/bin/sh -c "du -sb ${cachePath} | cut -f1"`,
Expand Down

0 comments on commit 1e6bbb0

Please sign in to comment.