load.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. define("RMQ_INI_SECTION", "rabbitmq");
  3. require_once dirname(dirname( __DIR__)) . '/library/php-amqplib/amqp.inc';
  4. function booleanReduce($a, $b) {
  5. return $a && $b;
  6. }
  7. /**
  8. * Check to see if Airtime is properly configured.
  9. *
  10. * @return boolean true if all Airtime dependencies and services are
  11. * properly configured and running
  12. */
  13. function checkConfiguration() {
  14. $r1 = array_reduce(checkPhpDependencies(), "booleanReduce", true);
  15. $r2 = array_reduce(checkExternalServices(), "booleanReduce", true);
  16. return $r1 && $r2;
  17. }
  18. /**
  19. * Check for Airtime's PHP dependencies and return an associative
  20. * array with the results
  21. *
  22. * @return array associative array of dependency check results
  23. */
  24. function checkPhpDependencies() {
  25. return array(
  26. "zend" => checkZendDependencies(),
  27. "postgres" => checkDatabaseDependencies()
  28. );
  29. }
  30. /**
  31. * Check that the Zend framework libraries are installed
  32. *
  33. * @return boolean true if Zend exists in /usr/share/php
  34. */
  35. function checkZendDependencies() {
  36. return file_exists('/usr/share/php/libzend-framework-php')
  37. || file_exists('/usr/share/php/Zend'); // Debian version
  38. }
  39. /**
  40. * Check that the PHP dependencies for the database exist
  41. *
  42. * @return boolean true if the database dependencies exist
  43. */
  44. function checkDatabaseDependencies() {
  45. global $extensions;
  46. // Check the PHP extension list for the Postgres db extensions
  47. return (in_array('pdo_pgsql', $extensions)
  48. && in_array('pgsql', $extensions));
  49. }
  50. /**
  51. * Check that all external services are configured correctly and return an associative
  52. * array with the results
  53. *
  54. * @return array associative array of external service check results
  55. */
  56. function checkExternalServices() {
  57. return array(
  58. "database" => checkDatabaseConfiguration(),
  59. "media-monitor" => checkMediaMonitorService(),
  60. "pypo" => checkPlayoutService(),
  61. "liquidsoap" => checkLiquidsoapService(),
  62. "rabbitmq" => checkRMQConnection()
  63. );
  64. }
  65. /**
  66. * Check the database configuration by fetching a connection from Propel
  67. *
  68. * @return boolean true if a connection is made to the database
  69. */
  70. function checkDatabaseConfiguration() {
  71. configureDatabase();
  72. try {
  73. // Try to establish a database connection. If something goes
  74. // wrong, the database is improperly configured
  75. Propel::getConnection();
  76. Propel::close();
  77. } catch (Exception $e) {
  78. return false;
  79. }
  80. return true;
  81. }
  82. /**
  83. * Initialize Propel to configure the Airtime database
  84. */
  85. function configureDatabase() {
  86. Propel::init(CONFIG_PATH . 'airtime-conf-production.php');
  87. }
  88. /**
  89. * Check that we can connect to RabbitMQ
  90. *
  91. * @return true if the RabbitMQ connection can be established
  92. */
  93. function checkRMQConnection() {
  94. // Check for airtime.conf in /etc/airtime/ first, then check in the build directory,
  95. if (file_exists(AIRTIME_CONFIG_STOR . AIRTIME_CONFIG)) {
  96. $ini = parse_ini_file(AIRTIME_CONFIG_STOR . AIRTIME_CONFIG, true);
  97. } else {
  98. $ini = parse_ini_file(BUILD_PATH . "airtime.example.conf", true);
  99. }
  100. $conn = new AMQPConnection($ini[RMQ_INI_SECTION]["host"],
  101. $ini[RMQ_INI_SECTION]["port"],
  102. $ini[RMQ_INI_SECTION]["user"],
  103. $ini[RMQ_INI_SECTION]["password"],
  104. $ini[RMQ_INI_SECTION]["vhost"]);
  105. return isset($conn);
  106. }
  107. /**
  108. * Check if airtime-media-monitor is currently running
  109. *
  110. * @return boolean true if airtime-media-monitor is running
  111. */
  112. function checkMediaMonitorService() {
  113. exec("pgrep -f -u www-data airtime-media-monitor", $out, $status);
  114. if (array_key_exists(0, $out) && $status == 0) {
  115. return posix_kill(rtrim($out[0]), 0);
  116. }
  117. return $status == 0;
  118. }
  119. /**
  120. * Check if airtime-playout is currently running
  121. *
  122. * @return boolean true if airtime-playout is running
  123. */
  124. function checkPlayoutService() {
  125. exec("pgrep -f -u www-data airtime-playout", $out, $status);
  126. if (array_key_exists(0, $out) && $status == 0) {
  127. return posix_kill(rtrim($out[0]), 0);
  128. }
  129. return $status == 0;
  130. }
  131. /**
  132. * Check if airtime-liquidsoap is currently running
  133. *
  134. * @return boolean true if airtime-liquidsoap is running
  135. */
  136. function checkLiquidsoapService() {
  137. exec("pgrep -f -u www-data airtime-liquidsoap", $out, $status);
  138. if (array_key_exists(0, $out) && $status == 0) {
  139. return posix_kill(rtrim($out[0]), 0);
  140. }
  141. return $status == 0;
  142. }