_releaseId = date('YmdHis');
}
public function run()
{
$this->_startTime = time();
$lockFile = '.mage/' . $this->getConfig()->getEnvironment() . '.lock';
if (file_exists($lockFile)) {
Mage_Console::output('This environment is locked!', 1, 2);
return;
}
// Run Pre-Deployment Tasks
$this->_runNonDeploymentTasks('pre-deploy', $this->getConfig(), 'Pre-Deployment');
// Run Tasks for Deployment
$hosts = $this->getConfig()->getHosts();
if (count($hosts) == 0) {
Mage_Console::output('Warning! No hosts defined, skipping deployment tasks.', 1, 3);
} else {
$this->_startTimeHosts = time();
foreach ($hosts as $host) {
$this->_hostsCount++;
$this->getConfig()->setHost($host);
$tasks = 0;
$completedTasks = 0;
Mage_Console::output('Deploying to ' . $this->getConfig()->getHost() . '');
$tasksToRun = $this->getConfig()->getTasks();
array_unshift($tasksToRun, 'deployment/rsync');
if ($this->getConfig()->release('enabled', false) == true) {
$this->getConfig()->setReleaseId($this->_releaseId);
array_push($tasksToRun, 'deployment/releases');
}
$tasksToRun = $tasksToRun + $this->getConfig()->getTasks('post-release');
if (count($tasksToRun) == 0) {
Mage_Console::output('Warning! No Deployment tasks defined.', 2);
Mage_Console::output('Deployment to ' . $config->getHost() . ' skipped!', 1, 3);
} else {
foreach ($tasksToRun as $taskName) {
$tasks++;
$task = Mage_Task_Factory::get($taskName, $this->getConfig(), false, 'deploy');
$task->init();
$runTask = true;
Mage_Console::output('Running ' . $task->getName() . ' ... ', 2, false);
if (($task instanceOf Mage_Task_Releases_SkipOnOverride) && $this->getConfig()->getParameter('overrideRelease', false)) {
$runTask == false;
}
if ($runTask == true) {
$result = $task->run();
if ($result == true) {
Mage_Console::output('OK', 0);
$completedTasks++;
} else {
Mage_Console::output('FAIL', 0);
}
} else {
Mage_Console::output('SKIPPED', 0);
}
}
if ($completedTasks == $tasks) {
$tasksColor = 'green';
} else {
$tasksColor = 'red';
}
Mage_Console::output('Deployment to ' . $this->getConfig()->getHost() . ' compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '' . $tasksColor . '> tasks done.', 1, 3);
}
}
$this->_endTimeHosts = time();
}
// Run Post-Deployment Tasks
$this->_runNonDeploymentTasks('post-deploy', $this->getConfig(), 'Post-Deployment');
// Time Information Hosts
if ($this->_hostsCount > 0) {
$timeTextHost = $this->_transcurredTime($this->_endTimeHosts - $this->_startTimeHosts);
Mage_Console::output('Time for deployment: ' . $timeTextHost . '.');
$timeTextPerHost = $this->_transcurredTime(round(($this->_endTimeHosts - $this->_startTimeHosts) / $this->_hostsCount));
Mage_Console::output('Average time per host: ' . $timeTextPerHost . '.');
}
// Time Information General
$timeText = $this->_transcurredTime(time() - $this->_startTime);
Mage_Console::output('Total time: ' . $timeText . '.', 1, 2);
}
/**
* Execute Pre and Post Deployment Tasks
*
* @param string $stage
* @param Mage_Config $config
* @param string $title
*/
private function _runNonDeploymentTasks($stage, Mage_Config $config, $title)
{
$tasksToRun = $config->getTasks($stage);
// Look for Remote Source
if (is_array($this->_config->deployment('source', null))) {
if ($stage == 'pre-deploy') {
array_unshift($tasksToRun, 'scm/clone');
} elseif ($stage == 'post-deploy') {
array_unshift($tasksToRun, 'scm/remove-clone');
}
}
if (count($tasksToRun) == 0) {
Mage_Console::output('No ' . $title . ' tasks defined.', 1, 3);
} else {
Mage_Console::output('Starting ' . $title . ' tasks:');
$tasks = 0;
$completedTasks = 0;
foreach ($tasksToRun as $taskName) {
$tasks++;
$task = Mage_Task_Factory::get($taskName, $config, false, $stage);
$task->init();
Mage_Console::output('Running ' . $task->getName() . ' ... ', 2, 0);
$result = $task->run();
if ($result == true) {
Mage_Console::output('OK', 0);
$completedTasks++;
} else {
Mage_Console::output('FAIL', 0);
}
}
if ($completedTasks == $tasks) {
$tasksColor = 'green';
} else {
$tasksColor = 'red';
}
Mage_Console::output('Finished ' . $title . ' tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '' . $tasksColor . '> tasks done.', 1, 3);
}
}
/**
* Humanize Transcurred time
* @param integer $time
* @return string
*/
private function _transcurredTime($time)
{
$hours = floor($time / 3600);
$minutes = floor(($time - ($hours * 3600)) / 60);
$seconds = $time - ($minutes * 60) - ($hours * 3600);
$timeText = array();
if ($hours > 0) {
$timeText[] = $hours . ' hours';
}
if ($minutes > 0) {
$timeText[] = $minutes . ' minutes';
}
if ($seconds > 0) {
$timeText[] = $seconds . ' seconds';
}
return implode(' ', $timeText);
}
}