Browse Source

Changes on console feedback colors. Example of User Tasks.

1.0
Andrs Montaez 13 years ago
parent
commit
e5f0fe9cfa
  1. 7
      .gitignore
  2. 6
      Mage/Autoload.php
  3. 6
      Mage/Config.php
  4. 17
      Mage/Console.php
  5. 91
      Mage/Console/Colors.php
  6. 8
      Mage/Task/BuiltIn/Deployment/Rsync.php
  7. 21
      Mage/Task/BuiltIn/Scm/Update.php
  8. 24
      Mage/Task/Deploy.php
  9. 20
      Mage/Task/Factory.php
  10. 24
      Mage/Task/TaskAbstract.php
  11. 24
      Mage/Task/Update.php
  12. 15
      bin/mage.php
  13. 4
      docs/example-config/.mage/config/environment/production.yaml

7
.gitignore vendored

@ -0,0 +1,7 @@
.project
.buildpath
.settings
.settings/*
.settings/org.eclipse.php.core.prefs
.settings/org.eclipse.php.core.prefs

6
Mage/Autoload.php

@ -7,4 +7,10 @@ class Mage_Autoload
$classFile = $baseDir . '/' . str_replace('_', '/', $className . '.php'); $classFile = $baseDir . '/' . str_replace('_', '/', $className . '.php');
require_once $classFile; require_once $classFile;
} }
public static function loadUserTask($taskName)
{
$classFile = '.mage/tasks/' . ucfirst($taskName) . '.php';
require_once $classFile;
}
} }

6
Mage/Config.php

@ -6,7 +6,9 @@ class Mage_Config
public function loadEnvironment($environment) public function loadEnvironment($environment)
{ {
$this->_environment = yaml_parse_file('.mage/config/environment/' . $environment . '.yaml'); if ($environment != '') {
$this->_environment = yaml_parse_file('.mage/config/environment/' . $environment . '.yaml');
}
} }
public function loadSCM() public function loadSCM()
@ -36,7 +38,7 @@ class Mage_Config
return $config['tasks']; return $config['tasks'];
} }
public function getConfig($host) public function getConfig($host = false)
{ {
$taskConfig = array(); $taskConfig = array();
$taskConfig['deploy'] = $this->getEnvironment(); $taskConfig['deploy'] = $this->getEnvironment();

17
Mage/Console.php

@ -37,9 +37,13 @@ class Mage_Console
return $this->_environment; return $this->_environment;
} }
public static function output($message) public static function output($message, $tabs = 1, $newLine = true)
{ {
echo $message; $output = str_repeat("\t", $tabs)
. Mage_Console_Colors::color($message)
. ($newLine ? PHP_EOL : '');
echo $output;
} }
public static function executeCommand($command) public static function executeCommand($command)
@ -52,7 +56,7 @@ class Mage_Console
} }
public function run() public function run()
{ {
$config = new Mage_Config; $config = new Mage_Config;
$config->loadEnvironment($this->getEnvironment()); $config->loadEnvironment($this->getEnvironment());
$config->loadSCM(); $config->loadSCM();
@ -64,12 +68,9 @@ class Mage_Console
break; break;
case 'update'; case 'update';
$config->loadCSM();
$task = new Mage_Task_Update; $task = new Mage_Task_Update;
$task->run($config); $task->run($config);
break; break;
} }
} }
} }
define('PHP_TAB', "\t");

91
Mage/Console/Colors.php

@ -1,53 +1,42 @@
<?php <?php
class Mage_Console_Colors { class Mage_Console_Colors
private static $foreground_colors = array (); {
private static $background_colors = array (); private static $foreground_colors = array(
'black' => '0;30',
public function __construct() { 'dark_gray' => '1;30',
// Set up shell colors 'blue' => '0;34',
self::$foreground_colors ['black'] = '0;30'; 'light_blue' => '1;34',
self::$foreground_colors ['dark_gray'] = '1;30'; 'green' => '0;32',
self::$foreground_colors ['blue'] = '0;34'; 'light_green' => '1;32',
self::$foreground_colors ['light_blue'] = '1;34'; 'cyan' => '0;36',
self::$foreground_colors ['green'] = '0;32'; 'light_cyan' => '1;36',
self::$foreground_colors ['light_green'] = '1;32'; 'red' => '0;31',
self::$foreground_colors ['cyan'] = '0;36'; 'light_red' => '1;31',
self::$foreground_colors ['light_cyan'] = '1;36'; 'purple' => '0;35',
self::$foreground_colors ['red'] = '0;31'; 'light_purple' => '1;35',
self::$foreground_colors ['light_red'] = '1;31'; 'brown' => '0;33',
self::$foreground_colors ['purple'] = '0;35'; 'yellow' => '1;33',
self::$foreground_colors ['light_purple'] = '1;35'; 'light_gray' => '0;37',
self::$foreground_colors ['brown'] = '0;33'; 'white' => '1;37'
self::$foreground_colors ['yellow'] = '1;33';
self::$foreground_colors ['light_gray'] = '0;37'; );
self::$foreground_colors ['white'] = '1;37';
// Returns colored string
self::$background_colors ['black'] = '40'; public static function color($string)
self::$background_colors ['red'] = '41'; {
self::$background_colors ['green'] = '42'; foreach (self::$foreground_colors as $key => $code) {
self::$background_colors ['yellow'] = '43'; $replaceFrom = array(
self::$background_colors ['blue'] = '44'; '<' . $key . '>',
self::$background_colors ['magenta'] = '45'; '</' . $key . '>'
self::$background_colors ['cyan'] = '46'; );
self::$background_colors ['light_gray'] = '47'; $replaceTo = array(
} "\033[" . $code . 'm',
"\033[0m"
// Returns colored string );
public static function g($string, $foreground_color = null, $background_color = null) {
$colored_string = ""; $string = str_replace($replaceFrom, $replaceTo, $string);
}
// Check if given foreground color found
if (isset ( self::$foreground_colors [$foreground_color] )) { return $string;
$colored_string .= "\033[" . self::$foreground_colors [$foreground_color] . "m"; }
}
// Check if given background color found
if (isset ( self::$background_colors [$background_color] )) {
$colored_string .= "\033[" . self::$background_colors [$background_color] . "m";
}
// Add string and end coloring
$colored_string .= $string . "\033[0m";
return $colored_string;
}
} }

8
Mage/Task/BuiltIn/Deployment/Rsync.php

@ -4,10 +4,10 @@ class Mage_Task_BuiltIn_Deployment_Rsync
{ {
public function getName() public function getName()
{ {
return 'Rsync (built-in)'; return 'Rsync [built-in]';
} }
public function run($config) public function run()
{ {
$ignores = array( $ignores = array(
'--exclude .git', '--exclude .git',
@ -18,8 +18,8 @@ class Mage_Task_BuiltIn_Deployment_Rsync
$command = 'rsync -avz ' $command = 'rsync -avz '
. implode(' ', $ignores) .' ' . implode(' ', $ignores) .' '
. $config['deploy']['deploy-from'] . ' ' . $this->_config['deploy']['deploy-from'] . ' '
. $config['deploy']['user'] . '@' . $config['deploy']['host'] . ':' . $config['deploy']['deploy-to']; . $this->_config['deploy']['user'] . '@' . $this->_config['deploy']['host'] . ':' . $this->_config['deploy']['deploy-to'];
$result = $this->_runLocalCommand($command); $result = $this->_runLocalCommand($command);

21
Mage/Task/BuiltIn/Scm/Update.php

@ -2,14 +2,29 @@
class Mage_Task_BuiltIn_Scm_Update class Mage_Task_BuiltIn_Scm_Update
extends Mage_Task_TaskAbstract extends Mage_Task_TaskAbstract
{ {
private $_name = 'SCM Update [built-in]';
public function getName() public function getName()
{ {
return 'SCM Update (built-in)'; return $this->_name;
}
public function init()
{
switch ($this->_config['scm']['type']) {
case 'git':
$this->_name = 'SCM Update (GIT) [built-in]';
break;
case 'svn':
$this->_name = 'SCM Update (Subversion) [built-in]';
break;
}
} }
public function run($config) public function run()
{ {
switch ($config['scm']['type']) { switch ($this->_config['scm']['type']) {
case 'git': case 'git':
$command = 'git pull'; $command = 'git pull';
break; break;

24
Mage/Task/Deploy.php

@ -7,30 +7,36 @@ class Mage_Task_Deploy
{ {
$this->_config = $config; $this->_config = $config;
foreach ($config->getHosts() as $host) foreach ($config->getHosts() as $host) {
{
$taskConfig = $config->getConfig($host); $taskConfig = $config->getConfig($host);
$tasks = 0; $tasks = 0;
$completedTasks = 0; $completedTasks = 0;
Mage_Console::output(PHP_TAB . 'Deploying to ' . $host . PHP_EOL); Mage_Console::output('Deploying to <dark_gray>' . $host . '</dark_gray>');
foreach ($config->getTasks() as $taskName) { foreach ($config->getTasks() as $taskName) {
$tasks++; $tasks++;
$task = Mage_Task_Factory::get($taskName); $task = Mage_Task_Factory::get($taskName, $taskConfig);
$task->init();
Mage_Console::output(PHP_TAB . PHP_TAB . 'Running ' . $task->getName() . ' ... '); Mage_Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false);
$result = $task->run($taskConfig); $result = $task->run();
if ($result == true) { if ($result == true) {
Mage_Console::output(PHP_TAB . 'OK' . PHP_EOL); Mage_Console::output('<green>OK</green>');
$completedTasks++; $completedTasks++;
} else { } else {
Mage_Console::output(PHP_TAB . 'FAIL' . PHP_EOL); Mage_Console::output('<red>FAIL</red>');
} }
} }
if ($completedTasks == $tasks) {
$tasksColor = 'green';
} else {
$tasksColor = 'red';
}
Mage_Console::output(PHP_TAB . 'Deployment to ' . $host . ' compted: ' . $completedTasks . '/' . $tasks . ' tasks done.' . PHP_EOL . PHP_EOL); Mage_Console::output('Deployment to <dark_gray>' . $host . '</dark_gray> compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.' . PHP_EOL . PHP_EOL);
} }
} }

20
Mage/Task/Factory.php

@ -7,10 +7,22 @@ class Mage_Task_Factory
* @param string $taskName * @param string $taskName
* @return Mage_Task_TaskAbstract * @return Mage_Task_TaskAbstract
*/ */
public static function get($taskName) public static function get($taskName, $taskConfig)
{ {
$taskName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $taskName))); $instance = null;
$className = 'Mage_Task_BuiltIn_' . $taskName;
return new $className; if (strpos($taskName, '/') === false) {
Mage_Autoload::loadUserTask($taskName);
$className = 'Task_' . ucfirst($taskName);
$instance = new $className($taskConfig);
} else {
$taskName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $taskName)));
$className = 'Mage_Task_BuiltIn_' . $taskName;
$instance = new $className($taskConfig);
}
assert($instance instanceOf Mage_Task_TaskAbstract);
return $instance;
} }
} }

24
Mage/Task/TaskAbstract.php

@ -1,17 +1,33 @@
<?php <?php
abstract class Mage_Task_TaskAbstract abstract class Mage_Task_TaskAbstract
{ {
protected $_config = null;
public abstract function getName(); public abstract function getName();
public abstract function run($config); public abstract function run();
public final function __construct($config)
{
$this->_config = $config;
}
public function init()
{
}
protected function _runLocalCommand($command) protected final function _runLocalCommand($command)
{ {
return Mage_Console::executeCommand($command); return Mage_Console::executeCommand($command);
} }
protected function _runRemoteCommand($command) protected final function _runRemoteCommand($command)
{ {
$localCommand = 'ssh '
. $this->_config['deploy']['user'] . '@' . $this->_config['deploy']['host'] . ' '
. '"cd ' . $this->_config['deploy']['deploy-to'] . ' && '
. $command . '"';
return $this->_runLocalCommand($localCommand);
} }
} }

24
Mage/Task/Update.php

@ -0,0 +1,24 @@
<?php
class Mage_Task_Update
{
private $_config = null;
public function run(Mage_Config $config)
{
$this->_config = $config;
$taskConfig = $config->getConfig();
$task = Mage_Task_Factory::get('scm/update', $taskConfig);
$task->init();
Mage_Console::output( PHP_TAB . 'Updating application via ' . $task->getName() . ' ... ');
$result = $task->run();
if ($result == true) {
Mage_Console::output(PHP_TAB . 'OK' . PHP_EOL);
} else {
Mage_Console::output(PHP_TAB . 'FAIL' . PHP_EOL);
}
}
}

15
bin/mage.php

@ -1,5 +1,5 @@
<?php <?php
# mage install # sudo mage install
# mage init # mage init
# mage config add environment [production] # mage config add environment [production]
@ -8,22 +8,23 @@
# mage config svn svn://example.com/repo # mage config svn svn://example.com/repo
# mage deploy to:production # mage deploy to:production
# mage update # mage task:deployment/rsync to:production
# mage up
# mage task:init to:production
# mage run:full-deployment to:production
# full-deployment = update, deploy to:production
$baseDir = dirname(dirname(__FILE__)); $baseDir = dirname(dirname(__FILE__));
require_once $baseDir . '/Mage/Autoload.php'; require_once $baseDir . '/Mage/Autoload.php';
spl_autoload_register(array('Mage_Autoload', 'autoload')); spl_autoload_register(array('Mage_Autoload', 'autoload'));
Mage_Console::output('Begining Magallanes' . PHP_EOL . PHP_EOL); Mage_Console::output('Starting <blue>Magallanes</blue>', 0);
Mage_Console::output('');
$console = new Mage_Console; $console = new Mage_Console;
$console->setArgs($argv); $console->setArgs($argv);
$console->parse(); $console->parse();
$console->run(); $console->run();
Mage_Console::output('Finished <blue>Magallanes</blue>', 0);

4
docs/example-config/.mage/config/environment/production.yaml

@ -6,6 +6,8 @@ hosts:
- s01.example.com - s01.example.com
- s02.example.com - s02.example.com
- s03.example.com - s03.example.com
- s05.example.com
tasks: tasks:
- scm/update - scm/update
- deployment/rsync - deployment/rsync
- privileges
Loading…
Cancel
Save