add_controller.js 15 KB

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