hostsConfig.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. class HostsConfigManifest {
  3. /**
  4. * The keys in the data structures are assumed to be already converted
  5. * to names required by puppet.
  6. */
  7. public static function getHostsConfigManifest($dbHostEntries,
  8. $dbConfigEntries) {
  9. $manifest = "";
  10. //Generate Hosts manifest
  11. foreach($dbHostEntries as $key => $hosts) {
  12. $hostList = "";
  13. if (is_array($hosts)) {
  14. $hostList = "[";
  15. $first = true;
  16. foreach ($hosts as $h) {
  17. if ($first) {
  18. $first = false;
  19. } else {
  20. $hostList = $hostList . ",";
  21. }
  22. $hostList = $hostList . "'" . $h . "'";
  23. }
  24. $hostList = $hostList . "]";
  25. } else {
  26. $hostList = "\"" . $hosts . "\"";
  27. }
  28. $manifest = $manifest . "$" . $key . " = " . $hostList . "\n";
  29. }
  30. $manifest = $manifest . "\n";
  31. foreach($dbConfigEntries as $key => $configEntry) {
  32. $manifest = $manifest .
  33. HostsConfigManifest::getConfigLine($key, $configEntry);
  34. }
  35. return $manifest;
  36. }
  37. private static function getPublicFqdnLine($hostName, $publicFqdn) {
  38. return "\"" . $hostName . "\"" . " => { " . "\"publicfqdn\" => " . "\"" . $publicFqdn . "\" }";
  39. }
  40. public static function getHostsAttributesManifest($hostAttributes) {
  41. $manifest = "";
  42. if (empty($hostAttributes["hosts"])) {
  43. return "";
  44. }
  45. $first = true;
  46. foreach ($hostAttributes["hosts"] as $hostDetails) {
  47. if ( (empty($hostDetails["hostName"])) ||
  48. (empty($hostDetails["attributes"])) ||
  49. (empty($hostDetails["attributes"]["publicFQDN"])) ) {
  50. continue;
  51. }
  52. $hostName = $hostDetails["hostName"];
  53. $publicFqdn = $hostDetails["attributes"]["publicFQDN"];
  54. if (!$first) {
  55. $manifest = $manifest . ",\n";
  56. } else {
  57. $first = false;
  58. }
  59. $manifest = $manifest . "\t" . HostsConfigManifest::getPublicFqdnLine($hostName, $publicFqdn) ;
  60. }
  61. if (!empty($manifest)) {
  62. $manifest = "\$" . "hostAttributes = {\n" . $manifest;
  63. $manifest = $manifest . "\n}\n";
  64. }
  65. return $manifest;
  66. }
  67. private static function getConfigLine($key, $value) {
  68. if (empty($value)) {
  69. return "";
  70. }
  71. $manifest = "$" . $key . " = ";
  72. if ( ($key == "lzo_enabled") ||
  73. ($key == "snappy_enabled") ||
  74. ($key == "wipeoff_data") ) {
  75. $manifest = $manifest . $value . "\n";
  76. } else {
  77. $manifest = $manifest . "\"" . $value . "\"\n";
  78. }
  79. return $manifest;
  80. }
  81. }
  82. ?>