#!/usr/bin/env node import { type Command, Either, LogTraceable, getRequiredEnvVars, getStdout, isObject, LogMetricTraceable, Metric, prependWith, TraceUtil, IEither, } from '@emprespresso/pengueno'; import { mkdir, readFile, rm } from 'fs/promises'; import { join } from 'path'; import { type CheckoutCiJob, type FetchCodeJob, PipelineImpl } from '@emprespresso/ci_model'; import { executeJob, executePipeline } from '@emprespresso/ci_worker'; const run = Date.now().toString(); const eitherJob = getRequiredEnvVars(['remote', 'refname', 'rev']).mapRight( (baseArgs) => { type: 'checkout_ci.js', arguments: { ...baseArgs, run, returnPath: process.cwd(), }, }, ); const afterJob = eitherJob.flatMapAsync((job) => Either.fromFailableAsync(() => rm(getWorkingDirectoryForCiJob(job), { recursive: true })), ); const ciRunMetric = Metric.fromName('checkout_ci.run'); const _logJob = LogTraceable.of(eitherJob).flatMap(TraceUtil.withTrace(`checkout_ci.${run}`)); await LogMetricTraceable.ofLogTraceable(_logJob) .flatMap(TraceUtil.withMetricTrace(ciRunMetric)) .map((tEitherJob) => tEitherJob.get().flatMapAsync((ciJob) => { const wd = getWorkingDirectoryForCiJob(ciJob); const fetchPackageJob = { type: 'fetch_code.js', arguments: { remoteUrl: ciJob.arguments.remote, checkout: ciJob.arguments.rev, path: getSrcDirectoryForCiJob(ciJob), }, }; return Either.fromFailableAsync(() => mkdir(wd, { recursive: true }) .then(() => process.chdir(wd)) .then(() => tEitherJob.move(fetchPackageJob).map(executeJob).get()) .then(() => ciJob), ); }), ) .map(async (tEitherCiJob) => { const eitherCiJob = await tEitherCiJob.get(); const repoCiFileContents = await eitherCiJob.flatMapAsync((ciJob) => Either.fromFailableAsync(() => readFile(join(getSrcDirectoryForCiJob(ciJob), CI_WORKFLOW_FILE), 'utf-8'), ), ); return repoCiFileContents .flatMap((fileText) => Either.fromFailable(() => JSON.parse(fileText))) .flatMap((json) => { return eitherCiJob.flatMap((ciJob): IEither => { if (!isCiWorkflow(json)) { const e = new Error("couldn't find any valid ci configuration (。•́︿•̀。), that's okay~"); return Either.left(e); } return Either.right({ cmd: getPipelineGenerationCommand(ciJob, json.workflow), job: ciJob, }); }); }); }) .map(async (tEitherPipelineGenerationCommand) => { const eitherJobCommand = await tEitherPipelineGenerationCommand.get(); const eitherPipeline = await eitherJobCommand.flatMapAsync((jobCommand) => tEitherPipelineGenerationCommand.move(jobCommand.cmd).map(getStdout).get(), ); return eitherPipeline .flatMap(PipelineImpl.from) .flatMap((pipeline) => eitherJobCommand.mapRight(({ job }) => ({ job, pipeline }))); }) .peek( TraceUtil.promiseify((tEitherPipeline) => tEitherPipeline .get() .mapRight((val) => val.pipeline.serialize()) .mapRight((pipeline) => `built the pipeline~ (◕ᴗ◕✿) let's make something amazing! ${pipeline}`) .mapRight((msg) => tEitherPipeline.trace.trace(msg)), ), ) .map(async (tEitherPipeline) => { const eitherPipeline = await tEitherPipeline.get(); return eitherPipeline.flatMapAsync(({ pipeline, job }) => tEitherPipeline .move(pipeline) .map((p) => executePipeline(p, { HOME: getWorkingDirectoryForCiJob(job), }), ) .get(), ); }) .map(async (tCompletePipeline) => { const completePipeline = await tCompletePipeline.get(); return completePipeline.fold( (e) => Promise.reject(e), () => afterJob, ); }) .get(); function getWorkingDirectoryForCiJob(job: CheckoutCiJob) { return `${job.arguments.returnPath}/${job.arguments.run}`; } function getSrcDirectoryForCiJob(job: CheckoutCiJob) { return `${job.arguments.returnPath}/${job.arguments.run}/src`; } const _runFlags = ('--rm --network none --cap-drop ALL' + '--security-opt no-new-privileges').split(' '); const _image = 'oci.liz.coffee/img/ci-worker:release'; function getPipelineGenerationCommand( job: CheckoutCiJob, pipelineGeneratorPath: string, image = _image, runFlags = _runFlags, ): Command { return [ 'docker', 'run', ...runFlags, ...prependWith( Object.entries(job.arguments).map(([key, val]) => `"${key}"="${val}"`), '-e', ), '-v', `${getSrcDirectoryForCiJob(job)}/${pipelineGeneratorPath}:/pipeline_generator`, image, '/pipeline_generator', ]; } export interface CiWorkflow { workflow: string; } export function isCiWorkflow(t: unknown): t is CiWorkflow { return isObject(t) && 'workflow' in t && typeof t.workflow === 'string' && !t.workflow.includes('..'); } const CI_WORKFLOW_FILE = '.ci/ci.json';