step9_controller.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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.WizardStep9Controller = Em.Controller.extend({
  20. name: 'wizardStep9Controller',
  21. hosts: [],
  22. progress: '0',
  23. isStepCompleted: false,
  24. isSubmitDisabled: function () {
  25. //return false;
  26. return !this.get('isStepCompleted'); //TODO: uncomment after the hook up
  27. }.property('isStepCompleted'),
  28. mockHostData: require('data/mock/step9_hosts'),
  29. pollDataCounter: 0,
  30. polledData: [],
  31. status: function () {
  32. if (this.hosts.everyProperty('status', 'success')) {
  33. return 'success';
  34. } else if (this.hosts.someProperty('status', 'failed')) {
  35. return 'failed';
  36. } else if (this.hosts.someProperty('status', 'warning')) {
  37. return 'warning';
  38. } else {
  39. return 'info';
  40. }
  41. }.property('hosts.@each.status'),
  42. navigateStep: function () {
  43. //TODO: uncomment following line after the hook up with the API call
  44. if (this.get('content.cluster.isCompleted') === false) {
  45. this.loadStep();
  46. if (App.db.getClusterStatus().isInstallError === true) {
  47. this.set('isStepCompleted', true);
  48. this.set('status', 'failed');
  49. this.set('progress', '100');
  50. } else if (App.db.getClusterStatus().isStartError === true) {
  51. this.launchStartServices();
  52. } else {
  53. this.startPolling(); //TODO: uncomment for the actual hookup
  54. }
  55. } else {
  56. this.set('isStepCompleted', true);
  57. this.set('progress', '100');
  58. }
  59. },
  60. clearStep: function () {
  61. this.hosts.clear();
  62. this.set('status', 'info');
  63. this.set('progress', '0');
  64. this.set('isStepCompleted', false);
  65. },
  66. loadStep: function () {
  67. console.log("TRACE: Loading step9: Install, Start and Test");
  68. this.clearStep();
  69. this.renderHosts(this.loadHosts());
  70. },
  71. loadHosts: function () {
  72. var hostInfo = [];
  73. hostInfo = App.db.getHosts();
  74. var hosts = new Ember.Set();
  75. for (var index in hostInfo) {
  76. hosts.add(hostInfo[index]);
  77. console.log("TRACE: host name is: " + hostInfo[index].name);
  78. }
  79. return hosts;
  80. //return hosts.filterProperty('bootStatus', 'success'); //TODO: uncomment after actual hookup with bootstrap
  81. },
  82. renderHosts: function (hostsInfo) {
  83. hostsInfo.forEach(function (_hostInfo) {
  84. var hostInfo = App.HostInfo.create({
  85. name: _hostInfo.name,
  86. status: _hostInfo.status,
  87. tasks: _hostInfo.tasks,
  88. message: _hostInfo.message,
  89. progress: _hostInfo.progress
  90. });
  91. console.log('pushing ' + hostInfo.name);
  92. this.hosts.pushObject(hostInfo);
  93. }, this);
  94. },
  95. replacePolledData: function (polledData) {
  96. this.polledData.clear;
  97. this.set('polledData', polledData);
  98. console.log('*******/ In replace PolledData function **********/');
  99. console.log("The value of polleddata is: " + polledData);
  100. console.log("2.The value of polleddata is: " + this.get('polledData'));
  101. this.get('polledData').forEach(function (_data) {
  102. console.log('The name of the host is: ' + _data.Tasks.host_name);
  103. console.log('The status of the task is: ' + _data.Tasks.status);
  104. }, this);
  105. },
  106. displayMessage: function (task) {
  107. console.log("In display message with task command value: " + task.command);
  108. switch (task.command) {
  109. case 'INSTALL':
  110. switch (task.status) {
  111. case 'PENDING':
  112. return 'Preparing to install ' + task.role;
  113. case 'QUEUED' :
  114. return task.role + ' is Queued for installation';
  115. case 'IN_PROGRESS':
  116. return 'Installing ' + task.role;
  117. case 'COMPLETED' :
  118. return 'Successfully installed ' + task.role;
  119. case 'FAILED':
  120. return 'Faliure in installing ' + task.role;
  121. }
  122. case 'UNINSTALL':
  123. switch (task.status) {
  124. case 'PENDING':
  125. return 'Preparing to uninstall ' + task.role;
  126. case 'QUEUED' :
  127. return task.role + ' is Queued for uninstallation';
  128. case 'IN_PROGRESS':
  129. return 'Unnstalling ' + task.role;
  130. case 'COMPLETED' :
  131. return 'Successfully uninstalled ' + task.role;
  132. case 'FAILED':
  133. return 'Faliure in uninstalling ' + task.role;
  134. }
  135. case 'START' :
  136. switch (task.status) {
  137. case 'PENDING':
  138. return 'Preparing to start ' + task.role;
  139. case 'QUEUED' :
  140. return task.role + ' is Queued for starting';
  141. case 'IN_PROGRESS':
  142. return 'Starting ' + task.role;
  143. case 'COMPLETED' :
  144. return task.role + ' started successfully';
  145. case 'FAILED':
  146. return task.role + ' failed to start';
  147. }
  148. case 'STOP' :
  149. switch (task.status) {
  150. case 'PENDING':
  151. return 'Preparing to stop ' + role;
  152. case 'QUEUED' :
  153. return task.role + ' is Queued for stopping';
  154. case 'IN_PROGRESS':
  155. return 'Stopping ' + task.role;
  156. case 'COMPLETED' :
  157. return role + ' stoped successfully';
  158. case 'FAILED':
  159. return role + ' failed to stop';
  160. }
  161. case 'EXECUTE' :
  162. switch (task.status) {
  163. case 'PENDING':
  164. return 'Preparing to execute' + task.role;
  165. case 'QUEUED' :
  166. return task.role + ' is Queued for execution';
  167. case 'IN_PROGRESS':
  168. return 'Execution of ' + task.role + ' in progress';
  169. case 'COMPLETED' :
  170. return task.role + ' executed successfully';
  171. case 'FAILED':
  172. return task.role + ' failed to execute';
  173. }
  174. case 'ABORT' :
  175. switch (task.status) {
  176. case 'PENDING':
  177. return 'Preparing to abort ' + task.role;
  178. case 'QUEUED' :
  179. return task.role + ' is Queued for Aborting';
  180. case 'IN_PROGRESS':
  181. return 'Aborting ' + task.role;
  182. case 'COMPLETED' :
  183. return task.role + ' aborted successfully';
  184. case 'FAILED':
  185. return task.role + ' failed to abort';
  186. }
  187. }
  188. },
  189. launchStartServices: function () {
  190. var self = this;
  191. var clusterName = this.get('content.cluster.name');
  192. var url = '/api/clusters/' + clusterName + '/services?state=INSTALLED';
  193. var data = '{"ServiceInfo": {"state": "STARTED"}}';
  194. $.ajax({
  195. type: 'PUT',
  196. url: url,
  197. async: false,
  198. data: data,
  199. dataType: 'text',
  200. timeout: 5000,
  201. success: function (data) {
  202. var jsonData = jQuery.parseJSON(data);
  203. console.log("TRACE: Step9 -> In success function for the startService call");
  204. console.log("TRACE: Step9 -> value of the url is: " + url);
  205. console.log("TRACE: Step9 -> value of the received data is: " + jsonData);
  206. var requestId = jsonData.href.match(/.*\/(.*)$/)[1];
  207. console.log('requestId is: ' + requestId);
  208. var clusterStatus = {
  209. status: 'INSTALLED',
  210. requestId: requestId,
  211. isStartError: false,
  212. isCompleted: false
  213. };
  214. App.router.get('installerController').saveClusterStatus(clusterStatus);
  215. this.startPolling();
  216. },
  217. error: function () {
  218. console.log("ERROR");
  219. var clusterStatus = {
  220. status: 'PENDING',
  221. isStartError: true,
  222. isCompleted: false
  223. };
  224. App.router.get('installerController').saveClusterStatus(clusterStatus);
  225. },
  226. statusCode: require('data/statusCodes')
  227. });
  228. },
  229. onSuccessPerHost: function (actions, contentHost) {
  230. if (actions.everyProperty('Tasks.status', 'COMPLETED') && this.get('content.cluster.status') === 'INSTALLED') {
  231. contentHost.set('status', 'success');
  232. }
  233. },
  234. onWarningPerHost: function (actions, contentHost) {
  235. if (actions.findProperty('Tasks.status', 'FAILED') || actions.findProperty('Tasks.status', 'ABORTED') || actions.findProperty('Tasks.status', 'TIMEDOUT')) {
  236. console.log('step9: In warning');
  237. contentHost.set('status', 'warning');
  238. this.set('status', 'warning');
  239. }
  240. },
  241. onInProgressPerHost: function (tasks, contentHost) {
  242. var runningAction = tasks.findProperty('Tasks.status', 'IN_PROGRESS');
  243. if (runningAction === undefined || runningAction === null) {
  244. runningAction = tasks.findProperty('Tasks.status', 'QUEUED');
  245. }
  246. if (runningAction === undefined || runningAction === null) {
  247. runningAction = tasks.findProperty('Tasks.status', 'PENDING');
  248. }
  249. if (runningAction !== null && runningAction !== undefined) {
  250. contentHost.set('message', this.displayMessage(runningAction.Tasks));
  251. }
  252. },
  253. progressPerHost: function (actions, contentHost) {
  254. var progress = 0;
  255. var actionsPerHost = actions.length;
  256. var completedActions = actions.filterProperty('Tasks.status', 'COMPLETED').length
  257. + actions.filterProperty('Tasks.status', 'IN_PROGRESS').length
  258. + actions.filterProperty('Tasks.status', 'FAILED').length
  259. + actions.filterProperty('Tasks.status', 'ABORTED').length
  260. + actions.filterProperty('Tasks.status', 'TIMEDOUT').length;
  261. if (this.get('content.cluster.status') === 'PENDING') {
  262. progress = Math.floor(((completedActions / actionsPerHost) * 100) / 3);
  263. } else if (this.get('content.cluster.status') === 'INSTALLED') {
  264. progress = 34 + Math.floor(((completedActions / actionsPerHost) * 100 * 2) / 3);
  265. }
  266. console.log('INFO: progressPerHost is: ' + progress);
  267. contentHost.set('progress', progress.toString());
  268. return progress;
  269. },
  270. isSuccess: function (polledData) {
  271. return polledData.everyProperty('Tasks.status', 'COMPLETED');
  272. },
  273. isStepFailed: function (polledData) {
  274. var self = this;
  275. var result = false;
  276. polledData.forEach(function (_polledData) {
  277. _polledData.Tasks.sf = 100; //TODO: Remove this line after hook up with actual success factor
  278. var successFactor = _polledData.Tasks.sf;
  279. console.log("Step9: isStepFailed sf value: " + successFactor);
  280. var actionsPerRole = polledData.filterProperty('Tasks.role', _polledData.Tasks.role);
  281. var actionsFailed = actionsPerRole.filterProperty('Tasks.status', 'FAILED');
  282. var actionsAborted = actionsPerRole.filterProperty('Tasks.status', 'ABORTED');
  283. var actionsTimedOut = actionsPerRole.filterProperty('Tasks.status', 'TIMEDOUT');
  284. if ((((actionsFailed.length + actionsAborted.length + actionsTimedOut.length) / actionsPerRole.length) * 100) > (100 - successFactor)) {
  285. console.log('TRACE: Entering success factor and result is failed');
  286. result = true;
  287. }
  288. });
  289. return result;
  290. },
  291. getFailedHostsForFailedRoles: function (polledData) {
  292. var hostArr = new Ember.Set();
  293. polledData.forEach(function (_polledData) {
  294. _polledData.sf = 100; //TODO: Remove this line after hook up with actual success factor
  295. var successFactor = _polledData.sf;
  296. var actionsPerRole = polledData.filterProperty('Tasks.role', _polledData.Tasks.role);
  297. var actionsFailed = actionsPerRole.filterProperty('Tasks.status', 'FAILED');
  298. var actionsAborted = actionsPerRole.filterProperty('Tasks.status', 'ABORTED');
  299. var actionsTimedOut = actionsPerRole.filterProperty('Tasks.status', 'TIMEDOUT');
  300. if ((((actionsFailed.length + actionsAborted.length + actionsTimedOut.length) / actionsPerRole.length) * 100) > (100 - successFactor)) {
  301. actionsFailed.forEach(function (_actionFailed) {
  302. hostArr.add(_actionFailed.Tasks.host_name);
  303. });
  304. actionsAborted.forEach(function (_actionFailed) {
  305. hostArr.add(_actionFailed.Tasks.host_name);
  306. });
  307. actionsTimedOut.forEach(function (_actionFailed) {
  308. hostArr.add(_actionFailed.Tasks.host_name);
  309. });
  310. }
  311. });
  312. return hostArr;
  313. },
  314. setHostsStatus: function (hosts, status) {
  315. hosts.forEach(function (_host) {
  316. var host = this.hosts.findProperty('name', _host.Tasks.host_name);
  317. host.set('status', status);
  318. host.set('progress', '100');
  319. }, this);
  320. },
  321. // polling from ui stops only when no action has 'pending', 'queued' or 'inprogress' status
  322. finishState: function (polledData) {
  323. var clusterStatus = {};
  324. var retVal = false;
  325. var requestId = this.get('content.cluster.requestId');
  326. if (this.get('content.cluster.status') === 'INSTALLED') {
  327. if (!polledData.someProperty('Tasks.status', 'PENDING') && !polledData.someProperty('Tasks.status', 'QUEUED') && !polledData.someProperty('Tasks.status', 'IN_PROGRESS')) {
  328. this.set('progress', '100');
  329. clusterStatus = {
  330. status: 'INSTALLED',
  331. requestId: requestId,
  332. isCompleted: true
  333. }
  334. if (this.isSuccess(polledData)) {
  335. clusterStatus.status = 'STARTED';
  336. this.set('status', 'success');
  337. } else {
  338. if (this.isStepFailed(polledData)) {
  339. clusterStatus.status = 'FAILED';
  340. this.set('status', 'failed');
  341. this.setHostsStatus(this.getFailedHostsForFailedRoles(polledData));
  342. }
  343. }
  344. App.router.get('installerController').saveClusterStatus(clusterStatus);
  345. this.set('isStepCompleted', true);
  346. retVal = true;
  347. }
  348. } else if (this.get('content.cluster.status') === 'PENDING') {
  349. if (!polledData.someProperty('Tasks.status', 'PENDING') && !polledData.someProperty('Tasks.status', 'QUEUED') && !polledData.someProperty('Tasks.status', 'IN_PROGRESS')) {
  350. clusterStatus = {
  351. status: 'PENDING',
  352. requestId: requestId,
  353. isCompleted: true
  354. }
  355. if (this.isStepFailed(polledData)) {
  356. clusterStatus.status = 'FAILED';
  357. this.set('progress', '100');
  358. this.set('status', 'failed');
  359. this.setHostsStatus(this.getFailedHostsForFailedRoles(polledData), 'failed');
  360. this.set('isStepCompleted', true);
  361. } else {
  362. clusterStatus.status = 'INSTALLED';
  363. this.set('progress', '34');
  364. App.router.get('installerController').saveInstalledHosts(this);
  365. this.launchStartServices(); //TODO: uncomment after the actual hookup
  366. }
  367. App.router.get('installerController').saveClusterStatus(clusterStatus);
  368. retVal = true;
  369. }
  370. }
  371. return retVal;
  372. },
  373. getCompletedTasksForHost: function (host) {
  374. var hostname = host.get('name');
  375. var tasksPerHost = host.tasks.filterProperty('Tasks.host_name',hostname);
  376. var succededTasks = tasksPerHost.filterProperty('Tasks.status', 'COMPLETED');
  377. var inProgressTasks = tasksPerHost.filterProperty('Tasks.status', 'IN_PROGRESS');
  378. var listedTasksPerHost = succededTasks.concat(inProgressTasks).uniq();
  379. return listedTasksPerHost;
  380. },
  381. // This is done at HostRole level.
  382. setTasksStatePerHost: function(tasksPerHost,host) {
  383. var tasks = [];
  384. tasksPerHost.forEach(function(_taskPerHost){
  385. if(_taskPerHost.Tasks.status !== 'PENDING' &&_taskPerHost.Tasks.status !== 'QUEUED') {
  386. var task = host.tasks.findProperty('Tasks.id',_taskPerHost.Tasks.id);
  387. if(!(task && (task.Tasks.command === _taskPerHost.Tasks.command))) {
  388. host.tasks.pushObject(_taskPerHost);
  389. }
  390. }
  391. },this);
  392. },
  393. parseHostInfo: function (polledData) {
  394. console.log('TRACE: Entering host info function');
  395. var self = this;
  396. var totalProgress = 0;
  397. /* if (this.get('content.cluster.status') === 'INSTALLED') {
  398. totalProgress = 34;
  399. } else {
  400. totalProgress = 0;
  401. } */
  402. var tasksData = polledData.tasks;
  403. console.log("The value of tasksData is: " + tasksData);
  404. if (!tasksData) {
  405. console.log("Step9: ERROR: NO tasks availaible to process");
  406. }
  407. this.replacePolledData(tasksData);
  408. this.hosts.forEach(function (_host) {
  409. var actionsPerHost = tasksData.filterProperty('Tasks.host_name', _host.name); // retrieved from polled Data
  410. if (actionsPerHost.length === 0) {
  411. //alert('For testing with mockData follow the sequence: hit referesh,"mockData btn", "pollData btn", again "pollData btn"');
  412. //exit();
  413. }
  414. if (actionsPerHost !== null && actionsPerHost !== undefined && actionsPerHost.length !== 0) {
  415. this.onSuccessPerHost(actionsPerHost, _host); // every action should be a success
  416. this.onWarningPerHost(actionsPerHost, _host); // any action should be a faliure
  417. this.onInProgressPerHost(actionsPerHost, _host); // current running action for a host
  418. this.setTasksStatePerHost(actionsPerHost,_host);
  419. totalProgress = self.progressPerHost(actionsPerHost, _host);
  420. }
  421. }, this);
  422. totalProgress = Math.floor(totalProgress / this.hosts.length);
  423. this.set('progress', totalProgress.toString());
  424. console.log("INFO: right now the progress is: " + this.get('progress'));
  425. return this.finishState(tasksData);
  426. },
  427. startPolling: function () {
  428. this.set('isSubmitDisabled', true);
  429. this.doPolling();
  430. },
  431. getUrl: function () {
  432. var clusterName = this.get('content.cluster.name');
  433. var requestId = App.db.getClusterStatus().requestId;
  434. var url = '/api/clusters/' + clusterName + '/requests/' + requestId;
  435. console.log("URL for step9 is: " + url);
  436. return url;
  437. },
  438. doPolling: function () {
  439. var self = this;
  440. var url = this.getUrl();
  441. $.ajax({
  442. type: 'GET',
  443. url: url,
  444. async: true,
  445. timeout: 10000,
  446. dataType: 'text',
  447. success: function (data) {
  448. console.log("TRACE: In success function for the GET bootstrap call");
  449. console.log("TRACE: STep9 -> The value is: " + jQuery.parseJSON(data));
  450. var result = self.parseHostInfo(jQuery.parseJSON(data));
  451. if (result !== true) {
  452. window.setTimeout(function () {
  453. self.doPolling();
  454. }, 4000);
  455. } else {
  456. self.stopPolling();
  457. }
  458. },
  459. error: function (request, ajaxOptions, error) {
  460. console.log("TRACE: STep9 -> In error function for the getService call");
  461. console.log("TRACE: STep9 -> value of the url is: " + url);
  462. console.log("TRACE: STep9 -> error code status is: " + request.status);
  463. self.stopPolling();
  464. },
  465. statusCode: require('data/statusCodes')
  466. });
  467. },
  468. stopPolling: function () {
  469. //TODO: uncomment following line after the hook up with the API call
  470. // this.set('isStepCompleted',true);
  471. },
  472. submit: function () {
  473. if (!this.get('isSubmitDisabled')) {
  474. this.set('content.cluster.status', this.get('status'));
  475. this.set('content.cluster.isCompleted', true);
  476. App.router.send('next');
  477. }
  478. },
  479. back: function () {
  480. if (!this.get('isSubmitDisabled')) {
  481. App.router.send('back');
  482. }
  483. },
  484. mockBtn: function () {
  485. this.set('isSubmitDisabled', false);
  486. this.hosts.clear();
  487. var hostInfo = this.mockHostData;
  488. this.renderHosts(hostInfo);
  489. },
  490. pollBtn: function () {
  491. this.set('isSubmitDisabled', false);
  492. var data1 = require('data/mock/step9PolledData/pollData_1');
  493. var data2 = require('data/mock/step9PolledData/pollData_2');
  494. var data3 = require('data/mock/step9PolledData/pollData_3');
  495. var data4 = require('data/mock/step9PolledData/pollData_4');
  496. var data5 = require('data/mock/step9PolledData/pollData_5');
  497. var data6 = require('data/mock/step9PolledData/pollData_6');
  498. var data7 = require('data/mock/step9PolledData/pollData_7');
  499. var data8 = require('data/mock/step9PolledData/pollData_8');
  500. var data9 = require('data/mock/step9PolledData/pollData_9');
  501. console.log("TRACE: In pollBtn function data1");
  502. var counter = parseInt(this.get('pollDataCounter')) + 1;
  503. this.set('pollDataCounter', counter.toString());
  504. switch (this.get('pollDataCounter')) {
  505. case '1':
  506. this.parseHostInfo(data1);
  507. break;
  508. case '2':
  509. this.parseHostInfo(data2);
  510. break;
  511. case '3':
  512. this.parseHostInfo(data3);
  513. break;
  514. case '4':
  515. this.parseHostInfo(data4);
  516. break;
  517. case '5':
  518. this.parseHostInfo(data5);
  519. break;
  520. case '6':
  521. this.set('content.cluster.status', 'INSTALLED');
  522. this.parseHostInfo(data6);
  523. break;
  524. case '7':
  525. this.parseHostInfo(data7);
  526. break;
  527. case '8':
  528. this.parseHostInfo(data8);
  529. break;
  530. case '9':
  531. this.parseHostInfo(data9);
  532. break;
  533. default:
  534. break;
  535. }
  536. }
  537. });