step3.js 19 KB

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