add_controller.js 14 KB

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