-
Notifications
You must be signed in to change notification settings - Fork 15
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
Improve ports #24
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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'; | ||
|
@@ -1028,6 +1030,71 @@ export default class RemoteConnector extends Disposable { | |
} | ||
} | ||
|
||
private async configureMachineSettings(wsConfig: WorkspaceConfig) { | ||
// 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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 ?? [])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
There was a problem hiding this comment.
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.