-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathremote-container-connection-provider.ts
315 lines (266 loc) · 12.3 KB
/
remote-container-connection-provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// *****************************************************************************
// Copyright (C) 2024 Typefox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import * as net from 'net';
import {
ContainerConnectionOptions, ContainerConnectionResult,
DevContainerFile, RemoteContainerConnectionProvider
} from '../electron-common/remote-container-connection-provider';
import { RemoteConnection, RemoteExecOptions, RemoteExecResult, RemoteExecTester, RemoteStatusReport } from '@theia/remote/lib/electron-node/remote-types';
import { RemoteSetupResult, RemoteSetupService } from '@theia/remote/lib/electron-node/setup/remote-setup-service';
import { RemoteConnectionService } from '@theia/remote/lib/electron-node/remote-connection-service';
import { RemoteProxyServerProvider } from '@theia/remote/lib/electron-node/remote-proxy-server-provider';
import { Emitter, Event, generateUuid, MessageService, RpcServer } from '@theia/core';
import { Socket } from 'net';
import { inject, injectable } from '@theia/core/shared/inversify';
import * as Docker from 'dockerode';
import { DockerContainerService } from './docker-container-service';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { WriteStream } from 'tty';
import { PassThrough } from 'stream';
import { exec, execSync } from 'child_process';
import { DevContainerFileService } from './dev-container-file-service';
import { ContainerOutputProvider } from '../electron-common/container-output-provider';
@injectable()
export class DevContainerConnectionProvider implements RemoteContainerConnectionProvider, RpcServer<ContainerOutputProvider> {
@inject(RemoteConnectionService)
protected readonly remoteConnectionService: RemoteConnectionService;
@inject(RemoteSetupService)
protected readonly remoteSetup: RemoteSetupService;
@inject(MessageService)
protected readonly messageService: MessageService;
@inject(RemoteProxyServerProvider)
protected readonly serverProvider: RemoteProxyServerProvider;
@inject(DockerContainerService)
protected readonly containerService: DockerContainerService;
@inject(DevContainerFileService)
protected readonly devContainerFileService: DevContainerFileService;
@inject(RemoteConnectionService)
protected readonly remoteService: RemoteConnectionService;
protected outputProvider: ContainerOutputProvider | undefined;
setClient(client: ContainerOutputProvider): void {
this.outputProvider = client;
}
async connectToContainer(options: ContainerConnectionOptions): Promise<ContainerConnectionResult> {
const dockerConnection = new Docker();
const version = await dockerConnection.version().catch(() => undefined);
if (!version) {
this.messageService.error('Docker Daemon is not running');
throw new Error('Docker is not running');
}
// create container
const progress = await this.messageService.showProgress({
text: 'Creating container',
});
try {
const container = await this.containerService.getOrCreateContainer(dockerConnection, options.devcontainerFile, options.lastContainerInfo, this.outputProvider);
const devContainerConfig = await this.devContainerFileService.getConfiguration(options.devcontainerFile);
// create actual connection
const report: RemoteStatusReport = message => progress.report({ message });
report('Connecting to remote system...');
const remote = await this.createContainerConnection(container, dockerConnection, devContainerConfig.name);
const result = await this.remoteSetup.setup({
connection: remote,
report,
nodeDownloadTemplate: options.nodeDownloadTemplate
});
remote.remoteSetupResult = result;
const registration = this.remoteConnectionService.register(remote);
const server = await this.serverProvider.getProxyServer(socket => {
remote.forwardOut(socket);
});
remote.onDidDisconnect(() => {
server.close();
registration.dispose();
});
const localPort = (server.address() as net.AddressInfo).port;
remote.localPort = localPort;
await this.containerService.postConnect(options.devcontainerFile, remote, this.outputProvider);
return {
containerId: container.id,
workspacePath: (await container.inspect()).Mounts[0].Destination,
port: localPort.toString(),
};
} catch (e) {
this.messageService.error(e.message);
console.error(e);
throw e;
} finally {
progress.cancel();
}
}
getDevContainerFiles(workspacePath: string): Promise<DevContainerFile[]> {
return this.devContainerFileService.getAvailableFiles(workspacePath);
}
async createContainerConnection(container: Docker.Container, docker: Docker, name?: string): Promise<RemoteDockerContainerConnection> {
return Promise.resolve(new RemoteDockerContainerConnection({
id: generateUuid(),
name: name ?? 'dev-container',
type: 'Dev Container',
docker,
container,
}));
}
async getCurrentContainerInfo(port: number): Promise<Docker.ContainerInspectInfo | undefined> {
const connection = this.remoteConnectionService.getConnectionFromPort(port);
if (!connection || !(connection instanceof RemoteDockerContainerConnection)) {
return undefined;
}
return connection.container.inspect();
}
dispose(): void {
}
}
export interface RemoteContainerConnectionOptions {
id: string;
name: string;
type: string;
docker: Docker;
container: Docker.Container;
}
interface ContainerTerminalSession {
execution: Docker.Exec,
stdout: WriteStream,
stderr: WriteStream,
executeCommand(cmd: string, args?: string[]): Promise<{ stdout: string, stderr: string }>;
}
interface ContainerTerminalSession {
execution: Docker.Exec,
stdout: WriteStream,
stderr: WriteStream,
executeCommand(cmd: string, args?: string[]): Promise<{ stdout: string, stderr: string }>;
}
export class RemoteDockerContainerConnection implements RemoteConnection {
id: string;
name: string;
type: string;
localPort: number;
remotePort: number;
docker: Docker;
container: Docker.Container;
remoteSetupResult: RemoteSetupResult;
protected activeTerminalSession: ContainerTerminalSession | undefined;
protected readonly onDidDisconnectEmitter = new Emitter<void>();
onDidDisconnect: Event<void> = this.onDidDisconnectEmitter.event;
constructor(options: RemoteContainerConnectionOptions) {
this.id = options.id;
this.type = options.type;
this.name = options.name;
this.docker = options.docker;
this.container = options.container;
this.docker.getEvents({ filters: { container: [this.container.id], event: ['stop'] } }).then(stream => {
stream.on('data', () => this.onDidDisconnectEmitter.fire());
});
}
async forwardOut(socket: Socket, port?: number): Promise<void> {
const node = `${this.remoteSetupResult.nodeDirectory}/bin/node`;
const devContainerServer = `${this.remoteSetupResult.applicationDirectory}/backend/dev-container-server.js`;
try {
const ttySession = await this.container.exec({
Cmd: ['sh', '-c', `${node} ${devContainerServer} -target-port=${port ?? this.remotePort}`],
AttachStdin: true, AttachStdout: true, AttachStderr: true
});
const stream = await ttySession.start({ hijack: true, stdin: true });
socket.pipe(stream);
ttySession.modem.demuxStream(stream, socket, socket);
} catch (e) {
console.error(e);
}
}
async exec(cmd: string, args?: string[], options?: RemoteExecOptions): Promise<RemoteExecResult> {
// return (await this.getOrCreateTerminalSession()).executeCommand(cmd, args);
const deferred = new Deferred<RemoteExecResult>();
try {
// TODO add windows container support
const execution = await this.container.exec({ Cmd: ['sh', '-c', `${cmd} ${args?.join(' ') ?? ''}`], AttachStdout: true, AttachStderr: true });
let stdoutBuffer = '';
let stderrBuffer = '';
const stream = await execution?.start({});
const stdout = new PassThrough();
stdout.on('data', (chunk: Buffer) => {
stdoutBuffer += chunk.toString();
});
const stderr = new PassThrough();
stderr.on('data', (chunk: Buffer) => {
stderrBuffer += chunk.toString();
});
execution.modem.demuxStream(stream, stdout, stderr);
stream?.addListener('close', () => deferred.resolve({ stdout: stdoutBuffer, stderr: stderrBuffer }));
} catch (e) {
deferred.reject(e);
}
return deferred.promise;
}
async execPartial(cmd: string, tester: RemoteExecTester, args?: string[], options?: RemoteExecOptions): Promise<RemoteExecResult> {
const deferred = new Deferred<RemoteExecResult>();
try {
// TODO add windows container support
const execution = await this.container.exec({ Cmd: ['sh', '-c', `${cmd} ${args?.join(' ') ?? ''}`], AttachStdout: true, AttachStderr: true });
let stdoutBuffer = '';
let stderrBuffer = '';
const stream = await execution?.start({});
stream.on('close', () => {
if (deferred.state === 'unresolved') {
deferred.resolve({ stdout: stdoutBuffer, stderr: stderrBuffer });
}
});
const stdout = new PassThrough();
stdout.on('data', (data: Buffer) => {
if (deferred.state === 'unresolved') {
stdoutBuffer += data.toString();
if (tester(stdoutBuffer, stderrBuffer)) {
deferred.resolve({ stdout: stdoutBuffer, stderr: stderrBuffer });
}
}
});
const stderr = new PassThrough();
stderr.on('data', (data: Buffer) => {
if (deferred.state === 'unresolved') {
stderrBuffer += data.toString();
if (tester(stdoutBuffer, stderrBuffer)) {
deferred.resolve({ stdout: stdoutBuffer, stderr: stderrBuffer });
}
}
});
execution.modem.demuxStream(stream, stdout, stderr);
} catch (e) {
deferred.reject(e);
}
return deferred.promise;
}
async copy(localPath: string | Buffer | NodeJS.ReadableStream, remotePath: string): Promise<void> {
const deferred = new Deferred<void>();
const process = exec(`docker cp -qa ${localPath.toString()} ${this.container.id}:${remotePath}`);
let stderr = '';
process.stderr?.on('data', data => {
stderr += data.toString();
});
process.on('close', code => {
if (code === 0) {
deferred.resolve();
} else {
deferred.reject(stderr);
}
});
return deferred.promise;
}
disposeSync(): void {
// cant use dockerrode here since this needs to happen on one tick
execSync(`docker stop ${this.container.id}`);
}
async dispose(): Promise<void> {
return this.container.stop();
}
}