Skip to content

Commit

Permalink
update compose-environment-manager
Browse files Browse the repository at this point in the history
use environment-manager to work with compose-based workspace environment

Signed-off-by: Oleksii Kurinnyi <[email protected]>
  • Loading branch information
Oleksii Kurinnyi committed Sep 11, 2016
1 parent 0da97e8 commit 234d594
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export class WorkspaceEnvironmentsController {
* Default constructor that is using resource injection
* @ngInject for Dependency injection
*/
constructor($q, $log, $scope, $timeout) {
constructor($q, $log, $scope, $timeout, cheEnvironmentRegistry) {
this.$q = $q;
this.$log = $log;
this.$scope = $scope;
this.cheEnvironmentRegistry = cheEnvironmentRegistry;

this.editorOptions = {
lineWrapping: true,
lineNumbers: false,
mode: 'text/x-yaml',
readOnly: true,
gutters: [],
onLoad: (editor) => {
Expand All @@ -40,6 +40,8 @@ export class WorkspaceEnvironmentsController {
}
};

this.machineConfigs = {};

this.init();
}

Expand All @@ -48,45 +50,16 @@ export class WorkspaceEnvironmentsController {
*/
init() {
this.newEnvironmentName = this.environmentName;
this.environmentValue = this.workspaceConfig.environments[this.environmentName];
this.environmentRecipe = this.parseRecipe(this.environmentValue.recipe.content, this.environmentValue.recipe.contentType);
}
this.environment = this.workspaceConfig.environments[this.environmentName];

/**
* Parses recipe content
* @param recipeContent {string}
* @param recipeContentType {string}
* @returns {object}
*/
parseRecipe(recipeContent, recipeContentType) {
let recipe = {};
if (/yaml/i.test(recipeContentType)){
try {
recipe = jsyaml.load(recipeContent);
} catch (e) {
this.$log.error(e);
}
}
return recipe;
}
let recipeType = this.environment.recipe.type;
this.environmentManager = this.cheEnvironmentRegistry.getEnvironmentManager(recipeType);

/**
* Dumps recipe object
* @param recipe {object}
* @param recipeContentType {string}
* @returns {string}
*/
stringifyRecipe(recipe, recipeContentType) {
let recipeContent = '';
if (/yaml/i.test(recipeContentType)) {
try {
recipeContent = jsyaml.dump(recipe);
} catch (e) {
this.$log.error(e);
}
}
this.environmentRecipe = this.environmentManager._parseRecipe(this.environment.recipe.content);

this.editorOptions.mode = this.environmentManager.editorMode;

return recipeContent;
this.machines = this.environmentManager.getMachines(this.environment);
}

/**
Expand All @@ -98,7 +71,7 @@ export class WorkspaceEnvironmentsController {
return;
}

this.workspaceConfig.environments[this.newEnvironmentName] = this.environmentValue;
this.workspaceConfig.environments[this.newEnvironmentName] = this.environment;
delete this.workspaceConfig.environments[this.environmentName];

if (this.workspaceConfig.defaultEnv === this.environmentName) {
Expand All @@ -113,13 +86,26 @@ export class WorkspaceEnvironmentsController {
* @returns {Promise}
*/
updateMachineConfig() {
let newRecipeContent = this.stringifyRecipe(this.environmentRecipe, this.environmentValue.recipe.contentType);
if (newRecipeContent) {
this.workspaceConfig.environments[this.environmentName].recipe.content = newRecipeContent;
}
let newEnvironment = this.environmentManager.getEnvironment(this.environment, this.machines);
this.workspaceConfig.environments[this.newEnvironmentName] = newEnvironment;
return this.doUpdateEnvironments();
}

updateMachineName(oldName, newName) {
let newEnvironment = this.environmentManager.renameMachine(this.environment, oldName, newName);
this.workspaceConfig.environments[this.newEnvironmentName] = newEnvironment;
return this.doUpdateEnvironments().then(() => {
this.init();
})
}

// TODO
deleteMachine(name) {
let newEnvironment = this.environmentManager.deleteMachine(this.environment, name);
this.workspaceConfig.environments[this.newEnvironmentName] = newEnvironment;
return this.doUpdateEnvironments().then(() => {
this.init();
});
})
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,39 @@
</ng-form>
</che-label-container>

<!-- Recipe -->
<che-label-container che-label-name="Recipe">
<!-- Recipe Content-->
<che-label-container che-label-name="Recipe"
ng-if="workspaceEnvironmentsController.environment.recipe.content">
<div layout="row" layout-wrap>
<div flex class="recipe-editor">
<ui-codemirror ui-codemirror="workspaceEnvironmentsController.editorOptions"
ng-model="workspaceEnvironmentsController.environmentValue.recipe.content"></ui-codemirror>
ng-model="workspaceEnvironmentsController.environment.recipe.content"></ui-codemirror>
</div>
</div>
</che-label-container>
<!-- OR Location -->
<che-label-container che-label-name="Recipe Location"
ng-if="workspaceEnvironmentsController.environment.recipe.location">
<div class="workspace-environments-input">
<che-input readonly
che-name="location"
aria-label="Recipe location"
ng-model="workspaceEnvironmentsController.recipe.location">
</che-input>
</div>
</che-label-container>

<!-- Machines -->
<che-label-container ng-if="workspaceEnvironmentsController.environmentValue.machines.length"
<che-label-container ng-if="workspaceEnvironmentsController.environment.machines.length"
che-label-name="Machines"
class="che-label-container-last">
</che-label-container>
<workspace-machine-config ng-repeat="(machineName, machineConfig) in workspaceEnvironmentsController.workspaceConfig.environments[workspaceEnvironmentsController.environmentName].machines"
machine-name="machineName"
machine-configs="workspaceEnvironmentsController.workspaceConfig.environments[workspaceEnvironmentsController.environmentName].machines"
environment-recipe="workspaceEnvironmentsController.environmentRecipe"
<workspace-machine-config ng-repeat="machine in workspaceEnvironmentsController.machines"
machine-name="machine.name"
machines-list="workspaceEnvironmentsController.machines"
environment-manager="workspaceEnvironmentsController.environmentManager"
machine-name-on-change="workspaceEnvironmentsController.updateMachineName(oldName,newName)"
machine-config-on-change="workspaceEnvironmentsController.updateMachineConfig()"
machine-on-delete="workspaceEnvironmentController.deleteMachine(name)"
machine-is-opened="$first"></workspace-machine-config>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<che-label-container>
<div layout="row">
<che-button-primary che-button-title="Update"
ng-disabled="editPortForm.$invalid || editPortDialogController.updateInProgress || editPortDialogController.isPortExists()"
ng-disabled="editPortForm.$invalid || editPortDialogController.updateInProgress"
ng-click="editPortDialogController.updatePort()"></che-button-primary>

<che-button-notice che-button-title="Cancel"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ export class WorkspaceMachineConfigController {
* Default constructor that is using resource injection
* @ngInject for Dependency injection
*/
constructor($mdDialog, $log, $scope, $timeout) {
constructor($mdDialog, $log, $scope, $timeout, lodash) {
this.$mdDialog = $mdDialog;
this.$log = $log;
this.$timeout = $timeout;
this.lodash = lodash;

this.timeoutPromise;
$scope.$on('$destroy', () => {
Expand All @@ -35,41 +36,55 @@ export class WorkspaceMachineConfigController {
});

this.init();

// check machine name uniqueness
let ctrl = this;
$scope.isUnique = (newMachineName) => {
let isUsed = ctrl.lodash.some(ctrl.machinesList, (machine) => {
return newMachineName !== ctrl.machineName && newMachineName === machine.name;
});
return !isUsed;
};
}

/**
* Sets initial values
*/
init() {
this.devMachineAgentName = 'ws-agent';
this.isDev = this.machineConfigs[this.machineName].agents.includes(this.devMachineAgentName);
this.newDev = this.isDev;
this.machine = this.lodash.find(this.machinesList, (machine) => {
return machine.name === this.machineName;
});

let machineRecipe = this.environmentRecipe.services[this.machineName];
this.source = machineRecipe.image;
this.ram = machineRecipe.mem_limit;
this.newRam = this.ram;
this.machineConfig = {
source: this.environmentManager.getSource(this.machine),
isDev: this.environmentManager.isDev(this.machine),
memoryLimitBytes: this.environmentManager.getMemoryLimit(this.machine),
servers: this.environmentManager.getServers(this.machine),
canEditEnvVariables: this.environmentManager.canEditEnvVariables,
canDeleteMachine: this.environmentManager.canDeleteMachine
};

if (this.environmentManager.canEditEnvVariables) {
this.machineConfig.envVariables = this.environmentManager.getEnvVariables(this.machine);
}

this.envVariables = machineRecipe.environment || {};
this.newDev = this.machineConfig.isDev;

this.newRam = this.machineConfig.memoryLimitBytes;
}

/**
* Modifies agents list in machine: adds or removes 'ws-agent'
* Modifies agents list in order to add or remove 'ws-agent'
*/
updateDev() {
this.$timeout.cancel(this.timeoutPromise);

if (this.isDev === this.newDev) {
if (this.machineConfig.isDev === this.newDev) {
return;
}

this.timeoutPromise = this.$timeout(() => {
let agentIndex = this.machineConfigs[this.machineName].agents.indexOf(this.devMachineAgentName);
if (this.newDev && agentIndex === -1) {
this.machineConfigs[this.machineName].agents.push(this.devMachineAgentName);
} else if (!this.newDev && agentIndex > -1) {
this.machineConfigs[this.machineName].agents.splice(agentIndex, 1);
}
this.environmentManager.setDev(this.machine, this.newDev);

this.doUpdateConfig().then(() => {
this.init();
Expand All @@ -89,7 +104,8 @@ export class WorkspaceMachineConfigController {
}

this.timeoutPromise = this.$timeout(() => {
this.environmentRecipe.services[this.machineName].mem_limit = this.newRam;
this.environmentManager.setMemoryLimit(this.machine, this.newRam);

this.doUpdateConfig().then(() => {
this.init();
});
Expand All @@ -101,6 +117,7 @@ export class WorkspaceMachineConfigController {
* @returns {Promise}
*/
updateServers() {
this.environmentManager.setServers(this.machine, this.machineConfig.servers);
return this.doUpdateConfig();
}

Expand All @@ -109,11 +126,7 @@ export class WorkspaceMachineConfigController {
* @returns {Promise}
*/
updateEnvVariables() {
if (Object.keys(this.envVariables).length) {
this.environmentRecipe.services[this.machineName].environment = this.envVariables;
} else {
delete this.environmentRecipe.services[this.machineName].environment;
}
this.environmentManager.setEnvVariables(this.machine, this.machineConfig.envVariables);

return this.doUpdateConfig().then(() => {
this.init();
Expand Down Expand Up @@ -159,22 +172,24 @@ export class WorkspaceMachineConfigController {
return defer.promise;
}

// update config
this.machineConfigs[newMachineName] = this.machineConfigs[this.machineName];
delete this.machineConfigs[this.machineName];

// update recipe
this.environmentRecipe.services[newMachineName] = this.environmentRecipe.services[this.machineName];
delete this.environmentRecipe.services[this.machineName];
this.machineNameOnChange({
oldName: this.machineName,
newName: newMachineName
}).then(() => {
this.init();
});

return this.doUpdateConfig();
}

// TODO
deleteMachine() {
this.showDeleteConfirmation().then(() => {
// todo
})
this.machineOnDelete({
name: this.machineName
}).then(() => {
this.init();
})
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ export class WorkspaceMachineConfig {

this.scope = {
machineName: '=',
machineConfigs: '=',
environmentRecipe: '=',
machinesList: '=',
environmentManager: '=',
machineNameOnChange: '&',
machineConfigOnChange: '&',
machineOnDelete: '&',
machineIsOpened: '='
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<che-input che-name="source"
che-place-holder="Source"
che-width="auto"
ng-model="workspaceMachineConfigController.source"
ng-model="workspaceMachineConfigController.machineConfig.source"
aria-label="Machine {{workspaceMachineConfigController.machineName}} source"
required
readonly>
Expand Down Expand Up @@ -66,26 +66,28 @@
<!-- Ports -->
<che-label-container che-label-name="Ports">
<div class="config-params-table">
<list-ports servers="workspaceMachineConfigController.machineConfigs[workspaceMachineConfigController.machineName].servers"
<list-ports servers="workspaceMachineConfigController.machineConfig.servers"
servers-on-change="workspaceMachineConfigController.updateServers()"></list-ports>
</div>
</che-label-container>

<!-- Environment Variables-->
<che-label-container che-label-name="Environment variables">
<che-label-container che-label-name="Environment variables"
ng-if="workspaceMachineConfigController.machineConfig.canEditEnvVariables">
<div class="config-params-table">
<list-env-variables env-variables="workspaceMachineConfigController.envVariables"
<list-env-variables env-variables="workspaceMachineConfigController.machineConfig.envVariables"
env-variables-on-change="workspaceMachineConfigController.updateEnvVariables()"></list-env-variables>
</div>
</che-label-container>

<!-- Delete -->
<che-label-container class="config-delete-label"
che-label-name="Delete machine"
che-label-description="Environment should contain at least one machine.">
che-label-description="Environment should contain at least one machine."
ng-if="workspaceMachineConfigController.machineConfig.canDeleteMachine">
<che-button-danger che-button-title="Delete"
ng-disabled="workspaceMachineConfigController.machinesNumber === 1"
ng-click="workspaceMachineConfigController.deleteMachine($event)"></che-button-danger>
ng-disabled="workspaceMachineConfigController.machinesList.length === 1"
ng-click="workspaceMachineConfigController.deleteMachine()"></che-button-danger>
</che-label-container>
</div>
</div>
Loading

0 comments on commit 234d594

Please sign in to comment.