assignMasters.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. var registeredAssignHostsEventHandlers = false;
  2. function getNodeInfo (allHosts, nodeName) {
  3. // globalYui.log("nodename: " + nodeName);
  4. if (nodeName == null) {
  5. return null;
  6. }
  7. for (host in allHosts) {
  8. if (allHosts[host].hostName == nodeName) {
  9. // globalYui.log("Get node info: " + allHosts[host].hostName);
  10. return allHosts[host];
  11. }
  12. }
  13. return null;
  14. }
  15. function addCommasToInt(num) {
  16. return num.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
  17. }
  18. function getTotalMemForDisplay(totalMem) {
  19. return Math.round(totalMem / 102.4)/10 + "GB";
  20. }
  21. function renderHostsToMasterServices(allHosts, hostsToMasterServices) {
  22. var markup = '';
  23. for (var host in hostsToMasterServices) {
  24. var hostInfo = getNodeInfo(allHosts, host);
  25. markup += '<div class="hostToMasterServices"><h3>' + host + '<span class="hostInfo">' + getTotalMemForDisplay(hostInfo.totalMem) + ', ' + hostInfo.cpuCount + ' cores</span></h3><ul>';
  26. for (var j in hostsToMasterServices[host]) {
  27. markup += '<li>' + hostsToMasterServices[host][j] + '</li>';
  28. }
  29. markup += '</ul><div style="clear:both"></div></div>';
  30. }
  31. $('#hostsToMasterServices').html(markup);
  32. }
  33. function addMasterServiceToHost(masterName, hostName, hostsToMasterServices, masterServices) {
  34. if (hostsToMasterServices[hostName] == null) {
  35. hostsToMasterServices[hostName] = {};
  36. }
  37. hostsToMasterServices[hostName][masterName] = masterServices[masterName].displayName;
  38. }
  39. function removeMasterServiceFromHost(masterName, hostName, hostsToMasterServices) {
  40. for (var i in hostsToMasterServices[hostName]) {
  41. if (i == masterName) {
  42. delete hostsToMasterServices[hostName][i];
  43. //alert(Object.keys(hostsToMasterServices[hostName]).length);
  44. if (Object.keys(hostsToMasterServices[hostName]).length == 0) {
  45. //alert('remove');
  46. delete hostsToMasterServices[hostName];
  47. }
  48. return;
  49. }
  50. }
  51. }
  52. function getMasterHostSelect(masterName, allHosts, chosenHostName) {
  53. var chosenHost = getNodeInfo(allHosts, chosenHostName);
  54. var markup = '<select name="' + masterName + '">';
  55. markup += '<option selected="selected" value="' + chosenHost.hostName + '">' + chosenHost.hostName + ' - ' + getTotalMemForDisplay(chosenHost.totalMem) + ', ' + chosenHost.cpuCount + ' cores</option>';
  56. for (var i in allHosts) {
  57. var host = allHosts[i];
  58. if (host.hostName != chosenHost.hostName) {
  59. markup += '<option value="' + host.hostName + '">' + host.hostName + ' - ' + getTotalMemForDisplay(host.totalMem) + ', ' + host.cpuCount + ' cores</option>';
  60. }
  61. }
  62. markup += '</select><div style="clear:both"></div><input type="hidden" style="display:none" id="' + masterName + 'ChosenHost" value="' + chosenHost.hostName + '">';
  63. return markup;
  64. }
  65. function renderAssignHosts(clusterInfo) {
  66. hideLoadingImg();
  67. globalYui.one('#assignHostsCoreDivId').setStyle("display", "block");
  68. if( !registeredAssignHostsEventHandlers ) {
  69. globalYui.one('#selectServiceMastersSubmitButtonId').on('click', function (e) {
  70. e.target.set('disabled', true);
  71. var assignHostsRequestData = {};
  72. for (var masterName in masterServices) {
  73. assignHostsRequestData[masterName] = $('select[name=' + masterName + ']').val();
  74. // globalYui.log("Assignment for " + masterName + " is " + assignHostsRequestData[masterName]);
  75. };
  76. globalYui.io("../php/frontend/assignMasters.php?clusterName="+clusterInfo.clusterName, {
  77. method: 'POST',
  78. data: globalYui.JSON.stringify(assignHostsRequestData),
  79. timeout : 10000,
  80. on: {
  81. start: function(x, o) {
  82. showLoadingImg();
  83. },
  84. complete: function(x, o) {
  85. e.target.set('disabled', false);
  86. hideLoadingImg();
  87. },
  88. success: function (x,o) {
  89. e.target.set('disabled', false);
  90. globalYui.log("RAW JSON DATA: " + o.responseText);
  91. // Process the JSON data returned from the server
  92. try {
  93. clusterConfigJson = globalYui.JSON.parse(o.responseText);
  94. }
  95. catch (e) {
  96. alert("JSON Parse failed!");
  97. return;
  98. }
  99. //globalYui.log("PARSED DATA: " + globalYui.Lang.dump(clusterConfigJson));
  100. if (clusterConfigJson.result != 0) {
  101. // Error!
  102. alert("Got error!" + clusterConfigJson.error);
  103. return;
  104. }
  105. clusterConfigJson = clusterConfigJson.response;
  106. /* Done with this stage, transition to the next. */
  107. transitionToNextStage( "#assignHostsCoreDivId", assignHostsRequestData,
  108. "#configureClusterCoreDivId", clusterConfigJson, renderConfigureCluster );
  109. },
  110. failure: function (x,o) {
  111. e.target.set('disabled', false);
  112. alert("Async call failed!");
  113. }
  114. }
  115. });
  116. });
  117. registeredAssignHostsEventHandlers = true;
  118. }
  119. var allHosts = clusterInfo.allHosts;
  120. var servicesInfo = globalYui.Array( clusterInfo.services );
  121. var masterServices = {};
  122. globalYui.Array.each(servicesInfo, function(serviceInfo) {
  123. if( serviceInfo.enabled == true ) {
  124. globalYui.Array.each(serviceInfo.masters, function(masterInfo) {
  125. var masterHostInfo = {
  126. 'name' : masterInfo.name,
  127. 'displayName' : masterInfo.displayName,
  128. 'host' : masterInfo.hostName
  129. };
  130. masterServices[masterInfo.name] = masterHostInfo;
  131. });
  132. }
  133. });
  134. var hostsToMasterServices = {};
  135. var markup = '';
  136. for (var i in masterServices) {
  137. markup += '<div class="masterServiceSelect"><label><b>'
  138. + masterServices[i].displayName
  139. + '</b></label>' + getMasterHostSelect(masterServices[i].name, allHosts, masterServices[i].host)
  140. + '</div>';
  141. if (hostsToMasterServices[masterServices[i].host] == null) {
  142. hostsToMasterServices[masterServices[i].host] = {};
  143. }
  144. hostsToMasterServices[masterServices[i].host][masterServices[i].name] = masterServices[i].displayName;
  145. }
  146. $('#masterServicesToHosts').html(markup);
  147. renderHostsToMasterServices(allHosts, hostsToMasterServices);
  148. $('select').change(function() {
  149. var masterName = $(this).attr('name');
  150. // masterServices[masterName] = $(this).val();
  151. var prevChosenHost = $('#' + masterName + 'ChosenHost').val();
  152. var newChosenHost = $(this).val();
  153. removeMasterServiceFromHost(masterName, prevChosenHost, hostsToMasterServices);
  154. addMasterServiceToHost(masterName, newChosenHost, hostsToMasterServices, masterServices);
  155. renderHostsToMasterServices(allHosts, hostsToMasterServices);
  156. $('#' + masterName + 'ChosenHost').val(newChosenHost);
  157. });
  158. }