_release = $releaseId;
return $this;
}
public function getRelease()
{
return $this->_release;
}
public function run()
{
if ($this->getConfig()->release('enabled', false) == true) {
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
$symlink = $this->getConfig()->release('symlink', 'current');
$output = '';
$result = $this->_runRemoteCommand('ls -1 ' . $releasesDirectory, $output);
$releases = ($output == '') ? array() : explode(PHP_EOL, $output);
if (count($releases) == 0) {
Mage_Console::output('Release are not available for ' . $this->getConfig()->getHost() . ' ... FAIL');
} else {
rsort($releases);
$releaseIsAvailable = false;
if ($this->getRelease() == '') {
$releaseId = $releases[0];
$releaseIsAvailable = true;
} else if ($this->getRelease() <= 0) {
$index = $this->getRelease() * -1;
if (isset($releases[$index])) {
$releaseId = $releases[$index];
$releaseIsAvailable = true;
}
} else {
if (in_array($this->getRelease(), $releases)) {
$releaseId = $this->getRelease();
$releaseIsAvailable = true;
}
}
if (!$releaseIsAvailable) {
Mage_Console::output('Release ' . $this->getRelease() . ' is invalid or unavailable for ' . $this->getConfig()->getHost() . ' ... FAIL');
} else {
Mage_Console::output('Rollback release on ' . $this->getConfig()->getHost() . '');
$rollbackTo = $releasesDirectory . '/' . $releaseId;
// Tasks
$tasks = 1;
$completedTasks = 0;
$tasksToRun = $this->getConfig()->getTasks();
$this->getConfig()->setReleaseId($releaseId);
if (count($tasksToRun) == 0) {
Mage_Console::output('Warning! No Deployment tasks defined.', 2);
Mage_Console::output('Deployment to ' . $this->getConfig()->getHost() . ' skipped!', 1, 3);
} else {
foreach ($tasksToRun as $taskData) {
$task = Mage_Task_Factory::get($taskData, $this->getConfig(), true, 'deploy');
$task->init();
Mage_Console::output('Running ' . $task->getName() . ' ... ', 2, false);
if ($task instanceOf Mage_Task_Releases_RollbackAware) {
$tasks++;
$result = $task->run();
if ($result == true) {
Mage_Console::output('OK', 0);
$completedTasks++;
} else {
Mage_Console::output('FAIL', 0);
}
} else {
Mage_Console::output('SKIPPED', 0);
}
}
}
// Changing Release
Mage_Console::output('Running Rollback Release [id=' . $releaseId . '] ... ', 2, false);
$userGroup = '';
$resultFetch = $this->_runRemoteCommand('ls -ld ' . $rollbackTo . ' | awk \'{print \$3":"\$4}\'', $userGroup);
$command = 'rm -f ' . $symlink
. ' && '
. 'ln -sf ' . $rollbackTo . ' ' . $symlink
. ' && '
. 'chown -h ' . $userGroup . ' ' . $symlink;
$result = $this->_runRemoteCommand($command);
if ($result) {
Mage_Console::output('OK', 0);
$completedTasks++;
} else {
Mage_Console::output('FAIL', 0);
}
if ($completedTasks == $tasks) {
$tasksColor = 'green';
} else {
$tasksColor = 'red';
}
Mage_Console::output('Release rollback on ' . $this->getConfig()->getHost() . ' compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '' . $tasksColor . '> tasks done.', 1, 3);
}
}
return $result;
} else {
return false;
}
}
}