step9_controller.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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. /**
  22. * Array of host Objects that are successfully registered on "Confirm Host Options" page
  23. * <code>
  24. * {
  25. * name: {String} host name.
  26. * status: {String} Current status of the host. This field is used in the step 9 view to set the css class of the
  27. * host progress bar and set the appropriate message. Possible values: info, warning, failed, heartbeat_lost and success
  28. * logTasks: {Array} Tasks that are scheduled on the host for the current request.
  29. * message: {String} Message to be shown in front of the host name.
  30. * progress: {Int} Progress of the tasks on the host. Amount of tasks completed for all the tasks scheduled on the host.
  31. * isNoTasksForInstall: {Boolean} Gets set when no task is scheduled for the host on install phase.
  32. * }
  33. * </code>
  34. */
  35. hosts: [],
  36. /**
  37. * overall progress of <Install,Start and Test> page shown as progress bar on the top of the page
  38. */
  39. progress: '0',
  40. /*
  41. * json file for the mock data to be used in mock mode
  42. */
  43. mockDataPrefix: '/data/wizard/deploy/5_hosts',
  44. /*
  45. * Current Request data polled from the API: api/v1/clusters/{clusterName}/requests/{RequestId}?fields=tasks/Tasks/command,
  46. * tasks/Tasks/exit_code,tasks/Tasks/start_time,tasks/Tasks/end_time,tasks/Tasks/host_name,tasks/Tasks/id,tasks/Tasks/role,
  47. * tasks/Tasks/status&minimal_response=true
  48. */
  49. polledData: [],
  50. /*
  51. * This flag is only used in UI mock mode as a counter for number of polls.
  52. */
  53. numPolls: 1,
  54. // Interval in milliseconds between API calls While polling the request status for Install and, Start and Test tasks
  55. POLL_INTERVAL: 4000,
  56. /**
  57. * Array of objects
  58. * <code>
  59. * {
  60. * hostName: {String} Name of host that has stopped heartbeating to ambari-server
  61. * componentNames: {Sting} Name of all components that are on the host
  62. * }
  63. * </code>
  64. */
  65. hostsWithHeartbeatLost: [],
  66. // Flag is set in the start all services error callback function
  67. startCallFailed: false,
  68. /*
  69. * Status of the page. Possible values: <info, warning, failed and success>.
  70. * This property is used in the step-9 view for displaying the appropriate color of the overall progress bar and
  71. * the appropriate result message at the bottom of the page
  72. */
  73. status: 'info',
  74. /**
  75. * This computed property is used to determine if the Next button and back button on the page should be disabled. The property is
  76. * used in the template to grey out Next and Back buttons. Although clicking on the greyed out button do trigger the event and
  77. * calls submit and back function of the controller.
  78. */
  79. isSubmitDisabled: function () {
  80. var validStates = ['STARTED', 'START FAILED'];
  81. var controllerName = this.get('content.controllerName');
  82. if (controllerName == 'addHostController' || controllerName == 'addServiceController') {
  83. validStates.push('INSTALL FAILED');
  84. }
  85. return !validStates.contains(this.get('content.cluster.status'));
  86. }.property('content.cluster.status'),
  87. /**
  88. * This function is called when a click event happens on Next button of <Install, Start and Test> page
  89. */
  90. submit: function () {
  91. App.router.send('next');
  92. },
  93. /**
  94. * This function is called when a click event happens on back button of <Install, Start and Test> page
  95. */
  96. back: function () {
  97. if (!this.get('isSubmitDisabled')) {
  98. App.router.send('back');
  99. }
  100. },
  101. /**
  102. * Observer function: Enables previous steps link if install task failed in installer wizard.
  103. */
  104. togglePreviousSteps: function () {
  105. if (App.testMode) {
  106. return;
  107. } else if ('INSTALL FAILED' === this.get('content.cluster.status') && this.get('content.controllerName') == 'installerController') {
  108. App.router.get('installerController').setStepsEnable();
  109. } else {
  110. App.router.get('installerController').setLowerStepsDisable(9);
  111. }
  112. }.observes('content.cluster.status', 'content.controllerName'),
  113. /*
  114. * Computed property to determine if the Retry button should be made visible on the page.
  115. */
  116. showRetry: function () {
  117. return this.get('content.cluster.status') == 'INSTALL FAILED';
  118. }.property('content.cluster.status'),
  119. /**
  120. * Observer function: Calls {hostStatusUpdates} function once with change in a host status from any registered hosts.
  121. */
  122. hostStatusObserver: function () {
  123. Ember.run.once(this, 'updateStatus');
  124. }.observes('hosts.@each.status'),
  125. /**
  126. * A flag that gets set with installation failure.
  127. */
  128. installFailed: false,
  129. /**
  130. * Observer function: Updates {status} field of the controller.
  131. */
  132. updateStatus: function () {
  133. var status = 'info';
  134. if (this.get('hosts').someProperty('status', 'failed')
  135. || this.get('hosts').someProperty('status', 'heartbeat_lost')
  136. || this.get('startCallFailed')) {
  137. status = 'failed';
  138. } else if (this.get('hosts').someProperty('status', 'warning')) {
  139. if (this.isStepFailed()) {
  140. status = 'failed';
  141. } else {
  142. status = 'warning';
  143. }
  144. } else if (this.get('progress') == '100' && this.get('content.cluster.status') !== 'INSTALL FAILED') {
  145. status = 'success';
  146. }
  147. this.set('status', status);
  148. }.observes('progress'),
  149. /**
  150. * Incremental flag that triggers an event in step 9 view to change the tasks related data and icons of hosts.
  151. */
  152. logTasksChangesCounter: 0,
  153. /**
  154. * navigateStep is called by App.WizardStep9View's didInsertElement and "retry" from router.
  155. * content.cluster.status can be:
  156. * PENDING: set upon successful transition from step 1 to step 2
  157. * INSTALLED: set upon successful completion of install phase as well as successful invocation of start services API
  158. * STARTED: set up on successful completion of start phase
  159. * INSTALL FAILED: set up upon encountering a failure in install phase
  160. * START FAILED: set upon unsuccessful invocation of start services API and also upon encountering a failure
  161. * during start phase
  162. * content.cluster.isCompleted
  163. * set to false upon successful transition from step 1 to step 2
  164. * set to true upon successful start of services in this step
  165. * note: looks like this is the same thing as checking content.cluster.status == 'STARTED'
  166. */
  167. navigateStep: function () {
  168. if (App.testMode) {
  169. // this is for repeatedly testing out installs in test mode
  170. this.set('content.cluster.status', 'PENDING');
  171. this.set('content.cluster.isCompleted', false);
  172. this.set('content.cluster.requestId', 1);
  173. }
  174. var clusterStatus = this.get('content.cluster.status');
  175. console.log('navigateStep: clusterStatus = ' + clusterStatus);
  176. if (this.get('content.cluster.isCompleted') === false) {
  177. // the cluster has not yet successfully installed and started
  178. if (clusterStatus === 'INSTALL FAILED') {
  179. this.loadStep();
  180. this.loadLogData(this.get('content.cluster.requestId'));
  181. } else if (clusterStatus === 'START FAILED') {
  182. this.loadStep();
  183. this.loadLogData(this.get('content.cluster.requestId'));
  184. } else {
  185. // handle PENDING, INSTALLED
  186. this.loadStep();
  187. this.loadLogData(this.get('content.cluster.requestId'));
  188. this.startPolling();
  189. }
  190. } else {
  191. // handle STARTED
  192. // the cluster has successfully installed and started
  193. this.loadStep();
  194. this.loadLogData(this.get('content.cluster.requestId'));
  195. this.set('progress', '100');
  196. }
  197. },
  198. /**
  199. * This is called on initial page load, refreshes and retry event.
  200. * clears all in memory stale data for retry event.
  201. */
  202. clearStep: function () {
  203. this.get('hosts').clear();
  204. this.set('hostsWithHeartbeatLost', []);
  205. this.set('startCallFailed',false);
  206. this.set('status', 'info');
  207. this.set('progress', '0');
  208. this.set('numPolls', 1);
  209. },
  210. /**
  211. * This is called on initial page load, refreshes and retry event.
  212. */
  213. loadStep: function () {
  214. console.log("TRACE: Loading step9: Install, Start and Test");
  215. this.clearStep();
  216. this.loadHosts();
  217. },
  218. /**
  219. * Reset status and message of all hosts when retry install
  220. */
  221. resetHostsForRetry: function () {
  222. var hosts = this.get('content.hosts');
  223. for (var name in hosts) {
  224. hosts[name].status = "pending";
  225. hosts[name].message = 'Waiting';
  226. hosts[name].isNoTasksForInstall = false;
  227. }
  228. this.set('content.hosts', hosts);
  229. },
  230. /**
  231. * Sets the {hosts} array for the controller
  232. */
  233. loadHosts: function () {
  234. var hosts = this.get('content.hosts');
  235. for (var index in hosts) {
  236. if (hosts[index].bootStatus === 'REGISTERED') {
  237. var hostInfo = App.HostInfo.create({
  238. name: hosts[index].name,
  239. status: (hosts[index].status) ? hosts[index].status : 'info',
  240. logTasks: [],
  241. message: (hosts[index].message) ? hosts[index].message : 'Waiting',
  242. progress: 0,
  243. isNoTasksForInstall: false
  244. });
  245. this.get('hosts').pushObject(hostInfo);
  246. }
  247. }
  248. },
  249. /**
  250. *
  251. * @param polledData: sets the {polledData} object of the controller
  252. */
  253. replacePolledData: function (polledData) {
  254. this.polledData.clear();
  255. this.set('polledData', polledData);
  256. },
  257. /**
  258. *
  259. * @param task
  260. * @returns {String} The appropriate message for the host as per the running task.
  261. */
  262. displayMessage: function (task) {
  263. var role = App.format.role(task.role);
  264. switch (task.command) {
  265. case 'INSTALL':
  266. switch (task.status) {
  267. case 'PENDING':
  268. return Em.I18n.t('installer.step9.serviceStatus.install.pending') + role;
  269. case 'QUEUED' :
  270. return Em.I18n.t('installer.step9.serviceStatus.install.queued') + role;
  271. case 'IN_PROGRESS':
  272. return Em.I18n.t('installer.step9.serviceStatus.install.inProgress') + role;
  273. case 'COMPLETED' :
  274. return Em.I18n.t('installer.step9.serviceStatus.install.completed') + role;
  275. case 'FAILED':
  276. return Em.I18n.t('installer.step9.serviceStatus.install.failed') + role;
  277. }
  278. case 'UNINSTALL':
  279. switch (task.status) {
  280. case 'PENDING':
  281. return Em.I18n.t('installer.step9.serviceStatus.uninstall.pending') + role;
  282. case 'QUEUED' :
  283. return Em.I18n.t('installer.step9.serviceStatus.uninstall.queued') + role;
  284. case 'IN_PROGRESS':
  285. return Em.I18n.t('installer.step9.serviceStatus.uninstall.inProgress') + role;
  286. case 'COMPLETED' :
  287. return Em.I18n.t('installer.step9.serviceStatus.uninstall.completed') + role;
  288. case 'FAILED':
  289. return Em.I18n.t('installer.step9.serviceStatus.uninstall.failed') + role;
  290. }
  291. case 'START' :
  292. switch (task.status) {
  293. case 'PENDING':
  294. return Em.I18n.t('installer.step9.serviceStatus.start.pending') + role;
  295. case 'QUEUED' :
  296. return Em.I18n.t('installer.step9.serviceStatus.start.queued') + role;
  297. case 'IN_PROGRESS':
  298. return Em.I18n.t('installer.step9.serviceStatus.start.inProgress') + role;
  299. case 'COMPLETED' :
  300. return role + Em.I18n.t('installer.step9.serviceStatus.start.completed');
  301. case 'FAILED':
  302. return role + Em.I18n.t('installer.step9.serviceStatus.start.failed');
  303. }
  304. case 'STOP' :
  305. switch (task.status) {
  306. case 'PENDING':
  307. return Em.I18n.t('installer.step9.serviceStatus.stop.pending') + role;
  308. case 'QUEUED' :
  309. return Em.I18n.t('installer.step9.serviceStatus.stop.queued') + role;
  310. case 'IN_PROGRESS':
  311. return Em.I18n.t('installer.step9.serviceStatus.stop.inProgress') + role;
  312. case 'COMPLETED' :
  313. return role + Em.I18n.t('installer.step9.serviceStatus.stop.completed');
  314. case 'FAILED':
  315. return role + Em.I18n.t('installer.step9.serviceStatus.stop.failed');
  316. }
  317. case 'EXECUTE' :
  318. case 'SERVICE_CHECK' :
  319. switch (task.status) {
  320. case 'PENDING':
  321. return Em.I18n.t('installer.step9.serviceStatus.execute.pending') + role;
  322. case 'QUEUED' :
  323. return Em.I18n.t('installer.step9.serviceStatus.execute.queued') + role;
  324. case 'IN_PROGRESS':
  325. return Em.I18n.t('installer.step9.serviceStatus.execute.inProgress') + role;
  326. case 'COMPLETED' :
  327. return role + Em.I18n.t('installer.step9.serviceStatus.execute.completed');
  328. case 'FAILED':
  329. return role + Em.I18n.t('installer.step9.serviceStatus.execute.failed');
  330. }
  331. case 'ABORT' :
  332. switch (task.status) {
  333. case 'PENDING':
  334. return Em.I18n.t('installer.step9.serviceStatus.abort.pending') + role;
  335. case 'QUEUED' :
  336. return Em.I18n.t('installer.step9.serviceStatus.abort.queued') + role;
  337. case 'IN_PROGRESS':
  338. return Em.I18n.t('installer.step9.serviceStatus.abort.inProgress') + role;
  339. case 'COMPLETED' :
  340. return role + Em.I18n.t('installer.step9.serviceStatus.abort.completed');
  341. case 'FAILED':
  342. return role + Em.I18n.t('installer.step9.serviceStatus.abort.failed');
  343. }
  344. }
  345. return '';
  346. },
  347. /**
  348. * Run start/check services after installation phase.
  349. * Does Ajax call to start all services
  350. */
  351. launchStartServices: function () {
  352. var data = {
  353. "RequestInfo": {
  354. "context": Em.I18n.t("requestInfo.startServices")
  355. },
  356. "Body": {
  357. "ServiceInfo": { "state": "STARTED" }
  358. }
  359. };
  360. var name = 'wizard.step9.installer.launch_start_services';
  361. if (this.get('content.controllerName') === 'addHostController') {
  362. var hostnames = [];
  363. for (var hostname in this.get('wizardController').getDBProperty('hosts')) {
  364. hostnames.push(hostname);
  365. }
  366. data = {
  367. "RequestInfo": {
  368. "context": Em.I18n.t("requestInfo.startHostComponents"),
  369. "query": "HostRoles/component_name.in(" + App.get('components.slaves').join(',') + ")&HostRoles/state=INSTALLED&HostRoles/host_name.in(" + hostnames.join(',') + ")"
  370. },
  371. "Body": {
  372. "HostRoles": { "state": "STARTED" }
  373. }
  374. };
  375. name = 'wizard.step9.add_host.launch_start_services';
  376. }
  377. data = JSON.stringify(data);
  378. if (App.testMode) {
  379. this.set('numPolls', 6);
  380. }
  381. App.ajax.send({
  382. name: name,
  383. sender: this,
  384. data: {
  385. data: data,
  386. cluster: this.get('content.cluster.name')
  387. },
  388. success: 'launchStartServicesSuccessCallback',
  389. error: 'launchStartServicesErrorCallback'
  390. });
  391. },
  392. /**
  393. * Success callback function for start services task.
  394. * @param jsonData: {json object} Contains Request id to poll.
  395. */
  396. launchStartServicesSuccessCallback: function (jsonData) {
  397. var clusterStatus = {};
  398. if (jsonData) {
  399. console.log("TRACE: Step9 -> In success function for the startService call");
  400. console.log("TRACE: Step9 -> value of the received data is: " + jsonData);
  401. var requestId = jsonData.Requests.id;
  402. console.log('requestId is: ' + requestId);
  403. clusterStatus = {
  404. status: 'INSTALLED',
  405. requestId: requestId,
  406. isStartError: false,
  407. isCompleted: false
  408. };
  409. this.hostHasClientsOnly(false);
  410. this.saveClusterStatus(clusterStatus);
  411. } else {
  412. console.log('ERROR: Error occurred in parsing JSON data');
  413. this.hostHasClientsOnly(true);
  414. clusterStatus = {
  415. status: 'STARTED',
  416. isStartError: false,
  417. isCompleted: true
  418. };
  419. this.saveClusterStatus(clusterStatus);
  420. this.set('status', 'success');
  421. this.set('progress', '100');
  422. }
  423. // We need to do recovery if there is a browser crash
  424. App.clusterStatus.setClusterStatus({
  425. clusterState: 'SERVICE_STARTING_3',
  426. wizardControllerName: this.get('content.controllerName'),
  427. localdb: App.db.data
  428. });
  429. if (jsonData) {
  430. this.startPolling();
  431. }
  432. },
  433. /**
  434. * This function will be called for Add host wizard only.
  435. * @param jsonError: {boolean} Boolean is true when API to start services returns 200 ok and no json data
  436. */
  437. hostHasClientsOnly: function (jsonError) {
  438. this.get('hosts').forEach(function (host) {
  439. var OnlyClients = true;
  440. var tasks = host.get('logTasks');
  441. tasks.forEach(function (task) {
  442. var component = App.StackServiceComponent.find().findProperty('componentName', task.Tasks.role);
  443. if (!(component && component.get('isClient'))) {
  444. OnlyClients = false;
  445. }
  446. });
  447. if (OnlyClients || jsonError) {
  448. host.set('status', 'success');
  449. host.set('progress', '100');
  450. }
  451. });
  452. },
  453. /**
  454. * Error callback function for start services task.
  455. */
  456. launchStartServicesErrorCallback: function (jqXHR) {
  457. console.log("ERROR");
  458. this.set('startCallFailed',true);
  459. var clusterStatus = {
  460. status: 'INSTALL FAILED',
  461. isStartError: false,
  462. isCompleted: false
  463. };
  464. this.saveClusterStatus(clusterStatus);
  465. this.get('hosts').forEach(function (host) {
  466. host.set('progress', '100');
  467. });
  468. this.set('progress','100');
  469. var params = {
  470. cluster: this.get('content.cluster.name')
  471. };
  472. if (this.get('content.controllerName') === 'addHostController') {
  473. params.name = 'wizard.step9.add_host.launch_start_services';
  474. } else {
  475. params.name = 'wizard.step9.installer.launch_start_services';
  476. }
  477. var opt = App.formatRequest.call(App.urls[params.name], params);
  478. App.ajax.defaultErrorHandler(jqXHR,opt.url,opt.type);
  479. },
  480. /**
  481. * marks a host's status as "success" if all tasks are in COMPLETED state
  482. */
  483. onSuccessPerHost: function (actions, contentHost) {
  484. if (actions.everyProperty('Tasks.status', 'COMPLETED') && this.get('content.cluster.status') === 'INSTALLED') {
  485. contentHost.set('status', 'success');
  486. }
  487. },
  488. /**
  489. * marks a host's status as "warning" if at least one of the tasks is FAILED, ABORTED, or TIMEDOUT and marks host's status as "failed" if at least one master component install task is FAILED.
  490. * note that if the master failed to install because of ABORTED or TIMEDOUT, we don't mark it as failed, because this would mark all hosts as "failed" and makes it difficult for the user
  491. * to find which host FAILED occurred on, if any
  492. * @param actions {Array} of tasks retrieved from polled data
  493. * @param contentHost {Object} A host object
  494. */
  495. onErrorPerHost: function (actions, contentHost) {
  496. if (!actions) return;
  497. if (actions.someProperty('Tasks.status', 'FAILED') || actions.someProperty('Tasks.status', 'ABORTED') || actions.someProperty('Tasks.status', 'TIMEDOUT')) {
  498. contentHost.set('status', 'warning');
  499. }
  500. if ((this.get('content.cluster.status') === 'PENDING' && actions.someProperty('Tasks.status', 'FAILED')) || (this.isMasterFailed(actions))) {
  501. contentHost.get('status') !== 'heartbeat_lost' ? contentHost.set('status', 'failed') : '';
  502. }
  503. },
  504. /**
  505. *
  506. * @param polledData : Json data polled from API.
  507. * @returns {boolean} true if there is at least one FAILED task of master component install
  508. */
  509. isMasterFailed: function (polledData) {
  510. var result = false;
  511. polledData.filterProperty('Tasks.command', 'INSTALL').filterProperty('Tasks.status', 'FAILED').mapProperty('Tasks.role').forEach(
  512. function (role) {
  513. if (!App.get('components.slaves').contains(role)) {
  514. result = true;
  515. }
  516. }
  517. );
  518. return result;
  519. },
  520. /**
  521. * Mark a host status as in_progress if the any task on the host if either in IN_PROGRESS, QUEUED or PENDONG state.
  522. * @param actions {Array} of tasks retrieved from polled data
  523. * @param contentHost {Object} A host object
  524. */
  525. onInProgressPerHost: function (actions, contentHost) {
  526. var runningAction = actions.findProperty('Tasks.status', 'IN_PROGRESS');
  527. if (runningAction === undefined || runningAction === null) {
  528. runningAction = actions.findProperty('Tasks.status', 'QUEUED');
  529. }
  530. if (runningAction === undefined || runningAction === null) {
  531. runningAction = actions.findProperty('Tasks.status', 'PENDING');
  532. }
  533. if (runningAction !== null && runningAction !== undefined) {
  534. contentHost.set('status', 'in_progress');
  535. contentHost.set('message', this.displayMessage(runningAction.Tasks));
  536. }
  537. },
  538. /**
  539. * calculate progress of tasks per host
  540. * @param actions
  541. * @param contentHost
  542. * @return {Number}
  543. */
  544. progressPerHost: function (actions, contentHost) {
  545. var progress = 0;
  546. var actionsPerHost = actions.length;
  547. var completedActions = 0;
  548. var queuedActions = 0;
  549. var inProgressActions = 0;
  550. actions.forEach(function (action) {
  551. completedActions += +(['COMPLETED', 'FAILED', 'ABORTED', 'TIMEDOUT'].contains(action.Tasks.status));
  552. queuedActions += +(action.Tasks.status === 'QUEUED');
  553. inProgressActions += +(action.Tasks.status === 'IN_PROGRESS');
  554. }, this);
  555. /** for the install phase (PENDING), % completed per host goes up to 33%; floor(100 / 3)
  556. * for the start phase (INSTALLED), % completed starts from 34%
  557. * when task in queued state means it's completed on 9%
  558. * in progress - 35%
  559. * completed - 100%
  560. */
  561. switch (this.get('content.cluster.status')) {
  562. case 'PENDING':
  563. progress = actionsPerHost ? (Math.ceil(((queuedActions * 0.09) + (inProgressActions * 0.35) + completedActions ) / actionsPerHost * 33)) : 33;
  564. break;
  565. case 'INSTALLED':
  566. progress = actionsPerHost ? (34 + Math.ceil(((queuedActions * 0.09) + (inProgressActions * 0.35) + completedActions ) / actionsPerHost * 66)) : 100;
  567. break;
  568. default:
  569. progress = 100;
  570. break;
  571. }
  572. contentHost.set('progress', progress.toString());
  573. return progress;
  574. },
  575. /**
  576. *
  577. * @param polledData : Josn data retrieved from API
  578. * @returns {Boolean} : Has step completed successfully
  579. */
  580. isSuccess: function (polledData) {
  581. return polledData.everyProperty('Tasks.status', 'COMPLETED');
  582. },
  583. /**
  584. * return true if:
  585. * 1. any of the master/client components failed to install
  586. * OR
  587. * 2. at least 50% of the slave host components for the particular service component fails to install
  588. */
  589. isStepFailed: function () {
  590. var failed = false;
  591. var polledData = this.get('polledData');
  592. polledData.filterProperty('Tasks.command', 'INSTALL').mapProperty('Tasks.role').uniq().forEach(function (role) {
  593. if (failed) {
  594. return;
  595. }
  596. var actionsPerRole = polledData.filterProperty('Tasks.role', role);
  597. if (App.get('components.slaves').contains(role)) {
  598. // check slave components for success factor.
  599. // partial failure for slave components are allowed.
  600. var actionsFailed = actionsPerRole.filterProperty('Tasks.status', 'FAILED');
  601. var actionsAborted = actionsPerRole.filterProperty('Tasks.status', 'ABORTED');
  602. var actionsTimedOut = actionsPerRole.filterProperty('Tasks.status', 'TIMEDOUT');
  603. if ((((actionsFailed.length + actionsAborted.length + actionsTimedOut.length) / actionsPerRole.length) * 100) > 50) {
  604. failed = true;
  605. }
  606. } else if (actionsPerRole.someProperty('Tasks.status', 'FAILED') || actionsPerRole.someProperty('Tasks.status', 'ABORTED') ||
  607. actionsPerRole.someProperty('Tasks.status', 'TIMEDOUT')) {
  608. // check non-salve components (i.e., masters and clients). all of these must be successfully installed.
  609. failed = true;
  610. }
  611. }, this);
  612. return failed;
  613. },
  614. /**
  615. * polling from ui stops only when no action has 'PENDING', 'QUEUED' or 'IN_PROGRESS' status
  616. * Makes a state transition
  617. * PENDING -> INSTALLED
  618. * PENDING -> INSTALL FAILED
  619. * INSTALLED -> STARTED
  620. * INSTALLED -> START_FAILED
  621. * @param polledData json data retrieved from API
  622. * @returns {Boolean} true if polling should stop; false otherwise
  623. */
  624. finishState: function (polledData) {
  625. if (this.get('content.cluster.status') === 'INSTALLED') {
  626. return this.isServicesStarted(polledData);
  627. } else if (this.get('content.cluster.status') === 'PENDING') {
  628. return this.isServicesInstalled(polledData);
  629. } else if (this.get('content.cluster.status') === 'INSTALL FAILED' || this.get('content.cluster.status') === 'START FAILED'
  630. || this.get('content.cluster.status') === 'STARTED') {
  631. this.set('progress', '100');
  632. return true;
  633. }
  634. return false;
  635. },
  636. /**
  637. * @param polledData Josn data retrieved from API
  638. * @returns {boolean} Has "Start All Services" request completed successfully
  639. */
  640. isServicesStarted: function (polledData) {
  641. var clusterStatus = {};
  642. if (!polledData.someProperty('Tasks.status', 'PENDING') && !polledData.someProperty('Tasks.status', 'QUEUED') && !polledData.someProperty('Tasks.status', 'IN_PROGRESS')) {
  643. this.set('progress', '100');
  644. clusterStatus = {
  645. status: 'INSTALLED',
  646. requestId: this.get('content.cluster.requestId'),
  647. isCompleted: true
  648. };
  649. if (this.isSuccess(polledData)) {
  650. clusterStatus.status = 'STARTED';
  651. var serviceStartTime = App.dateTime();
  652. clusterStatus.installTime = ((parseInt(serviceStartTime) - parseInt(this.get('content.cluster.installStartTime'))) / 60000).toFixed(2);
  653. } else {
  654. clusterStatus.status = 'START FAILED'; // 'START FAILED' implies to step10 that installation was successful but start failed
  655. }
  656. this.saveClusterStatus(clusterStatus);
  657. this.saveInstalledHosts(this);
  658. return true;
  659. }
  660. return false;
  661. },
  662. /**
  663. * @param polledData Josn data retrieved from API
  664. * @returns {boolean} Has "Install All Services" request completed successfully
  665. */
  666. isServicesInstalled: function (polledData) {
  667. var clusterStatus = {};
  668. if (!polledData.someProperty('Tasks.status', 'PENDING') && !polledData.someProperty('Tasks.status', 'QUEUED') && !polledData.someProperty('Tasks.status', 'IN_PROGRESS')) {
  669. clusterStatus = {
  670. status: 'PENDING',
  671. requestId: this.get('content.cluster.requestId'),
  672. isCompleted: false
  673. };
  674. if (this.get('status') === 'failed') {
  675. clusterStatus.status = 'INSTALL FAILED';
  676. this.saveClusterStatus(clusterStatus);
  677. this.set('progress', '100');
  678. this.get('hosts').forEach(function (host) {
  679. host.set('progress', '100');
  680. });
  681. this.isAllComponentsInstalled();
  682. } else {
  683. this.set('progress', '34');
  684. if (this.get('content.controllerName') === 'installerController') {
  685. this.isAllComponentsInstalled();
  686. } else {
  687. this.launchStartServices();
  688. }
  689. }
  690. this.saveInstalledHosts(this);
  691. return true;
  692. }
  693. return false;
  694. },
  695. /**
  696. * This is done at HostRole level.
  697. * @param tasksPerHost {Array}
  698. * @param host {Object}
  699. */
  700. setLogTasksStatePerHost: function (tasksPerHost, host) {
  701. tasksPerHost.forEach(function (_task) {
  702. var task = host.logTasks.findProperty('Tasks.id', _task.Tasks.id);
  703. if (task) {
  704. task.Tasks.status = _task.Tasks.status;
  705. task.Tasks.exit_code = _task.Tasks.exit_code;
  706. } else {
  707. host.logTasks.pushObject(_task);
  708. }
  709. }, this);
  710. },
  711. /**
  712. * Parses the Json data retrieved from API and sets the task on the host of {hosts} array binded to template
  713. * @param polledData Json data retrieved from API
  714. * @returns {Boolean} True if stage transition is completed.
  715. * On true, polling will be stopped.
  716. */
  717. parseHostInfo: function (polledData) {
  718. console.log('TRACE: Entering host info function');
  719. var self = this;
  720. var totalProgress = 0;
  721. var tasksData = polledData.tasks;
  722. console.log("The value of tasksData is: ", tasksData);
  723. if (!tasksData) {
  724. console.log("Step9: ERROR: NO tasks available to process");
  725. }
  726. var requestId = this.get('content.cluster.requestId');
  727. tasksData.setEach('Tasks.request_id', requestId);
  728. if (polledData.Requests && polledData.Requests.id && polledData.Requests.id != requestId) {
  729. // We don't want to use non-current requestId's tasks data to
  730. // determine the current install status.
  731. // Also, we don't want to keep polling if it is not the
  732. // current requestId.
  733. return false;
  734. }
  735. this.replacePolledData(tasksData);
  736. var tasksHostMap = {};
  737. tasksData.forEach(function (task) {
  738. if (tasksHostMap[task.Tasks.host_name]) {
  739. tasksHostMap[task.Tasks.host_name].push(task);
  740. } else {
  741. tasksHostMap[task.Tasks.host_name] = [task];
  742. }
  743. });
  744. this.get('hosts').forEach(function (_host) {
  745. var actionsPerHost = tasksHostMap[_host.name] || []; // retrieved from polled Data
  746. if (actionsPerHost.length === 0) {
  747. if (this.get('content.cluster.status') === 'PENDING' || this.get('content.cluster.status') === 'INSTALL FAILED') {
  748. _host.set('progress', '33');
  749. _host.set('isNoTasksForInstall', true);
  750. _host.set('status', 'pending');
  751. }
  752. if (this.get('content.cluster.status') === 'INSTALLED' || this.get('content.cluster.status') === 'FAILED') {
  753. _host.set('progress', '100');
  754. _host.set('status', 'success');
  755. }
  756. console.log("INFO: No task is hosted on the host");
  757. } else {
  758. _host.set('isNoTasksForInstall', false);
  759. }
  760. this.setLogTasksStatePerHost(actionsPerHost, _host);
  761. this.onSuccessPerHost(actionsPerHost, _host); // every action should be a success
  762. this.onErrorPerHost(actionsPerHost, _host); // any action should be a failure
  763. this.onInProgressPerHost(actionsPerHost, _host); // current running action for a host
  764. totalProgress += self.progressPerHost(actionsPerHost, _host);
  765. if (_host.get('progress') == '33' && _host.get('status') != 'failed' && _host.get('status') != 'warning') {
  766. _host.set('message', this.t('installer.step9.host.status.nothingToInstall'));
  767. _host.set('status', 'pending');
  768. }
  769. }, this);
  770. this.set('logTasksChangesCounter', this.get('logTasksChangesCounter') + 1);
  771. totalProgress = Math.floor(totalProgress / this.get('hosts.length'));
  772. this.set('progress', totalProgress.toString());
  773. console.log("INFO: right now the progress is: " + this.get('progress'));
  774. return this.finishState(tasksData);
  775. },
  776. /**
  777. * starts polling to the API.
  778. */
  779. startPolling: function () {
  780. this.set('isSubmitDisabled', true);
  781. this.doPolling();
  782. },
  783. /**
  784. *
  785. * @param requestId {Int} Request Id received on triggering install/start command successfully
  786. * @returns {string} URL to poll to track the result of the triggered command
  787. */
  788. getUrl: function (requestId) {
  789. var clusterName = this.get('content.cluster.name');
  790. var requestId = requestId || this.get('content.cluster.requestId');
  791. var url = App.apiPrefix + '/clusters/' + clusterName + '/requests/' + requestId + '?fields=tasks/Tasks/command,tasks/Tasks/exit_code,tasks/Tasks/start_time,tasks/Tasks/end_time,tasks/Tasks/host_name,tasks/Tasks/id,tasks/Tasks/role,tasks/Tasks/status&minimal_response=true';
  792. console.log("URL for step9 is: " + url);
  793. return url;
  794. },
  795. /**
  796. * This function calls API just once to fetch log data of all tasks.
  797. * @param requestId {Int} Request Id received on triggering install/start command successfully
  798. */
  799. loadLogData: function (requestId) {
  800. var url = this.getUrl(requestId);
  801. var requestsId = this.get('wizardController').getDBProperty('cluster').oldRequestsId;
  802. if (App.testMode) {
  803. this.POLL_INTERVAL = 1;
  804. }
  805. requestsId.forEach(function (requestId) {
  806. url = this.getUrl(requestId);
  807. if (App.testMode) {
  808. this.POLL_INTERVAL = 1;
  809. url = this.get('mockDataPrefix') + '/poll_' + this.numPolls + '.json';
  810. }
  811. this.getLogsByRequest(url, false);
  812. }, this);
  813. },
  814. /**
  815. * {Number}
  816. * <code>taskId</code> of current open task
  817. */
  818. currentOpenTaskId: 0,
  819. /**
  820. * {Number}
  821. * <code>requestId</code> of current open task
  822. */
  823. currentOpenTaskRequestId: 0,
  824. /**
  825. * Load form server <code>stderr, stdout</code> of current open task
  826. */
  827. loadCurrentTaskLog: function () {
  828. var taskId = this.get('currentOpenTaskId');
  829. var requestId = this.get('currentOpenTaskRequestId');
  830. var clusterName = this.get('content.cluster.name');
  831. if (!taskId) {
  832. console.log('taskId is null.');
  833. return;
  834. }
  835. App.ajax.send({
  836. name: 'background_operations.get_by_task',
  837. sender: this,
  838. data: {
  839. 'taskId': taskId,
  840. 'requestId': requestId,
  841. 'clusterName': clusterName,
  842. 'sync': true
  843. },
  844. success: 'loadCurrentTaskLogSuccessCallback',
  845. error: 'loadCurrentTaskLogErrorCallback'
  846. });
  847. },
  848. /**
  849. * success callback function for getting log data of the opened task
  850. * @param data json object
  851. */
  852. loadCurrentTaskLogSuccessCallback: function (data) {
  853. var taskId = this.get('currentOpenTaskId');
  854. if (taskId) {
  855. var currentTask = this.get('hosts').findProperty('name', data.Tasks.host_name).get('logTasks').findProperty('Tasks.id', data.Tasks.id);
  856. if (currentTask) {
  857. currentTask.Tasks.stderr = data.Tasks.stderr;
  858. currentTask.Tasks.stdout = data.Tasks.stdout;
  859. }
  860. }
  861. this.set('logTasksChangesCounter', this.get('logTasksChangesCounter') + 1);
  862. },
  863. /**
  864. * Error callback function for getting log data of the opened task
  865. */
  866. loadCurrentTaskLogErrorCallback: function () {
  867. this.set('currentOpenTaskId', 0);
  868. },
  869. /**
  870. * Function polls the API to retrieve data for the request.
  871. * @param url {string} url to poll
  872. * @param polling {Boolean} whether to continue polling for status or not
  873. */
  874. getLogsByRequest: function (url, polling) {
  875. var self = this;
  876. $.ajax({
  877. type: 'GET',
  878. url: url,
  879. async: true,
  880. timeout: App.timeout,
  881. dataType: 'text',
  882. success: function (data) {
  883. var parsedData = jQuery.parseJSON(data);
  884. console.log("TRACE: In success function for the GET logs data");
  885. console.log("TRACE: Step9 -> The value is: ", parsedData);
  886. var result = self.parseHostInfo(parsedData);
  887. if (!polling) {
  888. if (self.get('content.cluster.status') === 'INSTALL FAILED') {
  889. self.isAllComponentsInstalled();
  890. }
  891. return;
  892. }
  893. if (result !== true) {
  894. window.setTimeout(function () {
  895. if (self.get('currentOpenTaskId')) {
  896. self.loadCurrentTaskLog();
  897. }
  898. self.doPolling();
  899. }, self.POLL_INTERVAL);
  900. }
  901. },
  902. error: function (request, ajaxOptions, error) {
  903. console.log("TRACE: STep9 -> In error function for the GET logs data");
  904. console.log("TRACE: STep9 -> value of the url is: " + url);
  905. console.log("TRACE: STep9 -> error code status is: " + request.status);
  906. },
  907. statusCode: require('data/statusCodes')
  908. }).retry({times: App.maxRetries, timeout: App.timeout}).then(null,
  909. function () {
  910. App.showReloadPopup();
  911. console.log('Install services all retries failed');
  912. }
  913. );
  914. },
  915. /**
  916. * Delegates the function call to {getLogsByRequest} with appropriate params
  917. */
  918. doPolling: function () {
  919. var url = this.getUrl();
  920. if (App.testMode) {
  921. this.numPolls++;
  922. url = this.get('mockDataPrefix') + '/poll_' + this.get('numPolls') + '.json';
  923. }
  924. this.getLogsByRequest(url, true);
  925. },
  926. /**
  927. * Check that all components are in INSTALLED state before issuing start command
  928. */
  929. isAllComponentsInstalled: function () {
  930. if (this.get('content.controllerName') !== 'installerController') {
  931. return;
  932. }
  933. var name = 'wizard.step9.installer.get_host_status';
  934. App.ajax.send({
  935. name: name,
  936. sender: this,
  937. data: {
  938. cluster: this.get('content.cluster.name')
  939. },
  940. success: 'isAllComponentsInstalledSuccessCallback',
  941. error: 'isAllComponentsInstalledErrorCallback'
  942. });
  943. },
  944. /**
  945. * Success callback function for API checking host state and host_components state.
  946. * @param jsonData {Object}
  947. */
  948. isAllComponentsInstalledSuccessCallback: function (jsonData) {
  949. var clusterStatus = {
  950. status: 'INSTALL FAILED',
  951. isStartError: true,
  952. isCompleted: false
  953. };
  954. var hostsWithHeartbeatLost = [];
  955. jsonData.items.filterProperty('Hosts.host_state', 'HEARTBEAT_LOST').forEach(function (host) {
  956. var hostComponentObj = {hostName: host.Hosts.host_name};
  957. var componentArr = [];
  958. host.host_components.forEach(function (_hostComponent) {
  959. var componentName = App.format.role(_hostComponent.HostRoles.component_name);
  960. componentArr.pushObject(componentName);
  961. }, this);
  962. hostComponentObj.componentNames = this.getComponentMessage(componentArr);
  963. hostsWithHeartbeatLost.pushObject(hostComponentObj);
  964. }, this);
  965. this.set('hostsWithHeartbeatLost', hostsWithHeartbeatLost);
  966. if (hostsWithHeartbeatLost.length) {
  967. this.get('hosts').forEach(function (host) {
  968. if (hostsWithHeartbeatLost.someProperty(('hostName'), host.get('name'))) {
  969. host.set('status', 'heartbeat_lost');
  970. } else if (host.get('status') !== 'failed' && host.get('status') !== 'warning') {
  971. host.set('message', Em.I18n.t('installer.step9.host.status.startAborted'));
  972. }
  973. host.set('progress', '100');
  974. });
  975. this.set('progress', '100');
  976. this.saveClusterStatus(clusterStatus);
  977. } else if (this.get('content.cluster.status') === 'PENDING') {
  978. this.launchStartServices();
  979. }
  980. },
  981. /**
  982. * Error callback function for API checking host state and host_components state
  983. */
  984. isAllComponentsInstalledErrorCallback: function () {
  985. console.log("ERROR");
  986. var clusterStatus = {
  987. status: 'INSTALL FAILED',
  988. isStartError: true,
  989. isCompleted: false
  990. };
  991. this.set('progress', '100');
  992. this.get('hosts').forEach(function (host) {
  993. if (host.get('status') !== 'failed' && host.get('status') !== 'warning') {
  994. host.set('message', Em.I18n.t('installer.step9.host.status.startAborted'));
  995. host.set('progress', '100');
  996. }
  997. });
  998. this.saveClusterStatus(clusterStatus);
  999. },
  1000. /**
  1001. * @param componentArr {Array} Array of components
  1002. * @returns {String} Formatted string of components to display on the UI.
  1003. */
  1004. getComponentMessage: function (componentArr) {
  1005. var label;
  1006. componentArr.forEach(function (_component) {
  1007. if (_component === componentArr[0]) {
  1008. label = _component;
  1009. } else if (_component !== componentArr[componentArr.length - 1]) { // [clients.length - 1]
  1010. label = label + ' ' + _component;
  1011. if (_component !== componentArr[componentArr.length - 2]) {
  1012. label = label + ',';
  1013. }
  1014. } else {
  1015. label = label + ' ' + Em.I18n.t('and') + ' ' + _component;
  1016. }
  1017. }, this);
  1018. return label;
  1019. },
  1020. /**
  1021. * save cluster status in the parentController and localdb
  1022. * @param clusterStatus {Object}
  1023. */
  1024. saveClusterStatus: function(clusterStatus) {
  1025. if (!App.testMode) {
  1026. App.router.get(this.get('content.controllerName')).saveClusterStatus(clusterStatus);
  1027. } else {
  1028. this.set('content.cluster',clusterStatus);
  1029. }
  1030. },
  1031. /**
  1032. * save cluster status in the parentController and localdb
  1033. * @param context
  1034. */
  1035. saveInstalledHosts: function(context) {
  1036. if (!App.testMode) {
  1037. App.router.get(this.get('content.controllerName')).saveInstalledHosts(context)
  1038. }
  1039. }
  1040. });