assignMasters.js 6.4 KB

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