add_controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.AddHostController = App.WizardController.extend({
  20. name: 'addHostController',
  21. totalSteps: 7,
  22. /**
  23. * Used for hiding back button in wizard
  24. */
  25. hideBackButton: true,
  26. /**
  27. * All wizards data will be stored in this variable
  28. *
  29. * cluster - cluster name
  30. * hosts - hosts, ssh key, repo info, etc.
  31. * services - services list
  32. * hostsInfo - list of selected hosts
  33. * slaveComponentHosts, hostSlaveComponents - info about slave hosts
  34. * masterComponentHosts - info about master hosts
  35. * serviceConfigGroups - info about selected config group for service
  36. * configGroups - all config groups
  37. * config??? - to be described later
  38. */
  39. content: Em.Object.create({
  40. cluster: null,
  41. hosts: null,
  42. installOptions: null,
  43. services: null,
  44. slaveComponentHosts: null,
  45. masterComponentHosts: null,
  46. serviceConfigProperties: null,
  47. advancedServiceConfig: null,
  48. controllerName: 'addHostController',
  49. serviceConfigGroups: null,
  50. configGroups: null
  51. }),
  52. /**
  53. * save info about wizard progress, particularly current step of wizard
  54. * @param currentStep
  55. * @param completed
  56. */
  57. setCurrentStep: function (currentStep, completed) {
  58. this._super(currentStep, completed);
  59. App.clusterStatus.setClusterStatus({
  60. wizardControllerName: this.get('name'),
  61. localdb: App.db.data
  62. });
  63. var self = this;
  64. Em.run.next(function(){
  65. if (self.isConfigGroupsEmpty()) {
  66. self.disableStep(4);
  67. }
  68. });
  69. },
  70. /**
  71. * return new object extended from clusterStatusTemplate
  72. * @return Object
  73. */
  74. getCluster: function () {
  75. return jQuery.extend({}, this.get('clusterStatusTemplate'), {name: App.router.getClusterName()});
  76. },
  77. /**
  78. * Remove host from model. Used at <code>Confirm hosts</code> step
  79. * @param hosts Array of hosts, which we want to delete
  80. */
  81. removeHosts: function (hosts) {
  82. var dbHosts = this.getDBProperty('hosts');
  83. hosts.forEach(function (_hostInfo) {
  84. var host = _hostInfo.name;
  85. delete dbHosts[host];
  86. });
  87. this.setDBProperty('hosts', dbHosts);
  88. },
  89. disableStep: function(step) {
  90. this.get('isStepDisabled').findProperty('step', step).set('value', true);
  91. },
  92. isConfigGroupsEmpty: function() {
  93. return !this.get('content.configGroups') || !this.get('content.configGroups').length;
  94. },
  95. /**
  96. * Load services data. Will be used at <code>Select services(step4)</code> step
  97. */
  98. loadServices: function () {
  99. var services = this.getDBProperty('services');
  100. if (!services) {
  101. services = {
  102. selectedServices: [],
  103. installedServices: []
  104. };
  105. App.StackService.find().forEach(function (item) {
  106. var isInstalled = App.Service.find().someProperty('serviceName', item.get('serviceName'));
  107. item.set('isSelected', isInstalled);
  108. item.set('isInstalled', isInstalled);
  109. if (isInstalled) {
  110. services.selectedServices.push(item.get('serviceName'));
  111. services.installedServices.push(item.get('serviceName'));
  112. }
  113. }, this);
  114. this.setDBProperty('services', services);
  115. } else {
  116. App.StackService.find().forEach(function (item) {
  117. var isSelected = services.selectedServices.contains(item.get('serviceName'));
  118. var isInstalled = services.installedServices.contains(item.get('serviceName'));
  119. item.set('isSelected', isSelected);
  120. item.set('isInstalled', isInstalled);
  121. }, this);
  122. }
  123. this.set('content.services', App.StackService.find());
  124. },
  125. /**
  126. * Load slave component hosts data for using in required step controllers
  127. * TODO move to mixin
  128. */
  129. loadSlaveComponentHosts: function () {
  130. var slaveComponentHosts = this.getDBProperty('slaveComponentHosts') || [];
  131. if (slaveComponentHosts.length) {
  132. var hosts = this.getDBProperty('hosts'),
  133. host_names = Em.keys(hosts);
  134. slaveComponentHosts.forEach(function (component) {
  135. component.hosts.forEach(function (host) {
  136. //Em.set(host, 'hostName', hosts[host.host_id].name);
  137. for (var i = 0; i < host_names.length; i++) {
  138. if (hosts[host_names[i]].id === host.host_id) {
  139. host.hostName = host_names[i];
  140. break;
  141. }
  142. }
  143. });
  144. });
  145. }
  146. this.set("content.slaveComponentHosts", slaveComponentHosts);
  147. console.log("AddHostController.loadSlaveComponentHosts: loaded hosts ", slaveComponentHosts);
  148. },
  149. /**
  150. * Generate clients list for selected services and save it to model
  151. */
  152. saveClients: function () {
  153. var serviceComponents = App.StackServiceComponent.find();
  154. var services = this.get('content.services').filterProperty('isInstallable').filterProperty('isSelected');
  155. var clients = this.getClientsToInstall(services, serviceComponents);
  156. this.setDBProperty('clientInfo', clients);
  157. this.set('content.clients', clients);
  158. console.log("AddHostController.saveClients: saved list ", clients);
  159. },
  160. /**
  161. * get list of clients which will be installed on host
  162. * @param services {Array} of service objects
  163. * @param components {Array} of component objects
  164. * @returns {Array} returns array of clients
  165. * @method getClientsToInstall;
  166. */
  167. getClientsToInstall: function(services, components) {
  168. var clients = [];
  169. services.forEach(function (_service) {
  170. var serviceClients = components.filter(function(component) {
  171. return (component.get('serviceName') == _service.get('serviceName') && component.get('isClient'));
  172. });
  173. if (serviceClients.length) {
  174. serviceClients.forEach(function(client) {
  175. clients.push({
  176. component_name: client.get('componentName'),
  177. display_name: client.get('displayName'),
  178. isInstalled: false
  179. });
  180. });
  181. }
  182. }, this);
  183. return clients;
  184. },
  185. /**
  186. * Apply config groups from step4 Configurations
  187. */
  188. applyConfigGroup: function () {
  189. var serviceConfigGroups = this.get('content.configGroups');
  190. serviceConfigGroups.forEach(function (group) {
  191. if (group.configGroups.someProperty('ConfigGroup.group_name', group.selectedConfigGroup)) {
  192. var configGroup = group.configGroups.findProperty('ConfigGroup.group_name', group.selectedConfigGroup);
  193. group.hosts.forEach(function (host) {
  194. configGroup.ConfigGroup.hosts.push({
  195. host_name: host
  196. });
  197. }, this);
  198. delete configGroup.href;
  199. App.ajax.send({
  200. name: 'config_groups.update_config_group',
  201. sender: this,
  202. data: {
  203. id: configGroup.ConfigGroup.id,
  204. configGroup: configGroup
  205. }
  206. });
  207. }
  208. }, this);
  209. },
  210. /**
  211. * Load information about selected config groups
  212. */
  213. getServiceConfigGroups: function () {
  214. var serviceConfigGroups = this.getDBProperty('serviceConfigGroups');
  215. this.set('content.configGroups', serviceConfigGroups);
  216. },
  217. /**
  218. * Save information about selected config groups
  219. */
  220. saveServiceConfigGroups: function () {
  221. this.setDBProperty('serviceConfigGroups', this.get('content.configGroups'));
  222. },
  223. /**
  224. * Set content.configGroups for step4
  225. */
  226. loadServiceConfigGroups: function () {
  227. var selectedServices = [];
  228. this.loadServiceConfigGroupsBySlaves(selectedServices);
  229. this.loadServiceConfigGroupsByClients(selectedServices);
  230. this.sortServiceConfigGroups(selectedServices);
  231. this.set('content.configGroups', selectedServices);
  232. },
  233. /**
  234. * sort config groups by name
  235. * @param selectedServices
  236. */
  237. sortServiceConfigGroups: function (selectedServices) {
  238. selectedServices.forEach(function (selectedService) {
  239. selectedService.configGroups.sort(function (cfgA, cfgB) {
  240. if (cfgA.ConfigGroup.group_name < cfgB.ConfigGroup.group_name) return -1;
  241. if (cfgA.ConfigGroup.group_name > cfgB.ConfigGroup.group_name) return 1;
  242. return 0;
  243. });
  244. });
  245. },
  246. /**
  247. * load service config groups by slave components,
  248. * push them into selectedServices
  249. * @param selectedServices
  250. */
  251. loadServiceConfigGroupsBySlaves: function (selectedServices) {
  252. var slaveComponentHosts = this.get('content.slaveComponentHosts');
  253. if (slaveComponentHosts && slaveComponentHosts.length > 0) {
  254. slaveComponentHosts.forEach(function (slave) {
  255. if (slave.hosts.length > 0) {
  256. if (slave.componentName !== "CLIENT") {
  257. var service = App.StackServiceComponent.find(slave.componentName).get('stackService');
  258. var serviceName = service.get('serviceName');
  259. var configGroups = this.get('content.configGroups').filterProperty('ConfigGroup.tag', serviceName);
  260. var configGroupsNames = configGroups.mapProperty('ConfigGroup.group_name');
  261. var defaultGroupName = service.get('displayName') + ' Default';
  262. var selectedService = selectedServices.findProperty('serviceId', serviceName);
  263. configGroupsNames.unshift(defaultGroupName);
  264. if (selectedService) {
  265. Em.set(selectedService, 'hosts', Em.getWithDefault(selectedService, 'hosts', []).concat(slave.hosts.mapProperty('hostName')).uniq());
  266. } else {
  267. selectedServices.push({
  268. serviceId: serviceName,
  269. displayName: service.get('displayName'),
  270. hosts: slave.hosts.mapProperty('hostName'),
  271. configGroupsNames: configGroupsNames,
  272. configGroups: configGroups,
  273. selectedConfigGroup: defaultGroupName
  274. });
  275. }
  276. }
  277. }
  278. }, this);
  279. return true;
  280. }
  281. return false;
  282. },
  283. /**
  284. * load service config groups by clients,
  285. * push them into selectedServices
  286. * @param selectedServices
  287. */
  288. loadServiceConfigGroupsByClients: function (selectedServices) {
  289. var slaveComponentHosts = this.get('content.slaveComponentHosts');
  290. var clients = this.get('content.clients');
  291. var client = slaveComponentHosts && slaveComponentHosts.findProperty('componentName', 'CLIENT');
  292. var selectedClientHosts = client && client.hosts.mapProperty('hostName');
  293. if (clients && selectedClientHosts && clients.length > 0 && selectedClientHosts.length > 0) {
  294. this.loadClients();
  295. clients.forEach(function (client) {
  296. var service = App.StackServiceComponent.find(client.component_name).get('stackService');
  297. var serviceName = service.get('serviceName');
  298. var serviceMatch = selectedServices.findProperty('serviceId', serviceName);
  299. if (serviceMatch) {
  300. serviceMatch.hosts = serviceMatch.hosts.concat(selectedClientHosts).uniq();
  301. } else {
  302. var configGroups = this.get('content.configGroups').filterProperty('ConfigGroup.tag', serviceName);
  303. var configGroupsNames = configGroups.mapProperty('ConfigGroup.group_name').sort();
  304. var defaultGroupName = service.get('displayName') + ' Default';
  305. configGroupsNames.unshift(defaultGroupName);
  306. selectedServices.push({
  307. serviceId: serviceName,
  308. displayName: service.get('displayName'),
  309. hosts: selectedClientHosts,
  310. configGroupsNames: configGroupsNames,
  311. configGroups: configGroups,
  312. selectedConfigGroup: defaultGroupName
  313. });
  314. }
  315. }, this);
  316. return true;
  317. }
  318. return false;
  319. },
  320. loadServiceConfigProperties: function () {
  321. var serviceConfigProperties = App.db.get('AddService', 'serviceConfigProperties');
  322. if (!serviceConfigProperties || !serviceConfigProperties.length) {
  323. serviceConfigProperties = App.db.get('Installer', 'serviceConfigProperties');
  324. }
  325. this.set('content.serviceConfigProperties', serviceConfigProperties);
  326. console.log("AddHostController.loadServiceConfigProperties: loaded config ", serviceConfigProperties);
  327. },
  328. /**
  329. * Load data for all steps until <code>current step</code>
  330. */
  331. loadAllPriorSteps: function () {
  332. var step = this.get('currentStep');
  333. switch (step) {
  334. case '7':
  335. case '6':
  336. case '5':
  337. this.loadServiceConfigProperties();
  338. this.getServiceConfigGroups();
  339. case '4':
  340. case '3':
  341. this.loadClients();
  342. this.loadServices();
  343. this.loadMasterComponentHosts();
  344. this.loadSlaveComponentHosts();
  345. this.load('hosts');
  346. case '2':
  347. this.loadServices();
  348. case '1':
  349. this.load('hosts');
  350. this.load('installOptions');
  351. this.load('cluster');
  352. }
  353. },
  354. /**
  355. * Remove all loaded data.
  356. * Created as copy for App.router.clearAllSteps
  357. */
  358. clearAllSteps: function () {
  359. this.clearInstallOptions();
  360. // clear temporary information stored during the install
  361. this.set('content.cluster', this.getCluster());
  362. },
  363. clearStorageData: function () {
  364. this._super();
  365. this.resetDbNamespace();
  366. },
  367. /**
  368. * Clear all temporary data
  369. */
  370. finish: function () {
  371. this.setCurrentStep('1');
  372. this.clearAllSteps();
  373. this.clearStorageData();
  374. App.router.get('updateController').updateAll();
  375. App.updater.immediateRun('updateHost');
  376. App.router.get('clusterController').getAllHostNames();
  377. },
  378. /**
  379. * send request to server in order to install services
  380. * @param isRetry
  381. */
  382. installServices: function (isRetry, callback, errorCallback) {
  383. callback = callback || Em.K;
  384. this.set('content.cluster.oldRequestsId', []);
  385. var clusterName = this.get('content.cluster.name');
  386. var hostNames = [];
  387. for (var hostname in this.getDBProperty('hosts')) {
  388. if(this.getDBProperty('hosts')[hostname].isInstalled == false){
  389. hostNames.push(hostname);
  390. }
  391. }
  392. if(!clusterName || hostNames.length === 0) return false;
  393. App.ajax.send({
  394. name: "common.host_components.update",
  395. sender: this,
  396. data: {
  397. "context": Em.I18n.t('requestInfo.installComponents'),
  398. "query": "HostRoles/host_name.in(" + hostNames.join(',') + ")",
  399. "HostRoles": {"state": "INSTALLED"},
  400. "level": "HOST_COMPONENT"
  401. },
  402. success: 'installServicesSuccessCallback',
  403. error: 'installServicesErrorCallback'
  404. }).then(callback, errorCallback || callback);
  405. return true;
  406. }
  407. });