From ce9726ffa02c8910ac698ac05670522c2b1fb0a4 Mon Sep 17 00:00:00 2001 From: Alex <apuret@takima.fr> Date: Thu, 9 Dec 2021 18:38:48 +0100 Subject: [PATCH] chores: handle config local/prod, better error handling --- .gitlab-ci.yml | 2 - Dockerfile | 2 +- build-plugins.sh | 4 ++ .../deadlock-extension/src/core/config.ts | 37 ++++++---- .../deadlock-extension/src/core/userConfig.ts | 8 ++- .../deadlock-extension/src/extension.ts | 3 +- .../deadlock-extension/src/recorder/index.ts | 72 ++++++++++++------- .../deadlock-extension/src/recorder/utils.ts | 15 ++-- 8 files changed, 93 insertions(+), 50 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 99734f7d..34d91e32 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -17,8 +17,6 @@ build: services: - docker:18.09.6-dind script: - # setup config for extension - - cp -rf ./deadlock-plugins/deadlock-extension/src/core/config.prod.ts ./deadlock-plugins/deadlock-extension/src/core/config.ts - ./build.sh - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.e-biz.fr - docker build . -t $CI_REGISTRY_IMAGE diff --git a/Dockerfile b/Dockerfile index f0f98e21..29b766a6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM registry.e-biz.fr/deadlock-public/theia:1.20 +FROM registry.e-biz.fr/deadlock-public/theia:1.201 USER root diff --git a/build-plugins.sh b/build-plugins.sh index cd04431c..d3f7861f 100755 --- a/build-plugins.sh +++ b/build-plugins.sh @@ -5,7 +5,11 @@ set -e CI_COMMIT_SHORT_SHA=${CI_COMMIT_SHORT_SHA:-local} for dir in deadlock-plugins/*/; do + echo + echo "===============================================" echo "Building $dir" + echo "===============================================" + echo cd $dir CURRENT_PLUGIN_DIR=$(basename "$PWD") ./install.sh diff --git a/deadlock-plugins/deadlock-extension/src/core/config.ts b/deadlock-plugins/deadlock-extension/src/core/config.ts index ba0d1972..c99f4e45 100644 --- a/deadlock-plugins/deadlock-extension/src/core/config.ts +++ b/deadlock-plugins/deadlock-extension/src/core/config.ts @@ -1,19 +1,32 @@ import * as path from 'path'; +import * as os from 'os'; -// DEV CONFIG +const homeDir = os.homedir(); +// if we are on container, means the directory will depend differently +const onContainer = homeDir.includes('theia') || homeDir.includes('root'); -if (!process.env['HOME']) { - console.error('Could not get home user.'); -} +const deadlockExtensionPath = path.join(homeDir, 'deadlock-extension'); -const home = process.env['HOME'] ?? ''; +export const PROJECT_SRC_PATH = onContainer + ? '/project' + : path.join(homeDir, 'deadlock-extension', '/project'); -export const DEADLOCK_EXTENSION_PATH = path.join(home, 'deadlock-extension'); -export const DOCS_PATH = path.join(DEADLOCK_EXTENSION_PATH, 'docs'); -export const CONFIG_PATH = path.join(DEADLOCK_EXTENSION_PATH, 'config'); -export const USER_CHALLENGE_PATH = path.join(CONFIG_PATH, 'user-challenge.json'); -export const PROJECT_SRC_PATH = path.join(DEADLOCK_EXTENSION_PATH, 'project'); -export const PROJECT_THEIA_PATH = path.join(DEADLOCK_EXTENSION_PATH, 'project-theia'); +export const PROJECT_THEIA_PATH = onContainer + ? path.join('/home/project') + : path.join(deadlockExtensionPath, 'project-theia'); +export const DOCS_PATH = path.join( + path.join(onContainer ? '/home/theia' : deadlockExtensionPath), + 'docs' +); -export const BRIEFING_FILE_NAME = 'briefing.md'; \ No newline at end of file +export const CONFIG_PATH = onContainer + ? '/home/config/' + : path.join(deadlockExtensionPath, 'config'); + +export const USER_CHALLENGE_PATH = path.join( + CONFIG_PATH, + 'user-challenge.json' +); + +export const BRIEFING_FILE_NAME = 'briefing.md'; diff --git a/deadlock-plugins/deadlock-extension/src/core/userConfig.ts b/deadlock-plugins/deadlock-extension/src/core/userConfig.ts index bf4ab4f4..34fa4c07 100644 --- a/deadlock-plugins/deadlock-extension/src/core/userConfig.ts +++ b/deadlock-plugins/deadlock-extension/src/core/userConfig.ts @@ -16,6 +16,8 @@ * } */ +import { error } from '../recorder/utils'; + export interface UserDetails { firstName: string; lastName: string; @@ -74,7 +76,7 @@ export default abstract class UserConfig { return this.userConfigJson?.remoteUserDetails; } - abstract async loadText(): Promise<string>; + abstract loadText(): Promise<string>; public async init() { try { @@ -83,8 +85,8 @@ export default abstract class UserConfig { this.userConfigJson = JSON.parse(userConfig); return Promise.resolve(); } catch (e) { - console.error('Cannot load userConfig'); - console.error(e); + error('Cannot load userConfig'); + error(e); return Promise.reject(); } } diff --git a/deadlock-plugins/deadlock-extension/src/extension.ts b/deadlock-plugins/deadlock-extension/src/extension.ts index 0e984fe8..1840979d 100644 --- a/deadlock-plugins/deadlock-extension/src/extension.ts +++ b/deadlock-plugins/deadlock-extension/src/extension.ts @@ -3,6 +3,7 @@ import { DepNodeProvider } from './theia/deadlockPanel'; import BriefingView, { BRIEFING_ID } from './view/briefingView'; import { OPEN_BRIEFING_COMMAND } from './theia/command'; import UserConfigTheia from './theia/userConfigTheia'; +import { error } from './recorder/utils'; export function initViews() { new BriefingView(); @@ -16,7 +17,7 @@ export async function activate(context: vscode.ExtensionContext) { try { await userConfig.init(); } catch (e) { - console.error('Cannot init userConfig'); + error('Cannot init userConfig'); } initViews(); diff --git a/deadlock-plugins/deadlock-extension/src/recorder/index.ts b/deadlock-plugins/deadlock-extension/src/recorder/index.ts index fc876d2b..0c3b2f4f 100644 --- a/deadlock-plugins/deadlock-extension/src/recorder/index.ts +++ b/deadlock-plugins/deadlock-extension/src/recorder/index.ts @@ -1,43 +1,63 @@ -import CommandRecorder from "./command-recorder" +import CommandRecorder from './command-recorder'; import GitMission from '../core/gitMission'; -import UserConfigNode from "./userConfigNode"; +import UserConfigNode from './userConfigNode'; import { PROJECT_SRC_PATH, PROJECT_THEIA_PATH } from '../core/config'; -import { copyProjectSources, clearFilesExceptGit, log } from "./utils"; +import { copyProjectSources, clearFilesExceptGit, log, error } from './utils'; +import UserConfig from '../core/userConfig'; export default class Recorder { + async setupProject(userConfig: UserConfig, gitMission?: GitMission) { + log('Setup user project..'); + + if (!userConfig.isProfessor()) { + await copyProjectSources(PROJECT_SRC_PATH, PROJECT_THEIA_PATH, ['.git/']); + if (gitMission) { + log('Starting CommandRecorder..'); + new CommandRecorder(gitMission).run(); + } else { + error('Cannot start command recorder, gitMission not found'); + } + } else { + await copyProjectSources(PROJECT_SRC_PATH, PROJECT_THEIA_PATH); + } + } + + async setupFromRemoteRepo(gitMission: GitMission) { + log('Check if remote repo exist'); + const isRemoteRepoExist = await gitMission.isRemoteRepoExist(); + if (isRemoteRepoExist) { + // rm all except git directory pull remote code and setup + log('Cleaning files to pull repo'); + + await clearFilesExceptGit(PROJECT_SRC_PATH); + + log('Pulling user repo'); + await gitMission.pull(); + } + } + async run() { const userConfig = new UserConfigNode(); + let gitMission; + try { log('Init User config'); await userConfig.init(); log('Init GitMission'); - const gitMission = await new GitMission(userConfig).init(); - log('Check if remote repo exist'); - const isRemoteRepoExist = await gitMission.isRemoteRepoExist(); - if (isRemoteRepoExist) { - // rm all except git directory pull remote code and setup - log('Cleaning files to pull repo'); - - await clearFilesExceptGit(PROJECT_SRC_PATH); - - log('Pulling user repo'); - await gitMission.pull(); - } - log('Setup user project..'); - - if (!userConfig.isProfessor()) { - await copyProjectSources(PROJECT_SRC_PATH, PROJECT_THEIA_PATH, ['.git/']); - log('Starting CommandRecorder..'); - new CommandRecorder(gitMission).run(); - } else { - await copyProjectSources(PROJECT_SRC_PATH, PROJECT_THEIA_PATH); - } + gitMission = await new GitMission(userConfig).init(); + await this.setupFromRemoteRepo(gitMission); + } catch (e) { + error('Cannot setup user repo.'); + error(e); + } + try { + await this.setupProject(userConfig, gitMission); } catch (e) { - console.error('Cannot setup user repo.'); - console.error(e); + error('Error while setup project sources'); + error(e); } } } diff --git a/deadlock-plugins/deadlock-extension/src/recorder/utils.ts b/deadlock-plugins/deadlock-extension/src/recorder/utils.ts index 88e8e126..b5eff5ae 100644 --- a/deadlock-plugins/deadlock-extension/src/recorder/utils.ts +++ b/deadlock-plugins/deadlock-extension/src/recorder/utils.ts @@ -12,22 +12,27 @@ const Path = require('path'); const PREFIX = '[DEADLOCK-RECORDER]'; -export const log = (message, args?) => { +export const log = (message: any, args?: any[]) => { if (args) { console.log(`${PREFIX} ${message}`, args); } else { console.log(`${PREFIX} ${message}`); } -} -export const error = (message, args?) => { +}; + +export const error = (message: any, args?: any[]) => { if (args) { console.error(`${PREFIX} ${message}`, args); } else { console.error(`${PREFIX} ${message}`); } -} +}; -export async function copyProjectSources(srcPath: string, theiaPath: string, excludes: Array<string> = []) { +export async function copyProjectSources( + srcPath: string, + theiaPath: string, + excludes: Array<string> = []) +{ const excludeCmd = excludes.map((current) => `--exclude ${current}`).join(' '); return exec(`rsync -a ${srcPath}/ ${theiaPath} ${excludeCmd} && chown -R theia:theia /home/project`); } -- GitLab