summaryrefslogtreecommitdiff
path: root/worker/scripts/checkout_ci.ts
blob: 6a1dcad850d2013acea345ec98d25f0b1226fd98 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env node

import {
    type Command,
    Either,
    LogTraceable,
    getRequiredEnvVars,
    isObject,
    LogMetricTraceable,
    Metric,
    prependWith,
    TraceUtil,
    getStdoutMany,
} 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';

interface CiWorkflow {
    workflow: string;
}
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';
const OCI_REGISTRY = 'oci.liz.coffee';
const PIPELINE_IMAGE = OCI_REGISTRY + '/emprespresso/ci_worker:release';
const READONLY_CREDENTIALS = { username: 'readonly', password: 'readonly' };

// use a different directory per job run
// even though the Laminar run is unique per job, there's potential for "children" we spawn
// to be a checkout_ci job as well. in which case, we don't want any conflicts with whatever
// the "parent" was doing, so we create a unique directory for each.
// i.e.
// Laminar Run: 57, CWD=/var/lib/laminar/run/57
//   ci_pipeline (1000.uuidA)
//     -> checkout_ci
//       -> [...children]
//          -> checkout_ci (1000.uuidB)
const run = `${Date.now()}.${crypto.randomUUID().replaceAll('-', '')}`;

const eitherJob = getRequiredEnvVars(['remote', 'refname', 'rev', 'executorLaminarPath']).mapRight(
    (baseArgs) =>
        <CheckoutCiJob>{
            type: 'checkout_ci.js',
            arguments: {
                ...baseArgs,
                run,
                returnPath: process.cwd(),
            },
        },
);
const afterJob = eitherJob.flatMapAsync((job) =>
    Either.fromFailableAsync(() => rm(getWorkingDirectoryForCiJob(job), { recursive: true })),
);

const logTraceableJob = LogTraceable.of(eitherJob).flatMap(TraceUtil.withTrace(`checkout_ci.run.${run}`));
const ciRunMetric = Metric.fromName('checkout_ci.run');
await LogMetricTraceable.ofLogTraceable(logTraceableJob)
    .flatMap(TraceUtil.withMetricTrace(ciRunMetric))
    .map((tEitherJob) =>
        tEitherJob.get().flatMapAsync((ciJob) => {
            const wd = getWorkingDirectoryForCiJob(ciJob);
            const fetchPackageJob = <FetchCodeJob>{
                type: 'fetch_code',
                arguments: {
                    remoteUrl: ciJob.arguments.remote,
                    checkout: ciJob.arguments.rev,
                    path: getSrcDirectoryForCiJob(ciJob),
                },
            };
            return Either.fromFailableAsync<Error, CheckoutCiJob>(() =>
                mkdir(wd, { recursive: true })
                    .then(() => process.chdir(wd))
                    .then(() => tEitherJob.move(fetchPackageJob).map(executeJob).get())
                    .then((e) =>
                        e.fold(
                            (err) => {
                                throw err;
                            },
                            () => ciJob,
                        ),
                    ),
            );
        }),
    )
    .map(async (tEitherCiJob) => {
        const eitherCiJob = await tEitherCiJob.get();
        const repoCiFileContents = await eitherCiJob.flatMapAsync((ciJob) =>
            Either.fromFailableAsync<Error, string>(() =>
                readFile(join(getSrcDirectoryForCiJob(ciJob), CI_WORKFLOW_FILE), 'utf-8'),
            ),
        );
        return repoCiFileContents
            .flatMap((fileText) =>
                Either.fromFailable<Error, CiWorkflow>(() => JSON.parse(fileText)).filter((json) => isCiWorkflow(json)),
            )
            .joinRight(eitherCiJob, (job: CheckoutCiJob, { workflow }) => ({
                job,
                commands: getPipelineGenerationCommand(job, workflow),
            }));
    })
    .map(async (tEitherPipelineGenerationCommand) => {
        const eitherJobCommand = await tEitherPipelineGenerationCommand.get();
        const pipelineSerialized = await eitherJobCommand.flatMapAsync(({ commands }) =>
            getStdoutMany(
                tEitherPipelineGenerationCommand.move(commands).peek((t) => t.trace.trace(JSON.stringify(t.get()))),
            ),
        );
        return pipelineSerialized
            .flatMap((results) => {
                tEitherPipelineGenerationCommand.trace.trace(JSON.stringify(results));
                const pipeline = results.at(-1)!;
                return PipelineImpl.from(pipeline);
            })
            .joinRight(eitherJobCommand, (job, pipeline) => ({ 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 `${getWorkingDirectoryForCiJob(job)}/src`;
}

function getSrcDirectoryForChildContainer(job: CheckoutCiJob) {
    // 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.
    return getSrcDirectoryForCiJob(job).replace('/var/lib/laminar', job.arguments.executorLaminarPath);
}

function getPipelineGenerationCommand(
    job: CheckoutCiJob,
    pipelineGeneratorPath: string,
    credentials = READONLY_CREDENTIALS,
    registry = OCI_REGISTRY,
    image = PIPELINE_IMAGE,
    runFlags = '--rm --network none --cap-drop ALL --security-opt no-new-privileges'.split(' '),
): Array<Command> {
    return [
        `docker login --username ${credentials.username} --password ${credentials.password} ${registry}`.split(' '),
    ].concat([
        [
            'docker',
            'run',
            ...runFlags,
            ...prependWith(
                Object.entries(job.arguments).map(([key, val]) => `"${key}"="${val}"`),
                '-e',
            ),
            '-v',
            `${getSrcDirectoryForChildContainer(job)}:/src`,
            image,
            `/src/${pipelineGeneratorPath}`,
        ],
    ]);
}