nodeManifest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. define("OPT", "opts");
  3. define("STAGE", "stage");
  4. define("SERVICESTATE", "service_state");
  5. class NodeManifest {
  6. private $node;
  7. private $roles;
  8. function __construct($n) {
  9. $this->node = $n;
  10. $this->roles = array();
  11. }
  12. public function setRoleStage($r, $stage) {
  13. $this->setRoleState($r, STAGE, $stage);
  14. }
  15. public function setRoleState($r, $stateKey, $stateValue) {
  16. if (!isset($this->roles[$r])) {
  17. $this->roles[$r] = array();
  18. $this->roles[$r][$stateKey] = $stateValue;
  19. } else {
  20. $this->roles[$r][$stateKey] = $stateValue;
  21. }
  22. }
  23. public function setRoleOpt($r, $optKey, $optVal) {
  24. if (!isset($this->roles[$r])) {
  25. $this->roles[$r] = array();
  26. }
  27. if (!isset($this->roles[$r][OPT])) {
  28. $this->roles[$r][OPT] = array();
  29. $this->roles[$r][OPT][$optKey] = $optVal;
  30. } else {
  31. $this->roles[$r][OPT][$optKey] = $optVal;
  32. }
  33. }
  34. private function generateStageChain() {
  35. $chain = "";
  36. $stages = array();
  37. foreach($this->roles as $roleName => $val) {
  38. $stages[] = $val[STAGE];
  39. }
  40. asort($stages, SORT_NUMERIC);
  41. $first = true;
  42. foreach ($stages as $s) {
  43. if ($first) {
  44. $first = false;
  45. } else {
  46. $chain = $chain . " -> ";
  47. }
  48. $chain = $chain . "stage{" . $s . " :}";
  49. }
  50. return $chain;
  51. }
  52. private function generateOptList($optList) {
  53. $optChain = "";
  54. $first = true;
  55. foreach($optList as $key => $val) {
  56. if ($first) {
  57. $first = false;
  58. } else {
  59. $optChain = $optChain . ", ";
  60. }
  61. $optChain = $optChain . $key . " => " . $val ;
  62. }
  63. return $optChain;
  64. }
  65. private function generateRoleLine($r) {
  66. $roleLine = "";
  67. $roleLine = $roleLine . "class {'" . $r . "': ";
  68. $isFirst = true;
  69. if (isset($this->roles[$r][OPT])) {
  70. $roleLine = $roleLine . OPT . " => {";
  71. $roleLine = $roleLine . $this->generateOptList($this->roles[$r][OPT]);
  72. $roleLine = $roleLine . "}";
  73. $isFirst = false;
  74. }
  75. foreach($this->roles[$r] as $key => $value) {
  76. if ($key != OPT) {
  77. if (!$isFirst) {
  78. $roleLine = $roleLine . ", ";
  79. } else {
  80. $isFirst = false;
  81. }
  82. $roleLine = $roleLine . $key . " => " . $value;
  83. }
  84. }
  85. $roleLine = $roleLine . "}";
  86. return $roleLine;
  87. }
  88. public function generateNodeManifest() {
  89. $manifest = "node /" . $this->node . "/ {\n";
  90. $manifest = $manifest . $this->generateStageChain();
  91. $manifest = $manifest . "\n";
  92. $stages = array();
  93. foreach($this->roles as $roleName => $val) {
  94. $stages[] = $val[STAGE];
  95. }
  96. asort($stages, SORT_NUMERIC);
  97. foreach($stages as $theStage) {
  98. //Print in the order of the stages
  99. foreach ($this->roles as $roleName => $roleVal) {
  100. if ($roleVal[STAGE] == $theStage) {
  101. $manifest = $manifest . $this->generateRoleLine($roleName);
  102. $manifest = $manifest . "\n";
  103. }
  104. }
  105. }
  106. $manifest = $manifest . "}\n";
  107. return $manifest;
  108. }
  109. public function setNamenodeRoleState($state, $stage) {
  110. $role = "hdp-hadoop::namenode";
  111. $this->setRoleState($role, SERVICESTATE, $state);
  112. $this->setRoleStage($role, $stage);
  113. }
  114. public function setSecondaryNamenodeRoleState($state, $stage) {
  115. $role = "hdp-hadoop::snamenode";
  116. $this->setRoleState($role, SERVICESTATE, $state);
  117. $this->setRoleStage($role, $stage);
  118. }
  119. public function setDatanodeRoleState($state, $stage) {
  120. $role = "hdp-hadoop::datanode";
  121. $this->setRoleState($role, SERVICESTATE, $state);
  122. $this->setRoleStage($role, $stage);
  123. }
  124. public function setJobTrackerRoleState($state, $stage) {
  125. $role = "hdp-hadoop::jobtracker";
  126. $this->setRoleState($role, SERVICESTATE, $state);
  127. $this->setRoleStage($role, $stage);
  128. }
  129. public function setTaskTrackerRoleState($state, $stage) {
  130. $role = "hdp-hadoop::tasktracker";
  131. $this->setRoleState($role, SERVICESTATE, $state);
  132. $this->setRoleStage($role, $stage);
  133. }
  134. public function setHadoopClientRoleState($state, $stage) {
  135. $role = "hdp-hadoop::client";
  136. $this->setRoleState($role, SERVICESTATE, $state);
  137. $this->setRoleStage($role, $stage);
  138. }
  139. public function setZooKeeperRoleState($state, $stage) {
  140. $role = "hdp-zookeeper";
  141. $this->setRoleState($role, SERVICESTATE, $state);
  142. $this->setRoleStage($role, $stage);
  143. }
  144. public function setZooKeeperClientRoleState($state, $stage) {
  145. $role = "hdp-zookeeper::client";
  146. $this->setRoleState($role, SERVICESTATE, $state);
  147. $this->setRoleStage($role, $stage);
  148. }
  149. public function setHbaseMasterRoleState($state, $stage) {
  150. $role = "hdp-hbase::master";
  151. $this->setRoleState($role, SERVICESTATE, $state);
  152. $this->setRoleStage($role, $stage);
  153. }
  154. public function setHbaseRegionServerRoleState($state, $stage) {
  155. $role = "hdp-hbase::regionserver";
  156. $this->setRoleState($role, SERVICESTATE, $state);
  157. $this->setRoleStage($role, $stage);
  158. }
  159. public function setHbaseClientRoleState($state, $stage) {
  160. $role = "hdp-hbase::client";
  161. $this->setRoleState($role, SERVICESTATE, $state);
  162. $this->setRoleStage($role, $stage);
  163. }
  164. }
  165. ?>