123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- <?php
- class UpgradeManager
- {
-
- public static function getSupportedSchemaVersions()
- {
-
- return array('2.5.2');
- }
- public static function checkIfUpgradeIsNeeded()
- {
- $schemaVersion = Application_Model_Preference::GetSchemaVersion();
- $supportedSchemaVersions = self::getSupportedSchemaVersions();
- $upgradeNeeded = !in_array($schemaVersion, $supportedSchemaVersions);
- if ($upgradeNeeded) {
- self::doUpgrade();
- }
- }
- public static function doUpgrade()
- {
- $upgradeManager = new UpgradeManager();
- $upgraders = array();
- array_push($upgraders, new AirtimeUpgrader252());
-
- return $upgradeManager->runUpgrades(array(new AirtimeUpgrader252()), (dirname(__DIR__) . "/controllers"));
- }
-
- public function runUpgrades($upgraders, $dir) {
- $upgradePerformed = false;
-
- for($i = 0; $i < count($upgraders); $i++) {
- $upgrader = $upgraders[$i];
- if ($upgrader->checkIfUpgradeSupported()) {
-
- $upgrader->upgrade($dir);
- $upgradePerformed = true;
- $i = 0;
- }
- }
-
- return $upgradePerformed;
- }
- }
- abstract class AirtimeUpgrader
- {
-
- abstract protected function getSupportedSchemaVersions();
-
- abstract public function getNewVersion();
- public static function getCurrentSchemaVersion()
- {
- CcPrefPeer::clearInstancePool();
-
-
- $pref = CcPrefQuery::create()
- ->filterByKeystr('system_version')
- ->findOne();
- if (empty($pref)) {
-
- $pref = CcPrefQuery::create()
- ->filterByKeystr('schema_version')
- ->findOne();
- }
- $schema_version = $pref->getValStr();
- return $schema_version;
- }
-
-
- public function checkIfUpgradeSupported()
- {
- if (!in_array(AirtimeUpgrader::getCurrentSchemaVersion(), $this->getSupportedSchemaVersions())) {
- return false;
- }
- return true;
- }
-
- protected function toggleMaintenanceScreen($toggle)
- {
- if ($toggle)
- {
-
-
-
-
- $this->maintenanceFile = isset($_SERVER['AIRTIME_BASE']) ? $_SERVER['AIRTIME_BASE']."maintenance.txt" : "/tmp/maintenance.txt";
- $file = fopen($this->maintenanceFile, 'w');
- fclose($file);
- } else {
-
- if ($this->maintenanceFile) {
- unlink($this->maintenanceFile);
- }
- }
- }
-
-
- abstract public function upgrade();
- }
- class AirtimeUpgrader252 extends AirtimeUpgrader {
- protected function getSupportedSchemaVersions() {
- return array (
- '2.5.1'
- );
- }
-
- public function getNewVersion() {
- return '2.5.2';
- }
-
- public function upgrade($dir = __DIR__) {
- Cache::clear();
- assert($this->checkIfUpgradeSupported());
-
- $newVersion = $this->getNewVersion();
-
- try {
- $this->toggleMaintenanceScreen(true);
- Cache::clear();
-
-
- $airtimeConf = isset($_SERVER['AIRTIME_CONF']) ? $_SERVER['AIRTIME_CONF'] : "/etc/airtime/airtime.conf";
- $values = parse_ini_file($airtimeConf, true);
-
- $username = $values['database']['dbuser'];
- $password = $values['database']['dbpass'];
- $host = $values['database']['host'];
- $database = $values['database']['dbname'];
-
- 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\"");
-
- Application_Model_Preference::SetSchemaVersion($newVersion);
- Cache::clear();
-
- $this->toggleMaintenanceScreen(false);
-
- return true;
- } catch(Exception $e) {
- $this->toggleMaintenanceScreen(false);
- throw $e;
- }
- }
- }
- class AirtimeUpgrader253 extends AirtimeUpgrader
- {
- protected function getSupportedSchemaVersions()
- {
- return array('2.5.2');
- }
- public function getNewVersion()
- {
- return '2.5.3';
- }
-
- public function upgrade($dir = __DIR__)
- {
- Cache::clear();
- assert($this->checkIfUpgradeSupported());
-
- $con = Propel::getConnection();
- $con->beginTransaction();
- try {
-
- $this->toggleMaintenanceScreen(true);
- Cache::clear();
-
-
-
-
- $musicDir = CcMusicDirsQuery::create()
- ->filterByType('stor')
- ->filterByExists(true)
- ->findOne();
- $storPath = $musicDir->getDirectory();
-
-
- $storDir = isset($_SERVER['AIRTIME_BASE']) ? $_SERVER['AIRTIME_BASE']."srv/airtime/stor" : "/srv/airtime/stor";
- $diskUsage = shell_exec("du -sb $storDir | awk '{print $1}'");
-
- Application_Model_Preference::setDiskUsage($diskUsage);
-
-
- Cache::clear();
-
- $con->commit();
-
-
- $airtimeConf = isset($_SERVER['AIRTIME_CONF']) ? $_SERVER['AIRTIME_CONF'] : "/etc/airtime/airtime.conf";
- $values = parse_ini_file($airtimeConf, true);
-
- $username = $values['database']['dbuser'];
- $password = $values['database']['dbpass'];
- $host = $values['database']['host'];
- $database = $values['database']['dbname'];
-
- 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\"");
-
- Application_Model_Preference::SetSchemaVersion($this->getNewVersion());
-
- Cache::clear();
-
- $this->toggleMaintenanceScreen(false);
-
- } catch (Exception $e) {
- $con->rollback();
- $this->toggleMaintenanceScreen(false);
- }
- }
- }
- class AirtimeUpgrader254 extends AirtimeUpgrader
- {
- protected function getSupportedSchemaVersions()
- {
- return array('2.5.3');
- }
- public function getNewVersion()
- {
- return '2.5.4';
- }
-
- public function upgrade()
- {
- Cache::clear();
-
- assert($this->checkIfUpgradeSupported());
-
- $newVersion = $this->getNewVersion();
-
- $con = Propel::getConnection();
-
- try {
- $this->toggleMaintenanceScreen(true);
- Cache::clear();
-
-
-
- $numberOfSuperAdmins = CcSubjsQuery::create()
- ->filterByDbType(UTYPE_SUPERADMIN)
- ->filterByDbLogin("sourcefabric_admin", Criteria::NOT_EQUAL)
- ->count();
-
-
- if ($numberOfSuperAdmins == 0)
- {
-
- $adminUser = CcSubjsQuery::create()
- ->filterByDbLogin('admin')
- ->findOne();
- if (!$adminUser)
- {
-
-
- $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.");
-
-
- $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.");
- }
- }
-
-
- Application_Model_Preference::SetSchemaVersion($newVersion);
- Cache::clear();
-
- $this->toggleMaintenanceScreen(false);
-
- return true;
-
- } catch(Exception $e) {
-
- $this->toggleMaintenanceScreen(false);
- throw $e;
- }
- }
- }
-
- class AirtimeUpgrader256 extends AirtimeUpgrader {
- protected function getSupportedSchemaVersions() {
- return array (
- '2.5.4', '2.5.5'
- );
- }
-
- public function getNewVersion() {
- return '2.5.6';
- }
-
- public function upgrade($dir = __DIR__) {
- Cache::clear();
- assert($this->checkIfUpgradeSupported());
-
- $newVersion = $this->getNewVersion();
-
- try {
-
- return true;
- } catch(Exception $e) {
- $this->toggleMaintenanceScreen(false);
- throw $e;
- }
- }
- }
|