add_controller.js 14 KB

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