security_progress_controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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.MainAdminSecurityProgressController = Em.Controller.extend({
  20. name: 'mainAdminSecurityProgressController',
  21. commands: [],
  22. configs: [],
  23. serviceConfigTags: [],
  24. globalProperties: [],
  25. totalSteps: 3,
  26. isSubmitDisabled: true,
  27. hasHostPopup: true,
  28. services: [],
  29. serviceTimestamp: null,
  30. operationsInfo: [
  31. {
  32. name: 'STOP_SERVICES',
  33. realUrl: '/services',
  34. testUrl: '/data/wizard/deploy/2_hosts/poll_1.json',
  35. data: '{"RequestInfo": {"context" :"' + Em.I18n.t('requestInfo.stopAllServices') + '"}, "Body": {"ServiceInfo": {"state": "INSTALLED"}}}'
  36. },
  37. {
  38. name: 'START_SERVICES',
  39. realUrl: '/services?params/run_smoke_test=true',
  40. testUrl: '/data/wizard/deploy/2_hosts/poll_1.json',
  41. data: '{"RequestInfo": {"context": "' + Em.I18n.t('requestInfo.startAllServices') + '"}, "Body": {"ServiceInfo": {"state": "STARTED"}}}'
  42. }
  43. ],
  44. secureMapping: function () {
  45. return (App.get('isHadoop2Stack')) ? require('data/HDP2/secure_mapping') : require('data/secure_mapping');
  46. }.property('App.isHadoop2Stack'),
  47. secureProperties: function () {
  48. if (App.get('isHadoop2Stack')) {
  49. return require('data/HDP2/secure_properties').configProperties;
  50. } else {
  51. return require('data/secure_properties').configProperties;
  52. }
  53. }.property('App.isHadoop2Stack'),
  54. /**
  55. * prepare and restart failed command
  56. */
  57. retry: function () {
  58. var failedCommand = this.get('commands').findProperty('isError');
  59. if (failedCommand) {
  60. failedCommand.set('requestId', null);
  61. failedCommand.set('isStarted', false);
  62. failedCommand.set('isError', false);
  63. this.startCommand(failedCommand);
  64. }
  65. },
  66. /**
  67. * start updating current task in parallel
  68. * @param requestId
  69. * @param taskId
  70. * @return {Boolean}
  71. */
  72. startUpdatingTask: function (requestId, taskId) {
  73. if (!requestId || !taskId) return false;
  74. var command = this.get('commands').findProperty('requestId', requestId);
  75. command.updateTaskLog(taskId);
  76. return true;
  77. },
  78. /**
  79. * stop updating current task
  80. * @param requestId
  81. * @return {Boolean}
  82. */
  83. stopUpdatingTask: function (requestId) {
  84. if (!requestId) return false;
  85. var command = this.get('commands').findProperty('requestId', requestId);
  86. command.set('currentTaskId', null);
  87. return true;
  88. },
  89. /**
  90. * update info about progress of operation of commands
  91. */
  92. updateServices: function () {
  93. this.services.clear();
  94. var services = this.get("services");
  95. this.get("commands").forEach(function (command) {
  96. var polledData = command.get('polledData');
  97. var newService = Ember.Object.create({
  98. name: command.get('label'),
  99. hosts: []
  100. });
  101. if (polledData) {
  102. var hostNames = polledData.mapProperty('Tasks.host_name').uniq();
  103. hostNames.forEach(function (name) {
  104. newService.hosts.push({
  105. name: name,
  106. publicName: name,
  107. logTasks: polledData.filterProperty("Tasks.host_name", name)
  108. });
  109. });
  110. services.push(newService);
  111. }
  112. });
  113. this.set('serviceTimestamp', App.dateTime());
  114. }.observes('commands.@each.polledData'),
  115. /**
  116. * initialize default commands
  117. */
  118. loadCommands: function () {
  119. this.get('commands').pushObjects([
  120. App.Poll.create({name: 'STOP_SERVICES', label: Em.I18n.translations['admin.addSecurity.apply.stop.services'], isPolling: true }),
  121. App.Poll.create({name: 'APPLY_CONFIGURATIONS', label: Em.I18n.translations['admin.addSecurity.apply.save.config'], isPolling: false }),
  122. App.Poll.create({name: 'START_SERVICES', label: Em.I18n.translations['admin.addSecurity.apply.start.services'], isPolling: true })
  123. ]);
  124. },
  125. addObserverToCommands: function () {
  126. this.setIndex(this.get('commands'));
  127. this.addObserver('commands.@each.isSuccess', this, 'onCompleteCommand');
  128. },
  129. /**
  130. * set index to each command
  131. * @param commandArray
  132. */
  133. setIndex: function (commandArray) {
  134. commandArray.forEach(function (command, index) {
  135. command.set('index', index + 1);
  136. }, this);
  137. this.set('totalSteps', commandArray.length);
  138. },
  139. startCommand: function (command) {
  140. if (this.get('commands').length === this.get('totalSteps')) {
  141. if (!command) {
  142. var startedCommand = this.get('commands').filterProperty('isStarted', true);
  143. command = startedCommand.findProperty('isCompleted', false);
  144. }
  145. if (command) {
  146. if (command.get('isPolling')) {
  147. command.set('isStarted', true);
  148. command.start();
  149. } else if (command.get('name') === 'APPLY_CONFIGURATIONS') {
  150. command.set('isStarted', true);
  151. if (App.testMode) {
  152. command.set('isError', false);
  153. command.set('isSuccess', true);
  154. } else {
  155. this.loadClusterConfigs();
  156. }
  157. } else if (command.get('name') === 'DELETE_ATS') {
  158. command.set('isStarted', true);
  159. if (App.testMode) {
  160. command.set('isError', false);
  161. command.set('isSuccess', true);
  162. } else {
  163. var timeLineServer = App.HostComponent.find().findProperty('componentName', 'APP_TIMELINE_SERVER');
  164. this.deleteComponents('APP_TIMELINE_SERVER', timeLineServer.get('hostName'));
  165. }
  166. }
  167. return true;
  168. }
  169. }
  170. return false;
  171. },
  172. /**
  173. * on command completion move to next command
  174. * @return {Boolean}
  175. */
  176. onCompleteCommand: function () {
  177. if (this.get('commands').length === this.get('totalSteps')) {
  178. var index = this.get('commands').filterProperty('isSuccess', true).length;
  179. if (index > 0) {
  180. var lastCompletedCommandResult = this.get('commands').objectAt(index - 1).get('isSuccess');
  181. if (lastCompletedCommandResult) {
  182. var nextCommand = this.get('commands').objectAt(index);
  183. this.moveToNextCommand(nextCommand);
  184. return true;
  185. }
  186. }
  187. }
  188. return false;
  189. },
  190. /**
  191. * move to next command
  192. * @param nextCommand
  193. */
  194. moveToNextCommand: function (nextCommand) {
  195. nextCommand = nextCommand || this.get('commands').findProperty('isStarted', false);
  196. if (nextCommand) {
  197. this.startCommand(nextCommand);
  198. return true;
  199. }
  200. return false;
  201. },
  202. /**
  203. * add query information(url, data) to commands
  204. */
  205. addInfoToCommands: function () {
  206. var operationsInfo = this.get('operationsInfo');
  207. var urlPrefix = App.apiPrefix + '/clusters/' + App.get('clusterName');
  208. operationsInfo.forEach(function (operation) {
  209. var command = this.get('commands').findProperty('name', operation.name);
  210. var url = (App.testMode) ? operation.testUrl : urlPrefix + operation.realUrl;
  211. command.set('url', url);
  212. command.set('data', operation.data);
  213. }, this);
  214. },
  215. loadClusterConfigs: function () {
  216. App.ajax.send({
  217. name: 'admin.security.add.cluster_configs',
  218. sender: this,
  219. success: 'loadClusterConfigsSuccessCallback',
  220. error: 'loadClusterConfigsErrorCallback'
  221. });
  222. },
  223. loadClusterConfigsSuccessCallback: function (data) {
  224. //prepare tags to fetch all configuration for a service
  225. this.get('secureServices').forEach(function (_secureService) {
  226. this.setServiceTagNames(_secureService, data.Clusters.desired_configs);
  227. }, this);
  228. this.getAllConfigurations();
  229. },
  230. loadClusterConfigsErrorCallback: function (request, ajaxOptions, error) {
  231. var command = this.get('commands').findProperty('name', 'APPLY_CONFIGURATIONS');
  232. command.set('isSuccess', false);
  233. command.set('isError', true);
  234. console.log("TRACE: error code status is: " + request.status);
  235. },
  236. /**
  237. * set tag names according to installed services and desired configs
  238. * @param secureService
  239. * @param configs
  240. * @return {Object}
  241. */
  242. setServiceTagNames: function (secureService, configs) {
  243. //var serviceConfigTags = this.get('serviceConfigTags');
  244. for (var index in configs) {
  245. if (secureService.sites && secureService.sites.contains(index)) {
  246. var serviceConfigObj = {
  247. siteName: index,
  248. tagName: configs[index].tag,
  249. newTagName: null,
  250. configs: {}
  251. };
  252. this.get('serviceConfigTags').pushObject(serviceConfigObj);
  253. }
  254. }
  255. return serviceConfigObj;
  256. },
  257. /**
  258. * form query data and apply security configurations to server
  259. */
  260. applyConfigurationsToCluster: function () {
  261. var configData = [];
  262. this.get('serviceConfigTags').forEach(function (_serviceConfig) {
  263. var Clusters = {
  264. Clusters: {
  265. desired_config: {
  266. type: _serviceConfig.siteName,
  267. tag: _serviceConfig.newTagName,
  268. properties: _serviceConfig.configs
  269. }
  270. }
  271. };
  272. configData.pushObject(JSON.stringify(Clusters));
  273. }, this);
  274. var data = {
  275. configData: '[' + configData.toString() + ']'
  276. };
  277. App.ajax.send({
  278. name: 'admin.security.apply_configurations',
  279. sender: this,
  280. data: data,
  281. success: 'applyConfigurationToClusterSuccessCallback',
  282. error: 'applyConfigurationToClusterErrorCallback'
  283. });
  284. },
  285. applyConfigurationToClusterSuccessCallback: function (data) {
  286. var command = this.get('commands').findProperty('name', 'APPLY_CONFIGURATIONS');
  287. command.set('isSuccess', true);
  288. command.set('isError', false);
  289. },
  290. applyConfigurationToClusterErrorCallback: function (request, ajaxOptions, error) {
  291. var command = this.get('commands').findProperty('name', 'APPLY_CONFIGURATIONS');
  292. command.set('isSuccess', false);
  293. command.set('isError', true);
  294. },
  295. /**
  296. * gets site config properties from server and sets it for every configuration
  297. */
  298. getAllConfigurations: function () {
  299. var urlParams = [];
  300. this.get('serviceConfigTags').forEach(function (_tag) {
  301. urlParams.push('(type=' + _tag.siteName + '&tag=' + _tag.tagName + ')');
  302. }, this);
  303. if (urlParams.length > 0) {
  304. App.ajax.send({
  305. name: 'admin.get.all_configurations',
  306. sender: this,
  307. data: {
  308. urlParams: urlParams.join('|')
  309. },
  310. success: 'getAllConfigurationsSuccessCallback',
  311. error: 'getAllConfigurationsErrorCallback'
  312. });
  313. }
  314. },
  315. getAllConfigurationsSuccessCallback: function (data) {
  316. console.log("TRACE: In success function for the GET getServiceConfigsFromServer call");
  317. var command = this.get('commands').findProperty('name', 'APPLY_CONFIGURATIONS');
  318. this.get('serviceConfigTags').forEach(function (_tag) {
  319. if (!data.items.someProperty('type', _tag.siteName)) {
  320. console.log("Error: Metadata for secure services (secure_configs.js) is having config tags that are not being retrieved from server");
  321. command.set('isSuccess', false);
  322. command.set('isError', true);
  323. }
  324. _tag.configs = data.items.findProperty('type', _tag.siteName).properties;
  325. }, this);
  326. if (this.manageSecureConfigs()) {
  327. this.escapeXMLCharacters(this.get('serviceConfigTags'));
  328. this.applyConfigurationsToCluster();
  329. }
  330. },
  331. getAllConfigurationsErrorCallback: function (request, ajaxOptions, error) {
  332. var command = this.get('commands').findProperty('name', 'APPLY_CONFIGURATIONS');
  333. command.set('isSuccess', false);
  334. command.set('isError', true);
  335. console.log("TRACE: In error function for the getServiceConfigsFromServer call");
  336. console.log("TRACE: error code status is: " + request.status);
  337. },
  338. /*
  339. Iterate over keys of all configurations and escape xml characters in their values
  340. */
  341. escapeXMLCharacters: function (serviceConfigTags) {
  342. serviceConfigTags.forEach(function (_serviceConfigTags) {
  343. var configs = _serviceConfigTags.configs;
  344. for (var key in configs) {
  345. configs[key] = this.setServerConfigValue(key, configs[key]);
  346. }
  347. }, this);
  348. },
  349. /**
  350. * set specific server values to config
  351. * @param configName
  352. * @param value
  353. * @return {*}
  354. */
  355. setServerConfigValue: function (configName, value) {
  356. switch (configName) {
  357. case 'storm.zookeeper.servers':
  358. return value;
  359. default:
  360. return App.config.escapeXMLCharacters(value);
  361. }
  362. },
  363. /**
  364. * save commands to server and local storage
  365. */
  366. saveCommands: function () {
  367. var commands = [];
  368. if (this.get('commands').length === this.get('totalSteps')) {
  369. this.get('commands').forEach(function (_command) {
  370. var command = {
  371. name: _command.get('name'),
  372. label: _command.get('label'),
  373. isPolling: _command.get('isPolling'),
  374. isVisible: _command.get('isVisible'),
  375. isStarted: _command.get('isStarted'),
  376. requestId: _command.get('requestId'),
  377. isSuccess: _command.get('isSuccess'),
  378. isError: _command.get('isError'),
  379. url: _command.get('url'),
  380. polledData: _command.get('polledData'),
  381. data: _command.get('data')
  382. };
  383. commands.pushObject(command);
  384. }, this);
  385. App.db.setSecurityDeployCommands(commands);
  386. if (!App.testMode) {
  387. App.clusterStatus.setClusterStatus({
  388. clusterName: this.get('clusterName'),
  389. clusterState: 'ADD_SECURITY_STEP_4',
  390. wizardControllerName: App.router.get('addSecurityController.name'),
  391. localdb: App.db.data
  392. });
  393. }
  394. }
  395. }.observes('commands.@each.isCompleted', 'commands.@each.requestId')
  396. });