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