diff options
Diffstat (limited to 'worker/scripts')
-rwxr-xr-x | worker/scripts/ansible_playbook.ts | 4 | ||||
-rwxr-xr-x | worker/scripts/build_docker_image.ts | 49 | ||||
-rwxr-xr-x | worker/scripts/checkout_ci.ts | 72 |
3 files changed, 58 insertions, 67 deletions
diff --git a/worker/scripts/ansible_playbook.ts b/worker/scripts/ansible_playbook.ts index c6d8f2c..4a22984 100755 --- a/worker/scripts/ansible_playbook.ts +++ b/worker/scripts/ansible_playbook.ts @@ -28,9 +28,9 @@ const eitherJob = getRequiredEnvVars(['path', 'playbooks']).mapRight( const eitherVault = Bitwarden.getConfigFromEnvironment().mapRight((config) => new Bitwarden(config)); const playbookMetric = Metric.fromName('ansiblePlaybook.playbook'); -const _logJob = LogTraceable.of(eitherJob).bimap(TraceUtil.withTrace('ansible_playbook')); +const _logJob = LogTraceable.of(eitherJob).flatMap(TraceUtil.withTrace('ansible_playbook')); await LogMetricTraceable.ofLogTraceable(_logJob) - .bimap(TraceUtil.withMetricTrace(playbookMetric)) + .flatMap(TraceUtil.withMetricTrace(playbookMetric)) .peek((tEitherJob) => tEitherJob.trace.trace('starting ansible playbook job! (⑅˘꒳˘)')) .map((tEitherJob) => tEitherJob.get().flatMapAsync((job) => diff --git a/worker/scripts/build_docker_image.ts b/worker/scripts/build_docker_image.ts index 228dfcc..b35031a 100755 --- a/worker/scripts/build_docker_image.ts +++ b/worker/scripts/build_docker_image.ts @@ -3,7 +3,6 @@ import { getRequiredEnvVars, getStdout, - LogLevel, LogTraceable, LogMetricTraceable, Metric, @@ -29,17 +28,18 @@ const eitherJob = getRequiredEnvVars([ ); const eitherVault = Bitwarden.getConfigFromEnvironment().mapRight((config) => new Bitwarden(config)); -const buildImageMetric = Metric.fromName('dockerImage.build'); -const loginMetric = Metric.fromName('dockerRegistry.login'); -const _logJob = LogTraceable.of(eitherJob).bimap((tEitherJob) => { - const trace = - 'build_docker_image.' + - tEitherJob.get().fold(({ isRight, value }) => (isRight ? value.arguments.buildTarget : '')); - return [tEitherJob.get(), trace]; +const buildImageMetric = Metric.fromName('dockerImage.build').asResult(); +const loginMetric = Metric.fromName('dockerRegistry.login').asResult(); +const _logJob = LogTraceable.of(eitherJob).flatMap((tEitherJob) => { + const trace = tEitherJob.get().fold( + () => 'NO_BUILD_TARGET', + ({ arguments: { buildTarget } }) => buildTarget, + ); + return tEitherJob.traceScope(() => `build_docker_image.${trace}`); }); await LogMetricTraceable.ofLogTraceable(_logJob) - .bimap(TraceUtil.withMetricTrace(buildImageMetric)) - .bimap(TraceUtil.withMetricTrace(loginMetric)) + .flatMap(TraceUtil.withMetricTrace(buildImageMetric)) + .flatMap(TraceUtil.withMetricTrace(loginMetric)) .peek((tEitherJob) => tEitherJob.trace.trace('starting docker image build job! (⑅˘꒳˘)')) .map((tEitherJob) => tEitherJob.get().flatMapAsync((job) => @@ -68,12 +68,7 @@ await LogMetricTraceable.ofLogTraceable(_logJob) }), ); }) - .peek(async (tEitherWithAuthdRegistry) => { - const eitherWithAuthdRegistry = await tEitherWithAuthdRegistry.get(); - return tEitherWithAuthdRegistry.trace.trace( - eitherWithAuthdRegistry.fold(({ isLeft }) => loginMetric[isLeft ? 'failure' : 'success']), - ); - }) + .peek(TraceUtil.promiseify(TraceUtil.traceResultingEither(loginMetric))) .map(async (tEitherWithAuthdRegistryBuildJob) => { const eitherWithAuthdRegistryBuildJob = await tEitherWithAuthdRegistryBuildJob.get(); tEitherWithAuthdRegistryBuildJob.trace.trace('finally building the image~ (◕ᴗ◕✿)'); @@ -92,19 +87,15 @@ await LogMetricTraceable.ofLogTraceable(_logJob) eitherWithAuthdRegistryBuildJob.mapRight((job) => ({ buildOutput, job })), ); }) - .peek(async (tEitherWithBuiltImage) => { - const eitherWithBuiltImage = await tEitherWithBuiltImage.get(); - eitherWithBuiltImage.fold(({ isLeft, value }) => { - tEitherWithBuiltImage.trace.trace(buildImageMetric[isLeft ? 'failure' : 'success']); - if (isLeft) { - tEitherWithBuiltImage.trace - .addTrace(LogLevel.ERROR) - .trace(`oh nyoo we couldn't buiwd the img :(( ${value}`); - return; - } - tEitherWithBuiltImage.trace.addTrace('buildOutput').trace(value.buildOutput); - }); - }) + .flatMapAsync(TraceUtil.promiseify(TraceUtil.traceResultingEither(buildImageMetric))) + .peek( + TraceUtil.promiseify((tBuilt) => + tBuilt.get().fold( + (err) => tBuilt.trace.trace(`oh nyoo we couldn't buiwd the img :(( ${err}`), + (ok) => tBuilt.trace.traceScope('buildOutput').trace(ok.buildOutput), + ), + ), + ) .map(async (tEitherWithBuiltImage) => { const eitherWithBuiltImage = await tEitherWithBuiltImage.get(); return eitherWithBuiltImage diff --git a/worker/scripts/checkout_ci.ts b/worker/scripts/checkout_ci.ts index 8e4dcca..fb71a16 100755 --- a/worker/scripts/checkout_ci.ts +++ b/worker/scripts/checkout_ci.ts @@ -11,6 +11,7 @@ import { Metric, prependWith, TraceUtil, + IEither, } from '@emprespresso/pengueno'; import { mkdir, readFile, rm } from 'fs/promises'; import { join } from 'path'; @@ -29,11 +30,14 @@ const eitherJob = getRequiredEnvVars(['remote', 'refname', 'rev']).mapRight( }, }, ); +const afterJob = eitherJob.flatMapAsync((job) => + Either.fromFailableAsync(() => rm(getWorkingDirectoryForCiJob(job), { recursive: true })), +); const ciRunMetric = Metric.fromName('checkout_ci.run'); -const _logJob = LogTraceable.of(eitherJob).bimap(TraceUtil.withTrace(`checkout_ci.${run}`)); +const _logJob = LogTraceable.of(eitherJob).flatMap(TraceUtil.withTrace(`checkout_ci.${run}`)); await LogMetricTraceable.ofLogTraceable(_logJob) - .bimap(TraceUtil.withMetricTrace(ciRunMetric)) + .flatMap(TraceUtil.withMetricTrace(ciRunMetric)) .map((tEitherJob) => tEitherJob.get().flatMapAsync((ciJob) => { const wd = getWorkingDirectoryForCiJob(ciJob); @@ -53,29 +57,28 @@ await LogMetricTraceable.ofLogTraceable(_logJob) ); }), ) - .map((tEitherCiJob) => - tEitherCiJob.get().then((eitherCiJob) => - eitherCiJob.flatMapAsync<{ cmd: Command; job: CheckoutCiJob }>((ciJob) => - Either.fromFailableAsync<Error, string>(() => - readFile(join(getSrcDirectoryForCiJob(ciJob), CI_WORKFLOW_FILE), 'utf-8'), - ).then((eitherWorkflowJson) => - eitherWorkflowJson - .flatMap((json) => Either.fromFailable<Error, unknown>(JSON.parse(json))) - .flatMap((eitherWorkflowParse) => { - if (isCiWorkflow(eitherWorkflowParse)) { - return Either.right({ - cmd: getPipelineGenerationCommand(ciJob, eitherWorkflowParse.workflow), - job: ciJob, - }); - } - return Either.left( - new Error("couldn't find any valid ci configuration (。•́︿•̀。), that's okay~"), - ); - }), - ), + .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, unknown>(() => JSON.parse(fileText))) + .flatMap((json) => { + return eitherCiJob.flatMap((ciJob): IEither<Error, { cmd: Command; job: CheckoutCiJob }> => { + 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) => @@ -107,17 +110,14 @@ await LogMetricTraceable.ofLogTraceable(_logJob) .get(), ); }) - .get() - .then((e) => - e - .flatMap(() => eitherJob) - .fold(({ isLeft, isRight, value }) => { - if (isLeft || !isRight) throw value; - return rm(getWorkingDirectoryForCiJob(value), { - recursive: true, - }); - }), - ); + .map(async (tCompletePipeline) => { + const completePipeline = await tCompletePipeline.get(); + return completePipeline.fold( + (e) => Promise.reject(e), + () => afterJob, + ); + }) + .get(); const getWorkingDirectoryForCiJob = (job: CheckoutCiJob) => `${job.arguments.returnPath}/${job.arguments.run}`; @@ -130,7 +130,7 @@ const getPipelineGenerationCommand = ( pipelineGeneratorPath: string, image = _image, runFlags = _runFlags, -) => [ +): Command => [ 'docker', 'run', ...runFlags, |