diff options
Diffstat (limited to 'worker/fs.ts')
-rw-r--r-- | worker/fs.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/worker/fs.ts b/worker/fs.ts new file mode 100644 index 0000000..2c02b34 --- /dev/null +++ b/worker/fs.ts @@ -0,0 +1,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)); +} |