add_controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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('id', 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 clients = [];
  149. var serviceComponents = App.StackServiceComponent.find();
  150. var clientComponents = [];
  151. var hosts = this.get('content.hosts');
  152. for (var hostName in hosts) {
  153. if(hosts[hostName].isInstalled) {
  154. hosts[hostName].hostComponents.forEach(function (component) {
  155. clientComponents[component.HostRoles.component_name] = true;
  156. }, this);
  157. }
  158. }
  159. this.get('content.services').filterProperty('isSelected').forEach(function (_service) {
  160. var client = serviceComponents.filterProperty('serviceName', _service.get('serviceName')).findProperty('isClient');
  161. if (client) {
  162. clients.push({
  163. component_name: client.get('componentName'),
  164. display_name: client.get('displayName'),
  165. isInstalled: !!clientComponents[client.get('componentName')]
  166. });
  167. }
  168. }, this);
  169. this.setDBProperty('clientInfo', clients);
  170. this.set('content.clients', clients);
  171. console.log("AddHostController.saveClients: saved list ", 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 configGroups = this.get('content.configGroups').filterProperty('ConfigGroup.tag', service);
  247. var configGroupsNames = configGroups.mapProperty('ConfigGroup.group_name');
  248. var defaultGroupName = service.get('displayName') + ' Default';
  249. configGroupsNames.unshift(defaultGroupName);
  250. selectedServices.push({
  251. serviceId: service.get('serviceName'),
  252. displayName: service.get('displayName'),
  253. hosts: slave.hosts.mapProperty('hostName'),
  254. configGroupsNames: configGroupsNames,
  255. configGroups: configGroups,
  256. selectedConfigGroup: defaultGroupName
  257. });
  258. }
  259. }
  260. }, this);
  261. return true;
  262. }
  263. return false;
  264. },
  265. /**
  266. * load service config groups by clients,
  267. * push them into selectedServices
  268. * @param selectedServices
  269. */
  270. loadServiceConfigGroupsByClients: function (selectedServices) {
  271. var slaveComponentHosts = this.get('content.slaveComponentHosts');
  272. var clients = this.get('content.clients');
  273. var client = slaveComponentHosts && slaveComponentHosts.findProperty('componentName', 'CLIENT');
  274. var selectedClientHosts = client && client.hosts.mapProperty('hostName');
  275. if (clients && selectedClientHosts && clients.length > 0 && selectedClientHosts.length > 0) {
  276. this.loadClients();
  277. clients.forEach(function (client) {
  278. var service = App.StackServiceComponent.find(client.component_name).get('stackService');
  279. var serviceName = service.get('serviceName');
  280. var serviceMatch = selectedServices.findProperty('serviceId', serviceName);
  281. if (serviceMatch) {
  282. serviceMatch.hosts = serviceMatch.hosts.concat(selectedClientHosts).uniq();
  283. } else {
  284. var configGroups = this.get('content.configGroups').filterProperty('ConfigGroup.tag', serviceName);
  285. var configGroupsNames = configGroups.mapProperty('ConfigGroup.group_name').sort();
  286. var defaultGroupName = service.get('displayName') + ' Default';
  287. configGroupsNames.unshift(defaultGroupName);
  288. selectedServices.push({
  289. serviceId: serviceName,
  290. displayName: service.get('displayName'),
  291. hosts: selectedClientHosts,
  292. configGroupsNames: configGroupsNames,
  293. configGroups: configGroups,
  294. selectedConfigGroup: defaultGroupName
  295. });
  296. }
  297. }, this);
  298. return true;
  299. }
  300. return false;
  301. },
  302. loadServiceConfigProperties: function () {
  303. var serviceConfigProperties = App.db.get('AddService', 'serviceConfigProperties');
  304. if (!serviceConfigProperties || !serviceConfigProperties.length) {
  305. serviceConfigProperties = App.db.get('Installer', 'serviceConfigProperties');
  306. }
  307. this.set('content.serviceConfigProperties', serviceConfigProperties);
  308. console.log("AddHostController.loadServiceConfigProperties: loaded config ", serviceConfigProperties);
  309. },
  310. /**
  311. * Load data for all steps until <code>current step</code>
  312. */
  313. loadAllPriorSteps: function () {
  314. var step = this.get('currentStep');
  315. switch (step) {
  316. case '7':
  317. case '6':
  318. case '5':
  319. this.loadServiceConfigProperties();
  320. this.getServiceConfigGroups();
  321. case '4':
  322. case '3':
  323. this.loadClients();
  324. this.loadServices();
  325. this.loadMasterComponentHosts();
  326. this.loadSlaveComponentHosts();
  327. this.load('hosts');
  328. case '2':
  329. this.loadServices();
  330. case '1':
  331. this.load('hosts');
  332. this.load('installOptions');
  333. this.load('cluster');
  334. }
  335. },
  336. /**
  337. * Remove all loaded data.
  338. * Created as copy for App.router.clearAllSteps
  339. */
  340. clearAllSteps: function () {
  341. this.clearInstallOptions();
  342. // clear temporary information stored during the install
  343. this.set('content.cluster', this.getCluster());
  344. },
  345. clearStorageData: function () {
  346. this._super();
  347. App.db.cleanAddHost();
  348. },
  349. /**
  350. * save the local db data stored on the server as value to the key api/v1/persist/CLUSTER_CURRENT_STATUS
  351. */
  352. saveClusterState: function () {
  353. App.clusterStatus.setClusterStatus({
  354. clusterName: App.router.getClusterName(),
  355. clusterState: 'DEFAULT'
  356. });
  357. },
  358. clearData: function () {
  359. // Clear Add Host namespace in Browser Local storage and save it to server persist data
  360. this.clearStorageData();
  361. this.saveClusterState();
  362. },
  363. /**
  364. * Clear all temporary data
  365. */
  366. finish: function () {
  367. this.setCurrentStep('1');
  368. this.clearAllSteps();
  369. this.clearData();
  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, callback) {
  378. callback = callback || Em.K;
  379. this.set('content.cluster.oldRequestsId', []);
  380. var clusterName = this.get('content.cluster.name');
  381. var hostNames = [];
  382. for (var hostname in this.getDBProperty('hosts')) {
  383. if(this.getDBProperty('hosts')[hostname].isInstalled == false){
  384. hostNames.push(hostname);
  385. }
  386. }
  387. if(!clusterName || hostNames.length === 0) return false;
  388. App.ajax.send({
  389. name: "common.host_components.update",
  390. sender: this,
  391. data: {
  392. "context": Em.I18n.t('requestInfo.installComponents'),
  393. "query": "HostRoles/host_name.in(" + hostNames.join(',') + ")",
  394. "HostRoles": {"state": "INSTALLED"}
  395. },
  396. success: 'installServicesSuccessCallback',
  397. error: 'installServicesErrorCallback'
  398. }).then(callback, callback);
  399. return true;
  400. }
  401. });