rabbitmq-setup.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. require_once dirname(dirname( __DIR__)) . '/library/php-amqplib/amqp.inc';
  3. /**
  4. * User: sourcefabric
  5. * Date: 02/12/14
  6. *
  7. * Class RabbitMQSetup
  8. *
  9. * Wrapper class for validating and setting up RabbitMQ during the installation process
  10. */
  11. class RabbitMQSetup extends Setup {
  12. // airtime.conf section header
  13. const SECTION = "[rabbitmq]";
  14. // Constant form field names for passing errors back to the front-end
  15. const RMQ_USER = "rmqUser",
  16. RMQ_PASS = "rmqPass",
  17. RMQ_PORT = "rmqPort",
  18. RMQ_HOST = "rmqHost",
  19. RMQ_VHOST = "rmqVHost";
  20. // Form field values
  21. static $user, $pass, $host, $port, $vhost;
  22. // Array of key->value pairs for airtime.conf
  23. static $properties;
  24. // Message and error fields to return to the front-end
  25. static $message = null;
  26. static $errors = array();
  27. function __construct($settings) {
  28. self::$user = $settings[self::RMQ_USER];
  29. self::$pass = $settings[self::RMQ_PASS];
  30. self::$port = $settings[self::RMQ_PORT];
  31. self::$host = $settings[self::RMQ_HOST];
  32. self::$vhost = $settings[self::RMQ_VHOST];
  33. self::$properties = array(
  34. "host" => self::$host,
  35. "port" => self::$port,
  36. "user" => self::$user,
  37. "password" => self::$pass,
  38. "vhost" => self::$vhost,
  39. );
  40. }
  41. /**
  42. * @return array associative array containing a display message and fields with errors
  43. */
  44. function runSetup() {
  45. try {
  46. if ($this->checkRMQConnection()) {
  47. self::$message = "Connection successful!";
  48. } else {
  49. $this->identifyRMQConnectionError();
  50. }
  51. } catch(Exception $e) {
  52. $this->identifyRMQConnectionError();
  53. }
  54. if (count(self::$errors) <= 0) {
  55. $this->writeToTemp();
  56. }
  57. return array(
  58. "message" => self::$message,
  59. "errors" => self::$errors
  60. );
  61. }
  62. function writeToTemp() {
  63. parent::writeToTemp(self::SECTION, self::$properties);
  64. }
  65. function checkRMQConnection() {
  66. $conn = new AMQPConnection(self::$host,
  67. self::$port,
  68. self::$user,
  69. self::$pass,
  70. self::$vhost);
  71. return isset($conn);
  72. }
  73. function identifyRMQConnectionError() {
  74. // It's impossible to identify errors coming out of amqp.inc without a major
  75. // rewrite, so for now just tell the user ALL THE THINGS went wrong
  76. self::$message = "Couldn't connect to RabbitMQ server! Please check if the server "
  77. . "is running and your credentials are correct.";
  78. self::$errors[] = self::RMQ_USER;
  79. self::$errors[] = self::RMQ_PASS;
  80. self::$errors[] = self::RMQ_HOST;
  81. self::$errors[] = self::RMQ_PORT;
  82. self::$errors[] = self::RMQ_VHOST;
  83. }
  84. }