summaryrefslogtreecommitdiff
path: root/worker/fs.ts
blob: 2c02b345ddcf81075d6928fa24cb71fbb10c7ac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { Either, getStdout, IEither, LogMetricTraceable } from '@emprespresso/pengueno';
import { readFileSync } from 'node:fs';

// the container which runs the pipeline synthesizer (A) might be spawned by another container
// (B) ((which should be the one running this job)) by talking to the host's docker daemon
// (mounting /var/run/docker.sock) and executing the {@link getPipelineGenerationCommand}.
//
// so mounting {@link getSrcDirectoryForCiJob} has no meaning as it doesn't exist on the host;
// here we replace the path in (B) with the actual volume source on the host, where the src
// exists.
export async function getPathOnHost(path: string, home = '/var/lib/laminar'): Promise<IEither<Error, string>> {
    const container = await Either.fromFailable<Error, string>(() =>
        readFileSync('/etc/hostname', 'utf-8'),
    ).flatMapAsync((hostname) =>
        LogMetricTraceable.of(`docker inspect ${hostname}`.split(' '))
            .map((tCmd) => getStdout(tCmd))
            .get(),
    );
    if (container.left().present()) {
        return Either.right(path);
    }
    return container
        .flatMap((inspect) => Either.fromFailable(() => JSON.parse(inspect)))
        .mapRight(
            ([container]) =>
                container.Mounts.find(({ Destination }: { Destination: string }) => Destination === home).Source,
        )
        .mapRight((source) => path.replace(home, source));
}