step9_controller.js 20 KB

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