SAAS-582 - Added provisioning class to create database from within Airtime
This commit is contained in:
parent
dd095e8933
commit
ad5536dedd
|
@ -21,6 +21,7 @@ require_once "LocaleHelper.php";
|
||||||
require_once "HTTPHelper.php";
|
require_once "HTTPHelper.php";
|
||||||
require_once "OsPath.php";
|
require_once "OsPath.php";
|
||||||
require_once "Database.php";
|
require_once "Database.php";
|
||||||
|
require_once "ProvisioningHelper.php";
|
||||||
require_once "Timezone.php";
|
require_once "Timezone.php";
|
||||||
require_once "Auth.php";
|
require_once "Auth.php";
|
||||||
require_once __DIR__.'/forms/helpers/ValidationTypes.php';
|
require_once __DIR__.'/forms/helpers/ValidationTypes.php';
|
||||||
|
@ -33,6 +34,11 @@ require_once __DIR__.'/modules/rest/controllers/MediaController.php';
|
||||||
require_once (APPLICATION_PATH."/logging/Logging.php");
|
require_once (APPLICATION_PATH."/logging/Logging.php");
|
||||||
Logging::setLogPath('/var/log/airtime/zendphp.log');
|
Logging::setLogPath('/var/log/airtime/zendphp.log');
|
||||||
|
|
||||||
|
if (strpos("/provisioning/create-database", $_SERVER["REDIRECT_URL"]) !== false) {
|
||||||
|
(new ProvisioningHelper($CC_CONFIG["apiKey"][0]))->createDatabaseAction();
|
||||||
|
die;
|
||||||
|
}
|
||||||
|
|
||||||
Config::setAirtimeVersion();
|
Config::setAirtimeVersion();
|
||||||
require_once __DIR__."/configs/navigation.php";
|
require_once __DIR__."/configs/navigation.php";
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,132 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: sourcefabric
|
||||||
|
* Date: 12/02/15
|
||||||
|
* Time: 2:10 PM
|
||||||
|
*/
|
||||||
|
|
||||||
|
class ProvisioningHelper {
|
||||||
|
|
||||||
|
/* @var $dbh PDO */
|
||||||
|
static $dbh;
|
||||||
|
|
||||||
|
// Parameter values
|
||||||
|
private $dbuser, $dbpass, $dbname, $dbhost, $dbowner, $apikey;
|
||||||
|
|
||||||
|
public function __construct($apikey) {
|
||||||
|
$this->apikey = $apikey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoint for setting up and installing the Airtime database
|
||||||
|
*/
|
||||||
|
public function createDatabaseAction() {
|
||||||
|
Logging::info("Create Database action received");
|
||||||
|
|
||||||
|
$this->getParams();
|
||||||
|
Logging::info("Parameters: "
|
||||||
|
. "\nUser: " . $this->dbuser
|
||||||
|
. "\nPass: " . $this->dbpass
|
||||||
|
. "\nName: " . $this->dbname
|
||||||
|
. "\nHost: " . $this->dbhost
|
||||||
|
. "\nOwner: " . $this->dbowner);
|
||||||
|
|
||||||
|
$apikey = $_SERVER['PHP_AUTH_USER'];
|
||||||
|
if (!isset($apikey) || $apikey != $this->apikey) {
|
||||||
|
Logging::info("Invalid API Key: $apikey");
|
||||||
|
http_response_code(403);
|
||||||
|
echo "ERROR: Incorrect API key";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->setNewDatabaseConnection();
|
||||||
|
if ($this->checkDatabaseExists()) {
|
||||||
|
throw new Exception("ERROR: Airtime database already exists");
|
||||||
|
}
|
||||||
|
$this->createDatabase();
|
||||||
|
$this->createDatabaseTables();
|
||||||
|
} catch(Exception $e) {
|
||||||
|
http_response_code(400);
|
||||||
|
Logging::info($e->getMessage());
|
||||||
|
echo $e->getMessage();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
http_response_code(201);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the database settings and credentials given are valid
|
||||||
|
* @return boolean true if the database given exists and the user is valid and can access it
|
||||||
|
*/
|
||||||
|
private function checkDatabaseExists() {
|
||||||
|
$statement = self::$dbh->prepare("SELECT datname FROM pg_database WHERE datname = :dbname");
|
||||||
|
$statement->execute(array(":dbname" => $this->dbname));
|
||||||
|
$result = $statement->fetch();
|
||||||
|
return isset($result[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getParams() {
|
||||||
|
$params = [];
|
||||||
|
parse_str($_SERVER["QUERY_STRING"], $params);
|
||||||
|
foreach ($params as $k => $v) {
|
||||||
|
$this->$k = $v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up a new database connection based on the parameters in the request
|
||||||
|
* @throws PDOException upon failure to connect
|
||||||
|
*/
|
||||||
|
private function setNewDatabaseConnection() {
|
||||||
|
self::$dbh = new PDO("pgsql:host=" . $this->dbhost
|
||||||
|
. ";dbname=postgres"
|
||||||
|
. ";port=5432" . ";user=" . $this->dbuser
|
||||||
|
. ";password=" . $this->dbpass);
|
||||||
|
$err = self::$dbh->errorInfo();
|
||||||
|
if ($err[1] != null) {
|
||||||
|
throw new PDOException("ERROR: Could not connect to database");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the Airtime database using the given credentials
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function createDatabase() {
|
||||||
|
Logging::info("Creating database...");
|
||||||
|
$statement = self::$dbh->prepare("CREATE DATABASE " . pg_escape_string($this->dbname)
|
||||||
|
. " WITH ENCODING 'UTF8' TEMPLATE template0"
|
||||||
|
. " OWNER " . pg_escape_string($this->dbowner));
|
||||||
|
if (!$statement->execute()) {
|
||||||
|
throw new Exception("ERROR: Failed to create Airtime database");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Install the Airtime database
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function createDatabaseTables() {
|
||||||
|
$sqlDir = dirname(APPLICATION_PATH) . "/build/sql/";
|
||||||
|
$files = array("schema.sql", "sequences.sql", "views.sql", "triggers.sql", "defaultdata.sql");
|
||||||
|
foreach ($files as $f) {
|
||||||
|
try {
|
||||||
|
/*
|
||||||
|
* Unfortunately, we need to use exec here due to PDO's lack of support for importing
|
||||||
|
* multi-line .sql files. PDO->exec() almost works, but any SQL errors stop the import,
|
||||||
|
* so the necessary DROPs on non-existent tables make it unusable. Prepared statements
|
||||||
|
* 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->dbpass . " && psql -U " . $this->dbuser . " --dbname "
|
||||||
|
. $this->dbname . " -h " . $this->dbhost . " -f $sqlDir$f 2>/dev/null", $out, $status);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
throw new Exception("ERROR: Failed to create database tables");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,11 +7,6 @@ use Aws\S3\S3Client;
|
||||||
class ProvisioningController extends Zend_Controller_Action
|
class ProvisioningController extends Zend_Controller_Action
|
||||||
{
|
{
|
||||||
|
|
||||||
static $dbh;
|
|
||||||
|
|
||||||
// Parameter values
|
|
||||||
private $dbuser, $dbpass, $dbname, $dbhost;
|
|
||||||
|
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -40,75 +35,4 @@ class ProvisioningController extends Zend_Controller_Action
|
||||||
->appendBody("OK");
|
->appendBody("OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* RESTful endpoint for setting up and installing the Airtime database
|
|
||||||
*/
|
|
||||||
public function createDatabaseAction() {
|
|
||||||
Logging::info("Create Database action received");
|
|
||||||
|
|
||||||
if (!RestAuth::verifyAuth(true, true, $this)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
$this->getParams();
|
|
||||||
$this->setNewDatabaseConnection();
|
|
||||||
$this->createDatabaseTables();
|
|
||||||
} catch(Exception $e) {
|
|
||||||
$this->getResponse()
|
|
||||||
->setHttpResponseCode(400)
|
|
||||||
->appendBody($e->getMessage());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->getResponse()
|
|
||||||
->setHttpResponseCode(201);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getParams() {
|
|
||||||
$this->dbuser = $this->_getParam('dbuser', '');
|
|
||||||
$this->dbpass = $this->_getParam('dbpass', '');
|
|
||||||
$this->dbname = $this->_getParam('dbname', '');
|
|
||||||
$this->dbhost = $this->_getParam('dbhost', '');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set up a new database connection based on the parameters in the request
|
|
||||||
* @throws PDOException upon failure to connect
|
|
||||||
*/
|
|
||||||
private function setNewDatabaseConnection() {
|
|
||||||
self::$dbh = new PDO("pgsql:host=" . $this->dbhost
|
|
||||||
. ";dbname=" . $this->dbname
|
|
||||||
. ";port=5432" . ";user=" . $this->dbuser
|
|
||||||
. ";password=" . $this->dbpass);
|
|
||||||
$err = self::$dbh->errorInfo();
|
|
||||||
if ($err[1] != null) {
|
|
||||||
throw new PDOException("ERROR: Could not connect to database");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Install the Airtime database
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function createDatabaseTables() {
|
|
||||||
$sqlDir = dirname(APPLICATION_PATH) . "/build/sql/";
|
|
||||||
$files = array("schema.sql", "sequences.sql", "views.sql", "triggers.sql", "defaultdata.sql");
|
|
||||||
foreach ($files as $f) {
|
|
||||||
try {
|
|
||||||
/*
|
|
||||||
* Unfortunately, we need to use exec here due to PDO's lack of support for importing
|
|
||||||
* multi-line .sql files. PDO->exec() almost works, but any SQL errors stop the import,
|
|
||||||
* so the necessary DROPs on non-existent tables make it unusable. Prepared statements
|
|
||||||
* 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->dbpass . " && psql -U " . $this->dbuser . " --dbname "
|
|
||||||
. $this->dbname . " -h " . $this->dbhost . " -f $sqlDir$f 2>/dev/null", $out, $status);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
throw new Exception("ERROR: Failed to create database tables");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue