generateManifest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. foreach ($p as $apack) {
  144. if (!in_array($p, $packageList)) {
  145. $packageList[] = $apack;
  146. }
  147. }
  148. }
  149. }
  150. if (empty($packageList)) {
  151. //No packages don't bother
  152. return;
  153. }
  154. //lzo and snappy
  155. $snappyPackages = self::$rolesToPackageMap["snappy"];
  156. foreach ($snappyPackages as $spack) {
  157. if (!in_array($spack, $packageList)) {
  158. $packageList[] = $spack;
  159. }
  160. }
  161. if ($configInfo["lzo_enabled"] == "true") {
  162. $p = self::$rolesToPackageMap["lzo"];
  163. foreach ($p as $apack) {
  164. if (!in_array($p, $packageList)) {
  165. $packageList[] = $apack;
  166. }
  167. }
  168. }
  169. $firstP = true;
  170. $pList = "\"";
  171. foreach ($packageList as $p) {
  172. if ($firstP) {
  173. $firstP = false;
  174. } else {
  175. $pList = $pList . " ";
  176. }
  177. $pList = $pList . $p;
  178. }
  179. $pList = $pList . "\"";
  180. $manifestObj->setRoleState("hdp", SERVICE_STATE_KEY, $serviceState);
  181. $manifestObj->setRoleState("hdp", "pre_installed_pkgs", $pList);
  182. }
  183. private static function getAllImports($modulesDir) {
  184. $importString = "";
  185. $importString = $importString . "import \"" . $modulesDir . "/hdp/manifests/*.pp" ."\"\n";
  186. $importString = $importString . "import \"" . $modulesDir . "/hdp-hadoop/manifests/*.pp" ."\"\n";
  187. $importString = $importString . "import \"" . $modulesDir . "/hdp-hbase/manifests/*.pp" ."\"\n";
  188. $importString = $importString . "import \"" . $modulesDir . "/hdp-zookeeper/manifests/*.pp" ."\"\n";
  189. $importString = $importString . "import \"" . $modulesDir . "/hdp-oozie/manifests/*.pp" ."\"\n";
  190. $importString = $importString . "import \"" . $modulesDir . "/hdp-pig/manifests/*.pp" ."\"\n";
  191. $importString = $importString . "import \"" . $modulesDir . "/hdp-sqoop/manifests/*.pp" ."\"\n";
  192. $importString = $importString . "import \"" . $modulesDir . "/hdp-templeton/manifests/*.pp" ."\"\n";
  193. $importString = $importString . "import \"" . $modulesDir . "/hdp-hive/manifests/*.pp" ."\"\n";
  194. $importString = $importString . "import \"" . $modulesDir . "/hdp-hcat/manifests/*.pp" ."\"\n";
  195. $importString = $importString . "import \"" . $modulesDir . "/hdp-mysql/manifests/*.pp" ."\"\n";
  196. $importString = $importString . "import \"" . $modulesDir . "/hdp-monitor-webserver/manifests/*.pp" ."\"\n";
  197. return $importString;
  198. }
  199. private static $rolesToPackageMap = array (
  200. "hdp-hadoop::namenode" => array ("hadoop", "hadoop-libhdfs.x86_64", "hadoop-native.x86_64", "hadoop-pipes.x86_64",
  201. "hadoop-sbin.x86_64", "hadoop-lzo"),
  202. "hdp-hadoop::snamenode" => array ("hadoop", "hadoop-libhdfs.x86_64", "hadoop-native.x86_64", "hadoop-pipes.x86_64",
  203. "hadoop-sbin.x86_64", "hadoop-lzo"),
  204. "hdp-hadoop::jobtracker" => array ("hadoop", "hadoop-libhdfs.x86_64", "hadoop-native.x86_64", "hadoop-pipes.x86_64",
  205. "hadoop-sbin.x86_64", "hadoop-lzo"),
  206. "hdp-hadoop::client" => array ("hadoop hadoop-libhdfs.i386", "hadoop-native.i386",
  207. "hadoop-pipes.i386", "hadoop-sbin.i386", "hadoop-lzo"),
  208. "hdp-hadoop::client" => array ("hadoop hadoop-libhdfs.i386", "hadoop-native.i386",
  209. "hadoop-pipes.i386", "hadoop-sbin.i386", "hadoop-lzo"),
  210. "hdp-hadoop::datanode" => array ("hadoop hadoop-libhdfs.i386", "hadoop-native.i386",
  211. "hadoop-pipes.i386", "hadoop-sbin.i386", "hadoop-lzo"),
  212. "hdp-hadoop::tasktracker" => array ("hadoop hadoop-libhdfs.i386", "hadoop-native.i386",
  213. "hadoop-pipes.i386", "hadoop-sbin.i386", "hadoop-lzo"),
  214. "hdp-zookeeper" => array ("zookeeper"),
  215. "hdp-zookeeper::client" => array ("zookeeper"),
  216. "hdp-hbase::master" => array ("hbase"),
  217. "hdp-hbase::regionserver" => array("hbase"),
  218. "hdp-hbase::client" => array("hbase"),
  219. "hdp-pig" => array("pig.noarch"),
  220. "hdp-sqoop" => array("sqoop", "mysql-connector-java"),
  221. "hdp-hive::server" => array("hive", "mysql-connector-java"),
  222. "hdp-hive::client" => array("hive"),
  223. "hdp-hcat" => array("hcatalog"),
  224. "hdp-oozie::server" => array("oozie.noarch", "extjs-2.2-1"),
  225. "hdp-oozie::client" => array("oozie-client.noarch"),
  226. "hdp-mysql::server" => array("mysql-server"),
  227. "hdp-templeton::server" => array("templeton", "templeton-tar-pig-0.0.1-1", "templeton-tar-hive-0.0.1-1"),
  228. "hdp-templeton::client" => array("templeton"),
  229. "lzo" => array("lzo", "lzo.i386", "lzo-devel", "lzo-devel.i386"),
  230. "snappy" => array("snappy", "snappy-devel"),
  231. "hdp-ganglia::server" => array("ganglia-gmetad-3.2.0", "ganglia-gmond-3.2.0", "gweb", "hdp_mon_ganglia_addons"),
  232. "hdp-ganglia::monitor" => array("ganglia-gmond-3.2.0", "gweb", "hdp_mon_ganglia_addons"),
  233. "hdp-nagios::server" => array("hdp_mon_nagios_addons", "nagios-3.2.3", "nagios-plugins-1.4.9", "fping", "net-snmp-utils"),
  234. "hdp-dashboard" => array("hdp_mon_dashboard"),
  235. );
  236. }
  237. ?>