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