add_controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 hosts = this.get('content.hosts');
  151. this.get('content.services').filterProperty('isSelected').forEach(function (_service) {
  152. var client = serviceComponents.filterProperty('serviceName', _service.get('serviceName')).findProperty('isClient');
  153. if (client) {
  154. clients.push({
  155. component_name: client.get('componentName'),
  156. display_name: client.get('displayName'),
  157. isInstalled: false
  158. });
  159. }
  160. }, this);
  161. this.setDBProperty('clientInfo', clients);
  162. this.set('content.clients', clients);
  163. console.log("AddHostController.saveClients: saved list ", clients);
  164. },
  165. /**
  166. * Apply config groups from step4 Configurations
  167. */
  168. applyConfigGroup: function () {
  169. var serviceConfigGroups = this.get('content.configGroups');
  170. serviceConfigGroups.forEach(function (group) {
  171. if (group.configGroups.someProperty('ConfigGroup.group_name', group.selectedConfigGroup)) {
  172. var configGroup = group.configGroups.findProperty('ConfigGroup.group_name', group.selectedConfigGroup);
  173. group.hosts.forEach(function (host) {
  174. configGroup.ConfigGroup.hosts.push({
  175. host_name: host
  176. });
  177. }, this);
  178. delete configGroup.href;
  179. App.ajax.send({
  180. name: 'config_groups.update_config_group',
  181. sender: this,
  182. data: {
  183. id: configGroup.ConfigGroup.id,
  184. configGroup: configGroup
  185. }
  186. });
  187. }
  188. }, this);
  189. },
  190. /**
  191. * Load information about selected config groups
  192. */
  193. getServiceConfigGroups: function () {
  194. var serviceConfigGroups = this.getDBProperty('serviceConfigGroups');
  195. this.set('content.configGroups', serviceConfigGroups);
  196. },
  197. /**
  198. * Save information about selected config groups
  199. */
  200. saveServiceConfigGroups: function () {
  201. this.setDBProperty('serviceConfigGroups', this.get('content.configGroups'));
  202. },
  203. /**
  204. * Set content.configGroups for step4
  205. */
  206. loadServiceConfigGroups: function () {
  207. var selectedServices = [];
  208. this.loadServiceConfigGroupsBySlaves(selectedServices);
  209. this.loadServiceConfigGroupsByClients(selectedServices);
  210. this.sortServiceConfigGroups(selectedServices);
  211. this.set('content.configGroups', selectedServices);
  212. },
  213. /**
  214. * sort config groups by name
  215. * @param selectedServices
  216. */
  217. sortServiceConfigGroups: function (selectedServices) {
  218. selectedServices.forEach(function (selectedService) {
  219. selectedService.configGroups.sort(function (cfgA, cfgB) {
  220. if (cfgA.ConfigGroup.group_name < cfgB.ConfigGroup.group_name) return -1;
  221. if (cfgA.ConfigGroup.group_name > cfgB.ConfigGroup.group_name) return 1;
  222. return 0;
  223. });
  224. });
  225. },
  226. /**
  227. * load service config groups by slave components,
  228. * push them into selectedServices
  229. * @param selectedServices
  230. */
  231. loadServiceConfigGroupsBySlaves: function (selectedServices) {
  232. var slaveComponentHosts = this.get('content.slaveComponentHosts');
  233. if (slaveComponentHosts && slaveComponentHosts.length > 0) {
  234. slaveComponentHosts.forEach(function (slave) {
  235. if (slave.hosts.length > 0) {
  236. if (slave.componentName !== "CLIENT") {
  237. var service = App.StackServiceComponent.find(slave.componentName).get('stackService');
  238. var serviceName = service.get('serviceName');
  239. var configGroups = this.get('content.configGroups').filterProperty('ConfigGroup.tag', serviceName);
  240. var configGroupsNames = configGroups.mapProperty('ConfigGroup.group_name');
  241. var defaultGroupName = service.get('displayName') + ' Default';
  242. configGroupsNames.unshift(defaultGroupName);
  243. selectedServices.push({
  244. serviceId: serviceName,
  245. displayName: service.get('displayName'),
  246. hosts: slave.hosts.mapProperty('hostName'),
  247. configGroupsNames: configGroupsNames,
  248. configGroups: configGroups,
  249. selectedConfigGroup: defaultGroupName
  250. });
  251. }
  252. }
  253. }, this);
  254. return true;
  255. }
  256. return false;
  257. },
  258. /**
  259. * load service config groups by clients,
  260. * push them into selectedServices
  261. * @param selectedServices
  262. */
  263. loadServiceConfigGroupsByClients: function (selectedServices) {
  264. var slaveComponentHosts = this.get('content.slaveComponentHosts');
  265. var clients = this.get('content.clients');
  266. var client = slaveComponentHosts && slaveComponentHosts.findProperty('componentName', 'CLIENT');
  267. var selectedClientHosts = client && client.hosts.mapProperty('hostName');
  268. if (clients && selectedClientHosts && clients.length > 0 && selectedClientHosts.length > 0) {
  269. this.loadClients();
  270. clients.forEach(function (client) {
  271. var service = App.StackServiceComponent.find(client.component_name).get('stackService');
  272. var serviceName = service.get('serviceName');
  273. var serviceMatch = selectedServices.findProperty('serviceId', serviceName);
  274. if (serviceMatch) {
  275. serviceMatch.hosts = serviceMatch.hosts.concat(selectedClientHosts).uniq();
  276. } else {
  277. var configGroups = this.get('content.configGroups').filterProperty('ConfigGroup.tag', serviceName);
  278. var configGroupsNames = configGroups.mapProperty('ConfigGroup.group_name').sort();
  279. var defaultGroupName = service.get('displayName') + ' Default';
  280. configGroupsNames.unshift(defaultGroupName);
  281. selectedServices.push({
  282. serviceId: serviceName,
  283. displayName: service.get('displayName'),
  284. hosts: selectedClientHosts,
  285. configGroupsNames: configGroupsNames,
  286. configGroups: configGroups,
  287. selectedConfigGroup: defaultGroupName
  288. });
  289. }
  290. }, this);
  291. return true;
  292. }
  293. return false;
  294. },
  295. loadServiceConfigProperties: function () {
  296. var serviceConfigProperties = App.db.get('AddService', 'serviceConfigProperties');
  297. if (!serviceConfigProperties || !serviceConfigProperties.length) {
  298. serviceConfigProperties = App.db.get('Installer', 'serviceConfigProperties');
  299. }
  300. this.set('content.serviceConfigProperties', serviceConfigProperties);
  301. console.log("AddHostController.loadServiceConfigProperties: loaded config ", serviceConfigProperties);
  302. },
  303. /**
  304. * Load data for all steps until <code>current step</code>
  305. */
  306. loadAllPriorSteps: function () {
  307. var step = this.get('currentStep');
  308. switch (step) {
  309. case '7':
  310. case '6':
  311. case '5':
  312. this.loadServiceConfigProperties();
  313. this.getServiceConfigGroups();
  314. case '4':
  315. case '3':
  316. this.loadClients();
  317. this.loadServices();
  318. this.loadMasterComponentHosts();
  319. this.loadSlaveComponentHosts();
  320. this.load('hosts');
  321. case '2':
  322. this.loadServices();
  323. case '1':
  324. this.load('hosts');
  325. this.load('installOptions');
  326. this.load('cluster');
  327. }
  328. },
  329. /**
  330. * Remove all loaded data.
  331. * Created as copy for App.router.clearAllSteps
  332. */
  333. clearAllSteps: function () {
  334. this.clearInstallOptions();
  335. // clear temporary information stored during the install
  336. this.set('content.cluster', this.getCluster());
  337. },
  338. clearStorageData: function () {
  339. this._super();
  340. this.resetDbNamespace();
  341. },
  342. /**
  343. * Clear all temporary data
  344. */
  345. finish: function () {
  346. this.setCurrentStep('1');
  347. this.clearAllSteps();
  348. this.clearStorageData();
  349. App.router.get('updateController').updateAll();
  350. App.updater.immediateRun('updateHost');
  351. App.router.get('clusterController').getAllHostNames();
  352. },
  353. /**
  354. * send request to server in order to install services
  355. * @param isRetry
  356. */
  357. installServices: function (isRetry, callback) {
  358. callback = callback || Em.K;
  359. this.set('content.cluster.oldRequestsId', []);
  360. var clusterName = this.get('content.cluster.name');
  361. var hostNames = [];
  362. for (var hostname in this.getDBProperty('hosts')) {
  363. if(this.getDBProperty('hosts')[hostname].isInstalled == false){
  364. hostNames.push(hostname);
  365. }
  366. }
  367. if(!clusterName || hostNames.length === 0) return false;
  368. App.ajax.send({
  369. name: "common.host_components.update",
  370. sender: this,
  371. data: {
  372. "context": Em.I18n.t('requestInfo.installComponents'),
  373. "query": "HostRoles/host_name.in(" + hostNames.join(',') + ")",
  374. "HostRoles": {"state": "INSTALLED"},
  375. "level": "HOST_COMPONENT"
  376. },
  377. success: 'installServicesSuccessCallback',
  378. error: 'installServicesErrorCallback'
  379. }).then(callback, callback);
  380. return true;
  381. }
  382. });