feat(scaffolder): starting to write a job processor

This commit is contained in:
blam
2020-06-24 13:56:25 +02:00
parent f42a541ade
commit 73c8c69c1c
4 changed files with 33 additions and 16 deletions
+1
View File
@@ -36,6 +36,7 @@
"helmet": "^3.22.0",
"morgan": "^1.10.0",
"nodegit": "0.26.5",
"uuid": "^8.2.0",
"winston": "^3.2.1"
},
"devDependencies": {
@@ -16,3 +16,4 @@
export * from './templater';
export * from './prepare';
export * from './templater/cookiecutter';
export * from './jobs';
@@ -16,6 +16,7 @@
import type { Writable } from 'stream';
import Docker from 'dockerode';
import { JsonValue } from '@backstage/config';
export interface RequiredTemplateValues {
component_id: string;
@@ -23,7 +24,7 @@ export interface RequiredTemplateValues {
export interface TemplaterRunOptions {
directory: string;
values: RequiredTemplateValues & object;
values: RequiredTemplateValues & Record<string, JsonValue>;
logStream?: Writable;
dockerClient: Docker;
}
@@ -17,10 +17,10 @@
import { Logger } from 'winston';
import Router from 'express-promise-router';
import express from 'express';
import { PreparerBuilder, TemplaterBase } from '../scaffolder';
import { PreparerBuilder, TemplaterBase, JobProcessor } from '../scaffolder';
import { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
import Docker from 'dockerode';
import {} from '@backstage/backend-common';
export interface RouterOptions {
preparers: PreparerBuilder;
templater: TemplaterBase;
@@ -35,10 +35,32 @@ export async function createRouter(
const { preparers, templater, logger: parentLogger, dockerClient } = options;
const logger = parentLogger.child({ plugin: 'scaffolder' });
const jobProcessor = new JobProcessor({
preparers,
templater,
logger,
dockerClient,
});
router.get('/v1/job/:jobId', ({ params }, res) => {
const job = jobProcessor.get(params.jobId);
if (!job) {
return res.status(404).send({ error: 'job not found' });
}
res.send({
id: job.id,
metadata: job.metadata,
status: job.status,
log: job.log,
error: job.error,
});
});
router.post('/v1/jobs', async (_, res) => {
// TODO(blam): Create a unique job here and return the ID so that
// The end user can poll for updates on the current job
res.status(201).json({ accepted: true });
// TODO(blam): Take this entity from the post body sent from the frontend
const mockEntity: TemplateEntityV1alpha1 = {
@@ -64,20 +86,12 @@ export async function createRouter(
},
};
// Get the preparer for the mock entity
const preparer = preparers.get(mockEntity);
const job = jobProcessor.create(mockEntity, { component_id: 'test' });
res.status(201).json({ jobId: job.id });
// Run the preparer for the mock entity to produce a temporary directory with template in
const skeletonPath = await preparer.prepare(mockEntity);
jobProcessor.run(job);
// Run the templater on the mock directory with values from the post body
const templatedPath = await templater.run({
directory: skeletonPath,
values: { component_id: 'test' },
dockerClient,
});
console.warn(templatedPath);
// console.warn(templatedPath);
});
const app = express();