From 6890dcc3bc5eb044a625855fdba532c640e9c341 Mon Sep 17 00:00:00 2001 From: drigato Date: Fri, 25 Apr 2014 11:05:33 -0400 Subject: [PATCH] CC-5802: Upgrade application.ini file Used a transaction for upgrades. If upgrading the application.ini file fails, the database upgrades will get rolled back. --- .../controllers/UpgradeController.php | 143 +++++++++--------- 1 file changed, 75 insertions(+), 68 deletions(-) diff --git a/airtime_mvc/application/controllers/UpgradeController.php b/airtime_mvc/application/controllers/UpgradeController.php index ae7ad3e7e..60a48fd8c 100644 --- a/airtime_mvc/application/controllers/UpgradeController.php +++ b/airtime_mvc/application/controllers/UpgradeController.php @@ -16,81 +16,88 @@ class UpgradeController extends Zend_Controller_Action if (!$this->verifyAirtimeVersion()) { return; } - - //Disable Airtime UI - //create a temporary maintenance notification file - //when this file is on the server, zend framework redirects all - //requests to the maintenance page and sets a 503 response code - $maintenanceFile = '/tmp/maintenance.txt'; - $file = fopen($maintenanceFile, 'w'); - fclose($file); - - //Begin upgrade - $filename = isset($_SERVER['AIRTIME_CONF']) ? $_SERVER['AIRTIME_CONF'] : "/etc/airtime/airtime.conf"; - $values = parse_ini_file($filename, true); - - $username = $values['database']['dbuser']; - $password = $values['database']['dbpass']; - $host = $values['database']['host']; - $database = $values['database']['dbname']; - $dir = __DIR__; - - passthru("export PGPASSWORD=$password && psql -h $host -U $username -q -f $dir/upgrade_sql/airtime_$airtime_upgrade_version/upgrade.sql $database 2>&1 | grep -v \"will create implicit index\""); - - $musicDir = CcMusicDirsQuery::create() - ->filterByType('stor') - ->filterByExists(true) - ->findOne(); - $storPath = $musicDir->getDirectory(); - - $freeSpace = disk_free_space($storPath); - $totalSpace = disk_total_space($storPath); - - Application_Model_Preference::setDiskUsage($totalSpace - $freeSpace); - $iniFile = isset($_SERVER['AIRTIME_BASE']) ? $_SERVER['AIRTIME_BASE']."application.ini" : "/usr/share/airtime/application/configs/application.ini"; - - //update application.ini - $newLines = "resources.frontController.moduleDirectory = APPLICATION_PATH '/modules'\n". - "resources.frontController.plugins.putHandler = 'Zend_Controller_Plugin_PutHandler'\n". - ";load everything in the modules directory including models\n". - "resources.modules[] = ''\n"; + $con = Propel::getConnection(); + $con->beginTransaction(); + try { + //Disable Airtime UI + //create a temporary maintenance notification file + //when this file is on the server, zend framework redirects all + //requests to the maintenance page and sets a 503 response code + $maintenanceFile = '/tmp/maintenance.txt'; + $file = fopen($maintenanceFile, 'w'); + fclose($file); + + //Begin upgrade + $filename = isset($_SERVER['AIRTIME_CONF']) ? $_SERVER['AIRTIME_CONF'] : "/etc/airtime/airtime.conf"; + $values = parse_ini_file($filename, true); + + $username = $values['database']['dbuser']; + $password = $values['database']['dbpass']; + $host = $values['database']['host']; + $database = $values['database']['dbname']; + $dir = __DIR__; + + passthru("export PGPASSWORD=$password && psql -h $host -U $username -q -f $dir/upgrade_sql/airtime_$airtime_upgrade_version/upgrade.sql $database 2>&1 | grep -v \"will create implicit index\""); + + $musicDir = CcMusicDirsQuery::create() + ->filterByType('stor') + ->filterByExists(true) + ->findOne(); + $storPath = $musicDir->getDirectory(); + + $freeSpace = disk_free_space($storPath); + $totalSpace = disk_total_space($storPath); + + Application_Model_Preference::setDiskUsage($totalSpace - $freeSpace); + + $iniFile = isset($_SERVER['AIRTIME_BASE']) ? $_SERVER['AIRTIME_BASE']."application.ini" : "/usr/share/airtime/application/configs/application.ini"; + + //update application.ini + $newLines = "resources.frontController.moduleDirectory = APPLICATION_PATH '/modules'\n". + "resources.frontController.plugins.putHandler = 'Zend_Controller_Plugin_PutHandler'\n". + ";load everything in the modules directory including models\n". + "resources.modules[] = ''\n"; + + $currentIniFile = file_get_contents($iniFile); + + /* We want to add the new lines immediately after the first line, '[production]' + * We read the first line into $beginning, and the rest of the file into $end. + * Then overwrite the current application.ini file with $beginning, $newLines, and $end + */ + $lines = explode("\n", $currentIniFile); + $beginning = implode("\n", array_slice($lines, 0,1)); + + //check that first line is '[production]' + if ($beginning != '[production]') { + throw new Exception('Upgrade to Airtime 2.5.3 FAILED. Could not upgrade application.ini - Invalid format'); + } + $end = implode("\n", array_slice($lines, 1)); + + if (!is_writeable($iniFile)) { + throw new Exception('Upgrade to Airtime 2.5.3 FAILED. Could not upgrade application.ini - Permission denied.'); + } + $file = new SplFileObject($iniFile, "w"); + $file->fwrite($beginning."\n".$newLines.$end); + + //delete maintenance.txt to give users access back to Airtime + unlink($maintenanceFile); + + //TODO: clear out the cache - $currentIniFile = file_get_contents($iniFile); + $con->commit(); - /* We want to add the new lines immediately after the first line, '[production]' - * We read the first line into $beginning, and the rest of the file into $end. - * Then overwrite the current application.ini file with $beginning, $newLines, and $end - */ - $lines = explode("\n", $currentIniFile); - $beginning = implode("\n", array_slice($lines, 0,1)); + $this->getResponse() + ->setHttpResponseCode(200) + ->appendBody("Upgrade to Airtime 2.5.3 OK"); - //check that first line is '[production]' - if ($beginning != '[production]') { + } catch(Exception $e) { + $con->rollback(); + unlink($maintenanceFile); $this->getResponse() ->setHttpResponseCode(400) - ->appendBody('Upgrade to Airtime 2.5.3 FAILED. Could not upgrade application.ini'); - return; + ->appendBody($e->getMessage()); } - $end = implode("\n", array_slice($lines, 1)); - - if (!is_writeable($iniFile)) { - $this->getResponse() - ->setHttpResponseCode(400) - ->appendBody('Upgrade to Airtime 2.5.3 FAILED. Could not upgrade application.ini'); - return; - } - $file = new SplFileObject($iniFile, "w"); - $file->fwrite($beginning."\n".$newLines.$end); - - //delete maintenance.txt to give users access back to Airtime - unlink($maintenanceFile); - - //TODO: clear out the cache - - $this->getResponse() - ->setHttpResponseCode(200) - ->appendBody("Upgrade to Airtime 2.5.3 OK"); } private function verifyAuth()