step3.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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.MainAdminSecurityAddStep3Controller = Em.Controller.extend({
  20. name: 'mainAdminSecurityAddStep3Controller',
  21. configMapping: require('data/secure_mapping'),
  22. stages: [],
  23. configs: [],
  24. noOfWaitingAjaxCalls: 0,
  25. secureServices: [],
  26. serviceConfigTags: [],
  27. globalProperties: [],
  28. clearStep: function () {
  29. this.get('stages').clear();
  30. },
  31. loadStep: function () {
  32. this.clearStep();
  33. this.loadStages();
  34. this.addInfoToStages();
  35. this.prepareSecureConfigs();
  36. // this.populateSuccededStages();
  37. this.moveToNextStage();
  38. },
  39. loadStages: function () {
  40. this.get('stages').pushObjects([
  41. App.Poll.create({stage: 'stage2', label: Em.I18n.translations['admin.addSecurity.apply.stage2'], isPolling: true}),
  42. App.Poll.create({stage: 'stage3', label: Em.I18n.translations['admin.addSecurity.apply.stage3'], isPolling: false}),
  43. App.Poll.create({stage: 'stage4', label: Em.I18n.translations['admin.addSecurity.apply.stage4'], isPolling: true})
  44. ]);
  45. },
  46. populateSuccededStages: function () {
  47. var currentStage = 'stage' + this.get('content').loadCurrentStage();
  48. var inc = 1;
  49. while (inc < currentStage) {
  50. var stage = 'stage' + inc;
  51. this.get('stages').findProperty('stage', stage).setProperties({ isStarted: true, isCompleted: true });
  52. }
  53. },
  54. startStage: function () {
  55. var startedStages = this.get('stages').filterProperty('isStarted', true);
  56. if (startedStages.length) {
  57. var currentStage = startedStages.findProperty('isCompleted', false);
  58. if (currentStage && currentStage.get('isPolling') === true) {
  59. currentStage.start();
  60. } else if (currentStage && currentStage.get('stage') === 'stage3') {
  61. if (App.testMode) {
  62. currentStage.set('isSuccess', true);
  63. currentStage.set('isCompleted', true);
  64. this.moveToNextStage();
  65. } else {
  66. this.loadConfigsForAllServices();
  67. }
  68. }
  69. }
  70. }.observes('stages.@each.isStarted'),
  71. onCompleteStage: function () {
  72. var index = this.get('stages').filterProperty('isSuccess', true).length;
  73. if (index > 0) {
  74. this.moveToNextStage();
  75. }
  76. }.observes('stages.@each.isSuccess'),
  77. addInfoToStages: function () {
  78. // this.addInfoToStage1();
  79. this.addInfoToStage2();
  80. this.addInfoToStage3();
  81. this.addInfoToStage4();
  82. },
  83. addInfoToStage1: function () {
  84. var stage1 = this.get('stages').findProperty('stage', 'stage1');
  85. if (App.testMode) {
  86. stage1.set('isSucces', true);
  87. stage1.set('isStarted', true);
  88. stage1.set('isCompleted', true);
  89. }
  90. },
  91. addInfoToStage2: function () {
  92. var stage2 = this.get('stages').findProperty('stage', 'stage2');
  93. var url = (App.testMode) ? '/data/wizard/deploy/2_hosts/poll_1.json' : App.apiPrefix + '/clusters/' + App.router.getClusterName() + '/services';
  94. var data = '{"ServiceInfo": {"state": "INSTALLED"}}';
  95. stage2.set('url', url);
  96. stage2.set('data', data);
  97. },
  98. addInfoToStage3: function () {
  99. },
  100. addInfoToStage4: function () {
  101. var stage4 = this.get('stages').findProperty('stage', 'stage4');
  102. var url = (App.testMode) ? '/data/wizard/deploy/2_hosts/poll_1.json' : App.apiPrefix + '/clusters/' + App.router.getClusterName() + '/services';
  103. var data = '{"ServiceInfo": {"state": "STARTED"}}';
  104. stage4.set('url', url);
  105. stage4.set('data', data);
  106. },
  107. loadUiSideConfigs: function () {
  108. var uiConfig = [];
  109. var configs = this.get('configMapping').filterProperty('foreignKey', null);
  110. configs.forEach(function (_config) {
  111. var value = this.getGlobConfigValue(_config.templateName, _config.value, _config.name);
  112. uiConfig.pushObject({
  113. "id": "site property",
  114. "name": _config.name,
  115. "value": value,
  116. "filename": _config.filename
  117. });
  118. }, this);
  119. var dependentConfig = this.get('configMapping').filterProperty('foreignKey');
  120. dependentConfig.forEach(function (_config) {
  121. this.setConfigValue(uiConfig, _config);
  122. uiConfig.pushObject({
  123. "id": "site property",
  124. "name": _config.name,
  125. "value": _config.value,
  126. "filename": _config.filename
  127. });
  128. }, this);
  129. return uiConfig;
  130. },
  131. /**
  132. * Set all site property that are derived from other puppet-variable
  133. */
  134. getGlobConfigValue: function (templateName, expression, name) {
  135. var express = expression.match(/<(.*?)>/g);
  136. var value = expression;
  137. if (express == null) {
  138. return expression;
  139. }
  140. express.forEach(function (_express) {
  141. console.log("The value of template is: " + _express);
  142. if (_express.match(/\[([\d]*)(?=\])/ === null)) {
  143. }
  144. var index = parseInt(_express.match(/\[([\d]*)(?=\])/)[1]);
  145. if (this.get('globalProperties').someProperty('name', templateName[index])) {
  146. //console.log("The name of the variable is: " + this.get('content.serviceConfigProperties').findProperty('name', templateName[index]).name);
  147. var globValue = this.get('globalProperties').findProperty('name', templateName[index]).value;
  148. if (value !== null) { // if the property depends on more than one template name like <templateName[0]>/<templateName[1]> then don't proceed to the next if the prior is null or not found in the global configs
  149. value = value.replace(_express, globValue);
  150. }
  151. } else {
  152. /*
  153. console.log("ERROR: The variable name is: " + templateName[index]);
  154. console.log("ERROR: mapped config from configMapping file has no corresponding variable in " +
  155. "content.serviceConfigProperties. Two possible reasons for the error could be: 1) The service is not selected. " +
  156. "and/OR 2) The service_config metadata file has no corresponding global var for the site property variable");
  157. */
  158. value = null;
  159. }
  160. }, this);
  161. return value;
  162. },
  163. /**
  164. * Set all site property that are derived from other site-properties
  165. */
  166. setConfigValue: function (uiConfig, config) {
  167. if (config.value == null) {
  168. return;
  169. }
  170. var fkValue = config.value.match(/<(foreignKey.*?)>/g);
  171. if (fkValue) {
  172. fkValue.forEach(function (_fkValue) {
  173. var index = parseInt(_fkValue.match(/\[([\d]*)(?=\])/)[1]);
  174. if (uiConfig.someProperty('name', config.foreignKey[index])) {
  175. var globalValue = uiConfig.findProperty('name', config.foreignKey[index]).value;
  176. config.value = config.value.replace(_fkValue, globalValue);
  177. } else if (this.get('content.serviceConfigProperties').someProperty('name', config.foreignKey[index])) {
  178. var globalValue;
  179. if (this.get('content.serviceConfigProperties').findProperty('name', config.foreignKey[index]).value === '') {
  180. globalValue = this.get('content.serviceConfigProperties').findProperty('name', config.foreignKey[index]).defaultValue;
  181. } else {
  182. globalValue = this.get('content.serviceConfigProperties').findProperty('name', config.foreignKey[index]).value;
  183. }
  184. config.value = config.value.replace(_fkValue, globalValue);
  185. }
  186. }, this);
  187. }
  188. if (fkValue = config.name.match(/<(foreignKey.*?)>/g)) {
  189. fkValue.forEach(function (_fkValue) {
  190. var index = parseInt(_fkValue.match(/\[([\d]*)(?=\])/)[1]);
  191. if (uiConfig.someProperty('name', config.foreignKey[index])) {
  192. var globalValue = uiConfig.findProperty('name', config.foreignKey[index]).value;
  193. config.name = config.name.replace(_fkValue, globalValue);
  194. } else if (this.get('content.serviceConfigProperties').someProperty('name', config.foreignKey[index])) {
  195. var globalValue;
  196. if (this.get('content.serviceConfigProperties').findProperty('name', config.foreignKey[index]).value === '') {
  197. globalValue = this.get('content.serviceConfigProperties').findProperty('name', config.foreignKey[index]).defaultValue;
  198. } else {
  199. globalValue = this.get('content.serviceConfigProperties').findProperty('name', config.foreignKey[index]).value;
  200. }
  201. config.name = config.name.replace(_fkValue, globalValue);
  202. }
  203. }, this);
  204. }
  205. //For properties in the configMapping file having foreignKey and templateName properties.
  206. var templateValue = config.value.match(/<(templateName.*?)>/g);
  207. if (templateValue) {
  208. templateValue.forEach(function (_value) {
  209. var index = parseInt(_value.match(/\[([\d]*)(?=\])/)[1]);
  210. if (this.get('globalProperties').someProperty('name', config.templateName[index])) {
  211. var globalValue = this.get('globalProperties').findProperty('name', config.templateName[index]).value;
  212. config.value = config.value.replace(_value, globalValue);
  213. } else {
  214. config.value = null;
  215. }
  216. }, this);
  217. }
  218. },
  219. prepareSecureConfigs: function () {
  220. this.loadGlobals();
  221. this.loadInstanceName();
  222. var storedConfigs = this.get('content.serviceConfigProperties').filterProperty('id', 'site property');
  223. var uiConfigs = this.loadUiSideConfigs();
  224. this.set('configs', storedConfigs.concat(uiConfigs));
  225. },
  226. loadGlobals: function () {
  227. var globals = this.get('content.serviceConfigProperties').filterProperty('id', 'puppet var');
  228. this.set('globalProperties', globals);
  229. },
  230. loadInstanceName: function () {
  231. var isInstanceName = this.get('globalProperties').findProperty('name', 'instance_name');
  232. if (isInstanceName) {
  233. this.get('globalProperties').forEach(function (_globalProperty) {
  234. if (/primary_name?$/.test(_globalProperty.name)) {
  235. if (!/_HOST?$/.test(_globalProperty.value)) {
  236. _globalProperty.value = _globalProperty.value + "/_HOST";
  237. }
  238. }
  239. }, this);
  240. }
  241. },
  242. loadConfigsForAllServices: function () {
  243. this.set('noOfWaitingAjaxCalls', this.get('content.services').length - 2);
  244. this.get('content.services').forEach(function (_secureService, index) {
  245. if (_secureService.serviceName !== 'GENERAL' && _secureService.serviceName !== 'NAGIOS') {
  246. this.getConfigDetailsFromServer(_secureService, index);
  247. }
  248. }, this);
  249. },
  250. getConfigDetailsFromServer: function (secureService, id) {
  251. var self = this;
  252. var url = App.apiPrefix + '/clusters/' + App.router.getClusterName() + '/services/' + secureService.serviceName;
  253. $.ajax({
  254. type: 'GET',
  255. url: url,
  256. timeout: 10000,
  257. dataType: 'text',
  258. success: function (data) {
  259. console.log("TRACE: In success function for the GET getServciceConfigs call");
  260. console.log("TRACE: The url is: " + url);
  261. var jsonData = jQuery.parseJSON(data);
  262. //prepare tags to fetch all configuration for a service
  263. self.setServiceTagNames(secureService.serviceName, jsonData.ServiceInfo.desired_configs);
  264. self.set('noOfWaitingAjaxCalls', self.get('noOfWaitingAjaxCalls') - 1);
  265. if (self.get('noOfWaitingAjaxCalls') == 0) {
  266. self.getAllConfigsFromServer();
  267. }
  268. },
  269. error: function (request, ajaxOptions, error) {
  270. self.get('stages').findProperty('stage', 'stage3').set('isError', true);
  271. console.log("TRACE: In error function for the getServciceConfigs call");
  272. console.log("TRACE: value of the url is: " + url);
  273. console.log("TRACE: error code status is: " + request.status);
  274. },
  275. statusCode: require('data/statusCodes')
  276. });
  277. },
  278. /**
  279. * set tagnames for configuration of the *-site.xml
  280. */
  281. setServiceTagNames: function (secureServiceName, configs) {
  282. console.log("TRACE: In setServiceTagNames function:");
  283. //var serviceConfigTags = this.get('serviceConfigTags');
  284. for (var index in configs) {
  285. var serviceConfigObj = {
  286. serviceName: secureServiceName,
  287. siteName: index,
  288. tagName: configs[index],
  289. newTagName: null,
  290. configs: {}
  291. };
  292. console.log("The value of serviceConfigTags[index]: " + configs[index]);
  293. this.get('serviceConfigTags').pushObject(serviceConfigObj);
  294. }
  295. return serviceConfigObj;
  296. },
  297. getAllConfigsFromServer: function () {
  298. this.set('noOfWaitingAjaxCalls', this.get('serviceConfigTags').length - 1);
  299. this.get('serviceConfigTags').forEach(function (_serviceConfigTags) {
  300. if (_serviceConfigTags.serviceName !== 'MAPREDUCE' || _serviceConfigTags.siteName !== 'core-site') { //skip MapReduce core-site configuration
  301. this.getServiceConfigsFromServer(_serviceConfigTags);
  302. }
  303. }, this);
  304. },
  305. createConfigurations: function () {
  306. this.set('noOfWaitingAjaxCalls', this.get('serviceConfigTags').length - 1);
  307. this.get('serviceConfigTags').forEach(function (_serviceConfigTags) {
  308. if (_serviceConfigTags.serviceName !== 'MAPREDUCE' || _serviceConfigTags.siteName !== 'core-site') { //skip MapReduce core-site configuration
  309. this.createConfiguration(_serviceConfigTags);
  310. }
  311. }, this);
  312. },
  313. createConfiguration: function (serviceConfigTags) {
  314. var self = this;
  315. var clusterName = App.router.getClusterName();
  316. var url = App.apiPrefix + '/clusters/' + clusterName + '/configurations';
  317. var data = this.createConfigurationData(serviceConfigTags);
  318. $.ajax({
  319. type: 'POST',
  320. url: url,
  321. data: JSON.stringify(data),
  322. dataType: 'text',
  323. timeout: 5000,
  324. success: function (data) {
  325. var jsonData = jQuery.parseJSON(data);
  326. self.set('noOfWaitingAjaxCalls', self.get('noOfWaitingAjaxCalls') - 1);
  327. if (self.get('noOfWaitingAjaxCalls') == 0) {
  328. self.applyConfigurationToServices();
  329. }
  330. },
  331. error: function (request, ajaxOptions, error) {
  332. self.get('stages').findProperty('stage', 'stage3').set('isError', true);
  333. console.log('TRACE: In Error ');
  334. console.log('TRACE: Error message is: ' + request.responseText);
  335. console.log("TRACE: value of the url is: " + url);
  336. },
  337. statusCode: require('data/statusCodes')
  338. });
  339. },
  340. createConfigurationData: function (serviceConfigTags) {
  341. return {"type": serviceConfigTags.siteName, "tag": serviceConfigTags.newTagName, "properties": serviceConfigTags.configs};
  342. },
  343. applyConfigurationToServices: function () {
  344. this.applyHdfsCoretoMaprCore();
  345. this.set('noOfWaitingAjaxCalls', this.get('content.services').length-2);
  346. this.get('content.services').forEach(function (_service) {
  347. if (_service.serviceName !== 'GENERAL' && _service.serviceName !== 'NAGIOS') {
  348. var data = {config: {}};
  349. this.get('serviceConfigTags').filterProperty('serviceName', _service.serviceName).forEach(function (_serviceConfig) {
  350. data.config[_serviceConfig.siteName] = _serviceConfig.newTagName;
  351. }, this);
  352. this.applyConfToService(_service.serviceName, data);
  353. }
  354. }, this);
  355. },
  356. applyHdfsCoretoMaprCore: function () {
  357. this.get('serviceConfigTags').filterProperty('serviceName', 'MAPREDUCE').findProperty('siteName', 'core-site').newTagName = this.get('serviceConfigTags').filterProperty('serviceName', 'HDFS').findProperty('siteName', 'core-site').newTagName;
  358. },
  359. applyConfToService: function (serviceName, data) {
  360. var self = this;
  361. var clusterName = App.router.getClusterName();
  362. var url = App.apiPrefix + '/clusters/' + clusterName + '/services/' + serviceName;
  363. $.ajax({
  364. type: 'PUT',
  365. url: url,
  366. async: false,
  367. dataType: 'text',
  368. data: JSON.stringify(data),
  369. timeout: 5000,
  370. success: function (data) {
  371. var jsonData = jQuery.parseJSON(data);
  372. console.log("TRACE: In success function for the applyCreatedConfToService call");
  373. console.log("TRACE: value of the url is: " + url);
  374. self.set('noOfWaitingAjaxCalls', self.get('noOfWaitingAjaxCalls') - 1);
  375. if (self.get('noOfWaitingAjaxCalls') == 0) {
  376. var currentStage = self.get('stages').findProperty('stage', 'stage3');
  377. currentStage.set('isSuccess', true);
  378. currentStage.set('isCompleted', true);
  379. }
  380. },
  381. error: function (request, ajaxOptions, error) {
  382. self.get('stages').findProperty('stage', 'stage3').set('isError', true);
  383. console.log('Error: In Error of apply');
  384. console.log('Error: Error message is: ' + request.responseText);
  385. },
  386. statusCode: require('data/statusCodes')
  387. });
  388. console.log("Exiting applyCreatedConfToService");
  389. },
  390. moveToNextStage: function () {
  391. var nextStage = this.get('stages').findProperty('isStarted', false);
  392. if (nextStage) {
  393. // this.get('content').saveCurrentStage(nextStage.get('stage').charAt(nextStage.get('stage').length - 1));
  394. nextStage.set('isStarted', true);
  395. } else {
  396. this.set('isSubmitDisabled', false);
  397. }
  398. },
  399. /**
  400. * gets site config properties from server and sets it for every configuration
  401. * @param serviceConfigTags
  402. */
  403. getServiceConfigsFromServer: function (serviceConfigTags) {
  404. var self = this;
  405. var properties = {};
  406. var url = App.apiPrefix + '/clusters/' + App.router.getClusterName() + '/configurations/?type=' + serviceConfigTags.siteName + '&tag=' + serviceConfigTags.tagName;
  407. $.ajax({
  408. type: 'GET',
  409. url: url,
  410. async: true,
  411. timeout: 10000,
  412. dataType: 'json',
  413. success: function (data) {
  414. console.log("TRACE: In success function for the GET getServiceConfigsFromServer call");
  415. console.log("TRACE: The url is: " + url);
  416. serviceConfigTags.configs = data.items.findProperty('tag', serviceConfigTags.tagName).properties;
  417. self.set('noOfWaitingAjaxCalls', self.get('noOfWaitingAjaxCalls') - 1);
  418. if (self.get('noOfWaitingAjaxCalls') == 0) {
  419. self.addSecureConfigs();
  420. self.createConfigurations();
  421. }
  422. },
  423. error: function (request, ajaxOptions, error) {
  424. self.get('stages').findProperty('stage', 'stage3').set('isError', true);
  425. console.log("TRACE: In error function for the getServiceConfigsFromServer call");
  426. console.log("TRACE: value of the url is: " + url);
  427. console.log("TRACE: error code status is: " + request.status);
  428. },
  429. statusCode: require('data/statusCodes')
  430. });
  431. },
  432. addSecureConfigs: function () {
  433. this.get('serviceConfigTags').forEach(function (_serviceConfigTags, index) {
  434. if (_serviceConfigTags.siteName === 'global') {
  435. _serviceConfigTags.newTagName = 'version' + (new Date).getTime() + index;
  436. this.get('globalProperties').forEach(function (_globalProperty) {
  437. _serviceConfigTags.configs[_globalProperty.name] = _globalProperty.value;
  438. }, this);
  439. } else {
  440. _serviceConfigTags.newTagName = 'version' + (new Date).getTime();
  441. this.get('configs').filterProperty('id', 'site property').filterProperty('filename', _serviceConfigTags.siteName + '.xml').forEach(function (_config) {
  442. _serviceConfigTags.configs[_config.name] = _config.value;
  443. }, this);
  444. }
  445. }, this);
  446. }
  447. });