From e5f0fe9cfa6477008fbe225dabf5a390c19c4fc3 Mon Sep 17 00:00:00 2001 From: Andrs Montaez Date: Wed, 23 Nov 2011 09:38:52 -0200 Subject: [PATCH] Changes on console feedback colors. Example of User Tasks. --- .gitignore | 7 ++ Mage/Autoload.php | 6 ++ Mage/Config.php | 6 +- Mage/Console.php | 17 ++-- Mage/Console/Colors.php | 91 ++++++++----------- Mage/Task/BuiltIn/Deployment/Rsync.php | 8 +- Mage/Task/BuiltIn/Scm/Update.php | 21 ++++- Mage/Task/Deploy.php | 24 +++-- Mage/Task/Factory.php | 20 +++- Mage/Task/TaskAbstract.php | 24 ++++- Mage/Task/Update.php | 24 +++++ bin/mage.php | 15 +-- .../.mage/config/environment/production.yaml | 4 +- 13 files changed, 174 insertions(+), 93 deletions(-) create mode 100644 .gitignore create mode 100644 Mage/Task/Update.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8de3ebb --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.project +.buildpath +.settings +.settings/* +.settings/org.eclipse.php.core.prefs + +.settings/org.eclipse.php.core.prefs diff --git a/Mage/Autoload.php b/Mage/Autoload.php index 1173232..95ee460 100644 --- a/Mage/Autoload.php +++ b/Mage/Autoload.php @@ -7,4 +7,10 @@ class Mage_Autoload $classFile = $baseDir . '/' . str_replace('_', '/', $className . '.php'); require_once $classFile; } + + public static function loadUserTask($taskName) + { + $classFile = '.mage/tasks/' . ucfirst($taskName) . '.php'; + require_once $classFile; + } } \ No newline at end of file diff --git a/Mage/Config.php b/Mage/Config.php index fb5953d..9fce95a 100644 --- a/Mage/Config.php +++ b/Mage/Config.php @@ -6,7 +6,9 @@ class Mage_Config 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() @@ -36,7 +38,7 @@ class Mage_Config return $config['tasks']; } - public function getConfig($host) + public function getConfig($host = false) { $taskConfig = array(); $taskConfig['deploy'] = $this->getEnvironment(); diff --git a/Mage/Console.php b/Mage/Console.php index 0b65664..5a8e63b 100644 --- a/Mage/Console.php +++ b/Mage/Console.php @@ -37,9 +37,13 @@ class Mage_Console 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) @@ -52,7 +56,7 @@ class Mage_Console } public function run() - { + { $config = new Mage_Config; $config->loadEnvironment($this->getEnvironment()); $config->loadSCM(); @@ -64,12 +68,9 @@ class Mage_Console break; case 'update'; - $config->loadCSM(); $task = new Mage_Task_Update; - $task->run($config); + $task->run($config); break; } } -} - -define('PHP_TAB', "\t"); \ No newline at end of file +} \ No newline at end of file diff --git a/Mage/Console/Colors.php b/Mage/Console/Colors.php index 04d481f..df53ca7 100644 --- a/Mage/Console/Colors.php +++ b/Mage/Console/Colors.php @@ -1,53 +1,42 @@ '0;30', + 'dark_gray' => '1;30', + 'blue' => '0;34', + 'light_blue' => '1;34', + 'green' => '0;32', + 'light_green' => '1;32', + 'cyan' => '0;36', + 'light_cyan' => '1;36', + 'red' => '0;31', + 'light_red' => '1;31', + 'purple' => '0;35', + 'light_purple' => '1;35', + 'brown' => '0;33', + 'yellow' => '1;33', + 'light_gray' => '0;37', + 'white' => '1;37' + + ); + + // Returns colored string + public static function color($string) + { + foreach (self::$foreground_colors as $key => $code) { + $replaceFrom = array( + '<' . $key . '>', + '' + ); + $replaceTo = array( + "\033[" . $code . 'm', + "\033[0m" + ); + + $string = str_replace($replaceFrom, $replaceTo, $string); + } + + return $string; + } } diff --git a/Mage/Task/BuiltIn/Deployment/Rsync.php b/Mage/Task/BuiltIn/Deployment/Rsync.php index af4d499..e001e3a 100644 --- a/Mage/Task/BuiltIn/Deployment/Rsync.php +++ b/Mage/Task/BuiltIn/Deployment/Rsync.php @@ -4,10 +4,10 @@ class Mage_Task_BuiltIn_Deployment_Rsync { public function getName() { - return 'Rsync (built-in)'; + return 'Rsync [built-in]'; } - public function run($config) + public function run() { $ignores = array( '--exclude .git', @@ -18,8 +18,8 @@ class Mage_Task_BuiltIn_Deployment_Rsync $command = 'rsync -avz ' . implode(' ', $ignores) .' ' - . $config['deploy']['deploy-from'] . ' ' - . $config['deploy']['user'] . '@' . $config['deploy']['host'] . ':' . $config['deploy']['deploy-to']; + . $this->_config['deploy']['deploy-from'] . ' ' + . $this->_config['deploy']['user'] . '@' . $this->_config['deploy']['host'] . ':' . $this->_config['deploy']['deploy-to']; $result = $this->_runLocalCommand($command); diff --git a/Mage/Task/BuiltIn/Scm/Update.php b/Mage/Task/BuiltIn/Scm/Update.php index ca2a1c9..ef7a00f 100644 --- a/Mage/Task/BuiltIn/Scm/Update.php +++ b/Mage/Task/BuiltIn/Scm/Update.php @@ -2,14 +2,29 @@ class Mage_Task_BuiltIn_Scm_Update extends Mage_Task_TaskAbstract { + private $_name = 'SCM Update [built-in]'; + 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': $command = 'git pull'; break; diff --git a/Mage/Task/Deploy.php b/Mage/Task/Deploy.php index 272faf2..6a4b230 100644 --- a/Mage/Task/Deploy.php +++ b/Mage/Task/Deploy.php @@ -7,30 +7,36 @@ class Mage_Task_Deploy { $this->_config = $config; - foreach ($config->getHosts() as $host) - { + foreach ($config->getHosts() as $host) { $taskConfig = $config->getConfig($host); $tasks = 0; $completedTasks = 0; - Mage_Console::output(PHP_TAB . 'Deploying to ' . $host . PHP_EOL); + Mage_Console::output('Deploying to ' . $host . ''); foreach ($config->getTasks() as $taskName) { $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() . ' ... '); - $result = $task->run($taskConfig); + Mage_Console::output('Running ' . $task->getName() . ' ... ', 2, false); + $result = $task->run(); if ($result == true) { - Mage_Console::output(PHP_TAB . 'OK' . PHP_EOL); + Mage_Console::output('OK'); $completedTasks++; } else { - Mage_Console::output(PHP_TAB . 'FAIL' . PHP_EOL); + Mage_Console::output('FAIL'); } } + + 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 ' . $host . ' compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.' . PHP_EOL . PHP_EOL); } } diff --git a/Mage/Task/Factory.php b/Mage/Task/Factory.php index d37f72d..34ee2b5 100644 --- a/Mage/Task/Factory.php +++ b/Mage/Task/Factory.php @@ -7,10 +7,22 @@ class Mage_Task_Factory * @param string $taskName * @return Mage_Task_TaskAbstract */ - public static function get($taskName) + public static function get($taskName, $taskConfig) { - $taskName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $taskName))); - $className = 'Mage_Task_BuiltIn_' . $taskName; - return new $className; + $instance = null; + + 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; } } \ No newline at end of file diff --git a/Mage/Task/TaskAbstract.php b/Mage/Task/TaskAbstract.php index 89de7b2..14a66ac 100644 --- a/Mage/Task/TaskAbstract.php +++ b/Mage/Task/TaskAbstract.php @@ -1,17 +1,33 @@ _config = $config; + } + + public function init() + { + } - protected function _runLocalCommand($command) + protected final function _runLocalCommand($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); } } \ No newline at end of file diff --git a/Mage/Task/Update.php b/Mage/Task/Update.php new file mode 100644 index 0000000..8a36aa3 --- /dev/null +++ b/Mage/Task/Update.php @@ -0,0 +1,24 @@ +_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); + } + } + +} \ No newline at end of file diff --git a/bin/mage.php b/bin/mage.php index 72d1e8a..e9ae32a 100644 --- a/bin/mage.php +++ b/bin/mage.php @@ -1,5 +1,5 @@ Magallanes', 0); +Mage_Console::output(''); + $console = new Mage_Console; $console->setArgs($argv); $console->parse(); $console->run(); + + +Mage_Console::output('Finished Magallanes', 0); diff --git a/docs/example-config/.mage/config/environment/production.yaml b/docs/example-config/.mage/config/environment/production.yaml index 485098d..1599f2e 100644 --- a/docs/example-config/.mage/config/environment/production.yaml +++ b/docs/example-config/.mage/config/environment/production.yaml @@ -6,6 +6,8 @@ hosts: - s01.example.com - s02.example.com - s03.example.com + - s05.example.com tasks: - scm/update - - deployment/rsync \ No newline at end of file + - deployment/rsync + - privileges \ No newline at end of file