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

Merge branch 'open_tool_for_self_hosting' into v5.0

parents 4e33e70f 64dc4066
No related branches found
No related tags found
No related merge requests found
import axios from 'axios';
import DojoBackendResponse from '../../shared/types/Dojo/DojoBackendResponse';
import ApiRoute from '../types/Dojo/ApiRoute';
interface ClientsConfig {
gitlabUrl: string,
gitlabAccountId: number,
gitlabAccountUsername: string,
loginGitlabClientId: string
}
class ClientsSharedConfig { class ClientsSharedConfig {
public apiURL: string; private static config: ClientsSharedConfig | undefined = undefined;
public assignment: { public apiURL!: string;
filename: string, neededFiles: Array<string>
public gitlab!: {
URL: string, dojoAccount: { id: number; username: string; };
}; };
public gitlab: { public login!: {
dojoAccount: { id: number; username: string; }; gitlab: {
client: {
id: string
}, url: {
redirect: string, token: string
}
}
}; };
public readonly dockerCompose: {
public assignment!: {
filename: string, neededFiles: Array<string>
};
public dockerCompose!: {
projectName: string projectName: string
}; };
public readonly exerciseResultsFolderMaxSizeInBytes: number; public exerciseResultsFolderMaxSizeInBytes!: number;
public readonly filenames: { public filenames!: {
results: string; results: string;
}; };
constructor() { public constructor() {
this.apiURL = process.env.API_URL ?? ''; this.login = {
gitlab: {
client: {
id: ''
},
url : {
redirect: '',
token : ''
}
}
};
}
this.assignment = { public envVarGetter(): (envName: string, defaultValue: string) => string {
filename : process.env.ASSIGNMENT_FILENAME ?? '', return (envName: string, defaultValue: string) => {
neededFiles: JSON.parse(process.env.EXERCISE_NEEDED_FILES ?? '[]') let value = process.env[envName] ?? defaultValue;
if ( value.includes('{{GITLAB_URL}}') ) {
value = value.replace('{{GITLAB_URL}}', this.gitlab.URL);
}
if ( value.includes('{{GITLAB_ACCOUNT_ID}}') ) {
value = value.replace('{{GITLAB_ACCOUNT_ID}}', String(this.gitlab.dojoAccount.id));
}
if ( value.includes('{{GITLAB_ACCOUNT_USERNAME}}') ) {
value = value.replace('{{GITLAB_ACCOUNT_USERNAME}}', this.gitlab.dojoAccount.username);
}
if ( value.includes('{{LOGIN_GITLAB_CLIENT_ID}}') ) {
value = value.replace('{{LOGIN_GITLAB_CLIENT_ID}}', this.login.gitlab.client.id);
}
return value;
}; };
}
private async fetchConfigFromApi() {
const downloadedConfig: ClientsConfig = (await axios.get<DojoBackendResponse<ClientsConfig>>(`${ this.apiURL }${ ApiRoute.CLIENTS_CONFIG }`)).data.data;
this.gitlab = { this.gitlab = {
URL : downloadedConfig.gitlabUrl,
dojoAccount: { dojoAccount: {
id : Number(process.env.GITLAB_DOJO_ACCOUNT_ID ?? -1), id : downloadedConfig.gitlabAccountId,
username: process.env.GITLAB_DOJO_ACCOUNT_USERNAME ?? '' username: downloadedConfig.gitlabAccountUsername
} }
}; };
this.login.gitlab.client.id = downloadedConfig.loginGitlabClientId;
}
async init(apiUrl: string) {
this.apiURL = apiUrl;
await this.fetchConfigFromApi();
const getEnvVar = this.envVarGetter();
this.login.gitlab.url = {
redirect: getEnvVar('LOGIN_GITLAB_URL_REDIRECT', ''),
token : getEnvVar('LOGIN_GITLAB_URL_TOKEN', '')
};
this.assignment = {
filename : getEnvVar('ASSIGNMENT_FILENAME', ''),
neededFiles: JSON.parse(getEnvVar('EXERCISE_NEEDED_FILES', '[]'))
};
this.dockerCompose = { this.dockerCompose = {
projectName: process.env.DOCKER_COMPOSE_PROJECT_NAME ?? '' projectName: getEnvVar('DOCKER_COMPOSE_PROJECT_NAME', '')
}; };
this.exerciseResultsFolderMaxSizeInBytes = Number(process.env.EXERCISE_RESULTS_FOLDER_MAX_SIZE_IN_BYTES ?? 0); this.exerciseResultsFolderMaxSizeInBytes = Number(getEnvVar('EXERCISE_RESULTS_FOLDER_MAX_SIZE_IN_BYTES', '0'));
this.filenames = { this.filenames = {
results: process.env.EXERCISE_RESULTS_FILENAME ?? '' results: getEnvVar('EXERCISE_RESULTS_FILENAME', '')
}; };
} }
} }
......
...@@ -4,13 +4,13 @@ import SharedAssignmentHelper from '../../../shared/helpers/Dojo/SharedAssign ...@@ -4,13 +4,13 @@ import SharedAssignmentHelper from '../../../shared/helpers/Dojo/SharedAssign
import path from 'node:path'; import path from 'node:path';
import AssignmentCheckerError from '../../../shared/types/Dojo/AssignmentCheckerError.js'; import AssignmentCheckerError from '../../../shared/types/Dojo/AssignmentCheckerError.js';
import fs from 'fs-extra'; import fs from 'fs-extra';
import ClientsSharedConfig from '../../config/ClientsSharedConfig.js';
import YAML from 'yaml'; import YAML from 'yaml';
import DojoDockerCompose from '../../types/Dojo/DojoDockerCompose.js'; import DojoDockerCompose from '../../types/Dojo/DojoDockerCompose.js';
import { exec, spawn } from 'child_process'; import { exec, spawn } from 'child_process';
import AssignmentFile from '../../../shared/types/Dojo/AssignmentFile.js'; import AssignmentFile from '../../../shared/types/Dojo/AssignmentFile.js';
import ExerciseDockerCompose from './ExerciseDockerCompose.js'; import ExerciseDockerCompose from './ExerciseDockerCompose.js';
import util from 'util'; import util from 'util';
import ClientsSharedConfig from '../../config/ClientsSharedConfig';
const execAsync = util.promisify(exec); const execAsync = util.promisify(exec);
......
import ApiRoute from '../../types/Dojo/ApiRoute.js'; import ApiRoute from '../../types/Dojo/ApiRoute.js';
import ClientsSharedConfig from '../../config/ClientsSharedConfig.js'; import ClientsSharedConfig from '../../config/ClientsSharedConfig';
class DojoBackendHelper { class DojoBackendHelper {
......
...@@ -2,12 +2,12 @@ import { TypedEmitter } from 'tiny-typed-emitter'; ...@@ -2,12 +2,12 @@ import { TypedEmitter } from 'tiny-typed-emitter';
import ExerciseRunningEvents from '../../types/Dojo/ExerciseRunningEvents.js'; import ExerciseRunningEvents from '../../types/Dojo/ExerciseRunningEvents.js';
import ExerciseCheckerError from '../../../shared/types/Dojo/ExerciseCheckerError.js'; import ExerciseCheckerError from '../../../shared/types/Dojo/ExerciseCheckerError.js';
import path from 'node:path'; import path from 'node:path';
import ClientsSharedConfig from '../../config/ClientsSharedConfig.js';
import Toolbox from '../../../shared/helpers/Toolbox.js'; import Toolbox from '../../../shared/helpers/Toolbox.js';
import * as fs from 'fs-extra'; import * as fs from 'fs-extra';
import ExerciseResultsFile from '../../../shared/types/Dojo/ExerciseResultsFile.js'; import ExerciseResultsFile from '../../../shared/types/Dojo/ExerciseResultsFile.js';
import JSON5 from 'json5'; import JSON5 from 'json5';
import Json5FileValidator from '../../../shared/helpers/Json5FileValidator.js'; import Json5FileValidator from '../../../shared/helpers/Json5FileValidator.js';
import ClientsSharedConfig from '../../config/ClientsSharedConfig';
class ExerciseResultsSanitizerAndValidator { class ExerciseResultsSanitizerAndValidator {
......
enum ApiRoute { enum ApiRoute {
CLIENTS_CONFIG = '/clients_config',
LOGIN = '/login', LOGIN = '/login',
REFRESH_TOKENS = '/refresh_tokens', REFRESH_TOKENS = '/refresh_tokens',
TEST_SESSION = '/test_session', TEST_SESSION = '/test_session',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment