@theia/dev-container 1.46.0-next.241

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 (92) hide show
  1. package/README.md +44 -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 +28 -0
  7. package/lib/electron-browser/container-connection-contribution.d.ts.map +1 -0
  8. package/lib/electron-browser/container-connection-contribution.js +147 -0
  9. package/lib/electron-browser/container-connection-contribution.js.map +1 -0
  10. package/lib/electron-browser/container-info-contribution.d.ts +11 -0
  11. package/lib/electron-browser/container-info-contribution.d.ts.map +1 -0
  12. package/lib/electron-browser/container-info-contribution.js +51 -0
  13. package/lib/electron-browser/container-info-contribution.js.map +1 -0
  14. package/lib/electron-browser/container-output-provider.d.ts +8 -0
  15. package/lib/electron-browser/container-output-provider.d.ts.map +1 -0
  16. package/lib/electron-browser/container-output-provider.js +41 -0
  17. package/lib/electron-browser/container-output-provider.js.map +1 -0
  18. package/lib/electron-browser/dev-container-frontend-module.d.ts +4 -0
  19. package/lib/electron-browser/dev-container-frontend-module.d.ts.map +1 -0
  20. package/lib/electron-browser/dev-container-frontend-module.js +39 -0
  21. package/lib/electron-browser/dev-container-frontend-module.js.map +1 -0
  22. package/lib/electron-common/container-output-provider.d.ts +4 -0
  23. package/lib/electron-common/container-output-provider.d.ts.map +1 -0
  24. package/lib/electron-common/container-output-provider.js +18 -0
  25. package/lib/electron-common/container-output-provider.js.map +1 -0
  26. package/lib/electron-common/dev-container-workspaces.d.ts +3 -0
  27. package/lib/electron-common/dev-container-workspaces.d.ts.map +1 -0
  28. package/lib/electron-common/dev-container-workspaces.js +21 -0
  29. package/lib/electron-common/dev-container-workspaces.js.map +1 -0
  30. package/lib/electron-common/remote-container-connection-provider.d.ts +30 -0
  31. package/lib/electron-common/remote-container-connection-provider.d.ts.map +1 -0
  32. package/lib/electron-common/remote-container-connection-provider.js +21 -0
  33. package/lib/electron-common/remote-container-connection-provider.js.map +1 -0
  34. package/lib/electron-node/dev-container-backend-module.d.ts +5 -0
  35. package/lib/electron-node/dev-container-backend-module.d.ts.map +1 -0
  36. package/lib/electron-node/dev-container-backend-module.js +58 -0
  37. package/lib/electron-node/dev-container-backend-module.js.map +1 -0
  38. package/lib/electron-node/dev-container-file-service.d.ts +10 -0
  39. package/lib/electron-node/dev-container-file-service.d.ts.map +1 -0
  40. package/lib/electron-node/dev-container-file-service.js +69 -0
  41. package/lib/electron-node/dev-container-file-service.js.map +1 -0
  42. package/lib/electron-node/dev-container-workspace-handler.d.ts +7 -0
  43. package/lib/electron-node/dev-container-workspace-handler.d.ts.map +1 -0
  44. package/lib/electron-node/dev-container-workspace-handler.js +21 -0
  45. package/lib/electron-node/dev-container-workspace-handler.js.map +1 -0
  46. package/lib/electron-node/devcontainer-contributions/cli-enhancing-creation-contributions.d.ts +17 -0
  47. package/lib/electron-node/devcontainer-contributions/cli-enhancing-creation-contributions.d.ts.map +1 -0
  48. package/lib/electron-node/devcontainer-contributions/cli-enhancing-creation-contributions.js +69 -0
  49. package/lib/electron-node/devcontainer-contributions/cli-enhancing-creation-contributions.js.map +1 -0
  50. package/lib/electron-node/devcontainer-contributions/main-container-creation-contributions.d.ts +38 -0
  51. package/lib/electron-node/devcontainer-contributions/main-container-creation-contributions.d.ts.map +1 -0
  52. package/lib/electron-node/devcontainer-contributions/main-container-creation-contributions.js +192 -0
  53. package/lib/electron-node/devcontainer-contributions/main-container-creation-contributions.js.map +1 -0
  54. package/lib/electron-node/devcontainer-contributions/profile-file-modification-contribution.d.ts +11 -0
  55. package/lib/electron-node/devcontainer-contributions/profile-file-modification-contribution.d.ts.map +1 -0
  56. package/lib/electron-node/devcontainer-contributions/profile-file-modification-contribution.js +37 -0
  57. package/lib/electron-node/devcontainer-contributions/profile-file-modification-contribution.js.map +1 -0
  58. package/lib/electron-node/devcontainer-file.d.ts +350 -0
  59. package/lib/electron-node/devcontainer-file.d.ts.map +1 -0
  60. package/lib/electron-node/devcontainer-file.js +18 -0
  61. package/lib/electron-node/devcontainer-file.js.map +1 -0
  62. package/lib/electron-node/docker-container-service.d.ts +29 -0
  63. package/lib/electron-node/docker-container-service.d.ts.map +1 -0
  64. package/lib/electron-node/docker-container-service.js +106 -0
  65. package/lib/electron-node/docker-container-service.js.map +1 -0
  66. package/lib/electron-node/remote-container-connection-provider.d.ts +79 -0
  67. package/lib/electron-node/remote-container-connection-provider.d.ts.map +1 -0
  68. package/lib/electron-node/remote-container-connection-provider.js +260 -0
  69. package/lib/electron-node/remote-container-connection-provider.js.map +1 -0
  70. package/lib/package.spec.d.ts +1 -0
  71. package/lib/package.spec.d.ts.map +1 -0
  72. package/lib/package.spec.js +26 -0
  73. package/lib/package.spec.js.map +1 -0
  74. package/package.json +55 -0
  75. package/src/dev-container-server/dev-container-server.ts +53 -0
  76. package/src/electron-browser/container-connection-contribution.ts +151 -0
  77. package/src/electron-browser/container-info-contribution.ts +47 -0
  78. package/src/electron-browser/container-output-provider.ts +36 -0
  79. package/src/electron-browser/dev-container-frontend-module.ts +40 -0
  80. package/src/electron-common/container-output-provider.ts +19 -0
  81. package/src/electron-common/dev-container-workspaces.ts +18 -0
  82. package/src/electron-common/remote-container-connection-provider.ts +52 -0
  83. package/src/electron-node/dev-container-backend-module.ts +63 -0
  84. package/src/electron-node/dev-container-file-service.ts +67 -0
  85. package/src/electron-node/dev-container-workspace-handler.ts +33 -0
  86. package/src/electron-node/devcontainer-contributions/cli-enhancing-creation-contributions.ts +68 -0
  87. package/src/electron-node/devcontainer-contributions/main-container-creation-contributions.ts +196 -0
  88. package/src/electron-node/devcontainer-contributions/profile-file-modification-contribution.ts +35 -0
  89. package/src/electron-node/devcontainer-file.ts +411 -0
  90. package/src/electron-node/docker-container-service.ts +131 -0
  91. package/src/electron-node/remote-container-connection-provider.ts +315 -0
  92. package/src/package.spec.ts +28 -0
@@ -0,0 +1,315 @@
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, execSync } 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
+ @inject(RemoteConnectionService)
60
+ protected readonly remoteService: RemoteConnectionService;
61
+
62
+ protected outputProvider: ContainerOutputProvider | undefined;
63
+
64
+ setClient(client: ContainerOutputProvider): void {
65
+ this.outputProvider = client;
66
+ }
67
+
68
+ async connectToContainer(options: ContainerConnectionOptions): Promise<ContainerConnectionResult> {
69
+ const dockerConnection = new Docker();
70
+ const version = await dockerConnection.version().catch(() => undefined);
71
+
72
+ if (!version) {
73
+ this.messageService.error('Docker Daemon is not running');
74
+ throw new Error('Docker is not running');
75
+ }
76
+
77
+ // create container
78
+ const progress = await this.messageService.showProgress({
79
+ text: 'Creating container',
80
+ });
81
+ try {
82
+ const container = await this.containerService.getOrCreateContainer(dockerConnection, options, this.outputProvider);
83
+ const devContainerConfig = await this.devContainerFileService.getConfiguration(options.devcontainerFile);
84
+
85
+ // create actual connection
86
+ const report: RemoteStatusReport = message => progress.report({ message });
87
+ report('Connecting to remote system...');
88
+
89
+ const remote = await this.createContainerConnection(container, dockerConnection, devContainerConfig.name);
90
+ const result = await this.remoteSetup.setup({
91
+ connection: remote,
92
+ report,
93
+ nodeDownloadTemplate: options.nodeDownloadTemplate
94
+ });
95
+ remote.remoteSetupResult = result;
96
+
97
+ const registration = this.remoteConnectionService.register(remote);
98
+ const server = await this.serverProvider.getProxyServer(socket => {
99
+ remote.forwardOut(socket);
100
+ });
101
+ remote.onDidDisconnect(() => {
102
+ server.close();
103
+ registration.dispose();
104
+ });
105
+ const localPort = (server.address() as net.AddressInfo).port;
106
+ remote.localPort = localPort;
107
+
108
+ await this.containerService.postConnect(options.devcontainerFile, remote, this.outputProvider);
109
+
110
+ return {
111
+ containerId: container.id,
112
+ workspacePath: (await container.inspect()).Mounts[0].Destination,
113
+ port: localPort.toString(),
114
+ };
115
+ } catch (e) {
116
+ this.messageService.error(e.message);
117
+ console.error(e);
118
+ throw e;
119
+ } finally {
120
+ progress.cancel();
121
+ }
122
+ }
123
+
124
+ getDevContainerFiles(workspacePath: string): Promise<DevContainerFile[]> {
125
+ return this.devContainerFileService.getAvailableFiles(workspacePath);
126
+ }
127
+
128
+ async createContainerConnection(container: Docker.Container, docker: Docker, name?: string): Promise<RemoteDockerContainerConnection> {
129
+ return Promise.resolve(new RemoteDockerContainerConnection({
130
+ id: generateUuid(),
131
+ name: name ?? 'dev-container',
132
+ type: 'Dev Container',
133
+ docker,
134
+ container,
135
+ }));
136
+ }
137
+
138
+ async getCurrentContainerInfo(port: number): Promise<Docker.ContainerInspectInfo | undefined> {
139
+ const connection = this.remoteConnectionService.getConnectionFromPort(port);
140
+ if (!connection || !(connection instanceof RemoteDockerContainerConnection)) {
141
+ return undefined;
142
+ }
143
+ return connection.container.inspect();
144
+ }
145
+
146
+ dispose(): void {
147
+
148
+ }
149
+
150
+ }
151
+
152
+ export interface RemoteContainerConnectionOptions {
153
+ id: string;
154
+ name: string;
155
+ type: string;
156
+ docker: Docker;
157
+ container: Docker.Container;
158
+ }
159
+
160
+ interface ContainerTerminalSession {
161
+ execution: Docker.Exec,
162
+ stdout: WriteStream,
163
+ stderr: WriteStream,
164
+ executeCommand(cmd: string, args?: string[]): Promise<{ stdout: string, stderr: string }>;
165
+ }
166
+
167
+ interface ContainerTerminalSession {
168
+ execution: Docker.Exec,
169
+ stdout: WriteStream,
170
+ stderr: WriteStream,
171
+ executeCommand(cmd: string, args?: string[]): Promise<{ stdout: string, stderr: string }>;
172
+ }
173
+
174
+ export class RemoteDockerContainerConnection implements RemoteConnection {
175
+
176
+ id: string;
177
+ name: string;
178
+ type: string;
179
+ localPort: number;
180
+ remotePort: number;
181
+
182
+ docker: Docker;
183
+ container: Docker.Container;
184
+
185
+ remoteSetupResult: RemoteSetupResult;
186
+
187
+ protected activeTerminalSession: ContainerTerminalSession | undefined;
188
+
189
+ protected readonly onDidDisconnectEmitter = new Emitter<void>();
190
+ onDidDisconnect: Event<void> = this.onDidDisconnectEmitter.event;
191
+
192
+ constructor(options: RemoteContainerConnectionOptions) {
193
+ this.id = options.id;
194
+ this.type = options.type;
195
+ this.name = options.name;
196
+
197
+ this.docker = options.docker;
198
+ this.container = options.container;
199
+
200
+ this.docker.getEvents({ filters: { container: [this.container.id], event: ['stop'] } }).then(stream => {
201
+ stream.on('data', () => this.onDidDisconnectEmitter.fire());
202
+ });
203
+ }
204
+
205
+ async forwardOut(socket: Socket, port?: number): Promise<void> {
206
+ const node = `${this.remoteSetupResult.nodeDirectory}/bin/node`;
207
+ const devContainerServer = `${this.remoteSetupResult.applicationDirectory}/backend/dev-container-server.js`;
208
+ try {
209
+ const ttySession = await this.container.exec({
210
+ Cmd: ['sh', '-c', `${node} ${devContainerServer} -target-port=${port ?? this.remotePort}`],
211
+ AttachStdin: true, AttachStdout: true, AttachStderr: true
212
+ });
213
+
214
+ const stream = await ttySession.start({ hijack: true, stdin: true });
215
+
216
+ socket.pipe(stream);
217
+ ttySession.modem.demuxStream(stream, socket, socket);
218
+ } catch (e) {
219
+ console.error(e);
220
+ }
221
+ }
222
+
223
+ async exec(cmd: string, args?: string[], options?: RemoteExecOptions): Promise<RemoteExecResult> {
224
+ // return (await this.getOrCreateTerminalSession()).executeCommand(cmd, args);
225
+ const deferred = new Deferred<RemoteExecResult>();
226
+ try {
227
+ // TODO add windows container support
228
+ const execution = await this.container.exec({ Cmd: ['sh', '-c', `${cmd} ${args?.join(' ') ?? ''}`], AttachStdout: true, AttachStderr: true });
229
+ let stdoutBuffer = '';
230
+ let stderrBuffer = '';
231
+ const stream = await execution?.start({});
232
+ const stdout = new PassThrough();
233
+ stdout.on('data', (chunk: Buffer) => {
234
+ stdoutBuffer += chunk.toString();
235
+ });
236
+ const stderr = new PassThrough();
237
+ stderr.on('data', (chunk: Buffer) => {
238
+ stderrBuffer += chunk.toString();
239
+ });
240
+ execution.modem.demuxStream(stream, stdout, stderr);
241
+ stream?.addListener('close', () => deferred.resolve({ stdout: stdoutBuffer, stderr: stderrBuffer }));
242
+ } catch (e) {
243
+ deferred.reject(e);
244
+ }
245
+ return deferred.promise;
246
+ }
247
+
248
+ async execPartial(cmd: string, tester: RemoteExecTester, args?: string[], options?: RemoteExecOptions): Promise<RemoteExecResult> {
249
+ const deferred = new Deferred<RemoteExecResult>();
250
+ try {
251
+ // TODO add windows container support
252
+ const execution = await this.container.exec({ Cmd: ['sh', '-c', `${cmd} ${args?.join(' ') ?? ''}`], AttachStdout: true, AttachStderr: true });
253
+ let stdoutBuffer = '';
254
+ let stderrBuffer = '';
255
+ const stream = await execution?.start({});
256
+ stream.on('close', () => {
257
+ if (deferred.state === 'unresolved') {
258
+ deferred.resolve({ stdout: stdoutBuffer, stderr: stderrBuffer });
259
+ }
260
+ });
261
+ const stdout = new PassThrough();
262
+ stdout.on('data', (data: Buffer) => {
263
+ if (deferred.state === 'unresolved') {
264
+ stdoutBuffer += data.toString();
265
+
266
+ if (tester(stdoutBuffer, stderrBuffer)) {
267
+ deferred.resolve({ stdout: stdoutBuffer, stderr: stderrBuffer });
268
+ }
269
+ }
270
+ });
271
+ const stderr = new PassThrough();
272
+ stderr.on('data', (data: Buffer) => {
273
+ if (deferred.state === 'unresolved') {
274
+ stderrBuffer += data.toString();
275
+
276
+ if (tester(stdoutBuffer, stderrBuffer)) {
277
+ deferred.resolve({ stdout: stdoutBuffer, stderr: stderrBuffer });
278
+ }
279
+ }
280
+ });
281
+ execution.modem.demuxStream(stream, stdout, stderr);
282
+ } catch (e) {
283
+ deferred.reject(e);
284
+ }
285
+ return deferred.promise;
286
+ }
287
+
288
+ async copy(localPath: string | Buffer | NodeJS.ReadableStream, remotePath: string): Promise<void> {
289
+ const deferred = new Deferred<void>();
290
+ const process = exec(`docker cp -qa ${localPath.toString()} ${this.container.id}:${remotePath}`);
291
+
292
+ let stderr = '';
293
+ process.stderr?.on('data', data => {
294
+ stderr += data.toString();
295
+ });
296
+ process.on('close', code => {
297
+ if (code === 0) {
298
+ deferred.resolve();
299
+ } else {
300
+ deferred.reject(stderr);
301
+ }
302
+ });
303
+ return deferred.promise;
304
+ }
305
+
306
+ disposeSync(): void {
307
+ // cant use dockerrode here since this needs to happen on one tick
308
+ execSync(`docker stop ${this.container.id}`);
309
+ }
310
+
311
+ async dispose(): Promise<void> {
312
+ return this.container.stop();
313
+ }
314
+
315
+ }
@@ -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
+ });