add_controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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.serviceConfigGroups');
  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.serviceConfigGroups', serviceConfigGroups);
  201. },
  202. /**
  203. * Save information about selected config groups
  204. */
  205. saveServiceConfigGroups: function () {
  206. this.setDBProperty('serviceConfigGroups', this.get('content.serviceConfigGroups'));
  207. this.set('content.serviceConfigGroups', this.get('content.serviceConfigGroups'));
  208. },
  209. /**
  210. * Set content.serviceConfigGroups for step4
  211. */
  212. loadServiceConfigGroups: function () {
  213. var selectedServices = [];
  214. this.loadServiceConfigGroupsBySlaves(selectedServices);
  215. this.loadServiceConfigGroupsByClients(selectedServices);
  216. this.sortServiceConfigGroups(selectedServices);
  217. this.set('content.serviceConfigGroups', selectedServices);
  218. },
  219. /**
  220. * sort config groups by name
  221. * @param selectedServices
  222. */
  223. sortServiceConfigGroups: function (selectedServices) {
  224. selectedServices.forEach(function (selectedService) {
  225. selectedService.configGroups.sort(function (cfgA, cfgB) {
  226. if (cfgA.ConfigGroup.group_name < cfgB.ConfigGroup.group_name) return -1;
  227. if (cfgA.ConfigGroup.group_name > cfgB.ConfigGroup.group_name) return 1;
  228. return 0;
  229. });
  230. });
  231. },
  232. /**
  233. * load service config groups by slave components,
  234. * push them into selectedServices
  235. * @param selectedServices
  236. */
  237. loadServiceConfigGroupsBySlaves: function (selectedServices) {
  238. var componentServiceMap = App.QuickDataMapper.componentServiceMap();
  239. var slaveComponentHosts = this.get('content.slaveComponentHosts');
  240. if (slaveComponentHosts && slaveComponentHosts.length > 0) {
  241. slaveComponentHosts.forEach(function (slave) {
  242. if (slave.hosts.length > 0) {
  243. if (slave.componentName !== "CLIENT") {
  244. var service = componentServiceMap[slave.componentName];
  245. var configGroups = this.get('content.configGroups').filterProperty('ConfigGroup.tag', service);
  246. var configGroupsNames = configGroups.mapProperty('ConfigGroup.group_name');
  247. var defaultGroupName = App.Service.DisplayNames[service] + ' Default';
  248. configGroupsNames.unshift(defaultGroupName);
  249. selectedServices.push({
  250. serviceId: service,
  251. displayName: App.Service.DisplayNames[service],
  252. hosts: slave.hosts.mapProperty('hostName'),
  253. configGroupsNames: configGroupsNames,
  254. configGroups: configGroups,
  255. selectedConfigGroup: defaultGroupName
  256. });
  257. }
  258. }
  259. }, this);
  260. return true;
  261. }
  262. return false;
  263. },
  264. /**
  265. * load service config groups by clients,
  266. * push them into selectedServices
  267. * @param selectedServices
  268. */
  269. loadServiceConfigGroupsByClients: function (selectedServices) {
  270. var componentServiceMap = App.QuickDataMapper.componentServiceMap();
  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 = componentServiceMap[client.component_name];
  279. var serviceMatch = selectedServices.findProperty('serviceId', service);
  280. if (serviceMatch) {
  281. serviceMatch.hosts = serviceMatch.hosts.concat(selectedClientHosts).uniq();
  282. } else {
  283. var configGroups = this.get('content.configGroups').filterProperty('ConfigGroup.tag', service);
  284. var configGroupsNames = configGroups.mapProperty('ConfigGroup.group_name').sort();
  285. var defaultGroupName = App.Service.DisplayNames[service] + ' Default';
  286. configGroupsNames.unshift(defaultGroupName);
  287. selectedServices.push({
  288. serviceId: service,
  289. displayName: App.Service.DisplayNames[service],
  290. hosts: selectedClientHosts,
  291. configGroupsNames: configGroupsNames,
  292. configGroups: configGroups,
  293. selectedConfigGroup: defaultGroupName
  294. });
  295. }
  296. }, this);
  297. return true;
  298. }
  299. return false;
  300. },
  301. loadServiceConfigProperties: function () {
  302. var serviceConfigProperties = App.db.get('AddService', 'serviceConfigProperties');
  303. if (!serviceConfigProperties || !serviceConfigProperties.length) {
  304. serviceConfigProperties = App.db.get('Installer', 'serviceConfigProperties');
  305. }
  306. this.set('content.serviceConfigProperties', serviceConfigProperties);
  307. console.log("AddHostController.loadServiceConfigProperties: loaded config ", serviceConfigProperties);
  308. },
  309. /**
  310. * Load data for all steps until <code>current step</code>
  311. */
  312. loadAllPriorSteps: function () {
  313. var step = this.get('currentStep');
  314. switch (step) {
  315. case '7':
  316. case '6':
  317. case '5':
  318. this.loadServiceConfigProperties();
  319. this.getServiceConfigGroups();
  320. case '4':
  321. case '3':
  322. this.loadClients();
  323. this.loadServices();
  324. this.loadMasterComponentHosts();
  325. this.loadSlaveComponentHosts();
  326. this.load('hosts');
  327. case '2':
  328. this.loadServices();
  329. case '1':
  330. this.load('hosts');
  331. this.load('installOptions');
  332. this.load('cluster');
  333. }
  334. },
  335. /**
  336. * Remove all loaded data.
  337. * Created as copy for App.router.clearAllSteps
  338. */
  339. clearAllSteps: function () {
  340. this.clearInstallOptions();
  341. // clear temporary information stored during the install
  342. this.set('content.cluster', this.getCluster());
  343. },
  344. /**
  345. * Clear all temporary data
  346. */
  347. finish: function () {
  348. this.setCurrentStep('1');
  349. this.clearAllSteps();
  350. this.clearStorageData();
  351. App.router.get('updateController').updateAll();
  352. App.updater.immediateRun('updateHost');
  353. },
  354. /**
  355. * send request to server in order to install services
  356. * @param isRetry
  357. */
  358. installServices: function (isRetry, callback) {
  359. callback = callback || Em.K;
  360. this.set('content.cluster.oldRequestsId', []);
  361. var clusterName = this.get('content.cluster.name');
  362. var hostNames = [];
  363. for (var hostname in this.getDBProperty('hosts')) {
  364. if(this.getDBProperty('hosts')[hostname].isInstalled == false){
  365. hostNames.push(hostname);
  366. }
  367. }
  368. if(!clusterName || hostNames.length === 0) return false;
  369. App.ajax.send({
  370. name: "common.host_components.update",
  371. sender: this,
  372. data: {
  373. "context": Em.I18n.t('requestInfo.installComponents'),
  374. "query": "HostRoles/host_name.in(" + hostNames.join(',') + ")",
  375. "HostRoles": {"state": "INSTALLED"}
  376. },
  377. success: 'installServicesSuccessCallback',
  378. error: 'installServicesErrorCallback'
  379. }).then(callback, callback);
  380. return true;
  381. }
  382. });