Moved all install functions into an AirtimeInstall class.

Formatted all the install files according to our code formatting standards.
Output more info if something goes wrong during DB create or DB user create.
This commit is contained in:
Paul Baranowski 2011-03-16 13:21:40 -04:00
parent 4b27fcc0c9
commit ef0bb8ed78
5 changed files with 205 additions and 201 deletions

View File

@ -1,7 +1,6 @@
<?php <?php
/** /**
* @package Airtime * @package Airtime
* @subpackage StorageServer
* @copyright 2010 Sourcefabric O.P.S. * @copyright 2010 Sourcefabric O.P.S.
* @license http://www.gnu.org/licenses/gpl.txt * @license http://www.gnu.org/licenses/gpl.txt
*/ */
@ -20,34 +19,33 @@ require_once(dirname(__FILE__).'/installInit.php');
echo "******************************** Install Begin *********************************".PHP_EOL; echo "******************************** Install Begin *********************************".PHP_EOL;
checkIfRoot(); AirtimeInstall::ExitIfNotRoot();
createAPIKey(); AirtimeInstall::CreateApiKey();
updateINIKeyValues('../build/build.properties', 'project.home', realpath(__dir__.'/../')); AirtimeInstall::UpdateIniValue('../build/build.properties', 'project.home', realpath(__dir__.'/../'));
echo PHP_EOL."*** Database Installation ***".PHP_EOL; echo PHP_EOL."*** Database Installation ***".PHP_EOL;
echo "* Creating Airtime Database User".PHP_EOL; echo "* Creating Airtime Database User".PHP_EOL;
createAirtimeDatabaseUser(); AirtimeInstall::CreateDatabaseUser();
echo "* Creating Airtime Database".PHP_EOL; echo "* Creating Airtime Database".PHP_EOL;
createAirtimeDatabase(); AirtimeInstall::CreateDatabase();
AirtimeInstall::DbConnect(true);
airtime_db_connect(true);
echo "* Install Postgresql Scripting Language".PHP_EOL; echo "* Install Postgresql Scripting Language".PHP_EOL;
installPostgresScriptingLanguage(); AirtimeInstall::InstallPostgresScriptingLanguage();
echo "* Creating Database Tables".PHP_EOL; echo "* Creating Database Tables".PHP_EOL;
createAirtimeDatabaseTables(); AirtimeInstall::CreateDatabaseTables();
doctrineMigrateTables(__DIR__); AirtimeInstall::MigrateTables(__DIR__);
echo "* Storage Directory Setup".PHP_EOL; echo "* Storage Directory Setup".PHP_EOL;
storageDirectorySetup($CC_CONFIG); AirtimeInstall::SetupStorageDirectory($CC_CONFIG);
echo "* Setting Dir Permissions".PHP_EOL; echo "* Setting Dir Permissions".PHP_EOL;
install_setDirPermissions($CC_CONFIG["storageDir"]); AirtimeInstall::ChangeDirOwnerToWebserver($CC_CONFIG["storageDir"]);
echo "* Importing Sample Audio Clips".PHP_EOL; echo "* Importing Sample Audio Clips".PHP_EOL;
system(__DIR__."/../utils/airtime-import --copy ../audio_samples/ > /dev/null"); system(__DIR__."/../utils/airtime-import --copy ../audio_samples/ > /dev/null");

View File

@ -18,13 +18,13 @@ require_once(dirname(__FILE__).'/../application/configs/conf.php');
require_once(dirname(__FILE__).'/installInit.php'); require_once(dirname(__FILE__).'/installInit.php');
// Need to check that we are superuser before running this. // Need to check that we are superuser before running this.
checkIfRoot(); AirtimeInstall::ExitIfNotRoot();
echo "******************************* Uninstall Begin ********************************".PHP_EOL; echo "******************************* Uninstall Begin ********************************".PHP_EOL;
//------------------------------------------------------------------------ //------------------------------------------------------------------------
// Delete the database // Delete the database
// Note: Do not put a call to airtime_db_connect() // Note: Do not put a call to AirtimeInstall::DbConnect()
// before this function, even if you called $CC_DBC->disconnect(), there will // before this function, even if you called $CC_DBC->disconnect(), there will
// still be a connection to the database and you wont be able to delete it. // still be a connection to the database and you wont be able to delete it.
//------------------------------------------------------------------------ //------------------------------------------------------------------------
@ -38,19 +38,22 @@ $command = "sudo -u postgres dropdb {$CC_CONFIG['dsn']['database']} 2> /dev/null
//------------------------------------------------------------------------ //------------------------------------------------------------------------
if ($dbDeleteFailed) { if ($dbDeleteFailed) {
echo " * Couldn't delete the database, so deleting all the DB tables...".PHP_EOL; echo " * Couldn't delete the database, so deleting all the DB tables...".PHP_EOL;
airtime_db_connect(true); AirtimeInstall::DbConnect(true);
if (!PEAR::isError($CC_DBC)) { if (!PEAR::isError($CC_DBC)) {
$sql = "select * from pg_tables where tableowner = 'airtime'"; $sql = "select * from pg_tables where tableowner = 'airtime'";
$rows = airtime_get_query($sql); $rows = $CC_DBC->GetAll($sql);
if (PEAR::isError($result)) {
$rows = array();
}
foreach ($rows as $row) { foreach ($rows as $row) {
$tablename = $row["tablename"]; $tablename = $row["tablename"];
echo " * Removing database table $tablename..."; echo " * Removing database table $tablename...";
if (airtime_db_table_exists($tablename)){ if (AirtimeInstall::DbTableExists($tablename)){
$sql = "DROP TABLE $tablename CASCADE"; $sql = "DROP TABLE $tablename CASCADE";
airtime_install_query($sql, false); AirtimeInstall::InstallQuery($sql, false);
$CC_DBC->dropSequence($tablename."_id"); $CC_DBC->dropSequence($tablename."_id");
} }
@ -75,7 +78,7 @@ if ($results == 0) {
//------------------------------------------------------------------------ //------------------------------------------------------------------------
// Delete files // Delete files
//------------------------------------------------------------------------ //------------------------------------------------------------------------
airtime_uninstall_delete_files($CC_CONFIG['storageDir']); AirtimeInstall::DeleteFilesRecursive($CC_CONFIG['storageDir']);
$command = "python ".__DIR__."/../pypo/install/pypo-uninstall.py"; $command = "python ".__DIR__."/../pypo/install/pypo-uninstall.py";

View File

@ -17,13 +17,13 @@ if (isset($arr["DOCUMENT_ROOT"]) && ($arr["DOCUMENT_ROOT"] != "") ) {
require_once(dirname(__FILE__).'/installInit.php'); require_once(dirname(__FILE__).'/installInit.php');
checkIfRoot(); AirtimeInstall::ExitIfNotRoot();
echo "******************************** Update Begin *********************************".PHP_EOL; echo "******************************** Update Begin *********************************".PHP_EOL;
updateINIKeyValues('../build/build.properties', 'project.home', realpath(__dir__.'/../')); AirtimeInstall::UpdateIniValue('../build/build.properties', 'project.home', realpath(__dir__.'/../'));
echo PHP_EOL."*** Updating Database Tables ***".PHP_EOL; echo PHP_EOL."*** Updating Database Tables ***".PHP_EOL;
doctrineMigrateTables(__DIR__); AirtimeInstall::MigrateTables(__DIR__);
echo PHP_EOL."*** Updating Pypo ***".PHP_EOL; echo PHP_EOL."*** Updating Pypo ***".PHP_EOL;
system("python ".__DIR__."/../pypo/install/pypo-install.py"); system("python ".__DIR__."/../pypo/install/pypo-install.py");

View File

@ -7,7 +7,9 @@ if (!function_exists('pg_connect')) {
require_once(dirname(__FILE__).'/../library/pear/DB.php'); require_once(dirname(__FILE__).'/../library/pear/DB.php');
require_once(dirname(__FILE__).'/../application/configs/conf.php'); require_once(dirname(__FILE__).'/../application/configs/conf.php');
function airtime_db_table_exists($p_name) class AirtimeInstall {
static function DbTableExists($p_name)
{ {
global $CC_DBC; global $CC_DBC;
$sql = "SELECT * FROM ".$p_name; $sql = "SELECT * FROM ".$p_name;
@ -18,17 +20,7 @@ function airtime_db_table_exists($p_name)
return true; return true;
} }
function airtime_get_query($sql) static function InstallQuery($sql, $verbose = true)
{
global $CC_DBC;
$result = $CC_DBC->GetAll($sql);
if (PEAR::isError($result)) {
return array();
}
return $result;
}
function airtime_install_query($sql, $verbose = true)
{ {
global $CC_DBC; global $CC_DBC;
$result = $CC_DBC->query($sql); $result = $CC_DBC->query($sql);
@ -43,7 +35,8 @@ function airtime_install_query($sql, $verbose = true)
} }
} }
function airtime_db_connect($p_exitOnError = true) { static function DbConnect($p_exitOnError = true)
{
global $CC_DBC, $CC_CONFIG; global $CC_DBC, $CC_CONFIG;
$CC_DBC = DB::connect($CC_CONFIG['dsn'], TRUE); $CC_DBC = DB::connect($CC_CONFIG['dsn'], TRUE);
if (PEAR::isError($CC_DBC)) { if (PEAR::isError($CC_DBC)) {
@ -61,7 +54,8 @@ function airtime_db_connect($p_exitOnError = true) {
} }
} }
function install_setDirPermissions($filePath) { static function ChangeDirOwnerToWebserver($filePath)
{
global $CC_CONFIG; global $CC_CONFIG;
$success = chgrp($filePath, $CC_CONFIG["webServerUser"]); $success = chgrp($filePath, $CC_CONFIG["webServerUser"]);
$fileperms=@fileperms($filePath); $fileperms=@fileperms($filePath);
@ -70,7 +64,7 @@ function install_setDirPermissions($filePath) {
chmod($filePath, $fileperms); chmod($filePath, $fileperms);
} }
function rand_string($len=20, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') private static function GenerateRandomString($len=20, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
{ {
$string = ''; $string = '';
for ($i = 0; $i < $len; $i++) for ($i = 0; $i < $len; $i++)
@ -81,14 +75,15 @@ function rand_string($len=20, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
return $string; return $string;
} }
function createAPIKey(){ public static function CreateApiKey()
{
$api_key = rand_string(); $api_key = AirtimeInstall::GenerateRandomString();
updateINIKeyValues(__DIR__.'/../build/airtime.conf', 'api_key', $api_key); AirtimeInstall::UpdateIniValue(__DIR__.'/../build/airtime.conf', 'api_key', $api_key);
updateINIKeyValues(__DIR__.'/../pypo/config.cfg', 'api_key', "'$api_key'"); AirtimeInstall::UpdateIniValue(__DIR__.'/../pypo/config.cfg', 'api_key', "'$api_key'");
} }
function checkIfRoot(){ public static function ExitIfNotRoot()
{
// Need to check that we are superuser before running this. // Need to check that we are superuser before running this.
if(exec("whoami") != "root"){ if(exec("whoami") != "root"){
echo "Must be root user.\n"; echo "Must be root user.\n";
@ -96,7 +91,8 @@ function checkIfRoot(){
} }
} }
function updateINIKeyValues($filename, $property, $value){ public static function UpdateIniValue($filename, $property, $value)
{
$lines = file($filename); $lines = file($filename);
$n=count($lines); $n=count($lines);
for ($i=0; $i<$n; $i++) { for ($i=0; $i<$n; $i++) {
@ -113,7 +109,8 @@ function updateINIKeyValues($filename, $property, $value){
fclose($fp); fclose($fp);
} }
function storageDirectorySetup($CC_CONFIG){ public static function SetupStorageDirectory($CC_CONFIG)
{
global $CC_CONFIG, $CC_DBC; global $CC_CONFIG, $CC_DBC;
echo PHP_EOL."*** Directory Setup ***".PHP_EOL; echo PHP_EOL."*** Directory Setup ***".PHP_EOL;
@ -138,7 +135,8 @@ function storageDirectorySetup($CC_CONFIG){
} }
} }
function createAirtimeDatabaseUser(){ public static function CreateDatabaseUser()
{
global $CC_CONFIG; global $CC_CONFIG;
// Create the database user // Create the database user
$command = "sudo -u postgres psql postgres --command \"CREATE USER {$CC_CONFIG['dsn']['username']} " $command = "sudo -u postgres psql postgres --command \"CREATE USER {$CC_CONFIG['dsn']['username']} "
@ -148,11 +146,12 @@ function createAirtimeDatabaseUser(){
if ($results == 0) { if ($results == 0) {
echo "* User {$CC_CONFIG['dsn']['username']} created.".PHP_EOL; echo "* User {$CC_CONFIG['dsn']['username']} created.".PHP_EOL;
} else { } else {
echo "* User {$CC_CONFIG['dsn']['username']} already exists.".PHP_EOL; echo "* Could not create user {$CC_CONFIG['dsn']['username']}: $output".PHP_EOL;
} }
} }
function createAirtimeDatabase(){ public static function CreateDatabase()
{
global $CC_CONFIG; global $CC_CONFIG;
$command = "sudo -u postgres createdb {$CC_CONFIG['dsn']['database']} --owner {$CC_CONFIG['dsn']['username']} 2> /dev/null"; $command = "sudo -u postgres createdb {$CC_CONFIG['dsn']['database']} --owner {$CC_CONFIG['dsn']['username']} 2> /dev/null";
@ -160,37 +159,41 @@ function createAirtimeDatabase(){
if ($results == 0) { if ($results == 0) {
echo "* Database '{$CC_CONFIG['dsn']['database']}' created.".PHP_EOL; echo "* Database '{$CC_CONFIG['dsn']['database']}' created.".PHP_EOL;
} else { } else {
echo "* Database '{$CC_CONFIG['dsn']['database']}' already exists.".PHP_EOL; echo "* Could not create database '{$CC_CONFIG['dsn']['database']}': $output".PHP_EOL;
} }
} }
function installPostgresScriptingLanguage(){ public static function InstallPostgresScriptingLanguage()
{
global $CC_DBC; global $CC_DBC;
// Install postgres scripting language // Install postgres scripting language
$langIsInstalled = $CC_DBC->GetOne('SELECT COUNT(*) FROM pg_language WHERE lanname = \'plpgsql\''); $langIsInstalled = $CC_DBC->GetOne('SELECT COUNT(*) FROM pg_language WHERE lanname = \'plpgsql\'');
if ($langIsInstalled == '0') { if ($langIsInstalled == '0') {
echo "* Installing Postgres scripting language".PHP_EOL; echo "* Installing Postgres scripting language".PHP_EOL;
$sql = "CREATE LANGUAGE 'plpgsql'"; $sql = "CREATE LANGUAGE 'plpgsql'";
airtime_install_query($sql, false); AirtimeInstall::InstallQuery($sql, false);
} else { } else {
echo "* Postgres scripting language already installed".PHP_EOL; echo "* Postgres scripting language already installed".PHP_EOL;
} }
} }
public static function CreateDatabaseTables()
function createAirtimeDatabaseTables(){ {
// Put Propel sql files in Database // Put Propel sql files in Database
$command = __DIR__."/../library/propel/generator/bin/propel-gen ../build/ insert-sql 2>propel-error.log"; $command = __DIR__."/../library/propel/generator/bin/propel-gen ../build/ insert-sql 2>propel-error.log";
@exec($command, $output, $results); @exec($command, $output, $results);
} }
function doctrineMigrateTables($dir){ public static function MigrateTables($dir)
{
$command = "php $dir/../library/doctrine/migrations/doctrine-migrations.phar --configuration=$dir/DoctrineMigrations/migrations.xml --db-configuration=$dir/../library/doctrine/migrations/migrations-db.php --no-interaction migrations:migrate"; $command = "php $dir/../library/doctrine/migrations/doctrine-migrations.phar --configuration=$dir/DoctrineMigrations/migrations.xml --db-configuration=$dir/../library/doctrine/migrations/migrations-db.php --no-interaction migrations:migrate";
system($command); system($command);
} }
function airtime_uninstall_delete_files($p_path) public static function DeleteFilesRecursive($p_path)
{ {
$command = "rm -rf $p_path"; $command = "rm -rf $p_path";
exec($command); exec($command);
} }
}

View File

@ -84,10 +84,10 @@ function airtime_empty_db($db) {
global $CC_CONFIG; global $CC_CONFIG;
if (!PEAR::isError($db)) { if (!PEAR::isError($db)) {
if (airtime_db_table_exists($CC_CONFIG['filesTable'])) { if (AirtimeInstall::DbTableExists($CC_CONFIG['filesTable'])) {
echo " * Deleting from database table ".$CC_CONFIG['filesTable']."\n"; echo " * Deleting from database table ".$CC_CONFIG['filesTable']."\n";
$sql = "DELETE FROM ".$CC_CONFIG['filesTable']; $sql = "DELETE FROM ".$CC_CONFIG['filesTable'];
airtime_install_query($sql, false); AirtimeInstall::InstallQuery($sql, false);
} }
else { else {
echo " * Skipping: database table ".$CC_CONFIG['filesTable']."\n"; echo " * Skipping: database table ".$CC_CONFIG['filesTable']."\n";