From 56d4f1119e2990ab0ba9450339db92624e81001d Mon Sep 17 00:00:00 2001 From: Datata1 Date: Tue, 14 May 2024 21:16:56 +0200 Subject: [PATCH 1/2] fixed on-demand problem and when connecting to tunnel the first view is the overview of the codesphere workspace --- src/SidebarProvider.ts | 80 ++++++++++++++++++++++++----- src/extension.ts | 27 ++-------- src/ts/wsService.ts | 26 ++++++++++ webviews/components/Overview.svelte | 24 +++++---- 4 files changed, 109 insertions(+), 48 deletions(-) diff --git a/src/SidebarProvider.ts b/src/SidebarProvider.ts index 42ed337..cd7f8b1 100644 --- a/src/SidebarProvider.ts +++ b/src/SidebarProvider.ts @@ -12,6 +12,7 @@ const { setupWs, wakeUpWorkspace, doesTunnelAlreadyExist, getPidFromServer, + waitForTerminal, serverIsUp } = require('./ts/wsService'); import { readBashFile } from "./ts/readBash"; import * as wsLib from 'ws'; @@ -61,8 +62,49 @@ export class SidebarProvider implements vscode.WebviewViewProvider { webviewView.webview.html = this._getHtmlForWebviewAfterSignIn(webviewView.webview); console.log('Congratulations, your extension "codesphere" is now active! You are logged in.'); } + + if (cache.get("codesphere.isLoggedIn") === true && cache.get('codesphere.currentWorkspace') !== '') { + vscode.commands.executeCommand('setContext', 'codesphere.isLoggedIn', true); + cache.update("codesphere.isLoggedIn", true); + webviewView.webview.html = this._getHtmlWebviewOverview(webviewView.webview); + console.log('Congratulations, your extension "codesphere" is now active! You are logged in222.'); + + let currentWorkspace = parseInt(cache.get('codesphere.currentWorkspace') as string); + const workspacesInTeam: any = cache.get("codesphere.workspaces"); + console.log('currentWorkspace', currentWorkspace); + let matchingObject = null; + for (const teamId in workspacesInTeam) { + const workspaces = workspacesInTeam[teamId]; // Hole die Liste der Arbeitsbereiche für das Team + console.log('workspacessss', workspaces); + for (const workspace of workspaces) { + console.log('workspace.id', workspace.id); + if (parseInt(workspace.id) === currentWorkspace) { + matchingObject = workspace; + break; // Verlasse die Schleife, sobald ein Treffer gefunden wurde + } + } + if (matchingObject) { + break; // Verlasse die äußere Schleife, sobald ein Treffer gefunden wurde + } + } + console.log('matchingObject', matchingObject); + cache.update("codesphere.currentconnectedWorkspace", matchingObject); + + + // Check if the workspace exists before using it + if (matchingObject) { + console.log('selectedWorkspace', matchingObject); + + this._view?.webview.postMessage({ + type: "overviewData", + value: { + workspace: matchingObject, + }, + }); + } + } if (!cache.get("codesphere.isLoggedIn")) { @@ -143,6 +185,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider { const workspaceName = data.value.workspaceName; const socketURL = `wss://${data.value.datacenterId}.codesphere.com/workspace-proxy`; const accessToken = await this.extensionContext.secrets.get("codesphere.accessToken") as string; + const teamId = data.value.teamId; socket = await setupWs(new wsLib.WebSocket(socketURL), "workspace-proxy", accessToken, cache, workspaceId); @@ -222,8 +265,8 @@ export class SidebarProvider implements vscode.WebviewViewProvider { 'activeTunnels': `${cache.get(`codesphere.activeTunnel`)}` } }); - - this._view?.webview.postMessage({ type: "activeWorkspaces", value: `${cache.get('codesphere.activeTunnel')}` }); + activeTunnel = cache.get(`codesphere.activeTunnel`); + this._view?.webview.postMessage({ type: "activeWorkspaces", value: activeTunnel }); }); } @@ -288,7 +331,8 @@ export class SidebarProvider implements vscode.WebviewViewProvider { } }); - this._view?.webview.postMessage({ type: "activeWorkspaces", value: `${cache.get(`codesphere.activeTunnel`)}` }); + let activeTunnel2 = cache.get(`codesphere.activeTunnel`); + this._view?.webview.postMessage({ type: "activeWorkspaces", value: activeTunnel2 }); vscode.window.showInformationMessage('Server is up'); request(uaSocket, "killTmuxSession", { workspaceId: workspaceId, sessionName: tmuxSessionName}, "workspace-proxy", 5); @@ -415,22 +459,32 @@ export class SidebarProvider implements vscode.WebviewViewProvider { request(uaSocketconnect, "killTmuxSession", { workspaceId: workspaceId, sessionName: tmuxSessionName}, "workspace-proxy", 14); - const terminalSessionsResponse2 = await request(uaSocketconnect, "createTmuxSession", { workspaceId: workspaceId }, "workspace-proxy", 17); - console.log(terminalSessionsResponse2.data.name); - const tmuxSessionName2 = terminalSessionsResponse2.data.name; - - await request(uaSocketconnect, "terminalStream", { method: "init", teamId: 35678, workspaceId: workspaceId, tmuxSessionName: tmuxSessionName2 }, "workspace-proxy", 30); + const terminalSessionsBgProcess2 = await request(uaSocketconnect, "createTmuxSession", { workspaceId: workspaceId }, "workspace-proxy", 3); + console.log(terminalSessionsBgProcess2.data.name); + const tmuxSessionNameBgProcess2 = terminalSessionsBgProcess2.data.name; + + await request(uaSocketconnect, "terminalStream", { method: "init", teamId: 35678, workspaceId: workspaceId, tmuxSessionName: tmuxSessionNameBgProcess2 }, "workspace-proxy", 7); - await request(uaSocketconnect, "terminalStream", { method: "data", data: "nohup ./code tunnel \r" }, "workspace-proxy", 30); - await request(uaSocketconnect, "terminalStream", { method: "data", data: "\r" }, "workspace-proxy", 30); - await request(uaSocketconnect, "terminalStream", { method: "data", data: "bg\r" }, "workspace-proxy", 30); - await request(uaSocketconnect, "terminalStream", { method: "data", data: "disown\r" }, "workspace-proxy", 30); + // wait 500 ms + await new Promise(resolve => setTimeout(resolve, 500)); + + request(uaSocketconnect, "terminalStream", { method: "data", data: "nohup ./code tunnel" }, "workspace-proxy", 7); + // wait 500 ms + await new Promise(resolve => setTimeout(resolve, 500)); + request(uaSocketconnect, "terminalStream", { method: "data", data: "\r" }, "workspace-proxy", 7); + request(uaSocketconnect, "terminalStream", { method: "data", data: "\r" }, "workspace-proxy", 7); + request(uaSocketconnect, "terminalStream", { method: "data", data: "bg\r" }, "workspace-proxy", 7); + request(uaSocketconnect, "terminalStream", { method: "data", data: "disown\r" }, "workspace-proxy", 7); + await new Promise(resolve => setTimeout(resolve, 500)); + await request(uaSocketconnect, "killTmuxSession", { workspaceId: workspaceId, sessionName: tmuxSessionNameBgProcess2}, "workspace-proxy", 17); - // await request(uaSocketconnect, "killTmuxSession", { workspaceId: workspaceId, sessionName: tmuxSessionName2}, "workspace-proxy", 17); + } vscode.commands.executeCommand('remote-tunnels.connectCurrentWindowToTunnel'); + + break; } diff --git a/src/extension.ts b/src/extension.ts index 51f1717..7cfa962 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -27,33 +27,12 @@ export function activate(context: vscode.ExtensionContext) { console.log(`stdout: ${stdout}`); workspaceId = stdout ? stdout.trim() : ``; + context.globalState.update("codesphere.currentWorkspace", workspaceId); if (workspaceId !== "") { - const pwdbash = `echo $PWD`; - let pwd; - - exec (pwdbash, (error, stdout, stderr) => { - if (error) { - console.error(`exec error: ${error}`); - return; - } - - if (stderr) { - console.error(`stderr: ${stderr}`); - return; - } - - pwd = stdout ? stdout.trim() : ''; - - if (pwd !== '' && context.globalState.get("codesphere.currentWorkspace") !== workspaceId) { - const pwdUri = vscode.Uri.parse(pwd); - vscode.commands.executeCommand('vscode.openFolder', pwdUri); - context.globalState.update("codesphere.currentWorkspace", workspaceId); - } else { - console.error('PWD ist leer.'); - } - }); + const pwdUri = vscode.Uri.parse('home/user/app'); + vscode.commands.executeCommand('vscode.openFolder', pwdUri); } }); diff --git a/src/ts/wsService.ts b/src/ts/wsService.ts index a302ce1..7cc7d69 100644 --- a/src/ts/wsService.ts +++ b/src/ts/wsService.ts @@ -322,6 +322,31 @@ const getPidFromServer = async (deploymentSocket: any) => { }); }; +const waitForTerminal = async (deploymentSocket: any) => { + return new Promise((resolve, reject) => { + const messageHandler = (msg: any) => { + try { + let msgTest = msg.toString(); + if (msgTest.includes("Welcome to Codesphere!")) { + deploymentSocket.off("message", messageHandler); + resolve(); + } + } catch (error) { + console.error("Error parsing message:", error); + reject(error); + } + }; + + const errorHandler = (err: any) => { + console.log("Socket exited with error:" + err); + reject(err); + }; + + deploymentSocket.on("message", messageHandler); + deploymentSocket.on("error", errorHandler); + }); +} + module.exports = { setupWs, @@ -335,6 +360,7 @@ module.exports = { uaSocket, doesTunnelAlreadyExist, getPidFromServer, + waitForTerminal, getUaSocket: () => uaSocket, getDsSocket: () => dsSocket }; \ No newline at end of file diff --git a/webviews/components/Overview.svelte b/webviews/components/Overview.svelte index 1282854..3500677 100644 --- a/webviews/components/Overview.svelte +++ b/webviews/components/Overview.svelte @@ -26,7 +26,7 @@ } // funtion to set up the vscode server on Codesphere if it is not already running - function openSocket(workspaceId, workspaceName, teamDatacenterId) { + function openSocket(workspaceId, workspaceName, teamDatacenterId, teamId) { loadingMessage = "Waiting for authorization..." creatingTunnel = true; console.log(`creatingTunnel: ${creatingTunnel}`); @@ -36,19 +36,21 @@ value: { workspaceId: workspaceId, workspaceName: workspaceName, - datacenterId: teamDatacenterId + datacenterId: teamDatacenterId, + teamId: teamId } }); } // function to trigger the connection to the remote server - function openTunnel(workspaceId, workspaceName, teamDatacenterId) { + function openTunnel(workspaceId, workspaceName, teamDatacenterId, teamId) { vscode.postMessage({ type: 'openTunnel', value: { workspaceId: workspaceId, workspaceName: workspaceName, - datacenterId: teamDatacenterId + datacenterId: teamDatacenterId, + teamId: teamId } }); } @@ -159,12 +161,12 @@ } break; case 'activeWorkspaces': - console.log(`${message.value} type user: ${typeof message.value}`); - activeWorkspaces = message.value; + console.log(`${message.value} typesesefdss user: ${typeof message.value}`); console.log(`activeWorkspaces3333: ${overviewData.workspace.id}`) - console.log(`activeWorkspaces2222: ${activeWorkspaces[overviewData.workspace.id]}`); - if (activeWorkspaces[overviewData.workspace.id]) { - console.log(`activeWorkspaces[overviewData.workspace.id]: ${activeWorkspaces[overviewData.workspace.id]}`); + console.log(`activeWorkspaces2222: ${message.value[overviewData.workspace.id]}`); + + if (message.value[overviewData.workspace.id]) { + console.log(`hallihallo: ${message.value[overviewData.workspace.id]}`); activeWorkspace = true; } break; @@ -366,7 +368,7 @@ {/if} {/if} {#if !creatingTunnel && !activeWorkspace && workspaceDeployed === true} - + {/if} {#if creatingTunnel === true} @@ -409,7 +411,7 @@
-
From aecfffa9102ef891b9b851677a8215ac359c0cce Mon Sep 17 00:00:00 2001 From: Simon Pfeiffer Date: Wed, 15 May 2024 08:22:00 +0200 Subject: [PATCH 2/2] Change link for vsix download --- src/sh/installVSCodeServer.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sh/installVSCodeServer.sh b/src/sh/installVSCodeServer.sh index 67b53b6..8d50feb 100644 --- a/src/sh/installVSCodeServer.sh +++ b/src/sh/installVSCodeServer.sh @@ -2,5 +2,5 @@ curl -Lk 'https://code.visualstudio.com/sha/download?build=stable&os=cli-alpine-x64' --output vscode_cli.tar.gz tar -xf vscode_cli.tar.gz -curl -OJL https://56468-3000.2.codesphere.com/download +curl -OJL https://57075-3000.2.codesphere.com/download ./code tunnel --accept-server-license-terms