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:23:07 -03:00
parent 05b102b273
commit 811c83e13a
29 changed files with 803 additions and 639 deletions
+58 -37
View File
@@ -1,44 +1,65 @@
<?php
class Mage_Task_Add
class Mage_Command_BuiltIn_Add
extends Mage_Command_CommandAbstract
{
public function environment($environmentName, $withRelases = false)
public function run()
{
$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);
$subCommand = $this->getConfig()->getArgument(1);
try {
switch ($subCommand) {
case 'environment':
$this->_environment();
break;
}
} catch (Exception $e) {
Mage_Console::output('<red>' . $e->getMessage() . '</red>', 1, 2);
}
}
private function _environment()
{
$withReleases = $this->getConfig()->getParameter('enableReleases', false);
$environmentName = strtolower($this->getConfig()->getParameter('name'));
if ($environmentName == '') {
throw new Exception('You must specify a name for the environment.');
}
$environmentConfigFile = '.mage/config/environment/' . $environmentName . '.yml';
if (file_exists($environmentConfigFile)) {
throw new Exception('The environment already exists.');
}
Mage_Console::output('Adding new environment: <dark_gray>' . $environmentName . '</dark_gray>');
$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
. ($withReleases ? $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);
}
}
}
+3 -3
View File
@@ -1,11 +1,11 @@
<?php
/**
* Class Mage_Task_Compile
* Class Mage_Command_BuiltIn_Compile
*
* @author Ismael Ambrosi<ismaambrosi@gmail.com>
*/
class Mage_Task_Compile
class Mage_Command_BuiltIn_Compile
extends Mage_Command_CommandAbstract
{
/**
* @see Mage_Compile::compile()
+45 -27
View File
@@ -1,8 +1,8 @@
<?php
class Mage_Task_Deploy
class Mage_Command_BuiltIn_Deploy
extends Mage_Command_CommandAbstract
implements Mage_Command_RequiresEnvironment
{
private $_config = null;
private $_releaseId = null;
private $_startTime = null;
private $_startTimeHosts = null;
private $_endTimeHosts = null;
@@ -13,27 +13,21 @@ class Mage_Task_Deploy
$this->_releaseId = date('YmdHis');
}
public function run(Mage_Config $config)
public function run()
{
$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';
$lockFile = '.mage/' . $this->getConfig()->getEnvironment() . '.lock';
if (file_exists($lockFile)) {
Mage_Console::output('<red>This environment is locked!</red>', 0, 2);
Mage_Console::output('<red>This environment is locked!</red>', 1, 2);
return;
}
// Run Pre-Deployment Tasks
$this->_runNonDeploymentTasks('pre-deploy', $config, 'Pre-Deployment');
$this->_runNonDeploymentTasks('pre-deploy', $this->getConfig(), 'Pre-Deployment');
// Run Tasks for Deployment
$hosts = $config->getHosts();
$hosts = $this->getConfig()->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);
@@ -42,20 +36,22 @@ class Mage_Task_Deploy
$this->_startTimeHosts = time();
foreach ($hosts as $host) {
$this->_hostsCount++;
$config->setHost($host);
$this->getConfig()->setHost($host);
$tasks = 0;
$completedTasks = 0;
Mage_Console::output('Deploying to <dark_gray>' . $config->getHost() . '</dark_gray>');
Mage_Console::output('Deploying to <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray>');
$tasksToRun = $config->getTasks();
$tasksToRun = $this->getConfig()->getTasks();
array_unshift($tasksToRun, 'deployment/rsync');
if ($config->release('enabled', false) == true) {
$config->setReleaseId($this->_releaseId);
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('<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);
@@ -63,17 +59,27 @@ class Mage_Task_Deploy
} else {
foreach ($tasksToRun as $taskName) {
$tasks++;
$task = Mage_Task_Factory::get($taskName, $config, false, 'deploy');
$task = Mage_Task_Factory::get($taskName, $this->getConfig(), false, 'deploy');
$task->init();
$runTask = true;
Mage_Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false);
$result = $task->run();
if ($result == true) {
Mage_Console::output('<green>OK</green>', 0);
$completedTasks++;
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('<green>OK</green>', 0);
$completedTasks++;
} else {
Mage_Console::output('<red>FAIL</red>', 0);
}
} else {
Mage_Console::output('<red>FAIL</red>', 0);
Mage_Console::output('<yellow>SKIPPED</yellow>', 0);
}
}
@@ -83,14 +89,14 @@ class Mage_Task_Deploy
$tasksColor = 'red';
}
Mage_Console::output('Deployment to <dark_gray>' . $config->getHost() . '</dark_gray> compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
Mage_Console::output('Deployment to <dark_gray>' . $this->getConfig()->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');
$this->_runNonDeploymentTasks('post-deploy', $this->getConfig(), 'Post-Deployment');
// Time Information Hosts
if ($this->_hostsCount > 0) {
@@ -106,6 +112,13 @@ class Mage_Task_Deploy
Mage_Console::output('Total time: <dark_gray>' . $timeText . '</dark_gray>.', 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);
@@ -156,6 +169,11 @@ class Mage_Task_Deploy
}
/**
* Humanize Transcurred time
* @param integer $time
* @return string
*/
private function _transcurredTime($time)
{
$hours = floor($time / 3600);
+5 -4
View File
@@ -1,12 +1,13 @@
<?php
class Mage_Task_Init
class Mage_Command_BuiltIn_Init
extends Mage_Command_CommandAbstract
{
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);
@@ -19,7 +20,7 @@ class Mage_Task_Init
$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);
+6 -5
View File
@@ -1,11 +1,12 @@
<?php
class Mage_Task_Install
class Mage_Command_BuiltIn_Install
extends Mage_Command_CommandAbstract
{
public function run ()
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');
}
@@ -18,7 +19,7 @@ class Mage_Task_Install
Mage_Console::output('<light_green>Success!</light_green>', 0, 2);
}
private function _recursiveCopy ($from, $to)
private function _recursiveCopy($from, $to)
{
if (is_dir($from)) {
mkdir($to);
@@ -38,7 +39,7 @@ class Mage_Task_Install
} else {
copy(
$from . DIRECTORY_SEPARATOR . $file,
$from . DIRECTORY_SEPARATOR . $file,
$to . DIRECTORY_SEPARATOR . $file
);
}
+7 -16
View File
@@ -1,23 +1,14 @@
<?php
class Mage_Task_Lock
class Mage_Command_BuiltIn_Lock
extends Mage_Command_CommandAbstract
implements Mage_Command_RequiresEnvironment
{
private $_config = null;
public function run(Mage_Config $config, $unlock = false)
public function run()
{
$this->_config = $config;
$lockFile = '.mage/' . $this->getConfig()->getEnvironment() . '.lock';
file_put_contents($lockFile, 'Locked environment at date: ' . date('Y-m-d H:i:s'));
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);
Mage_Console::output('Locked deployment to <light_purple>' . $this->getConfig()->getEnvironment() . '</light_purple> environment', 1, 2);
}
}
+16 -42
View File
@@ -1,67 +1,41 @@
<?php
class Mage_Task_Releases
class Mage_Command_BuiltIn_Releases
extends Mage_Command_CommandAbstract
implements Mage_Command_RequiresEnvironment
{
private $_config = null;
private $_action = null;
private $_release = null;
public function setAction($action)
public function run()
{
$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)) {
$subcommand = $this->getConfig()->getArgument(1);
$lockFile = '.mage/' . $this->getConfig()->getEnvironment() . '.lock';
if (file_exists($lockFile) && ($subcommand == 'rollback')) {
Mage_Console::output('<red>This environment is locked!</red>', 0, 2);
return;
}
// Run Tasks for Deployment
$hosts = $config->getHosts();
$hosts = $this->getConfig()->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()) {
$this->getConfig()->setHost($host);
switch ($subcommand) {
case 'list':
$task = Mage_Task_Factory::get('releases/list', $config);
$task = Mage_Task_Factory::get('releases/list', $this->getConfig());
$task->init();
$result = $task->run();
break;
case 'rollback':
$task = Mage_Task_Factory::get('releases/rollback', $config);
case 'rollback':
$releaseId = $this->getConfig()->getParameter('release', '');
$task = Mage_Task_Factory::get('releases/rollback', $this->getConfig());
$task->init();
$task->setRelease($this->getRelease());
$task->setRelease($releaseId);
$result = $task->run();
break;
}
+16
View File
@@ -0,0 +1,16 @@
<?php
class Mage_Command_BuiltIn_Unlock
extends Mage_Command_CommandAbstract
implements Mage_Command_RequiresEnvironment
{
public function run()
{
$lockFile = '.mage/' . $this->getConfig()->getEnvironment() . '.lock';
if (file_exists($lockFile)) {
@unlink($lockFile);
}
Mage_Console::output('Unlocked deployment to <light_purple>' . $this->getConfig()->getEnvironment() . '</light_purple> environment', 1, 2);
}
}
+8 -11
View File
@@ -1,22 +1,19 @@
<?php
class Mage_Task_Update
class Mage_Command_BuiltIn_Update
extends Mage_Command_CommandAbstract
{
private $_config = null;
public function run(Mage_Config $config)
public function run()
{
$this->_config = $config;
$task = Mage_Task_Factory::get('scm/update', $config);
$task = Mage_Task_Factory::get('scm/update', $this->getConfig());
$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);
Mage_Console::output('<green>OK</green>' . PHP_EOL, 0);
} else {
Mage_Console::output('FAIL' . PHP_EOL, 0);
Mage_Console::output('<red>FAIL</red>' . PHP_EOL, 0);
}
}
+12 -11
View File
@@ -1,19 +1,20 @@
<?php
class Mage_Task_Upgrade
class Mage_Command_BuiltIn_Upgrade
extends Mage_Command_CommandAbstract
{
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);
@@ -21,7 +22,7 @@ class Mage_Task_Upgrade
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');
@@ -29,7 +30,7 @@ class Mage_Task_Upgrade
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) {
@@ -40,14 +41,14 @@ class Mage_Task_Upgrade
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) {
@@ -66,13 +67,13 @@ class Mage_Task_Upgrade
Mage_Console::output('<green>OK</green>', 0, 1);
}
} else {
Mage_Console::output('<red>FAIL</red>', 0, 1);
Mage_Console::output('Corrupted download.', 2);
}
}
}
@@ -96,7 +97,7 @@ class Mage_Task_Upgrade
} else {
copy(
$from . DIRECTORY_SEPARATOR . $file,
$from . DIRECTORY_SEPARATOR . $file,
$to . DIRECTORY_SEPARATOR . $file
);
}
+10
View File
@@ -0,0 +1,10 @@
<?php
class Mage_Command_BuiltIn_Version
extends Mage_Command_CommandAbstract
{
public function run()
{
Mage_Console::output('Running <blue>Magallanes</blue> version <dark_gray>' . MAGALLANES_VERSION .'</dark_gray>', 0, 2);
}
}