generateManifest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. include "hostsConfig.php";
  3. include "nodeManifest.php";
  4. include "RoleDependencies.php";
  5. class ManifestGenerator {
  6. public static function generateManifest($dir, $hostInfo,
  7. $configInfo, $hostRoles, $hostAttributes, $modulesDir) {
  8. $sitePPFile = $dir . "/site.pp";
  9. //Delete file if exists
  10. if (file_exists($sitePPFile)) {
  11. unlink($sitePPFile);
  12. }
  13. $fh = fopen($dir . "/site.pp", "w");
  14. $importString = ManifestGenerator::getAllImports($modulesDir);
  15. fwrite($fh, $importString);
  16. $hostsConfig = HostsConfigManifest::getHostsConfigManifest($hostInfo,
  17. $configInfo);
  18. fwrite($fh, $hostsConfig . "\n");
  19. //Write internal to external mapping if exists
  20. $hostsAttributeManifest = HostsConfigManifest::getHostsAttributesManifest(
  21. $hostAttributes);
  22. fwrite($fh, $hostsAttributeManifest . "\n");
  23. $roleDepObj = new RoleDependencies();
  24. foreach ($hostRoles as $n => $rolesStates) {
  25. $nm = new NodeManifest($n);
  26. $roleList = array_keys($rolesStates);
  27. $rolesStatesOrig = $rolesStates;
  28. foreach($rolesStatesOrig as $role => $states) {
  29. //Add host level package dependencies
  30. $serviceState = SERVICE_STATE_NOT_APPLICABLE;
  31. if (isset($states[SERVICE_STATE_KEY])) {
  32. $serviceState = $states[SERVICE_STATE_KEY];
  33. }
  34. $hostLevelDependencies = $roleDepObj->getHostLevelDependencies($role,
  35. $serviceState);
  36. foreach ($hostLevelDependencies as $depRole => $rstates) {
  37. $roleList[] = $depRole;
  38. //The state could be conflicting, pick the appropriate state
  39. //e.g. a dependency shows up with two different service states
  40. ManifestGenerator::resolveDepRoleStates($depRole, $rstates, $rolesStates);
  41. }
  42. }
  43. $roleList = array_unique($roleList);
  44. //Determine stages for each role
  45. $roleStages = $roleDepObj->getRolesStages($roleList);
  46. asort($roleStages, SORT_NUMERIC);
  47. foreach ($roleStages as $r => $s) {
  48. $nm->setRoleStage($r, $s);
  49. if (isset($rolesStates[$r])) {
  50. foreach($rolesStates[$r] as $stateName => $stateVal) {
  51. $nm->setRoleState($r, $stateName, $stateVal);
  52. }
  53. }
  54. }
  55. ManifestGenerator::optimizePackageInstall($n, $nm, $rolesStates, $configInfo, $roleStages);
  56. $nodeManifestString = $nm->generateNodeManifest();
  57. fwrite($fh, $nodeManifestString . "\n");
  58. }
  59. fclose($fh);
  60. }
  61. private static function priorityState($u, $v) {
  62. $order = array (
  63. SERVICE_STATE_NOT_APPLICABLE => 0,
  64. SERVICE_STATE_UNINSTALLED => 1,
  65. SERVICE_STATE_STOPPED => 2,
  66. SERVICE_STATE_NO_OP => 3,
  67. SERVICE_STATE_INSTALLED_AND_CONFIGURED => 4,
  68. SERVICE_STATE_RUNNING => 5
  69. );
  70. if (!isset($v)) {
  71. return $u;
  72. } else if (!isset($u)) {
  73. return $v;
  74. } else if ($order[$u] > $order[$v]) {
  75. return $u;
  76. } else {
  77. return $v;
  78. }
  79. }
  80. private static function resolveDepRoleStates($depRole, $rstates, &$rolesStates) {
  81. foreach ($rstates as $k => $v) {
  82. if ($k == SERVICE_STATE_KEY) {
  83. $alreadySetSvcState = NULL;
  84. if (isset($rolesStates[$depRole][$k])) {
  85. $alreadySetSvcState = $rolesStates[$depRole][$k];
  86. }
  87. $rolesStates[$depRole][$k] =
  88. ManifestGenerator::priorityState($v, $alreadySetSvcState);
  89. } else {
  90. //Assume no conflicts for this key
  91. $rolesStates[$depRole][$k] = $v;
  92. }
  93. }
  94. }
  95. private static function optimizePackageInstall($node, &$manifestObj, $rolesStatesDs,
  96. $configInfo, $roleStages) {
  97. //Figure out the state
  98. $serviceState = SERVICE_STATE_INSTALLED_AND_CONFIGURED;
  99. foreach ($rolesStatesDs as $r => $stateList) {
  100. if ((isset($stateList[SERVICE_STATE_KEY])) &&
  101. ($stateList[SERVICE_STATE_KEY] != SERVICE_STATE_NO_OP )) {
  102. if ($stateList[SERVICE_STATE_KEY] != SERVICE_STATE_INSTALLED_AND_CONFIGURED) {
  103. $serviceState = NULL;
  104. break;
  105. }
  106. }
  107. }
  108. if (!isset($serviceState)) {
  109. $serviceState = SERVICE_STATE_UNINSTALLED;
  110. //See if it is uninstalled
  111. foreach ($rolesStatesDs as $r => $stateList) {
  112. if ( (isset($stateList[SERVICE_STATE_KEY])) &&
  113. ($stateList[SERVICE_STATE_KEY] != SERVICE_STATE_NO_OP )) {
  114. if ($stateList[SERVICE_STATE_KEY] != SERVICE_STATE_UNINSTALLED) {
  115. $serviceState = NULL;
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. if (!isset($serviceState)) {
  122. //No optimization needed
  123. return;
  124. }
  125. //get list of packages
  126. $stages = array();
  127. foreach($roleStages as $roleName => $val) {
  128. $stages[$val] = $roleName;
  129. }
  130. ksort($stages, SORT_NUMERIC);
  131. $packageList = array();
  132. foreach($stages as $theStage => $r) {
  133. if (!isset($rolesStatesDs[$r])) {
  134. continue;
  135. }
  136. //Add in the order of the stages
  137. $stateList = $rolesStatesDs[$r];
  138. if ($stateList[SERVICE_STATE_KEY] != $serviceState) {
  139. continue;
  140. }
  141. if (isset(self::$rolesToPackageMap[$r])) {
  142. $p = self::$rolesToPackageMap[$r];
  143. if (!in_array($p, $packageList)) {
  144. $packageList[] = $p;
  145. }
  146. }
  147. }
  148. if (empty($packageList)) {
  149. //No packages don't bother
  150. return;
  151. }
  152. //lzo and snappy
  153. $packageList[] = self::$rolesToPackageMap["snappy"];
  154. if ($configInfo["lzo_enabled"] == "true") {
  155. $packageList[] = self::$rolesToPackageMap["lzo"];
  156. }
  157. $firstP = true;
  158. $pList = "\"";
  159. foreach ($packageList as $p) {
  160. if ($firstP) {
  161. $firstP = false;
  162. } else {
  163. $pList = $pList . " ";
  164. }
  165. $pList = $pList . $p;
  166. }
  167. $pList = $pList . "\"";
  168. $manifestObj->setRoleState("hdp", SERVICE_STATE_KEY, $serviceState);
  169. $manifestObj->setRoleState("hdp", "pre_installed_pkgs", $pList);
  170. }
  171. private static function getAllImports($modulesDir) {
  172. $importString = "";
  173. $importString = $importString . "import \"" . $modulesDir . "/hdp/manifests/*.pp" ."\"\n";
  174. $importString = $importString . "import \"" . $modulesDir . "/hdp-hadoop/manifests/*.pp" ."\"\n";
  175. $importString = $importString . "import \"" . $modulesDir . "/hdp-hbase/manifests/*.pp" ."\"\n";
  176. $importString = $importString . "import \"" . $modulesDir . "/hdp-zookeeper/manifests/*.pp" ."\"\n";
  177. $importString = $importString . "import \"" . $modulesDir . "/hdp-oozie/manifests/*.pp" ."\"\n";
  178. $importString = $importString . "import \"" . $modulesDir . "/hdp-pig/manifests/*.pp" ."\"\n";
  179. $importString = $importString . "import \"" . $modulesDir . "/hdp-sqoop/manifests/*.pp" ."\"\n";
  180. $importString = $importString . "import \"" . $modulesDir . "/hdp-templeton/manifests/*.pp" ."\"\n";
  181. $importString = $importString . "import \"" . $modulesDir . "/hdp-hive/manifests/*.pp" ."\"\n";
  182. $importString = $importString . "import \"" . $modulesDir . "/hdp-hcat/manifests/*.pp" ."\"\n";
  183. $importString = $importString . "import \"" . $modulesDir . "/hdp-mysql/manifests/*.pp" ."\"\n";
  184. $importString = $importString . "import \"" . $modulesDir . "/hdp-monitor-webserver/manifests/*.pp" ."\"\n";
  185. return $importString;
  186. }
  187. private static $rolesToPackageMap = array (
  188. "hdp-hadoop::namenode" => "hadoop hadoop-libhdfs.x86_64 hadoop-native.x86_64 hadoop-pipes.x86_64 hadoop-sbin.x86_64",
  189. "hdp-hadoop::snamenode" => "hadoop hadoop-libhdfs.x86_64 hadoop-native.x86_64 hadoop-pipes.x86_64 hadoop-sbin.x86_64",
  190. "hdp-hadoop::jobtracker" => "hadoop hadoop-libhdfs.x86_64 hadoop-native.x86_64 hadoop-pipes.x86_64 hadoop-sbin.x86_64",
  191. "hdp-hadoop::client" => "hadoop hadoop-libhdfs.i386 hadoop-native.i386 hadoop-pipes.i386 hadoop-sbin.i386",
  192. "hdp-hadoop::datanode" => "hadoop hadoop-libhdfs.i386 hadoop-native.i386 hadoop-pipes.i386 hadoop-sbin.i386",
  193. "hdp-hadoop::tasktracker" => "hadoop hadoop-libhdfs.i386 hadoop-native.i386 hadoop-pipes.i386 hadoop-sbin.i386",
  194. "hdp-zookeeper" => "zookeeper",
  195. "hdp-zookeeper::client" => "zookeeper",
  196. "hdp-hbase::master" => "hbase",
  197. "hdp-hbase::regionserver" => "hbase",
  198. "hdp-hbase::client" => "hbase",
  199. "hdp-pig" => "pig.noarch",
  200. "hdp-sqoop" => "sqoop",
  201. "hdp-hive::server" => "hive mysql-connector-java-5.0.8-4.jpp5",
  202. "hdp-hive::client" => "hive",
  203. "hdp-hcat" => "hcatalog",
  204. "hdp-oozie::server" => "oozie.noarch extjs-2.2-1",
  205. "hdp-oozie::client" => "oozie-client.noarch",
  206. "hdp-mysql::server" => "mysql-server",
  207. "hdp-templeton::server" => "templeton templeton-tar-pig-0.0.1-1 templeton-tar-hive-0.0.1-1",
  208. "hdp-templeton::client" => "templeton",
  209. "lzo" => "lzo lzo.i386 lzo-devel lzo-devel.i386",
  210. "snappy" => "snappy snappy-devel",
  211. "hdp-ganglia::server" => "ganglia-gmetad-3.2.0 ganglia-gmond-3.2.0 gweb hdp_mon_ganglia_addons",
  212. "hdp-ganglia::monitor" => "ganglia-gmond-3.2.0 gweb hdp_mon_ganglia_addons",
  213. "hdp-nagios::server" => "hdp_mon_nagios_addons nagios-3.2.3 nagios-plugins-1.4.9 fping net-snmp-utils",
  214. "hdp-dashboard" => "hdp_mon_dashboard",
  215. );
  216. }
  217. ?>