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

Sonar => Resolve issues

parent eedbe869
No related branches found
No related tags found
No related merge requests found
...@@ -13,7 +13,7 @@ interface ClientsConfig { ...@@ -13,7 +13,7 @@ interface ClientsConfig {
class ClientsSharedConfig { class ClientsSharedConfig {
private static config: ClientsSharedConfig | undefined = undefined; private static readonly config: ClientsSharedConfig | undefined = undefined;
public apiURL!: string; public apiURL!: string;
...@@ -121,8 +121,8 @@ class ClientsSharedConfig { ...@@ -121,8 +121,8 @@ class ClientsSharedConfig {
this.assignment = { this.assignment = {
filename : getEnvVar('ASSIGNMENT_FILENAME', ''), filename : getEnvVar('ASSIGNMENT_FILENAME', ''),
neededFiles: JSON.parse(getEnvVar('EXERCISE_NEEDED_FILES', '[]')), neededFiles: JSON.parse(getEnvVar('EXERCISE_NEEDED_FILES', '[]')),
name : process.env.DOJO_ASSIGNMENT_NAME || '', name : process.env.DOJO_ASSIGNMENT_NAME ?? '',
secret : process.env.DOJO_ASSIGNMENT_SECRET || '' secret : process.env.DOJO_ASSIGNMENT_SECRET ?? ''
}; };
this.dockerCompose = { this.dockerCompose = {
......
...@@ -337,7 +337,7 @@ class AssignmentValidator { ...@@ -337,7 +337,7 @@ class AssignmentValidator {
this.newStep('ASSIGNMENT_RUN', 'Please wait while we are running the assignment...'); this.newStep('ASSIGNMENT_RUN', 'Please wait while we are running the assignment...');
const exerciseDockerCompose = new ExerciseDockerCompose(ClientsSharedConfig.dockerCompose.projectName, this.assignmentFile, this.folderAssignment); const exerciseDockerCompose = new ExerciseDockerCompose(ClientsSharedConfig.dockerCompose.projectName, this.assignmentFile, this.folderAssignment, this.doDown);
try { try {
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
...@@ -357,7 +357,7 @@ class AssignmentValidator { ...@@ -357,7 +357,7 @@ class AssignmentValidator {
exitCode !== 0 ? resolve() : reject(exitCode); exitCode !== 0 ? resolve() : reject(exitCode);
}); });
exerciseDockerCompose.run(this.doDown); exerciseDockerCompose.run();
}); });
} catch ( error ) { } catch ( error ) {
this.fatalErrorMessage = 'Assignment is already solved'; this.fatalErrorMessage = 'Assignment is already solved';
......
...@@ -16,6 +16,11 @@ class ExerciseDockerCompose { ...@@ -16,6 +16,11 @@ class ExerciseDockerCompose {
public success: boolean = false; public success: boolean = false;
public exitCode: number = -1; public exitCode: number = -1;
private containerExitCode: number = -1;
private readonly filesOverrideArguments: string;
private readonly dockerComposeCommand: string;
private currentStep: string = 'NOT_RUNNING'; private currentStep: string = 'NOT_RUNNING';
private readonly projectName: string; private readonly projectName: string;
...@@ -23,12 +28,19 @@ class ExerciseDockerCompose { ...@@ -23,12 +28,19 @@ class ExerciseDockerCompose {
private readonly executionFolder: string; private readonly executionFolder: string;
private readonly composeFileOverride: Array<string> = []; private readonly composeFileOverride: Array<string> = [];
constructor(projectName: string, assignmentFile: AssignmentFile, executionFolder: string, composeFileOverride: Array<string> = []) { private readonly doDown: boolean;
constructor(projectName: string, assignmentFile: AssignmentFile, executionFolder: string, doDown: boolean = false, composeFileOverride: Array<string> = []) {
this.projectName = projectName; this.projectName = projectName;
this.assignmentFile = assignmentFile; this.assignmentFile = assignmentFile;
this.executionFolder = executionFolder; this.executionFolder = executionFolder;
this.composeFileOverride = composeFileOverride; this.composeFileOverride = composeFileOverride;
this.filesOverrideArguments = this.composeFileOverride.map(file => `--file "${ file }"`).join(' ');
this.dockerComposeCommand = `docker compose --project-name ${ this.projectName } --progress plain --file docker-compose.yml ${ this.filesOverrideArguments }`;
this.doDown = doDown;
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;
this.displayableLogs += displayable ? log : ''; this.displayableLogs += displayable ? log : '';
...@@ -74,23 +86,19 @@ class ExerciseDockerCompose { ...@@ -74,23 +86,19 @@ class ExerciseDockerCompose {
}); });
} }
run(doDown: boolean = false) { /**
(async () => { * Step 1: Run the docker compose file
let containerExitCode: number = -1; * @private
*/
const filesOverrideArguments = this.composeFileOverride.map(file => `--file "${ file }"`).join(' '); private async serviceRun() {
const dockerComposeCommand = `docker compose --project-name ${ this.projectName } --progress plain --file docker-compose.yml ${ filesOverrideArguments }`;
// Run the service
{
try { try {
this.newStep('COMPOSE_RUN', 'Running Docker Compose file'); this.newStep('COMPOSE_RUN', 'Running Docker Compose file');
containerExitCode = await new Promise<number>((resolve, reject) => { this.containerExitCode = await new Promise<number>((resolve, reject) => {
this.log('####################################################### 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(`${ this.dockerComposeCommand } run --build --rm ${ this.assignmentFile.result.container }`, {
cwd : this.executionFolder, cwd : this.executionFolder,
shell: true, shell: true,
env : { env : {
...@@ -104,13 +112,16 @@ class ExerciseDockerCompose { ...@@ -104,13 +112,16 @@ class ExerciseDockerCompose {
} catch ( error ) { } catch ( error ) {
this.endStep(`Error while running the docker compose file`, true); this.endStep(`Error while running the docker compose file`, true);
this.finished(false, ExerciseCheckerError.DOCKER_COMPOSE_RUN_ERROR); this.finished(false, ExerciseCheckerError.DOCKER_COMPOSE_RUN_ERROR);
return; throw error;
} }
this.endStep(`Docker Compose file run successfully`, false); this.endStep(`Docker Compose file run successfully`, false);
} }
// Get linked services logs /**
{ * Step 2: Get the logs of the linked services
* @private
*/
private async getLogs() {
try { try {
this.newStep('COMPOSE_LOGS', 'Linked services logs acquisition'); this.newStep('COMPOSE_LOGS', 'Linked services logs acquisition');
...@@ -118,7 +129,7 @@ class ExerciseDockerCompose { ...@@ -118,7 +129,7 @@ class ExerciseDockerCompose {
this.log('####################################################### Other Services Logs #######################################################\n', false, false); this.log('####################################################### Other Services Logs #######################################################\n', false, false);
const dockerCompose = spawn(`${ dockerComposeCommand } logs --timestamps`, { const dockerCompose = spawn(`${ this.dockerComposeCommand } logs --timestamps`, {
cwd : this.executionFolder, cwd : this.executionFolder,
shell: true shell: true
}); });
...@@ -128,14 +139,17 @@ class ExerciseDockerCompose { ...@@ -128,14 +139,17 @@ class ExerciseDockerCompose {
} catch ( error ) { } catch ( error ) {
this.endStep(`Error while getting the linked services logs`, true); this.endStep(`Error while getting the linked services logs`, true);
this.finished(false, ExerciseCheckerError.DOCKER_COMPOSE_LOGS_ERROR); this.finished(false, ExerciseCheckerError.DOCKER_COMPOSE_LOGS_ERROR);
return; throw error;
} }
this.endStep(`Linked services logs acquired`, false); this.endStep(`Linked services logs acquired`, false);
} }
// Remove containers if asked /**
{ * Step 3: Remove the containers if asked
if ( doDown ) { * @private
*/
private async removeContainers() {
if ( this.doDown ) {
try { try {
this.newStep('COMPOSE_DOWN', 'Stopping and removing containers'); this.newStep('COMPOSE_DOWN', 'Stopping and removing containers');
...@@ -143,7 +157,7 @@ class ExerciseDockerCompose { ...@@ -143,7 +157,7 @@ class ExerciseDockerCompose {
this.log('####################################################### Stop and remove containers #######################################################\n', false, false); this.log('####################################################### Stop and remove containers #######################################################\n', false, false);
const dockerCompose = spawn(`${ dockerComposeCommand } down --volumes`, { const dockerCompose = spawn(`${ this.dockerComposeCommand } down --volumes`, {
cwd : this.executionFolder, cwd : this.executionFolder,
shell: true shell: true
}); });
...@@ -153,15 +167,18 @@ class ExerciseDockerCompose { ...@@ -153,15 +167,18 @@ class ExerciseDockerCompose {
} catch ( error ) { } catch ( error ) {
this.endStep(`Error while stopping and removing containers`, true); this.endStep(`Error while stopping and removing containers`, true);
this.finished(false, ExerciseCheckerError.DOCKER_COMPOSE_DOWN_ERROR); this.finished(false, ExerciseCheckerError.DOCKER_COMPOSE_DOWN_ERROR);
return; throw error;
} }
this.endStep(`Containers stopped and removed`, false); this.endStep(`Containers stopped and removed`, false);
} }
} }
// Remove images if asked /**
{ * Step 4: Remove the dangling images if asked
if ( doDown ) { * @private
*/
private async removeImages() {
if ( this.doDown ) {
try { try {
this.newStep('COMPOSE_REMOVE_DANGLING', 'Removing dangling images'); this.newStep('COMPOSE_REMOVE_DANGLING', 'Removing dangling images');
...@@ -179,13 +196,25 @@ class ExerciseDockerCompose { ...@@ -179,13 +196,25 @@ class ExerciseDockerCompose {
} catch ( error ) { } catch ( error ) {
this.endStep(`Error while removing dangling images`, true); this.endStep(`Error while removing dangling images`, true);
this.finished(false, ExerciseCheckerError.DOCKER_COMPOSE_REMOVE_DANGLING_ERROR); this.finished(false, ExerciseCheckerError.DOCKER_COMPOSE_REMOVE_DANGLING_ERROR);
return; throw error;
} }
this.endStep(`Dangling images removed`, false); this.endStep(`Dangling images removed`, false);
} }
} }
this.finished(true, containerExitCode); run() {
(async () => {
try {
await this.serviceRun();
await this.getLogs();
await this.removeContainers();
await this.removeImages();
} catch ( error ) {
return;
}
this.finished(true, this.containerExitCode);
})(); })();
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment