Skip to content
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

Create workspace from devfile #13469

Merged
merged 5 commits into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dashboard/src/app/ide/ide.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class IdeSvc {
}

startWorkspace(data: any): ng.IPromise<any> {
let startWorkspacePromise = this.cheAPI.getWorkspace().startWorkspace(data.id, data.config.defaultEnv);
let startWorkspacePromise = this.cheAPI.getWorkspace().startWorkspace(data.id, data.config ? data.config.defaultEnv: null);
return startWorkspacePromise;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
ICheButtonDropdownMainAction,
ICheButtonDropdownOtherAction
} from '../../../components/widget/button-dropdown/che-button-dropdown.directive';
import {DevfileRegistry} from '../../../components/api/devfile-registry.factory';

/**
* This class is handling the controller for workspace creation.
Expand All @@ -31,8 +32,8 @@ import {
*/
export class CreateWorkspaceController {

static $inject = ['$mdDialog', '$timeout', 'cheEnvironmentRegistry', 'createWorkspaceSvc', 'namespaceSelectorSvc', 'stackSelectorSvc',
'randomSvc', '$log', 'cheNotification'];
static $inject = ['$mdDialog', '$timeout', 'cheEnvironmentRegistry', 'createWorkspaceSvc', 'namespaceSelectorSvc',
'randomSvc', '$log', 'cheNotification', 'devfileRegistry'];

/**
* Dropdown button config.
Expand All @@ -58,10 +59,6 @@ export class CreateWorkspaceController {
* Namespace selector service.
*/
private namespaceSelectorSvc: NamespaceSelectorSvc;
/**
* Stack selector service.
*/
private stackSelectorSvc: StackSelectorSvc;
/**
* Generator for random strings.
*/
Expand All @@ -75,29 +72,21 @@ export class CreateWorkspaceController {
*/
private cheNotification: CheNotification;
/**
* The environment manager.
* Devfile registry.
*/
private environmentManager: EnvironmentManager;
private devfileRegistry: DevfileRegistry;
/**
* The selected stack.
* The environment manager.
*/
private stack: che.IStack;
private environmentManager: EnvironmentManager;
/**
* The workspace config of the current stack.
* The selected devfile.
*/
private workspaceConfig: che.IWorkspaceConfig;
private selectedDevfile: che.IWorkspaceDevfile;
/**
* The selected namespace ID.
*/
private namespaceId: string;
/**
* The list of machines of selected stack.
*/
private stackMachines: Array<IEnvironmentManagerMachine>;
/**
* Desired memory limit by machine name.
*/
private memoryByMachine: {[name: string]: number};
/**
* The map of forms.
*/
Expand All @@ -122,23 +111,21 @@ export class CreateWorkspaceController {
cheEnvironmentRegistry: CheEnvironmentRegistry,
createWorkspaceSvc: CreateWorkspaceSvc,
namespaceSelectorSvc: NamespaceSelectorSvc,
stackSelectorSvc: StackSelectorSvc,
randomSvc: RandomSvc,
$log: ng.ILogService,
cheNotification: CheNotification) {
cheNotification: CheNotification,
devfileRegistry: DevfileRegistry) {
this.$mdDialog = $mdDialog;
this.$timeout = $timeout;
this.cheEnvironmentRegistry = cheEnvironmentRegistry;
this.createWorkspaceSvc = createWorkspaceSvc;
this.namespaceSelectorSvc = namespaceSelectorSvc;
this.stackSelectorSvc = stackSelectorSvc;
this.randomSvc = randomSvc;
this.$log = $log;
this.cheNotification = cheNotification;
this.devfileRegistry = devfileRegistry;

this.usedNamesList = [];
this.stackMachines = [];
this.memoryByMachine = {};
this.forms = new Map();

this.namespaceId = this.namespaceSelectorSvc.getNamespaceId();
Expand Down Expand Up @@ -182,43 +169,12 @@ export class CreateWorkspaceController {
*
* @param {string} stackId the stack ID
*/
onStackSelected(stackId: string): void {
onDevfileSelected(devfile: che.IWorkspaceDevfile): void {
// tiny timeout for templates selector to be rendered
this.$timeout(() => {
this.hideLoader = true;
}, 10);

this.stack = this.stackSelectorSvc.getStackById(stackId);
this.workspaceConfig = angular.copy(this.stack.workspaceConfig);

if (!this.stack.workspaceConfig || !this.stack.workspaceConfig.defaultEnv) {
this.memoryByMachine = {};
this.stackMachines = [];
return;
}

const environmentName = this.stack.workspaceConfig.defaultEnv;
const environment = this.stack.workspaceConfig.environments[environmentName];
const recipeType = environment.recipe.type;
this.environmentManager = this.cheEnvironmentRegistry.getEnvironmentManager(recipeType);
if (!this.environmentManager) {
const errorMessage = `Unsupported recipe type '${recipeType}'`;
this.$log.error(errorMessage);
this.cheNotification.showError(errorMessage);
return;
}
this.memoryByMachine = {};
this.stackMachines = this.environmentManager.getMachines(environment);
}

/**
* Callback which is called when machine's memory limit is changes.
*
* @param {string} name a machine name
* @param {number} memoryLimitBytes a machine's memory limit in bytes
*/
onRamChanged(name: string, memoryLimitBytes: number): void {
this.memoryByMachine[name] = memoryLimitBytes;
this.selectedDevfile = devfile
}

/**
Expand Down Expand Up @@ -267,7 +223,7 @@ export class CreateWorkspaceController {
* @return {boolean}
*/
isCreateButtonDisabled(): boolean {
if (!this.namespaceId || (this.stack && !this.stack.workspaceConfig)) {
if (!this.namespaceId || !this.selectedDevfile) {
return true;
}

Expand Down Expand Up @@ -312,7 +268,7 @@ export class CreateWorkspaceController {
this.usedNamesList = workspaces.filter((workspace: che.IWorkspace) => {
return workspace.namespace === this.namespaceId;
}).map((workspace: che.IWorkspace) => {
return workspace.config.name;
return this.createWorkspaceSvc.getWorkspaceName(workspace);
});
});
}
Expand Down Expand Up @@ -342,23 +298,8 @@ export class CreateWorkspaceController {
*/
createWorkspace(): ng.IPromise<che.IWorkspace> {
// update workspace name
this.workspaceConfig.name = this.workspaceName;

// update memory limits of machines
if (Object.keys(this.memoryByMachine).length !== 0) {
this.stackMachines.forEach((machine: IEnvironmentManagerMachine) => {
if (this.memoryByMachine[machine.name]) {
this.environmentManager.setMemoryLimit(machine, this.memoryByMachine[machine.name]);
}
});
const environmentName = this.workspaceConfig.defaultEnv;
const environment = this.workspaceConfig.environments[environmentName];
const newEnvironment = this.environmentManager.getEnvironment(environment, this.stackMachines);
this.workspaceConfig.environments[environmentName] = newEnvironment;
}
let attributes = {stackId: this.stack.id};

return this.createWorkspaceSvc.createWorkspaceFromConfig(this.workspaceConfig, attributes);
this.selectedDevfile.metadata.name = this.workspaceName;
return this.createWorkspaceSvc.createWorkspaceFromDevfile(this.selectedDevfile, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,10 @@
on-namespace-change="createWorkspaceController.onNamespaceChanged(namespaceId)"></namespace-selector>
</che-label-container>

<!-- Select Stack -->
<che-label-container che-label-name="Select Stack"
che-label-description="Choose your workspace runtime environment used to build and run your projects.">
<stack-selector on-stack-select="createWorkspaceController.onStackSelected(stackId)"></stack-selector>
</che-label-container>

<!-- RAM Settings-->
<che-label-container che-label-name="Ram"
che-label-description="Allocate the workspace machine(s) memory.">
<ng-form name="ramSettingsForm">
<ram-settings ng-init="createWorkspaceController.registerForm('ramSettings', ramSettingsForm)"
machines="createWorkspaceController.stackMachines"
environment-manager="createWorkspaceController.environmentManager"
on-ram-change="createWorkspaceController.onRamChanged(name, memoryLimitBytes)"></ram-settings>
</ng-form>
<devfile-selector on-devfile-select="createWorkspaceController.onDevfileSelected(devfile)">
</devfile-selector>
</che-label-container>

<!-- Project source selector -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,26 @@ export class CreateWorkspaceSvc {
});
}

/*createWorkspaceFromDevfile(workspaceDevfile: che.IWorkspaceDevfile, attributes: any): ng.IPromise<che.IWorkspace> {
createWorkspaceFromDevfile(workspaceDevfile: che.IWorkspaceDevfile, attributes: any): ng.IPromise<che.IWorkspace> {
const namespaceId = this.namespaceSelectorSvc.getNamespaceId(),
projectTemplates = this.projectSourceSelectorService.getProjectTemplates();

let projects = [];
projectTemplates.forEach((template: che.IProjectTemplate) => {
let project = {
name: template.displayName,
source: {
type: template.source.type,
location: template.source.location
}
};
projects.push(project);
});

return this.checkEditingProgress().then(() => {
workspaceDevfile.projects = projectTemplates;
this.addProjectCommands({devfile: workspaceDevfile}, projectTemplates);
workspaceDevfile.projects = projects;
//TODO waits for fix https://github.com/eclipse/che/issues/13514
//this.addProjectCommands({devfile: workspaceDevfile}, projectTemplates);
return this.cheWorkspace.createWorkspaceFromDevfile(namespaceId, workspaceDevfile, attributes).then((workspace: che.IWorkspace) => {
return this.cheWorkspace.fetchWorkspaces().then(() => this.cheWorkspace.getWorkspaceById(workspace.id));
})
Expand All @@ -220,7 +233,7 @@ export class CreateWorkspaceSvc {
return this.$q.reject(error);
});
});
}*/
}

/**
* Show confirmation dialog when project editing is not completed.
Expand All @@ -244,7 +257,8 @@ export class CreateWorkspaceSvc {
* @param {che.IWorkspace} workspace the workspace to open in IDE
*/
redirectToIDE(workspace: che.IWorkspace): void {
const path = `/ide/${workspace.namespace}/${workspace.config.name}`;
let name = this.cheWorkspace.getWorkspaceDataManager().getName(workspace);
const path = `/ide/${workspace.namespace}/${name}`;
this.$location.path(path);
}

Expand All @@ -254,7 +268,8 @@ export class CreateWorkspaceSvc {
* @param {che.IWorkspace} workspace the workspace to open in IDE
*/
redirectToDetails(workspace: che.IWorkspace): void {
const path = `/workspace/${workspace.namespace}/${workspace.config.name}`;
let name = this.cheWorkspace.getWorkspaceDataManager().getName(workspace);
const path = `/workspace/${workspace.namespace}/${name}`;
this.$location.path(path);
}

Expand All @@ -273,4 +288,13 @@ export class CreateWorkspaceSvc {
});
});
}

/**
* Returns name of the pointed workspace.
*
* @param workspace workspace
*/
getWorkspaceName(workspace: che.IWorkspace): string {
return this.cheWorkspace.getWorkspaceDataManager().getName(workspace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2015-2018 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
'use strict';
import {CheWorkspace} from '../../../../components/api/workspace/che-workspace.factory';
import {DevfileRegistry, IDevfileMetaData} from '../../../../components/api/devfile-registry.factory';

/**
* @description This class is handling the controller of devfile selector.
* @author Ann Shumilova
*/
export class DevfileSelectorController {

static $inject = ['devfileRegistry', 'cheWorkspace'];

private devfileRegistry: DevfileRegistry;
private cheWorkspace: CheWorkspace;
private devfiles: Array<IDevfileMetaData>;
onDevfileSelect: Function;
selectedDevfile: any;

/**
* Default constructor that is using resource injection
*/
constructor(devfileRegistry: DevfileRegistry, cheWorkspace: CheWorkspace) {
this.devfileRegistry = devfileRegistry;
this.cheWorkspace = cheWorkspace;
this.loadDevfiles();
}

loadDevfiles(): void {
let location = this.cheWorkspace.getWorkspaceSettings().cheWorkspaceDevfileRegistryUrl;
this.devfileRegistry.fetchDevfiles(location).then((data: Array<IDevfileMetaData>) => {
this.devfiles = data;

if (this.devfiles && this.devfiles.length > 0) {
this.devfileOnClick(this.devfiles[0]);
}
});
}

devfileOnClick(devfile: any): void {
this.selectedDevfile = devfile;

let location = this.cheWorkspace.getWorkspaceSettings().cheWorkspaceDevfileRegistryUrl;

let devfileContent = this.devfileRegistry.getDevfile(location, devfile.links.self);
if (devfileContent) {
this.onDevfileSelect({devfile: devfileContent});
} else {
this.devfileRegistry.fetchDevfile(location, devfile.links.self).then((devfileContent: che.IWorkspaceDevfile) => {
this.onDevfileSelect({devfile: devfileContent});
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2015-2018 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
'use strict';

/**
* Defines a directive for displaying devfile selector widget.
*
* @author Ann Shumilova
*/
export class DevfileSelector implements ng.IDirective {
restrict: string = 'E';
templateUrl: string = 'app/workspaces/create-workspace/devfile-selector/devfile-selector.html';
replace: boolean = true;

controller: string = 'DevfileSelectorController';
controllerAs: string = 'devfileSelectorController';

bindToController: boolean = true;

scope: {
[propName: string]: string
};

/**
* Default constructor that is using resource
*/
constructor() {
this.scope = {
devfileIdSelected: '=',
onDevfileSelect: '&'
};
}
}
Loading