Update installer to work with saas branch

This commit is contained in:
Duncan Sommerville 2015-06-23 19:02:55 -04:00
parent d48e594dcd
commit 4c797cf100
13 changed files with 168 additions and 144 deletions

View file

@ -9,7 +9,7 @@
class DatabaseSetup extends Setup {
// airtime.conf section header
const SECTION = "[database]";
protected static $_settings = "[database]";
// Constant form field names for passing errors back to the front-end
const DB_USER = "dbUser",
@ -17,31 +17,26 @@ class DatabaseSetup extends Setup {
DB_NAME = "dbName",
DB_HOST = "dbHost";
// Form field values
private $user, $pass, $name, $host;
// Array of key->value pairs for airtime.conf
static $properties;
protected static $_properties;
/**
* @var PDO
*/
static $dbh = null;
public function __construct($settings) {
$this->user = $settings[self::DB_USER];
$this->pass = $settings[self::DB_PASS];
$this->name = $settings[self::DB_NAME];
$this->host = $settings[self::DB_HOST];
self::$properties = array(
"host" => $this->host,
"dbname" => $this->name,
"dbuser" => $this->user,
"dbpass" => $this->pass,
static::$_properties = array(
"host" => $settings[self::DB_HOST],
"dbname" => $settings[self::DB_NAME],
"dbuser" => $settings[self::DB_USER],
"dbpass" => $settings[self::DB_PASS],
);
}
private function setNewDatabaseConnection($dbName) {
self::$dbh = new PDO("pgsql:host=" . $this->host . ";dbname=" . $dbName . ";port=5432"
. ";user=" . $this->user . ";password=" . $this->pass);
self::$dbh = new PDO("pgsql:host=" . self::$_properties["host"] . ";dbname=" . $dbName . ";port=5432"
. ";user=" . self::$_properties["dbuser"] . ";password=" . self::$_properties["dbpass"]);
$err = self::$dbh->errorInfo();
if ($err[1] != null) {
throw new PDOException();
@ -69,11 +64,7 @@ class DatabaseSetup extends Setup {
throw new AirtimeDatabaseException("Couldn't establish a connection to the database! "
. "Please check your credentials and try again. "
. "PDO Exception: " . $e->getMessage(),
array(
self::DB_NAME,
self::DB_USER,
self::DB_PASS,
));
array(self::DB_NAME, self::DB_USER, self::DB_PASS));
}
$this->writeToTemp();
@ -85,13 +76,9 @@ class DatabaseSetup extends Setup {
);
}
protected function writeToTemp() {
parent::writeToTemp(self::SECTION, self::$properties);
}
private function installDatabaseTables() {
$this->checkDatabaseEncoding();
$this->setNewDatabaseConnection($this->name);
$this->setNewDatabaseConnection(self::$_properties["dbname"]);
$this->checkSchemaExists();
$this->createDatabaseTables();
}
@ -102,7 +89,7 @@ class DatabaseSetup extends Setup {
*/
private function checkDatabaseExists() {
$statement = self::$dbh->prepare("SELECT datname FROM pg_database WHERE datname = :dbname");
$statement->execute(array(":dbname" => $this->name));
$statement->execute(array(":dbname" => self::$_properties["dbname"]));
$result = $statement->fetch();
return isset($result[0]);
}
@ -126,16 +113,13 @@ class DatabaseSetup extends Setup {
*/
private function checkUserCanCreateDb() {
$statement = self::$dbh->prepare("SELECT 1 FROM pg_roles WHERE rolname=:dbuser AND rolcreatedb='t'");
$statement->execute(array(":dbuser" => $this->user));
$statement->execute(array(":dbuser" => self::$_properties["dbuser"]));
$result = $statement->fetch();
if (!isset($result[0])) {
throw new AirtimeDatabaseException("No database " . $this->name . " exists; user '" . $this->user
. "' does not have permission to create databases on " . $this->host,
array(
self::DB_NAME,
self::DB_USER,
self::DB_PASS,
));
throw new AirtimeDatabaseException("No database " . self::$_properties["dbname"] . " exists; user '"
. self::$_properties["dbuser"] . "' does not have permission to "
. "create databases on " . self::$_properties["host"],
array(self::DB_NAME, self::DB_USER, self::DB_PASS));
}
}
@ -144,9 +128,9 @@ class DatabaseSetup extends Setup {
* @throws AirtimeDatabaseException
*/
private function createDatabase() {
$statement = self::$dbh->prepare("CREATE DATABASE " . pg_escape_string($this->name)
$statement = self::$dbh->prepare("CREATE DATABASE " . pg_escape_string(self::$_properties["dbname"])
. " WITH ENCODING 'UTF8' TEMPLATE template0"
. " OWNER " . pg_escape_string($this->user));
. " OWNER " . pg_escape_string(self::$_properties["dbuser"]));
if (!$statement->execute()) {
throw new AirtimeDatabaseException("There was an error creating the database!",
array(self::DB_NAME,));
@ -169,8 +153,9 @@ class DatabaseSetup extends Setup {
* have multiple issues; they similarly die on any SQL errors, fail to read in multi-line
* commands, and fail on any unescaped ? or $ characters.
*/
exec("export PGPASSWORD=" . $this->pass . " && psql -U " . $this->user . " --dbname "
. $this->name . " -h " . $this->host . " -f $sqlDir$f 2>/dev/null", $out, $status);
exec("export PGPASSWORD=" . self::$_properties["dbpass"] . " && psql -U " . self::$_properties["dbuser"]
. " --dbname " . self::$_properties["dbname"] . " -h " . self::$_properties["host"]
. " -f $sqlDir$f 2>/dev/null", $out, $status);
} catch (Exception $e) {
throw new AirtimeDatabaseException("There was an error setting up the Airtime schema!",
array(self::DB_NAME,));
@ -185,7 +170,7 @@ class DatabaseSetup extends Setup {
private function checkDatabaseEncoding() {
$statement = self::$dbh->prepare("SELECT pg_encoding_to_char(encoding) "
. "FROM pg_database WHERE datname = :dbname");
$statement->execute(array(":dbname" => $this->name));
$statement->execute(array(":dbname" => self::$_properties["dbname"]));
$encoding = $statement->fetch();
if (!($encoding && $encoding[0] == "UTF8")) {
throw new AirtimeDatabaseException("The database was installed with an incorrect encoding type!",

View file

@ -11,37 +11,28 @@
class GeneralSetup extends Setup {
// airtime.conf section header
const SECTION = "[general]";
protected static $_section = "[general]";
// Constant form field names for passing errors back to the front-end
const GENERAL_PORT = "generalPort",
GENERAL_HOST = "generalHost";
// Form field values
static $user, $host, $port, $root;
// Array of key->value pairs for airtime.conf
static $properties;
protected static $_properties;
// Message and error fields to return to the front-end
static $message = null;
static $errors = array();
function __construct($settings) {
self::$host = $settings[self::GENERAL_HOST];
self::$port = $settings[self::GENERAL_PORT];
self::$properties = array(
self::$_properties = array(
"api_key" => $this->generateRandomString(),
"base_url" => self::$host,
"base_port" => self::$port,
"base_url" => $settings[self::GENERAL_HOST],
"base_port" => $settings[self::GENERAL_PORT],
);
}
function writeToTemp() {
parent::writeToTemp(self::SECTION, self::$properties);
}
/**
* @return array associative array containing a display message and fields with errors
*/

View file

@ -26,7 +26,9 @@ class MediaSetup extends Setup {
const MEDIA_FOLDER = "mediaFolder";
const AIRTIME_CONF_PATH = "/etc/airtime/airtime.conf";
const RMQ_INI_BASE_PATH = "/etc/airtime-saas/";
const RMQ_INI_FILE_NAME = "rabbitmq-analyzer.ini";
static $path;
static $message = null;
static $errors = array();
@ -62,10 +64,14 @@ class MediaSetup extends Setup {
// Finalize and move airtime.conf.temp
if (file_exists("/etc/airtime/")) {
if (!$this->moveAirtimeConfig()) {
$message = "Error moving airtime.conf or deleting /tmp/airtime.conf.temp!";
$errors[] = "ERR";
self::$message = "Error moving airtime.conf or deleting /tmp/airtime.conf.temp!";
self::$errors[] = "ERR";
}
if (!$this->moveRmqConfig()) {
self::$message = "Error moving rabbitmq-analyzer.ini or deleting /tmp/rabbitmq.ini.tmp!";
self::$errors[] = "ERR";
}
/*
* If we're upgrading from an old Airtime instance (pre-2.5.2) we rename their old
* airtime.conf to airtime.conf.tmp during the setup process. Now that we're done,
@ -75,8 +81,8 @@ class MediaSetup extends Setup {
rename(self::AIRTIME_CONF_PATH . ".tmp", self::AIRTIME_CONF_PATH . ".bak");
}
} else {
$message = "Failed to move airtime.conf; /etc/airtime doesn't exist!";
$errors[] = "ERR";
self::$message = "Failed to move airtime.conf; /etc/airtime doesn't exist!";
self::$errors[] = "ERR";
}
return array(
@ -84,7 +90,7 @@ class MediaSetup extends Setup {
"errors" => self::$errors
);
}
/**
* Moves /tmp/airtime.conf.temp to /etc/airtime.conf and then removes it to complete setup
* @return boolean false if either of the copy or removal operations fail
@ -94,6 +100,16 @@ class MediaSetup extends Setup {
&& unlink(AIRTIME_CONF_TEMP_PATH);
}
/**
* Moves /tmp/airtime.conf.temp to /etc/airtime.conf and then removes it to complete setup
* @return boolean false if either of the copy or removal operations fail
*/
function moveRmqConfig() {
return copy(RMQ_INI_TEMP_PATH, self::RMQ_INI_BASE_PATH . self::RMQ_INI_FILE_NAME)
&& copy(RMQ_INI_TEMP_PATH, self::RMQ_INI_BASE_PATH . "production/" . self::RMQ_INI_FILE_NAME)
&& unlink(RMQ_INI_TEMP_PATH);
}
/**
* Add the given directory to cc_music_dirs
* TODO Should we check for an existing entry in cc_music_dirs?

View file

@ -13,7 +13,10 @@ require_once dirname(dirname( __DIR__)) . '/library/php-amqplib/amqp.inc';
class RabbitMQSetup extends Setup {
// airtime.conf section header
const SECTION = "[rabbitmq]";
protected static $_section = "[rabbitmq]";
// Array of key->value pairs for airtime.conf
protected static $_properties;
// Constant form field names for passing errors back to the front-end
const RMQ_USER = "rmqUser",
@ -22,29 +25,17 @@ class RabbitMQSetup extends Setup {
RMQ_HOST = "rmqHost",
RMQ_VHOST = "rmqVHost";
// Form field values
static $user, $pass, $host, $port, $vhost;
// Array of key->value pairs for airtime.conf
static $properties;
// Message and error fields to return to the front-end
static $message = null;
static $errors = array();
function __construct($settings) {
self::$user = $settings[self::RMQ_USER];
self::$pass = $settings[self::RMQ_PASS];
self::$port = $settings[self::RMQ_PORT];
self::$host = $settings[self::RMQ_HOST];
self::$vhost = $settings[self::RMQ_VHOST];
self::$properties = array(
"host" => self::$host,
"port" => self::$port,
"user" => self::$user,
"password" => self::$pass,
"vhost" => self::$vhost,
static::$_properties = array(
"host" => $settings[self::RMQ_HOST],
"port" => $settings[self::RMQ_PORT],
"user" => $settings[self::RMQ_USER],
"password" => $settings[self::RMQ_PASS],
"vhost" => $settings[self::RMQ_VHOST],
);
}
@ -72,24 +63,20 @@ class RabbitMQSetup extends Setup {
);
}
function writeToTemp() {
parent::writeToTemp(self::SECTION, self::$properties);
}
function checkRMQConnection() {
$conn = new AMQPConnection(self::$host,
self::$port,
self::$user,
self::$pass,
self::$vhost);
$conn = new AMQPConnection(self::$_properties["host"],
self::$_properties["port"],
self::$_properties["user"],
self::$_properties["password"],
self::$_properties["vhost"]);
return isset($conn);
}
function identifyRMQConnectionError() {
// It's impossible to identify errors coming out of amqp.inc without a major
// rewrite, so for now just tell the user ALL THE THINGS went wrong
self::$message = "Couldn't connect to RabbitMQ server! Please check if the server "
. "is running and your credentials are correct.";
self::$message = _("Couldn't connect to RabbitMQ server! Please check if the server "
. "is running and your credentials are correct.");
self::$errors[] = self::RMQ_USER;
self::$errors[] = self::RMQ_PASS;
self::$errors[] = self::RMQ_HOST;
@ -97,4 +84,12 @@ class RabbitMQSetup extends Setup {
self::$errors[] = self::RMQ_VHOST;
}
protected function writeToTemp() {
if (!file_exists(RMQ_INI_TEMP_PATH)) {
copy(BUILD_PATH . "rabbitmq-analyzer.ini", RMQ_INI_TEMP_PATH);
}
$this->_write(RMQ_INI_TEMP_PATH);
parent::writeToTemp();
}
}

View file

@ -1,6 +1,7 @@
<?php
define("BUILD_PATH", dirname(dirname(__DIR__)) . "/build/");
define("AIRTIME_CONF_TEMP_PATH", "/tmp/airtime.conf.temp");
define("RMQ_INI_TEMP_PATH", "/tmp/rabbitmq.ini.tmp");
/**
* Class Setup
@ -11,50 +12,59 @@ define("AIRTIME_CONF_TEMP_PATH", "/tmp/airtime.conf.temp");
*/
abstract class Setup {
protected static $_section;
/**
* Array of key->value pairs for airtime.conf
*
* @var array
*/
protected static $_properties;
abstract function __construct($settings);
abstract function runSetup();
/**
* Write new property values to a given section in airtime.conf.temp
*
* @param string $section
* the configuration section to write to
* @param array $properties
* the configuration properties and values to overwrite
*/
protected function writeToTemp($section, $properties) {
protected function writeToTemp() {
if (!file_exists(AIRTIME_CONF_TEMP_PATH)) {
copy(BUILD_PATH . "airtime.example.conf", AIRTIME_CONF_TEMP_PATH);
}
$file = file(AIRTIME_CONF_TEMP_PATH);
$this->_write(AIRTIME_CONF_TEMP_PATH);
}
protected function _write($filePath) {
$file = file($filePath);
$fileOutput = "";
$inSection = false;
foreach ($file as $line) {
if (strpos($line, $section) !== false) {
if (strpos($line, static::$_section) !== false) {
$inSection = true;
} else if (strpos($line, "[") !== false) {
$inSection = false;
}
if (substr(trim($line), 0, 1) == "#") {
/* Workaround to strip comments from airtime.conf.
* We need to do this because python's ConfigObj and PHP
* have different (and equally strict) rules about comment
* have different (and equally strict) rules about comment
* characters in configuration files.
*/
} else if ($inSection) {
$key = trim(@substr($line, 0, strpos($line, "=")));
$fileOutput .= $key && isset($properties[$key]) ? $key . " = " . $properties[$key] . "\n" : $line;
$fileOutput .= $key && isset(static::$_properties[$key]) ?
$key . " = " . static::$_properties[$key] . "\n" : $line;
} else {
$fileOutput .= $line;
}
}
file_put_contents(AIRTIME_CONF_TEMP_PATH, $fileOutput);
file_put_contents($filePath, $fileOutput);
}
/**