Skip to content
Snippets Groups Projects
Commit e8114e05 authored by michael.minelli's avatar michael.minelli
Browse files

ExerciseDockerCompose => Add current step to log & refactor code

parent 06f4fcdc
Branches
Tags
No related merge requests found
...@@ -16,6 +16,8 @@ class ExerciseDockerCompose { ...@@ -16,6 +16,8 @@ class ExerciseDockerCompose {
public success: boolean = false; public success: boolean = false;
public exitCode: number = -1; public exitCode: number = -1;
private currentStep: string = 'NOT_RUNNING';
constructor(private projectName: string, private assignmentFile: AssignmentFile, private executionFolder: string, private composeFileOverride: Array<string> = []) { constructor(private projectName: string, private assignmentFile: AssignmentFile, private executionFolder: string, private composeFileOverride: Array<string> = []) {
this.events.on('logs', (log: string, _error: boolean, displayable: boolean) => { this.events.on('logs', (log: string, _error: boolean, displayable: boolean) => {
this.allLogs += log; this.allLogs += log;
...@@ -29,13 +31,26 @@ class ExerciseDockerCompose { ...@@ -29,13 +31,26 @@ class ExerciseDockerCompose {
}); });
} }
private registerChildProcess(childProcess: ChildProcessWithoutNullStreams, resolve: (value: (number | PromiseLike<number>)) => void, reject: (reason?: unknown) => void) { private newStep(name: string, message: string) {
this.currentStep = name;
this.events.emit('step', name, message);
}
private endStep(message: string, error: boolean) {
this.events.emit('endStep', this.currentStep, message, error);
}
private log(message: string, error: boolean, displayable: boolean) {
this.events.emit('logs', message, error, displayable, this.currentStep);
}
private registerChildProcess(childProcess: ChildProcessWithoutNullStreams, resolve: (value: (number | PromiseLike<number>)) => void, reject: (reason?: unknown) => void, displayable: boolean) {
childProcess.stdout.on('data', (data) => { childProcess.stdout.on('data', (data) => {
this.events.emit('logs', data.toString(), false, false); this.log(data.toString(), false, displayable);
}); });
childProcess.stderr.on('data', (data) => { childProcess.stderr.on('data', (data) => {
this.events.emit('logs', data.toString(), true, false); this.log(data.toString(), true, displayable);
}); });
childProcess.on('exit', (code) => { childProcess.on('exit', (code) => {
...@@ -52,11 +67,11 @@ class ExerciseDockerCompose { ...@@ -52,11 +67,11 @@ class ExerciseDockerCompose {
// Run the service // Run the service
{ {
try { try {
this.events.emit('step', 'COMPOSE_RUN', 'Running Docker Compose file'); this.newStep('COMPOSE_RUN', 'Running Docker Compose file');
containerExitCode = await new Promise<number>((resolve, reject) => { containerExitCode = await new Promise<number>((resolve, reject) => {
this.events.emit('logs', '####################################################### Docker Compose & Main Container Logs #######################################################\n', false, false); this.log('####################################################### Docker Compose & Main Container Logs #######################################################\n', false, false);
const dockerCompose = spawn(`${ dockerComposeCommand } run --build --rm ${ this.assignmentFile.result.container }`, { const dockerCompose = spawn(`${ dockerComposeCommand } run --build --rm ${ this.assignmentFile.result.container }`, {
cwd : this.executionFolder, cwd : this.executionFolder,
...@@ -67,63 +82,63 @@ class ExerciseDockerCompose { ...@@ -67,63 +82,63 @@ class ExerciseDockerCompose {
} }
}); });
this.registerChildProcess(dockerCompose, resolve, reject); this.registerChildProcess(dockerCompose, resolve, reject, true);
}); });
} catch ( error ) { } catch ( error ) {
this.events.emit('endStep', 'COMPOSE_RUN', `Error while running the docker compose file`, true); this.endStep(`Error while running the docker compose file`, true);
this.events.emit('finished', false, ExerciseCheckerError.DOCKER_COMPOSE_RUN_ERROR); this.events.emit('finished', false, ExerciseCheckerError.DOCKER_COMPOSE_RUN_ERROR);
return; return;
} }
this.events.emit('endStep', 'COMPOSE_RUN', `Docker Compose file run successfully`, false); this.endStep(`Docker Compose file run successfully`, false);
} }
// Get linked services logs // Get linked services logs
{ {
try { try {
this.events.emit('step', 'COMPOSE_LOGS', 'Linked services logs acquisition'); this.newStep('COMPOSE_LOGS', 'Linked services logs acquisition');
await new Promise<number>((resolve, reject) => { await new Promise<number>((resolve, reject) => {
this.events.emit('logs', '####################################################### Other Services Logs #######################################################\n', false, false); this.log('####################################################### Other Services Logs #######################################################\n', false, false);
const dockerCompose = spawn(`${ dockerComposeCommand } logs --timestamps`, { const dockerCompose = spawn(`${ dockerComposeCommand } logs --timestamps`, {
cwd : this.executionFolder, cwd : this.executionFolder,
shell: true shell: true
}); });
this.registerChildProcess(dockerCompose, resolve, reject); this.registerChildProcess(dockerCompose, resolve, reject, false);
}); });
} catch ( error ) { } catch ( error ) {
this.events.emit('endStep', 'COMPOSE_LOGS', `Error while getting the linked services logs`, true); this.endStep(`Error while getting the linked services logs`, true);
this.events.emit('finished', false, ExerciseCheckerError.DOCKER_COMPOSE_LOGS_ERROR); this.events.emit('finished', false, ExerciseCheckerError.DOCKER_COMPOSE_LOGS_ERROR);
return; return;
} }
this.events.emit('endStep', 'COMPOSE_LOGS', `Linked services logs acquired`, false); this.endStep(`Linked services logs acquired`, false);
} }
// Remove containers if asked // Remove containers if asked
{ {
if ( doDown ) { if ( doDown ) {
try { try {
this.events.emit('step', 'COMPOSE_DOWN', 'Stopping and removing containers'); this.newStep('COMPOSE_DOWN', 'Stopping and removing containers');
await new Promise<number>((resolve, reject) => { await new Promise<number>((resolve, reject) => {
this.events.emit('logs', '####################################################### Stop and remove containers #######################################################\n', false, false); this.log('####################################################### Stop and remove containers #######################################################\n', false, false);
const dockerCompose = spawn(`${ dockerComposeCommand } down --volumes --rmi`, { const dockerCompose = spawn(`${ dockerComposeCommand } down --volumes --rmi`, {
cwd : this.executionFolder, cwd : this.executionFolder,
shell: true shell: true
}); });
this.registerChildProcess(dockerCompose, resolve, reject); this.registerChildProcess(dockerCompose, resolve, reject, false);
}); });
} catch ( error ) { } catch ( error ) {
this.events.emit('endStep', 'COMPOSE_DOWN', `Error stop and remove containers`, true); this.endStep(`Error stop and remove containers`, true);
this.events.emit('finished', false, ExerciseCheckerError.DOCKER_COMPOSE_DOWN_ERROR); this.events.emit('finished', false, ExerciseCheckerError.DOCKER_COMPOSE_DOWN_ERROR);
return; return;
} }
this.events.emit('endStep', 'COMPOSE_DOWN', `Containers stopped and removed`, false); this.events.emit('endStep', this.currentStep, `Containers stopped and removed`, false);
} }
} }
...@@ -131,25 +146,25 @@ class ExerciseDockerCompose { ...@@ -131,25 +146,25 @@ class ExerciseDockerCompose {
{ {
if ( doDown ) { if ( doDown ) {
try { try {
this.events.emit('step', 'COMPOSE_REMOVE_DANGLING', 'Removing dangling images'); this.newStep('COMPOSE_REMOVE_DANGLING', 'Removing dangling images');
await new Promise<number>((resolve, reject) => { await new Promise<number>((resolve, reject) => {
this.events.emit('logs', '####################################################### Remove dangling images #######################################################\n', false, false); this.log('####################################################### Remove dangling images #######################################################\n', false, false);
const dockerCompose = spawn(`docker image prune --force`, { const dockerCompose = spawn(`docker image prune --force`, {
cwd : this.executionFolder, cwd : this.executionFolder,
shell: true shell: true
}); });
this.registerChildProcess(dockerCompose, resolve, reject); this.registerChildProcess(dockerCompose, resolve, reject, false);
}); });
} catch ( error ) { } catch ( error ) {
this.events.emit('endStep', 'COMPOSE_REMOVE_DANGLING', `Error while removing dangling images`, true); this.endStep(`Error while removing dangling images`, true);
this.events.emit('finished', false, ExerciseCheckerError.DOCKER_COMPOSE_REMOVE_DANGLING_ERROR); this.events.emit('finished', false, ExerciseCheckerError.DOCKER_COMPOSE_REMOVE_DANGLING_ERROR);
return; return;
} }
this.events.emit('endStep', 'COMPOSE_REMOVE_DANGLING', `Dangling images removed`, false); this.events.emit('endStep', this.currentStep, `Dangling images removed`, false);
} }
} }
......
interface ExerciseRunningEvents { interface ExerciseRunningEvents {
step: (name: string, message: string) => void; step: (name: string, message: string) => void;
endStep: (stepName: string, message: string, error: boolean) => void; endStep: (stepName: string, message: string, error: boolean) => void;
logs: (log: string, error: boolean, displayable: boolean) => void; logs: (log: string, error: boolean, displayable: boolean, currentStep: string) => void;
finished: (success: boolean, exitCode: number) => void; finished: (success: boolean, exitCode: number) => void;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment