-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
che-workspace.factory.ts
558 lines (489 loc) · 18.8 KB
/
che-workspace.factory.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
* Copyright (c) 2015-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*/
'use strict';
import {CheWorkspaceAgent} from './che-workspace-agent';
import {ComposeEnvironmentManager} from './environment/compose-environment-manager';
import {DockerFileEnvironmentManager} from './environment/docker-file-environment-manager';
import {DockerImageEnvironmentManager} from './environment/docker-image-environment-manager';
import {CheEnvironmentRegistry} from './environment/che-environment-registry.factory';
import {CheWebsocket} from './che-websocket.factory';
interface ICHELicenseResource<T> extends ng.resource.IResourceClass<T> {
getDetails: any;
create: any;
createWithNamespace: any;
deleteWorkspace: any;
updateWorkspace: any;
addProject: any;
deleteProject: any;
stopWorkspace: any;
startWorkspace: any;
startTemporaryWorkspace: any;
addCommand: any;
}
/**
* This class is handling the workspace retrieval
* It sets to the array workspaces the current workspaces which are not temporary
* @author Florent Benoit
*/
export class CheWorkspace {
$resource: ng.resource.IResourceService;
$q: ng.IQService;
listeners: Array<any>;
workspaces: Array<che.IWorkspace>;
subscribedWorkspacesIds: Array<string>;
workspaceAgents: Map<string, CheWorkspaceAgent>;
workspacesById: Map<string, che.IWorkspace>;
remoteWorkspaceAPI: ICHELicenseResource<any>;
lodash: any;
cheWebsocket: CheWebsocket;
statusDefers: Object;
/**
* Default constructor that is using resource
* @ngInject for Dependency injection
*/
constructor($resource: ng.resource.IResourceService, $q: ng.IQService, cheWebsocket: CheWebsocket, lodash: any, cheEnvironmentRegistry: CheEnvironmentRegistry, $log: ng.ILogService) {
// keep resource
this.$q = $q;
this.$resource = $resource;
this.lodash = lodash;
this.cheWebsocket = cheWebsocket;
// current list of workspaces
this.workspaces = [];
// per Id
this.workspacesById = new Map();
// workspace agents per workspace id:
this.workspaceAgents = new Map();
// listeners if workspaces are changed/updated
this.listeners = [];
// list of subscribed to websocket workspace Ids
this.subscribedWorkspacesIds = [];
this.statusDefers = {};
// remote call
this.remoteWorkspaceAPI = <ICHELicenseResource<any>>this.$resource('/api/workspace', {}, {
getDetails: {method: 'GET', url: '/api/workspace/:workspaceKey'},
// having 2 methods for creation to ensure namespace parameter won't be send at all if value is null or undefined
create: {method: 'POST', url: '/api/workspace'},
createWithNamespace: {method: 'POST', url: '/api/workspace?namespace=:namespace'},
deleteWorkspace: {method: 'DELETE', url: '/api/workspace/:workspaceId'},
updateWorkspace: {method: 'PUT', url: '/api/workspace/:workspaceId'},
addProject: {method: 'POST', url: '/api/workspace/:workspaceId/project'},
deleteProject: {method: 'DELETE', url: '/api/workspace/:workspaceId/project/:path'},
stopWorkspace: {method: 'DELETE', url: '/api/workspace/:workspaceId/runtime'},
startWorkspace: {method: 'POST', url: '/api/workspace/:workspaceId/runtime?environment=:envName'},
startTemporaryWorkspace: {method: 'POST', url: '/api/workspace/runtime?temporary=true'},
addCommand: {method: 'POST', url: '/api/workspace/:workspaceId/command'}
}
);
cheEnvironmentRegistry.addEnvironmentManager('compose', new ComposeEnvironmentManager($log));
cheEnvironmentRegistry.addEnvironmentManager('dockerfile', new DockerFileEnvironmentManager($log));
cheEnvironmentRegistry.addEnvironmentManager('dockerimage', new DockerImageEnvironmentManager());
}
/**
* Gets workspace agent
* @param workspaceId {string}
* @returns {CheWorkspaceAgent}
*/
getWorkspaceAgent(workspaceId: string): CheWorkspaceAgent {
if (this.workspaceAgents.has(workspaceId)) {
return this.workspaceAgents.get(workspaceId);
}
let runtimeConfig = this.getWorkspaceById(workspaceId).runtime;
if (runtimeConfig) {
let wsAgentLink = this.lodash.find(runtimeConfig.links, (link: any) => {
return link.rel === 'wsagent';
});
if (!wsAgentLink) {
return null;
}
let workspaceAgentData = {path: wsAgentLink.href};
let wsagent: CheWorkspaceAgent = new CheWorkspaceAgent(this.$resource, this.$q, this.cheWebsocket, workspaceAgentData);
this.workspaceAgents.set(workspaceId, wsagent);
return wsagent;
}
return null;
}
/**
* Gets all workspace agents of this remote
* @returns {Map<string, CheWorkspaceAgent>}
*/
getWorkspaceAgents(): Map<string, CheWorkspaceAgent> {
return this.workspaceAgents;
}
/**
* Add a listener that need to have the onChangeWorkspaces(workspaces: Array) method
* @param listener {Function} a changing listener
*/
addListener(listener: Function): void {
this.listeners.push(listener);
}
/**
* Gets the workspaces of this remote
* @returns {Array}
*/
getWorkspaces(): Array<che.IWorkspace> {
return this.workspaces;
}
/**
* Gets the workspaces per id
* @returns {Map}
*/
getWorkspacesById(): Map<string, che.IWorkspace> {
return this.workspacesById;
}
getWorkspaceByName(namespace: string, name: string): che.IWorkspace {
return this.lodash.find(this.workspaces, (workspace: che.IWorkspace) => {
return workspace.namespace === namespace && workspace.config.name === name;
});
}
/**
* Gets the workspace by id
* @param id {string} - workspace id
* @returns {che.IWorkspace}
*/
getWorkspaceById(id: string): che.IWorkspace {
return this.workspacesById.get(id);
}
/**
* Ask for loading the workspaces in asynchronous way
* If there are no changes, it's not updated
* @returns {ng.IPromise<any>}
*/
fetchWorkspaces(): ng.IPromise<any> {
let promise = this.remoteWorkspaceAPI.query().$promise;
let updatedPromise = promise.then((data: Array<che.IWorkspace>) => {
let remoteWorkspaces = [];
this.workspaces.length = 0;
//TODO It's a fix used not to loose account ID of the workspace.
// can be removed, when API will return accountId in the list of user workspaces response:
let copyWorkspaceById = new Map();
angular.copy(this.workspacesById, copyWorkspaceById);
this.workspacesById.clear();
// add workspace if not temporary
data.forEach((workspace: che.IWorkspace) => {
if (!workspace.temporary) {
remoteWorkspaces.push(workspace);
this.workspaces.push(workspace);
}
this.workspacesById.set(workspace.id, workspace);
this.startUpdateWorkspaceStatus(workspace.id);
});
return this.workspaces;
});
let callbackPromises = updatedPromise.then((data: any) => {
let promises = [];
promises.push(updatedPromise);
this.listeners.forEach((listener: any) => {
let promise = listener.onChangeWorkspaces(data);
promises.push(promise);
});
return this.$q.all(promises);
});
return callbackPromises;
}
/**
* Fetch workspace details by workspace's key.
*
* @param workspaceKey {string} workspace key: can be just id or namespace:workspaceName pair
* @returns {ng.IPromise<any>}
*/
fetchWorkspaceDetails(workspaceKey: string): ng.IPromise<any> {
let defer = this.$q.defer();
let promise = this.remoteWorkspaceAPI.getDetails({workspaceKey: workspaceKey}).$promise;
promise.then((data: che.IWorkspace) => {
this.workspacesById.set(data.id, data);
this.lodash.remove(this.workspaces, (workspace: che.IWorkspace) => {
return workspace.id === data.id;
});
this.workspaces.push(data);
this.startUpdateWorkspaceStatus(data.id);
defer.resolve();
}, (error: any) => {
if (error.status !== 304) {
defer.reject(error);
} else {
defer.resolve();
}
});
return defer.promise;
}
/**
* Adds a project on the workspace
* @param workspaceId {string} the workspace ID required to add a project
* @param project {che.IProject} the project JSON entry to add
* @returns {ng.IPromise<any>}
*/
addProject(workspaceId: string, project: che.IProject): ng.IPromise<any> {
return this.remoteWorkspaceAPI.addProject({workspaceId: workspaceId}, project).$promise;
}
/**
* Deletes a project of the workspace by it's path
* @param workspaceId {string} the workspace ID required to delete a project
* @param path {string} path to project to be deleted
* @returns {ng.IPromise<any>}
*/
deleteProject(workspaceId: string, path: string): ng.IPromise<any> {
return this.remoteWorkspaceAPI.deleteProject({workspaceId: workspaceId, path: path}).$promise;
}
/**
* Prepares workspace config using the data in provided one,
* workspace name, machine source, RAM.
*
* @param config {any} provided base workspace config
* @param workspaceName {string} workspace name
* @param source {any} machine source
* @param ram {number} workspace's RAM
* @returns {any} prepared workspace config
*/
formWorkspaceConfig(config: any, workspaceName: string, source: any, ram: number): any {
config = config || {};
config.name = workspaceName;
config.projects = [];
config.defaultEnv = config.defaultEnv || workspaceName;
config.description = null;
ram = ram || 2 * Math.pow(1024, 3);
// check environments were provided in config:
config.environments = (config.environments && Object.keys(config.environments).length > 0) ? config.environments : {};
let defaultEnvironment = config.environments[config.defaultEnv];
// check default environment is provided and add if there is no:
if (!defaultEnvironment) {
defaultEnvironment = {
'recipe': null,
'machines': {
'dev-machine': {
'attributes': {'memoryLimitBytes': ram},
'agents': ['org.eclipse.che.ws-agent', 'org.eclipse.che.terminal', 'org.eclipse.che.ssh']
}
}
};
config.environments[config.defaultEnv] = defaultEnvironment;
}
if (source && source.type && source.type === 'environment') {
let contentType = source.format === 'dockerfile' ? 'text/x-dockerfile' : 'application/x-yaml';
defaultEnvironment.recipe = {
'type': source.format,
'contentType': contentType
};
defaultEnvironment.recipe.content = source.content || null;
defaultEnvironment.recipe.location = source.location || null;
}
if (defaultEnvironment.recipe && defaultEnvironment.recipe.type === 'compose') {
return config;
}
let devMachine = this.lodash.find(defaultEnvironment.machines, (machine: any) => {
return machine.agents.indexOf('org.eclipse.che.ws-agent') >= 0;
});
// check dev machine is provided and add if there is no:
if (!devMachine) {
devMachine = {
'name': 'ws-machine',
'attributes': {'memoryLimitBytes': ram},
'type': 'docker',
'source': source,
'agents': ['org.eclipse.che.ws-agent', 'org.eclipse.che.terminal', 'org.eclipse.che.ssh']
};
defaultEnvironment.machines[devMachine.name] = devMachine;
} else {
if (devMachine.attributes) {
if (!devMachine.attributes.memoryLimitBytes) {
devMachine.attributes.memoryLimitBytes = ram;
}
} else {
devMachine.attributes = {'memoryLimitBytes': ram};
}
devMachine.source = source;
}
return config;
}
createWorkspace(namespace: string, workspaceName: string, source: any, ram: number, attributes: any): ng.IPromise<any> {
let data = this.formWorkspaceConfig({}, workspaceName, source, ram);
let attrs = this.lodash.map(this.lodash.pairs(attributes || {}), (item: any) => {
return item[0] + ':' + item[1];
});
let promise = namespace ? this.remoteWorkspaceAPI.createWithNamespace({
namespace: namespace,
attribute: attrs
}, data).$promise :
this.remoteWorkspaceAPI.create({attribute: attrs}, data).$promise;
return promise;
}
createWorkspaceFromConfig(namespace: string, workspaceConfig: che.IWorkspaceConfig, attributes: any): ng.IPromise<any> {
let attrs = this.lodash.map(this.lodash.pairs(attributes || {}), (item: any) => {
return item[0] + ':' + item[1];
});
return namespace ? this.remoteWorkspaceAPI.createWithNamespace({
namespace: namespace,
attribute: attrs
}, workspaceConfig).$promise :
this.remoteWorkspaceAPI.create({attribute: attrs}, workspaceConfig).$promise;
}
/**
* Add a command into the workspace
* @param workspaceId {string} the id of the workspace on which we want to add the command
* @param command {any} the command object that contains attribute like name, type, etc.
* @returns {ng.IPromise<any>}
*/
addCommand(workspaceId: string, command: any): ng.IPromise<any> {
return this.remoteWorkspaceAPI.addCommand({workspaceId: workspaceId}, command).$promise;
}
/**
* Starts the given workspace by specifying the ID and the environment name
* @param workspaceId the workspace ID
* @param envName the name of the environment
* @returns {ng.IPromise<any>} promise
*/
startWorkspace(workspaceId: string, envName: string): ng.IPromise<any> {
return this.remoteWorkspaceAPI.startWorkspace({workspaceId: workspaceId, envName: envName}, {}).$promise;
}
/**
* Starts a temporary workspace by specifying configuration
* @param workspaceConfig {che.IWorkspaceConfig}
* @returns {ng.IPromise<any>} promise
*/
startTemporaryWorkspace(workspaceConfig: che.IWorkspaceConfig): ng.IPromise<any> {
return this.remoteWorkspaceAPI.startTemporaryWorkspace({}, workspaceConfig).$promise;
}
/**
* Stop workspace
* @param workspaceId {string}
* @returns {ng.IPromise<any>} promise
*/
stopWorkspace(workspaceId: string): ng.IPromise<any> {
return this.remoteWorkspaceAPI.stopWorkspace({workspaceId: workspaceId}, {}).$promise;
}
/**
* Performs workspace config update by the given workspaceId and new data.
* @param workspaceId {string} the workspace ID
* @param data {che.IWorkspace} the new workspace details
* @returns {ng.IPromise<any>}
*/
updateWorkspace(workspaceId: string, data: che.IWorkspace): ng.IPromise<any> {
let defer = this.$q.defer();
let promise = this.remoteWorkspaceAPI.updateWorkspace({workspaceId: workspaceId}, data).$promise;
promise.then((data: che.IWorkspace) => {
this.workspacesById.set(data.id, data);
this.lodash.remove(this.workspaces, (workspace: che.IWorkspace) => {
return workspace.id === data.id;
});
this.workspaces.push(data);
this.startUpdateWorkspaceStatus(data.id);
defer.resolve(data);
}, (error: any) => {
defer.reject(error);
});
return defer.promise;
}
/**
* Performs workspace deleting by the given workspaceId.
* @param workspaceId {string} the workspace ID
* @returns {ng.IPromise<any>}
*/
deleteWorkspaceConfig(workspaceId: string): ng.IPromise<any> {
let defer = this.$q.defer();
let promise = this.remoteWorkspaceAPI.deleteWorkspace({workspaceId: workspaceId}).$promise;
promise.then(() => {
this.listeners.forEach((listener: any) => {
listener.onDeleteWorkspace(workspaceId);
});
defer.resolve();
}, (error: any) => {
defer.reject(error);
});
return defer.promise;
}
/**
* Gets the map of projects by workspace id.
* @returns {che.IWorkspaceProjects}
*/
getWorkspaceProjects(): che.IWorkspaceProjects {
let workspaceProjects: che.IWorkspaceProjects = {};
this.workspacesById.forEach((workspace: che.IWorkspace) => {
let projects = workspace.config.projects;
projects.forEach((project: che.IProject) => {
project.workspaceId = workspace.id;
project.workspaceName = workspace.config.name;
});
workspaceProjects[workspace.id] = projects;
});
return workspaceProjects;
}
getAllProjects(): Array<che.IProject> {
let projects = this.lodash.pluck(this.workspaces, 'config.projects');
return [].concat.apply([], projects);
}
/**
* Gets websocket for a given workspace. It needs to have fetched first the runtime configuration of the workspace
* @param workspaceId {string} the id of the workspace
* @returns {string}
*/
getWebsocketUrl(workspaceId: string): string {
let workspace = this.workspacesById.get(workspaceId);
if (!workspace || !workspace.runtime || !workspace.runtime.devMachine) {
return '';
}
let websocketLink = this.lodash.find(workspace.runtime.devMachine.links, (link: any) => {
return link.rel === 'wsagent.websocket';
});
return websocketLink ? websocketLink.href : '';
}
/**
* Gets IDE Url
* @param namespace {string}
* @param workspaceName {string}
* @returns {string}
*/
getIdeUrl(namespace: string, workspaceName: string): string {
return '/ide/' + namespace + '/' + workspaceName;
}
/**
* Creates deferred object which will be resolved
* when workspace change it's status to given
* @param workspaceId {string}
* @param status {string} needed to resolve deferred object
* @returns {ng.IPromise<any>}
*/
fetchStatusChange(workspaceId: string, status: string): ng.IPromise<any> {
let defer = this.$q.defer();
if (status === this.getWorkspaceById(workspaceId).status) {
defer.resolve();
} else {
if (!this.statusDefers[workspaceId]) {
this.statusDefers[workspaceId] = {};
}
if (!this.statusDefers[workspaceId][status]) {
this.statusDefers[workspaceId][status] = [];
}
this.statusDefers[workspaceId][status].push(defer);
}
return defer.promise;
}
/**
* Add subscribe to websocket channel for specified workspaceId
* to handle workspace's status changes.
* @param workspaceId {string}
*/
startUpdateWorkspaceStatus(workspaceId: string): void {
if (this.subscribedWorkspacesIds.indexOf(workspaceId) < 0) {
let bus = this.cheWebsocket.getBus();
this.subscribedWorkspacesIds.push(workspaceId);
bus.subscribe('workspace:' + workspaceId, (message: any) => {
let status = (message.status === 'STOPPED' && message.error) ? 'ERROR' : message.status;
this.getWorkspaceById(workspaceId).status = status;
if (!this.statusDefers[workspaceId] || !this.statusDefers[workspaceId][status]) {
return;
}
this.statusDefers[workspaceId][status].forEach((defer: any) => {
defer.resolve(message);
});
this.statusDefers[workspaceId][status].length = 0;
});
}
}
}