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> { const container = await Either.fromFailable(() => 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)); }