Skip to content

Improve ports #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"analytics-node": "^6.2.0",
"configcat-node": "^8.0.0",
"js-yaml": "^4.1.0",
"jsonc-parser": "^3.2.0",
"node-fetch": "2.6.7",
"pkce-challenge": "^3.0.0",
"semver": "^7.3.7",
Expand Down
69 changes: 69 additions & 0 deletions src/remoteConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { AutoTunnelRequest, ResolveSSHConnectionRequest, ResolveSSHConnectionResponse } from '@gitpod/local-app-api-grpcweb/lib/localapp_pb';
import { LocalAppClient } from '@gitpod/local-app-api-grpcweb/lib/localapp_pb_service';
import { WorkspaceConfig, PortConfig, PortOnOpen } from '@gitpod/gitpod-protocol/lib/protocol';
import { NodeHttpTransport } from '@improbable-eng/grpc-web-node-http-transport';
import { grpc } from '@improbable-eng/grpc-web';
import * as cp from 'child_process';
Expand All @@ -18,6 +19,7 @@ import { ParsedKey } from 'ssh2-streams';
import * as tmp from 'tmp';
import * as path from 'path';
import * as vscode from 'vscode';
import { parse as parseJson, modify as modifyJson, applyEdits as applyEditsJson } from 'jsonc-parser';
import Log from './common/logger';
import { Disposable } from './common/dispose';
import { withServerApi } from './internalApi';
Expand Down Expand Up @@ -1028,6 +1030,71 @@ export default class RemoteConnector extends Disposable {
}
}

private async configureMachineSettings(wsConfig: WorkspaceConfig) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not this logic implemented in remote extension? It has access to machine setitngs, supervisor and VS Code api. I don't see a point to implement in local extension.

// Let's reuse the `__gitpod.getGitpodRemoteLogsUri` command to get the vscode-server folder
let extRemoteLogsUri: vscode.Uri | undefined;
try {
extRemoteLogsUri = await retry(async () => {
return await vscode.commands.executeCommand('__gitpod.getGitpodRemoteLogsUri');
}, 3000, 15);
} catch {
}

if (!extRemoteLogsUri) {
return;
}

const remoteUserDataPath = path.posix.dirname(path.posix.dirname(path.posix.dirname(path.posix.dirname(extRemoteLogsUri.path))));
const machineSettingsResource = extRemoteLogsUri.with({ path: path.posix.join(remoteUserDataPath, 'Machine', 'settings.json') });
try {
let settingsStr: string = '{}';
const fileExists = await vscode.workspace.fs.stat(machineSettingsResource).then(() => true, () => false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not we use document API to respect dirty changes from a user?

if (fileExists) {
const settingsbuffer = await vscode.workspace.fs.readFile(machineSettingsResource);
settingsStr = new TextDecoder().decode(settingsbuffer);
}

// settings.json is a json with comments file so we use jsonc-parser library
const settingsJson = parseJson(settingsStr);
if (settingsJson['remote.autoForwardPortsSource'] === undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is necessary if we are listening to supervisor changes and call asExternalUri. Less VS Code needs to scan is better.

const edits = modifyJson(settingsStr, ['remote.autoForwardPortsSource'], 'process', { formattingOptions: { insertSpaces: true, tabSize: 4, insertFinalNewline: true } });
settingsStr = applyEditsJson(settingsStr, edits);
}
if (settingsJson['remote.portsAttributes'] === undefined && wsConfig.ports?.length) {
const mapOnOpen = (onOpen?: PortOnOpen)=>{
switch (onOpen) {
case 'open-browser': return 'openBrowser';
case 'open-preview': return 'openPreview';
default: return onOpen;
}
};

const portsAttributes: any = {};
for (const portConfig of wsConfig.ports) {
portsAttributes[portConfig.port] = {
label: portConfig.name,
onAutoForward: mapOnOpen(portConfig.onOpen)
};
}
const edits = modifyJson(settingsStr, ['remote.portsAttributes'], portsAttributes, { formattingOptions: { insertSpaces: true, tabSize: 4, insertFinalNewline: true } });
settingsStr = applyEditsJson(settingsStr, edits);
}

const settingsbuffer = new TextEncoder().encode(settingsStr);
await vscode.workspace.fs.writeFile(machineSettingsResource, settingsbuffer);
} catch (e) {
this.logger.error(`Could not update ${machineSettingsResource.toString()} resource`, e);
}
}

private async tunnelPortsFromConfig(portsConfig: PortConfig[]) {
for (const portConfig of portsConfig) {
if (portConfig.onOpen !== 'ignore') {
await vscode.env.asExternalUri(vscode.Uri.parse(`http://localhost:${portConfig.port}`));
}
}
}

private async onGitpodRemoteConnection() {
const remoteUri = vscode.workspace.workspaceFile || vscode.workspace.workspaceFolders?.[0].uri;
if (!remoteUri) {
Expand Down Expand Up @@ -1057,6 +1124,8 @@ export default class RemoteConnector extends Disposable {

await this.context.globalState.update(`${RemoteConnector.SSH_DEST_KEY}${sshDestStr}`, { ...connectionInfo, isFirstConnection: false });

this.configureMachineSettings(workspaceInfo.workspace.config).then(() => this.tunnelPortsFromConfig(workspaceInfo.workspace.config.ports ?? []));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config.ports are not real ports, they are intitial configuration for supervisor.

We should observe port status endpoint from supervisor and reconcile it with VS Code as here: https://github.com/gitpod-io/openvscode-server/blob/849af772ba8e6a3a2944ccaf004d70cb549d3f3e/extensions/gitpod-web/src/extension.ts#L551-L592


const gitpodVersion = await getGitpodVersion(connectionInfo.gitpodHost, this.logger);

const heartbeatSupported = session.scopes.includes(ScopeFeature.LocalHeartbeat);
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,11 @@ json-stable-stringify-without-jsonify@^1.0.1:
resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651"
integrity sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=

jsonc-parser@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-3.2.0.tgz#31ff3f4c2b9793f89c67212627c51c6394f88e76"
integrity sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==

kind-of@^6.0.2:
version "6.0.3"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.3.tgz#07c05034a6c349fa06e24fa35aa76db4580ce4dd"
Expand Down