security_progress_controller.js 13 KB

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