From 5af735654cb0bccfdaa72c4f9e85fe421c008d18 Mon Sep 17 00:00:00 2001 From: Duncan Sommerville Date: Thu, 18 Sep 2014 16:33:01 -0400 Subject: [PATCH] Added Airtime255 class in Upgrades --- .../controllers/UpgradeController.php | 17 ++--- airtime_mvc/application/upgrade/Upgrades.php | 62 ++++++------------- 2 files changed, 27 insertions(+), 52 deletions(-) diff --git a/airtime_mvc/application/controllers/UpgradeController.php b/airtime_mvc/application/controllers/UpgradeController.php index 30067ece7..c2a06cd79 100644 --- a/airtime_mvc/application/controllers/UpgradeController.php +++ b/airtime_mvc/application/controllers/UpgradeController.php @@ -16,6 +16,7 @@ class UpgradeController extends Zend_Controller_Action $upgraders = array(); array_push($upgraders, new AirtimeUpgrader253()); array_push($upgraders, new AirtimeUpgrader254()); + array_push($upgraders, new AirtimeUpgrader255()); $didWePerformAnUpgrade = false; try @@ -28,8 +29,8 @@ class UpgradeController extends Zend_Controller_Action $upgrader->upgrade(); //This will throw an exception if the upgrade fails. $didWePerformAnUpgrade = true; $this->getResponse() - ->setHttpResponseCode(200) - ->appendBody("Upgrade to Airtime " . $upgrader->getNewVersion() . " OK
"); + ->setHttpResponseCode(200) + ->appendBody("Upgrade to Airtime " . $upgrader->getNewVersion() . " OK
"); $i = 0; //Start over, in case the upgrade handlers are not in ascending order. } } @@ -37,15 +38,15 @@ class UpgradeController extends Zend_Controller_Action if (!$didWePerformAnUpgrade) { $this->getResponse() - ->setHttpResponseCode(200) - ->appendBody("No upgrade was performed. The current Airtime version is " . AirtimeUpgrader::getCurrentVersion() . ".
"); + ->setHttpResponseCode(200) + ->appendBody("No upgrade was performed. The current Airtime version is " . AirtimeUpgrader::getCurrentVersion() . ".
"); } } catch (Exception $e) { $this->getResponse() - ->setHttpResponseCode(400) - ->appendBody($e->getMessage()); + ->setHttpResponseCode(400) + ->appendBody($e->getMessage()); } } @@ -65,8 +66,8 @@ class UpgradeController extends Zend_Controller_Action if ($encodedRequestApiKey !== $encodedStoredApiKey) { $this->getResponse() - ->setHttpResponseCode(401) - ->appendBody("Error: Incorrect API key.
"); + ->setHttpResponseCode(401) + ->appendBody("Error: Incorrect API key.
"); return false; } return true; diff --git a/airtime_mvc/application/upgrade/Upgrades.php b/airtime_mvc/application/upgrade/Upgrades.php index cd0e24514..27fe15bd1 100644 --- a/airtime_mvc/application/upgrade/Upgrades.php +++ b/airtime_mvc/application/upgrade/Upgrades.php @@ -221,61 +221,35 @@ class AirtimeUpgrader255 extends AirtimeUpgrader { public function upgrade() { Cache::clear(); - assert ( $this->checkIfUpgradeSupported () ); + assert($this->checkIfUpgradeSupported()); - $newVersion = $this->getNewVersion (); + $newVersion = $this->getNewVersion(); - $con = Propel::getConnection (); - // $con->beginTransaction(); try { - $this->toggleMaintenanceScreen ( true ); - Cache::clear (); + $this->toggleMaintenanceScreen(true); + Cache::clear(); // Begin upgrade + $airtimeConf = isset($_SERVER['AIRTIME_CONF']) ? $_SERVER['AIRTIME_CONF'] : "/etc/airtime/airtime.conf"; + $values = parse_ini_file($airtimeConf, true); - // First, ensure there are no superadmins already. - $numberOfSuperAdmins = CcSubjsQuery::create ()->filterByDbType ( UTYPE_SUPERADMIN )->filterByDbLogin ( "sourcefabric_admin", Criteria::NOT_EQUAL )-> // Ignore sourcefabric_admin users - count (); + $username = $values['database']['dbuser']; + $password = $values['database']['dbpass']; + $host = $values['database']['host']; + $database = $values['database']['dbname']; + $dir = __DIR__; - // Only create a super admin if there isn't one already. - if ($numberOfSuperAdmins == 0) { - // Find the "admin" user and promote them to superadmin. - $adminUser = CcSubjsQuery::create ()->filterByDbLogin ( 'admin' )->findOne (); - if (! $adminUser) { - // TODO: Otherwise get the user with the lowest ID that is of type administrator: - // - $adminUser = CcSubjsQuery::create ()->filterByDbType ( UTYPE_ADMIN )->orderByDbId ( Criteria::ASC )->findOne (); - - if (! $adminUser) { - throw new Exception ( "Failed to find any users of type 'admin' ('A')." ); - } - } - - $adminUser = new Application_Model_User ( $adminUser->getDbId () ); - $adminUser->setType ( UTYPE_SUPERADMIN ); - $adminUser->save (); - Logging::info ( $_SERVER ['HTTP_HOST'] . ': ' . $newVersion . " Upgrade: Promoted user " . $adminUser->getLogin () . " to be a Super Admin." ); - - // Also try to promote the sourcefabric_admin user - $sofabAdminUser = CcSubjsQuery::create ()->filterByDbLogin ( 'sourcefabric_admin' )->findOne (); - if ($sofabAdminUser) { - $sofabAdminUser = new Application_Model_User ( $sofabAdminUser->getDbId () ); - $sofabAdminUser->setType ( UTYPE_SUPERADMIN ); - $sofabAdminUser->save (); - Logging::info ( $_SERVER ['HTTP_HOST'] . ': ' . $newVersion . " Upgrade: Promoted user " . $sofabAdminUser->getLogin () . " to be a Super Admin." ); - } - } + passthru("export PGPASSWORD=$password && psql -h $host -U $username -q -f $dir/upgrade_sql/airtime_" + .$this->getNewVersion()."/upgrade.sql $database 2>&1 | grep -v \"will create implicit index\""); - // $con->commit(); - Application_Model_Preference::SetAirtimeVersion ( $newVersion ); - Cache::clear (); + Application_Model_Preference::SetAirtimeVersion($newVersion); + Cache::clear(); - $this->toggleMaintenanceScreen ( false ); + $this->toggleMaintenanceScreen(false); return true; - } catch ( Exception $e ) { - // $con->rollback(); - $this->toggleMaintenanceScreen ( false ); + } catch(Exception $e) { + $this->toggleMaintenanceScreen(false); throw $e; } }