add_controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. var lazyLoading = require('utils/lazy_loading');
  20. App.AddHostController = App.WizardController.extend({
  21. name: 'addHostController',
  22. totalSteps: 7,
  23. /**
  24. * Used for hiding back button in wizard
  25. */
  26. hideBackButton: true,
  27. /**
  28. * All wizards data will be stored in this variable
  29. *
  30. * cluster - cluster name
  31. * hosts - hosts, ssh key, repo info, etc.
  32. * services - services list
  33. * hostsInfo - list of selected hosts
  34. * slaveComponentHosts, hostSlaveComponents - info about slave hosts
  35. * masterComponentHosts - info about master hosts
  36. * serviceConfigGroups - info about selected config group for service
  37. * configGroups - all config groups
  38. * config??? - to be described later
  39. */
  40. content: Em.Object.create({
  41. cluster: null,
  42. hosts: null,
  43. installOptions: null,
  44. services: null,
  45. slaveComponentHosts: null,
  46. masterComponentHosts: null,
  47. serviceConfigProperties: null,
  48. advancedServiceConfig: null,
  49. controllerName: 'addHostController',
  50. serviceConfigGroups: null,
  51. configGroups: null
  52. }),
  53. /**
  54. * save info about wizard progress, particularly current step of wizard
  55. * @param currentStep
  56. * @param completed
  57. */
  58. setCurrentStep: function (currentStep, completed) {
  59. this._super(currentStep, completed);
  60. App.clusterStatus.setClusterStatus({
  61. wizardControllerName: this.get('name'),
  62. localdb: App.db.data
  63. });
  64. },
  65. /**
  66. * return new object extended from clusterStatusTemplate
  67. * @return Object
  68. */
  69. getCluster: function () {
  70. return jQuery.extend({}, this.get('clusterStatusTemplate'), {name: App.router.getClusterName()});
  71. },
  72. /**
  73. * return new object extended from installOptionsTemplate
  74. * @return Object
  75. */
  76. getInstallOptions: function () {
  77. return jQuery.extend({}, this.get('installOptionsTemplate'));
  78. },
  79. /**
  80. * Remove host from model. Used at <code>Confirm hosts</code> step
  81. * @param hosts Array of hosts, which we want to delete
  82. */
  83. removeHosts: function (hosts) {
  84. var dbHosts = this.getDBProperty('hosts');
  85. hosts.forEach(function (_hostInfo) {
  86. var host = _hostInfo.hostName;
  87. delete dbHosts[host];
  88. });
  89. this.setDBProperty('hosts', dbHosts);
  90. },
  91. /**
  92. * Load services data from server.
  93. * TODO move to mixin
  94. */
  95. loadServicesFromServer: function () {
  96. var apiService = this.loadServiceComponents();
  97. apiService.forEach(function (item, index) {
  98. apiService[index].isSelected = App.Service.find().someProperty('id', item.serviceName);
  99. apiService[index].isDisabled = apiService[index].isSelected;
  100. apiService[index].isInstalled = apiService[index].isSelected;
  101. });
  102. this.set('content.services', apiService);
  103. this.setDBProperty('service', apiService);
  104. },
  105. /**
  106. * Load services data. Will be used at <code>Select services(step4)</code> step
  107. */
  108. loadServices: function () {
  109. var servicesInfo = this.getDBProperty('service');
  110. console.log('AddHostController.loadServices: loaded data ', servicesInfo);
  111. servicesInfo.forEach(function (item, index) {
  112. servicesInfo[index] = Em.Object.create(item);
  113. });
  114. this.set('content.services', servicesInfo);
  115. var serviceNames = servicesInfo.filterProperty('isSelected', true).mapProperty('serviceName');
  116. console.log('selected services ', serviceNames);
  117. },
  118. /**
  119. * Load master component hosts data for using in required step controllers
  120. * TODO move to mixin
  121. */
  122. loadSlaveComponentHosts: function () {
  123. var slaveComponentHosts = this.getDBProperty('slaveComponentHosts');
  124. var message = 'AddHostController.loadSlaveComponentHosts: loaded hosts ';
  125. var filterFunction = function (component) {
  126. return component.isInstalled;
  127. };
  128. if (!slaveComponentHosts) {
  129. this.getSlaveComponentHosts(this, 'content.slaveComponentHosts', message, filterFunction);
  130. } else {
  131. this.set('content.slaveComponentHosts', []);
  132. lazyLoading.run({
  133. initSize: 20,
  134. chunkSize: 50,
  135. delay: 50,
  136. destination: this.get('content.slaveComponentHosts'),
  137. source: slaveComponentHosts,
  138. context: this
  139. });
  140. console.log(message, slaveComponentHosts);
  141. }
  142. },
  143. /**
  144. * Generate clients list for selected services and save it to model
  145. */
  146. saveClients: function () {
  147. App.ajax.send({
  148. name: 'host_components.all',
  149. sender: this,
  150. data: {
  151. clusterName: App.get('clusterName')
  152. },
  153. success: 'saveClientsSuccessCallBack'
  154. });
  155. },
  156. saveClientsSuccessCallBack: function (response) {
  157. var clients = [];
  158. var serviceComponents = App.StackServiceComponent.find();
  159. this.get('content.services').filterProperty('isSelected').forEach(function (_service) {
  160. var client = serviceComponents.filterProperty('serviceName', _service.serviceName).findProperty('isClient');
  161. if (client) {
  162. clients.push({
  163. component_name: client.get('componentName'),
  164. display_name: client.get('displayName'),
  165. isInstalled: response.items.filterProperty('HostRoles.component_name', client.get('componentName')).length > 0
  166. });
  167. }
  168. }, this);
  169. this.setDBProperty('clientInfo', clients);
  170. this.set('content.clients', []);
  171. lazyLoading.run({
  172. initSize: 20,
  173. chunkSize: 50,
  174. delay: 50,
  175. destination: this.get('content.clients'),
  176. source: clients,
  177. context: this
  178. });
  179. console.log("AddHostController.saveClients: saved list ", clients);
  180. },
  181. /**
  182. * Apply config groups from step4 Configurations
  183. */
  184. applyConfigGroup: function () {
  185. var serviceConfigGroups = this.get('content.serviceConfigGroups');
  186. serviceConfigGroups.forEach(function (group) {
  187. if (group.configGroups.someProperty('ConfigGroup.group_name', group.selectedConfigGroup)) {
  188. var configGroup = group.configGroups.findProperty('ConfigGroup.group_name', group.selectedConfigGroup);
  189. group.hosts.forEach(function (host) {
  190. configGroup.ConfigGroup.hosts.push({
  191. host_name: host
  192. });
  193. }, this);
  194. delete configGroup.href;
  195. App.ajax.send({
  196. name: 'config_groups.update_config_group',
  197. sender: this,
  198. data: {
  199. id: configGroup.ConfigGroup.id,
  200. configGroup: configGroup
  201. }
  202. });
  203. }
  204. }, this);
  205. },
  206. /**
  207. * Load information about selected config groups
  208. */
  209. getServiceConfigGroups: function () {
  210. var serviceConfigGroups = this.getDBProperty('serviceConfigGroups');
  211. this.set('content.serviceConfigGroups', serviceConfigGroups);
  212. },
  213. /**
  214. * Save information about selected config groups
  215. */
  216. saveServiceConfigGroups: function () {
  217. this.setDBProperty('serviceConfigGroups', this.get('content.serviceConfigGroups'));
  218. this.set('content.serviceConfigGroups', this.get('content.serviceConfigGroups'));
  219. },
  220. /**
  221. * Set content.serviceConfigGroups for step4
  222. */
  223. loadServiceConfigGroups: function () {
  224. var selectedServices = [];
  225. this.loadServiceConfigGroupsBySlaves(selectedServices);
  226. this.loadServiceConfigGroupsByClients(selectedServices);
  227. this.sortServiceConfigGroups(selectedServices);
  228. this.set('content.serviceConfigGroups', selectedServices);
  229. },
  230. /**
  231. * sort config groups by name
  232. * @param selectedServices
  233. */
  234. sortServiceConfigGroups: function (selectedServices) {
  235. selectedServices.forEach(function (selectedService) {
  236. selectedService.configGroups.sort(function (cfgA, cfgB) {
  237. if (cfgA.ConfigGroup.group_name < cfgB.ConfigGroup.group_name) return -1;
  238. if (cfgA.ConfigGroup.group_name > cfgB.ConfigGroup.group_name) return 1;
  239. return 0;
  240. });
  241. });
  242. },
  243. /**
  244. * load service config groups by slave components,
  245. * push them into selectedServices
  246. * @param selectedServices
  247. */
  248. loadServiceConfigGroupsBySlaves: function (selectedServices) {
  249. var componentServiceMap = App.QuickDataMapper.componentServiceMap();
  250. var slaveComponentHosts = this.get('content.slaveComponentHosts');
  251. if (slaveComponentHosts && slaveComponentHosts.length > 0) {
  252. slaveComponentHosts.forEach(function (slave) {
  253. if (slave.hosts.length > 0) {
  254. if (slave.componentName !== "CLIENT") {
  255. var service = componentServiceMap[slave.componentName];
  256. var configGroups = this.get('content.configGroups').filterProperty('ConfigGroup.tag', service);
  257. var configGroupsNames = configGroups.mapProperty('ConfigGroup.group_name');
  258. var defaultGroupName = App.Service.DisplayNames[service] + ' Default';
  259. configGroupsNames.unshift(defaultGroupName);
  260. selectedServices.push({
  261. serviceId: service,
  262. displayName: App.Service.DisplayNames[service],
  263. hosts: slave.hosts.mapProperty('hostName'),
  264. configGroupsNames: configGroupsNames,
  265. configGroups: configGroups,
  266. selectedConfigGroup: defaultGroupName
  267. });
  268. }
  269. }
  270. }, this);
  271. return true;
  272. }
  273. return false;
  274. },
  275. /**
  276. * load service config groups by clients,
  277. * push them into selectedServices
  278. * @param selectedServices
  279. */
  280. loadServiceConfigGroupsByClients: function (selectedServices) {
  281. var componentServiceMap = App.QuickDataMapper.componentServiceMap();
  282. var slaveComponentHosts = this.get('content.slaveComponentHosts');
  283. var clients = this.get('content.clients');
  284. var client = slaveComponentHosts && slaveComponentHosts.findProperty('componentName', 'CLIENT');
  285. var selectedClientHosts = client && client.hosts.mapProperty('hostName');
  286. if (clients && selectedClientHosts && clients.length > 0 && selectedClientHosts.length > 0) {
  287. this.loadClients();
  288. clients.forEach(function (client) {
  289. var service = componentServiceMap[client.component_name];
  290. var serviceMatch = selectedServices.findProperty('serviceId', service);
  291. if (serviceMatch) {
  292. lazyLoading.run({
  293. initSize: 20,
  294. chunkSize: 50,
  295. delay: 50,
  296. destination: serviceMatch.hosts,
  297. source: selectedClientHosts,
  298. context: Em.Object.create()
  299. });
  300. serviceMatch.hosts = serviceMatch.hosts.uniq();
  301. } else {
  302. var configGroups = this.get('content.configGroups').filterProperty('ConfigGroup.tag', service);
  303. var configGroupsNames = configGroups.mapProperty('ConfigGroup.group_name').sort();
  304. var defaultGroupName = App.Service.DisplayNames[service] + ' Default';
  305. configGroupsNames.unshift(defaultGroupName);
  306. selectedServices.push({
  307. serviceId: service,
  308. displayName: App.Service.DisplayNames[service],
  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. /**
  364. * Clear all temporary data
  365. */
  366. finish: function () {
  367. this.setCurrentStep('1');
  368. this.clearAllSteps();
  369. this.clearStorageData();
  370. App.router.get('updateController').updateAll();
  371. App.updater.immediateRun('updateHost');
  372. },
  373. /**
  374. * send request to server in order to install services
  375. * @param isRetry
  376. */
  377. installServices: function (isRetry) {
  378. this.set('content.cluster.oldRequestsId', []);
  379. var clusterName = this.get('content.cluster.name');
  380. var hostNames = [];
  381. for (var hostname in this.getDBProperty('hosts')) {
  382. hostNames.push(hostname);
  383. }
  384. if(!clusterName || hostNames.length === 0) return false;
  385. var name = 'wizard.install_services.add_host_controller.';
  386. name += (isRetry) ? 'is_retry' : 'not_is_retry';
  387. var data = JSON.stringify({
  388. "RequestInfo": {
  389. "context": Em.I18n.t('requestInfo.installComponents'),
  390. "query": "HostRoles/host_name.in(" + hostNames.join(',') + ")"
  391. },
  392. "Body": {
  393. "HostRoles": {"state": "INSTALLED"}
  394. }
  395. });
  396. App.ajax.send({
  397. name: name,
  398. sender: this,
  399. data: {
  400. data: data,
  401. cluster: clusterName
  402. },
  403. success: 'installServicesSuccessCallback',
  404. error: 'installServicesErrorCallback'
  405. });
  406. return true;
  407. }
  408. });