@theia/dev-container 1.48.0

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