Major overhauling and refactoring of Magallanes Command and Tasks modulairty and workflow.

ADVICE: there is no Backwards Compatibility with custom tasks, those
using the _config instance will be broken or those using the
getEnvironmentName().
This commit is contained in:
Andrés Montañez
2012-09-21 00:22:22 -03:00
parent b8728f2aa2
commit 05b102b273
9 changed files with 0 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
<?php
class Mage_Task_Add
{
public function environment($environmentName, $withRelases = false)
{
$environmentName = strtolower($environmentName);
$environmentConfigFile = '.mage/config/environment/' . $environmentName . '.yml';
Mage_Console::output('Adding new environment: <dark_gray>' . $environmentName . '</dark_gray>');
// Check if there is already an environment with the same name
if (file_exists($environmentConfigFile)) {
Mage_Console::output('<light_red>Error!!</light_red> Already exists an environment called <dark_gray>' . $environmentName . '</dark_gray>', 1, 2);
} else {
$releasesConfig = 'releases:' . PHP_EOL
. ' enabled: true' . PHP_EOL
. ' max: 10' . PHP_EOL
. ' symlink: current' . PHP_EOL
. ' directory: releases' . PHP_EOL;
$baseConfig = '#' . $environmentName . PHP_EOL
. 'deployment:' . PHP_EOL
. ' user: dummy' . PHP_EOL
. ' from: ./' . PHP_EOL
. ' to: /var/www/vhosts/example.com/www' . PHP_EOL
. ' excludes:' . PHP_EOL
. ($withRelases ? $releasesConfig : '')
. 'hosts:' . PHP_EOL
. 'tasks:' . PHP_EOL
. ' pre-deploy:' . PHP_EOL
. ' on-deploy:' . PHP_EOL
. ' - deployment/rsync' . PHP_EOL
. ' post-deploy:' . PHP_EOL;
$result = file_put_contents($environmentConfigFile, $baseConfig);
if ($result) {
Mage_Console::output('<light_green>Success!!</light_green> Environment config file for <dark_gray>' . $environmentName . '</dark_gray> created successfully at <blue>' . $environmentConfigFile . '</blue>');
Mage_Console::output('<dark_gray>So please! Review and adjust its configuration.</dark_gray>', 2, 2);
} else {
Mage_Console::output('<light_red>Error!!</light_red> Unable to create config file for environment called <dark_gray>' . $environmentName . '</dark_gray>', 1, 2);
}
}
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
/**
* Class Mage_Task_Compile
*
* @author Ismael Ambrosi<ismaambrosi@gmail.com>
*/
class Mage_Task_Compile
{
/**
* @see Mage_Compile::compile()
*/
public function run ()
{
Mage_Console::output('Compiling <dark_gray>Magallanes</dark_gray>... ', 1, 0);
$compiler = new Mage_Compiler();
$compiler->compile();
Mage_Console::output('Mage compiled successfully');
}
}
+180
View File
@@ -0,0 +1,180 @@
<?php
class Mage_Task_Deploy
{
private $_config = null;
private $_releaseId = null;
private $_startTime = null;
private $_startTimeHosts = null;
private $_endTimeHosts = null;
private $_hostsCount = 0;
public function __construct()
{
$this->_releaseId = date('YmdHis');
}
public function run(Mage_Config $config)
{
$this->_startTime = time();
$this->_config = $config;
if ($config->getEnvironmentName() == '') {
Mage_Console::output('<red>You must specify an environment</red>', 0, 2);
return;
}
$lockFile = '.mage/' . $config->getEnvironmentName() . '.lock';
if (file_exists($lockFile)) {
Mage_Console::output('<red>This environment is locked!</red>', 0, 2);
return;
}
// Run Pre-Deployment Tasks
$this->_runNonDeploymentTasks('pre-deploy', $config, 'Pre-Deployment');
// Run Tasks for Deployment
$hosts = $config->getHosts();
if (count($hosts) == 0) {
Mage_Console::output('<light_purple>Warning!</light_purple> <dark_gray>No hosts defined, skipping deployment tasks.</dark_gray>', 1, 3);
} else {
$this->_startTimeHosts = time();
foreach ($hosts as $host) {
$this->_hostsCount++;
$config->setHost($host);
$tasks = 0;
$completedTasks = 0;
Mage_Console::output('Deploying to <dark_gray>' . $config->getHost() . '</dark_gray>');
$tasksToRun = $config->getTasks();
array_unshift($tasksToRun, 'deployment/rsync');
if ($config->release('enabled', false) == true) {
$config->setReleaseId($this->_releaseId);
array_push($tasksToRun, 'deployment/releases');
}
if (count($tasksToRun) == 0) {
Mage_Console::output('<light_purple>Warning!</light_purple> <dark_gray>No </dark_gray><light_cyan>Deployment</light_cyan> <dark_gray>tasks defined.</dark_gray>', 2);
Mage_Console::output('Deployment to <dark_gray>' . $config->getHost() . '</dark_gray> skipped!', 1, 3);
} else {
foreach ($tasksToRun as $taskName) {
$tasks++;
$task = Mage_Task_Factory::get($taskName, $config, false, 'deploy');
$task->init();
Mage_Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false);
$result = $task->run();
if ($result == true) {
Mage_Console::output('<green>OK</green>', 0);
$completedTasks++;
} else {
Mage_Console::output('<red>FAIL</red>', 0);
}
}
if ($completedTasks == $tasks) {
$tasksColor = 'green';
} else {
$tasksColor = 'red';
}
Mage_Console::output('Deployment to <dark_gray>' . $config->getHost() . '</dark_gray> compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}
}
$this->_endTimeHosts = time();
}
// Run Post-Deployment Tasks
$this->_runNonDeploymentTasks('post-deploy', $config, 'Post-Deployment');
// Time Information Hosts
if ($this->_hostsCount > 0) {
$timeTextHost = $this->_transcurredTime($this->_endTimeHosts - $this->_startTimeHosts);
Mage_Console::output('Time for deployment: <dark_gray>' . $timeTextHost . '</dark_gray>.');
$timeTextPerHost = $this->_transcurredTime(round(($this->_endTimeHosts - $this->_startTimeHosts) / $this->_hostsCount));
Mage_Console::output('Average time per host: <dark_gray>' . $timeTextPerHost . '</dark_gray>.');
}
// Time Information General
$timeText = $this->_transcurredTime(time() - $this->_startTime);
Mage_Console::output('Total time: <dark_gray>' . $timeText . '</dark_gray>.', 1, 2);
}
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('<dark_gray>No </dark_gray><light_cyan>' . $title . '</light_cyan> <dark_gray>tasks defined.</dark_gray>', 1, 3);
} else {
Mage_Console::output('Starting <dark_gray>' . $title . '</dark_gray> 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 <purple>' . $task->getName() . '</purple> ... ', 2, 0);
$result = $task->run();
if ($result == true) {
Mage_Console::output('<green>OK</green>', 0);
$completedTasks++;
} else {
Mage_Console::output('<red>FAIL</red>', 0);
}
}
if ($completedTasks == $tasks) {
$tasksColor = 'green';
} else {
$tasksColor = 'red';
}
Mage_Console::output('Finished <dark_gray>' . $title . '</dark_gray> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}
}
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);
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
class Mage_Task_Init
{
public function run()
{
$configDir = '.mage';
Mage_Console::output('Initiating managing process for application with <dark_gray>Magallanes</dark_gray>');
// Check if there is already a config dir
if (file_exists($configDir)) {
Mage_Console::output('<light_red>Error!!</light_red> Already exists <dark_gray>.mage</dark_gray> directory.', 1, 2);
} else {
$results = array();
$results[] = mkdir($configDir);
$results[] = mkdir($configDir . '/logs');
$results[] = mkdir($configDir . '/tasks');
$results[] = mkdir($configDir . '/config');
$results[] = mkdir($configDir . '/config/environment');
$results[] = file_put_contents($configDir . '/config/general.yml', '#global settings' . PHP_EOL . PHP_EOL);
$results[] = file_put_contents($configDir . '/config/scm.yml', '#scm settings' . PHP_EOL . PHP_EOL);
if (!in_array(false, $results)) {
Mage_Console::output('<light_green>Success!!</light_green> The configuration for <dark_gray>Magallanes</dark_gray> has been generated at <blue>.mage</blue> directory.');
Mage_Console::output('<dark_gray>Please!! Review and adjust the configuration.</dark_gray>', 2, 2);
} else {
Mage_Console::output('<light_red>Error!!</light_red> Unable to generate the configuration.', 1, 2);
}
}
}
}
+56
View File
@@ -0,0 +1,56 @@
<?php
class Mage_Task_Install
{
public function run ()
{
Mage_Console::output('Installing <dark_gray>Magallanes</dark_gray>... ', 1, 0);
$this->_recursiveCopy('./', '/opt/magallanes-' . MAGALLANES_VERSION);
if (file_exists('/opt/magallanes') && is_link('/opt/magallanes')) {
unlink('/opt/magallanes');
}
symlink('/opt/magallanes-' . MAGALLANES_VERSION, '/opt/magallanes');
chmod('/opt/magallanes/bin/mage', 0755);
if (!file_exists('/usr/bin/mage')) {
symlink('/opt/magallanes/bin/mage', '/usr/bin/mage');
}
Mage_Console::output('<light_green>Success!</light_green>', 0, 2);
}
private function _recursiveCopy ($from, $to)
{
if (is_dir($from)) {
mkdir($to);
$files = scandir($from);
if (count($files) > 0) {
foreach ($files as $file) {
if (strpos($file, '.') === 0) {
continue;
}
if (is_dir($from . DIRECTORY_SEPARATOR . $file)) {
$this->_recursiveCopy(
$from . DIRECTORY_SEPARATOR . $file,
$to . DIRECTORY_SEPARATOR . $file
);
} else {
copy(
$from . DIRECTORY_SEPARATOR . $file,
$to . DIRECTORY_SEPARATOR . $file
);
}
}
}
return true;
} elseif (is_file($from)) {
return copy($from, $to);
} else {
return false;
}
}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
class Mage_Task_Lock
{
private $_config = null;
public function run(Mage_Config $config, $unlock = false)
{
$this->_config = $config;
if ($config->getEnvironmentName() == '') {
Mage_Console::output('<red>You must specify an environment</red>', 0, 2);
return;
}
$lockFile = '.mage/' . $config->getEnvironmentName() . '.lock';
if (file_exists($lockFile)) {
@unlink($lockFile);
}
Mage_Console::output('Unlocked deployment to <light_purple>' . $config->getEnvironmentName() . '</light_purple> environment', 1, 2);
}
}
+71
View File
@@ -0,0 +1,71 @@
<?php
class Mage_Task_Releases
{
private $_config = null;
private $_action = null;
private $_release = null;
public function setAction($action)
{
$this->_action = $action;
return $this;
}
public function getAction()
{
return $this->_action;
}
public function setRelease($releaseId)
{
$this->_release = $releaseId;
return $this;
}
public function getRelease()
{
return $this->_release;
}
public function run(Mage_Config $config)
{
$this->_config = $config;
if ($config->getEnvironmentName() == '') {
Mage_Console::output('<red>You must specify an environment</red>', 0, 2);
return;
}
$lockFile = '.mage/' . $config->getEnvironmentName() . '.lock';
if (file_exists($lockFile)) {
Mage_Console::output('<red>This environment is locked!</red>', 0, 2);
return;
}
// Run Tasks for Deployment
$hosts = $config->getHosts();
if (count($hosts) == 0) {
Mage_Console::output('<light_purple>Warning!</light_purple> <dark_gray>No hosts defined, unable to get releases.</dark_gray>', 1, 3);
} else {
foreach ($hosts as $host) {
$config->setHost($host);
switch ($this->getAction()) {
case 'list':
$task = Mage_Task_Factory::get('releases/list', $config);
$task->init();
$result = $task->run();
break;
case 'rollback':
$task = Mage_Task_Factory::get('releases/rollback', $config);
$task->init();
$task->setRelease($this->getRelease());
$result = $task->run();
break;
}
}
}
}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
class Mage_Task_Update
{
private $_config = null;
public function run(Mage_Config $config)
{
$this->_config = $config;
$task = Mage_Task_Factory::get('scm/update', $config);
$task->init();
Mage_Console::output('Updating application via ' . $task->getName() . ' ... ', 1, 0);
$result = $task->run();
if ($result == true) {
Mage_Console::output('OK' . PHP_EOL, 0);
} else {
Mage_Console::output('FAIL' . PHP_EOL, 0);
}
}
}
+114
View File
@@ -0,0 +1,114 @@
<?php
class Mage_Task_Upgrade
{
const DOWNLOAD = 'https://github.com/andres-montanez/Magallanes/tarball/stable';
public function run ()
{
Mage_Console::output('Upgrading <dark_gray>Magallanes</dark_gray> ... ', 1, 0);
$user = '';
// Check if user is root
Mage_Console::executeCommand('whoami', $user);
if ($user != 'root') {
Mage_Console::output('<red>FAIL</red>', 0, 1);
Mage_Console::output('You need to be the <dark_gray>root</dark_gray> user to perform the upgrade.', 2);
} else {
// Download Package
$tarball = file_get_contents(self::DOWNLOAD);
$tarballFile = tempnam('/tmp', 'magallanes_download');
rename($tarballFile, $tarballFile . '.tar.gz');
$tarballFile .= '.tar.gz';
file_put_contents($tarballFile, $tarball);
// Unpackage
if (file_exists('/tmp/__magallanesDownload')) {
Mage_Console::executeCommand('rm -rf /tmp/__magallanesDownload');
}
Mage_Console::executeCommand('mkdir /tmp/__magallanesDownload');
Mage_Console::executeCommand('cd /tmp/__magallanesDownload && tar xfz ' . $tarballFile);
Mage_Console::executeCommand('rm -f ' . $tarballFile);
// Find Package
$tarballDir = opendir('/tmp/__magallanesDownload');
while (($file = readdir($tarballDir)) == true) {
if ($file == '.' || $file == '..') {
continue;
} else {
$packageDir = $file;
break;
}
}
// Get Version
$version = false;
if (file_exists('/tmp/__magallanesDownload/' . $packageDir . '/bin/mage')) {
list(, $version) = file('/tmp/__magallanesDownload/' . $packageDir . '/bin/mage');
$version = trim(str_replace('#VERSION:', '', $version));
}
if ($version != false) {
$versionCompare = version_compare(MAGALLANES_VERSION, $version);
if ($versionCompare == 0) {
Mage_Console::output('<yellow>SKIP</yellow>', 0, 1);
Mage_Console::output('Your current version is up to date.', 2);
} else if ($versionCompare > 0) {
Mage_Console::output('<yellow>SKIP</yellow>', 0, 1);
Mage_Console::output('Your current version is newer.', 2);
} else {
$this->_recursiveCopy('/tmp/__magallanesDownload/' . $packageDir, '/opt/magallanes-' . $version);
unlink('/opt/magallanes');
symlink('/opt/magallanes-' . $version, '/opt/magallanes');
chmod('/opt/magallanes/bin/mage', 0755);
Mage_Console::output('<green>OK</green>', 0, 1);
}
} else {
Mage_Console::output('<red>FAIL</red>', 0, 1);
Mage_Console::output('Corrupted download.', 2);
}
}
}
private function _recursiveCopy ($from, $to)
{
if (is_dir($from)) {
mkdir($to);
$files = scandir($from);
if (count($files) > 0) {
foreach ($files as $file) {
if (strpos($file, '.') === 0) {
continue;
}
if (is_dir($from . DIRECTORY_SEPARATOR . $file)) {
$this->_recursiveCopy(
$from . DIRECTORY_SEPARATOR . $file,
$to . DIRECTORY_SEPARATOR . $file
);
} else {
copy(
$from . DIRECTORY_SEPARATOR . $file,
$to . DIRECTORY_SEPARATOR . $file
);
}
}
}
return true;
} elseif (is_file($from)) {
return copy($from, $to);
} else {
return false;
}
}
}