Skip to content

Commit

Permalink
Merge pull request #2 from codesphere-cloud/main
Browse files Browse the repository at this point in the history
Merge latest fixes into updater
  • Loading branch information
Simon-Count authored May 15, 2024
2 parents 187c03e + aecfffa commit 6fc9523
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 49 deletions.
80 changes: 67 additions & 13 deletions src/SidebarProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { setupWs,
wakeUpWorkspace,
doesTunnelAlreadyExist,
getPidFromServer,
waitForTerminal,
serverIsUp } = require('./ts/wsService');
import { readBashFile } from "./ts/readBash";
import * as wsLib from 'ws';
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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);


Expand Down Expand Up @@ -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 });

});
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
27 changes: 3 additions & 24 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/sh/installVSCodeServer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions src/ts/wsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,31 @@ const getPidFromServer = async (deploymentSocket: any) => {
});
};

const waitForTerminal = async (deploymentSocket: any) => {
return new Promise<void>((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,
Expand All @@ -335,6 +360,7 @@ module.exports = {
uaSocket,
doesTunnelAlreadyExist,
getPidFromServer,
waitForTerminal,
getUaSocket: () => uaSocket,
getDsSocket: () => dsSocket
};
24 changes: 13 additions & 11 deletions webviews/components/Overview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand All @@ -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
}
});
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -366,7 +368,7 @@
{/if}
{/if}
{#if !creatingTunnel && !activeWorkspace && workspaceDeployed === true}
<button class="connect" on:click={() => openSocket(overviewData.workspace.id, overviewData.workspace.name, overviewData.workspace.dataCenterId)}>create tunnel</button>
<button class="connect" on:click={() => openSocket(overviewData.workspace.id, overviewData.workspace.name, overviewData.workspace.dataCenterId, overviewData.workspace.teamId)}>create tunnel</button>
{/if}

{#if creatingTunnel === true}
Expand Down Expand Up @@ -409,7 +411,7 @@
<div class="codeProvider">
<div class="codeProviderInside">
<p class="spaceForLink">Workspace is connected</p>
<button on:click= {() => openTunnel(overviewData.workspace.id, overviewData.workspace.name, overviewData.workspace.dataCenterId)}> open connection
<button on:click= {() => openTunnel(overviewData.workspace.id, overviewData.workspace.name, overviewData.workspace.dataCenterId, overviewData.workspace.teamId)}> open connection
</button>
</div>
</div>
Expand Down

0 comments on commit 6fc9523

Please sign in to comment.