add_controller.js 14 KB

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