/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var App = require('app');
var lazyLoading = require('utils/lazy_loading');
App.AddServiceController = App.WizardController.extend({
name: 'addServiceController',
serviceConfigs:require('data/service_configs'),
totalSteps: 7,
/**
* Used for hiding back button in wizard
*/
hideBackButton: true,
/**
* All wizards data will be stored in this variable
*
* cluster - cluster name
* installOptions - ssh key, repo info, etc.
* services - services list
* hosts - list of selected hosts
* slaveComponentHosts, - info about slave hosts
* masterComponentHosts - info about master hosts
* config??? - to be described later
*/
content: Em.Object.create({
cluster: null,
hosts: null,
installOptions: null,
services: null,
slaveComponentHosts: null,
masterComponentHosts: null,
serviceConfigProperties: null,
advancedServiceConfig: null,
controllerName: 'addServiceController',
configGroups: [],
additionalClients: []
}),
setCurrentStep: function (currentStep, completed) {
this._super(currentStep, completed);
App.clusterStatus.setClusterStatus({
wizardControllerName: this.get('name'),
localdb: App.db.data
});
},
/**
* return new object extended from clusterStatusTemplate
* @return Object
*/
getCluster: function(){
return jQuery.extend({}, this.get('clusterStatusTemplate'), {name: App.router.getClusterName()});
},
/**
* Load confirmed hosts.
* Will be used at Assign Masters(step5)
step
*/
loadConfirmedHosts: function(){
var hosts = this.getDBProperty('hosts');
this.set('content.hosts', hosts);
console.log('AddServiceController.loadConfirmedHosts: loaded hosts', hosts);
},
/**
* Load services data from server.
*/
loadServicesFromServer: function() {
if(this.getDBProperty('service')){
return;
}
var apiService = this.loadServiceComponents();
//
apiService.forEach(function(item, index){
apiService[index].isSelected = App.Service.find().someProperty('id', item.serviceName);
apiService[index].isDisabled = apiService[index].isSelected;
apiService[index].isInstalled = apiService[index].isSelected;
});
this.set('content.services', apiService);
this.setDBProperty('service', apiService);
},
/**
* Load services data. Will be used at Select services(step4)
step
*/
loadServices: function () {
var servicesInfo = this.getDBProperty('service');
servicesInfo.forEach(function (item, index) {
servicesInfo[index] = Em.Object.create(item);
});
this.set('content.services', servicesInfo);
console.log('AddServiceController.loadServices: loaded data ', servicesInfo);
var serviceNames = servicesInfo.filterProperty('isSelected', true).filterProperty('isDisabled', false).mapProperty('serviceName');
console.log('selected services ', serviceNames);
this.set('content.skipSlavesStep', !serviceNames.contains('MAPREDUCE') && !serviceNames.contains('HBASE') && !serviceNames.contains('STORM') && !serviceNames.contains('YARN'));
if (this.get('content.skipSlavesStep')) {
this.get('isStepDisabled').findProperty('step', 3).set('value', this.get('content.skipSlavesStep'));
}
},
/**
* Save data to model
* @param stepController App.WizardStep4Controller
*/
saveServices: function (stepController) {var serviceNames = [];
this.setDBProperty('service', stepController.get('content'));
console.log('AddServiceController.saveServices: saved data', stepController.get('content'));
stepController.filterProperty('isSelected', true).filterProperty('isInstalled', false).forEach(function (item) {
serviceNames.push(item.serviceName);
});
this.set('content.selectedServiceNames', serviceNames);
this.setDBProperty('selectedServiceNames',serviceNames);
console.log('AddServiceController.selectedServiceNames:', serviceNames);
this.set('content.skipSlavesStep', !serviceNames.contains('MAPREDUCE') && !serviceNames.contains('HBASE') && !serviceNames.contains('STORM') && !serviceNames.contains('YARN'));
if (this.get('content.skipSlavesStep')) {
this.get('isStepDisabled').findProperty('step', 3).set('value', this.get('content.skipSlavesStep'));
}
},
/**
* Save Master Component Hosts data to Main Controller
* @param stepController App.WizardStep5Controller
*/
saveMasterComponentHosts: function (stepController) {
App.ajax.send({
name: 'host_components.all',
sender: this,
data: {
clusterName: App.get('clusterName'),
stepController: stepController
},
success: 'saveMasterComponentHostsSuccessCallback'
});
},
saveMasterComponentHostsSuccessCallback: function (response, request, data) {
var obj = data.stepController.get('selectedServicesMasters');
var masterComponentHosts = [];
obj.forEach(function (_component) {
masterComponentHosts.push({
display_name: _component.display_name,
component: _component.component_name,
hostName: _component.selectedHost,
serviceId: _component.serviceId,
isInstalled: response.items.someProperty('HostRoles.component_name', _component.component_name)
});
});
console.log("AddServiceController.saveMasterComponentHosts: saved hosts ", masterComponentHosts);
this.setDBProperty('masterComponentHosts', masterComponentHosts);
this.set('content.masterComponentHosts', []);
lazyLoading.run({
initSize: 20,
chunkSize: 50,
delay: 50,
destination: this.get('content.masterComponentHosts'),
source: masterComponentHosts,
context: this
});
this.set('content.skipMasterStep', this.get('content.masterComponentHosts').everyProperty('isInstalled', true));
this.get('isStepDisabled').findProperty('step', 2).set('value', this.get('content.skipMasterStep'));
},
/**
* Does service have any configs
* @param {string} serviceName
* @returns {boolean}
*/
isServiceConfigurable: function(serviceName) {
return this.get('serviceConfigs').mapProperty('serviceName').contains(serviceName);
},
/**
* Should Config Step be skipped (based on selected services list)
* @returns {boolean}
*/
skipConfigStep: function() {
var skipConfigStep = true;
var selectedServices = this.get('content.services').filterProperty('isSelected', true).filterProperty('isInstalled', false).mapProperty('serviceName');
selectedServices.map(function(serviceName) {
skipConfigStep = skipConfigStep && !this.isServiceConfigurable(serviceName);
}, this);
return skipConfigStep;
},
loadServiceConfigProperties: function() {
this._super();
if (!this.get('content.services')) {
this.loadServices();
}
if (this.get('currentStep') > 1 && this.get('currentStep') < 6) {
this.set('content.skipConfigStep', this.skipConfigStep());
this.get('isStepDisabled').findProperty('step', 4).set('value', this.get('content.skipConfigStep'));
}
},
saveServiceConfigProperties: function(stepController) {
this._super(stepController);
if (this.get('currentStep') > 1 && this.get('currentStep') < 6) {
this.set('content.skipConfigStep', this.skipConfigStep());
this.get('isStepDisabled').findProperty('step', 4).set('value', this.get('content.skipConfigStep'));
}
},
/**
* Load master component hosts data for using in required step controllers
*/
loadSlaveComponentHosts: function () {
var slaveComponentHosts = this.getDBProperty('slaveComponentHosts');
var message = 'AddServiceController.loadSlaveComponentHosts: loaded hosts ';
this.set('content.slaveComponentHosts', []);
if(!slaveComponentHosts){
this.getSlaveComponentHosts(this, 'content.slaveComponentHosts', message);
} else {
this.set('content.slaveComponentHosts', []);
lazyLoading.run({
initSize: 20,
chunkSize: 50,
delay: 50,
destination: this.get('content.slaveComponentHosts'),
source: slaveComponentHosts,
context: this
});
console.log(message, slaveComponentHosts);
}
},
/**
* Generate clients list for selected services and save it to model
* @param stepController step4WizardController
*/
saveClients: function(stepController){
App.ajax.send({
name: 'host_components.all',
sender: this,
data: {
clusterName: App.get('clusterName'),
stepController: stepController
},
success: 'saveClientsSuccessCallback'
});
},
saveClientsSuccessCallback: function (response, request, data) {
var clients = [];
var serviceComponents = App.StackServiceComponent.find();
data.stepController.get('content').filterProperty('isSelected',true).forEach(function (_service) {
var client = serviceComponents.filterProperty('serviceName', _service.serviceName).findProperty('isClient', true);
if (client) {
clients.pushObject({
component_name: client.get('componentName'),
display_name: client.get('displayName'),
isInstalled: response.items.filterProperty('HostRoles.component_name', client.get('componentName')).length > 0
});
}
}, this);
this.setDBProperty('clientInfo', clients);
this.set('content.clients', []);
lazyLoading.run({
initSize: 20,
chunkSize: 50,
delay: 50,
destination: this.get('content.clients'),
source: clients,
context: this
});
console.log("AddServiceController.saveClients: saved list ", clients);
},
/**
* Load data for all steps until current step
*/
loadAllPriorSteps: function () {
var step = this.get('currentStep');
switch (step) {
case '7':
case '6':
case '5':
this.load('cluster');
this.set('content.additionalClients', []);
case '4':
this.loadServiceConfigGroups();
this.loadServiceConfigProperties();
case '3':
this.loadServices();
this.loadClients();
this.loadSlaveComponentHosts();//depends on loadServices
case '2':
this.loadMasterComponentHosts();
this.loadConfirmedHosts();
case '1':
this.loadServices();
}
},
/**
* Remove all loaded data.
* Created as copy for App.router.clearAllSteps
*/
clearAllSteps: function () {
this.clearInstallOptions();
// clear temporary information stored during the install
this.set('content.cluster', this.getCluster());
},
/**
* Clear all temporary data
*/
finish: function () {
this.setCurrentStep('1');
this.clearAllSteps();
this.clearStorageData();
App.router.get('updateController').updateAll();
},
installServices: function (isRetry) {
this.set('content.cluster.oldRequestsId', []);
var clusterName = this.get('content.cluster.name');
var data;
var name;
this.installAdditionalClients();
if (isRetry) {
this.getFailedHostComponents();
console.log('failedHostComponents', this.get('failedHostComponents'));
name = 'wizard.install_services.installer_controller.is_retry';
data = {
"RequestInfo": {
"context" : Em.I18n.t('requestInfo.installComponents'),
"query": "HostRoles/component_name.in(" + this.get('failedHostComponents').join(',') + ")"
},
"Body": {
"HostRoles": {
"state": "INSTALLED"
}
}
};
data = JSON.stringify(data);
}
else {
name = 'wizard.install_services.installer_controller.not_is_retry';
data = '{"RequestInfo": {"context" :"' + Em.I18n.t('requestInfo.installServices') + '"}, "Body": {"ServiceInfo": {"state": "INSTALLED"}}}';
}
App.ajax.send({
name: name,
sender: this,
data: {
data: data,
cluster: clusterName
},
success: 'installServicesSuccessCallback',
error: 'installServicesErrorCallback'
});
},
/**
* installs clients before install new services
* on host where some components require this
* @method installAdditionalClients
*/
installAdditionalClients: function() {
this.get('content.additionalClients').forEach(function(c){
App.ajax.send({
name: 'host.host_component.install',
sender: this,
data: {
hostName: c.hostName,
componentName: c.componentName,
data: JSON.stringify({
RequestInfo: {
"context": Em.I18n.t('requestInfo.installHostComponent') + " " + c.hostName
},
Body: {
HostRoles: {
state: 'INSTALLED'
}
}
})
}
});
}, this);
},
/**
* List of failed to install HostComponents while adding Service
*/
failedHostComponents: [],
getFailedHostComponents: function() {
App.ajax.send({
name: 'wizard.install_services.add_service_controller.get_failed_host_components',
sender: this,
success: 'getFailedHostComponentsSuccessCallback',
error: 'getFailedHostComponentsErrorCallback'
});
},
/**
* Parse all failed components and filter out installed earlier components (based on selected to install services)
* @param {Object} json
*/
getFailedHostComponentsSuccessCallback: function(json) {
var allFailed = json.items.filterProperty('HostRoles.state', 'INSTALL_FAILED');
var currentFailed = [];
var selectedServices = this.getDBProperty('service').filterProperty('isInstalled', false).filterProperty('isSelected', true).mapProperty('serviceName');
allFailed.forEach(function(failed) {
if (selectedServices.contains(failed.component[0].ServiceComponentInfo.service_name)) {
currentFailed.push(failed.HostRoles.component_name);
}
});
this.set('failedHostComponents', currentFailed);
},
getFailedHostComponentsErrorCallback: function(request, ajaxOptions, error) {
console.warn(error);
}
});