mirror of
				https://github.com/hauke68/Magallanes.git
				synced 2025-10-31 07:10:17 +01:00 
			
		
		
		
	Changes on console feedback colors. Example of User Tasks.
This commit is contained in:
		
							parent
							
								
									d6166a674d
								
							
						
					
					
						commit
						e5f0fe9cfa
					
				
							
								
								
									
										7
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,7 @@ | ||||
| .project | ||||
| .buildpath | ||||
| .settings | ||||
| .settings/* | ||||
| .settings/org.eclipse.php.core.prefs | ||||
| 
 | ||||
| .settings/org.eclipse.php.core.prefs | ||||
| @ -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; | ||||
|     } | ||||
| } | ||||
| @ -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(); | ||||
|  | ||||
| @ -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) | ||||
| @ -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"); | ||||
| @ -1,53 +1,42 @@ | ||||
| <?php | ||||
| class Mage_Console_Colors { | ||||
| 	private static $foreground_colors = array (); | ||||
| 	private static $background_colors = array (); | ||||
| class Mage_Console_Colors | ||||
| { | ||||
|     private static $foreground_colors = array( | ||||
|         'black' => '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' | ||||
|      | ||||
| 	public function __construct() { | ||||
| 		// Set up shell colors
 | ||||
| 		self::$foreground_colors ['black'] = '0;30'; | ||||
| 		self::$foreground_colors ['dark_gray'] = '1;30'; | ||||
| 		self::$foreground_colors ['blue'] = '0;34'; | ||||
| 		self::$foreground_colors ['light_blue'] = '1;34'; | ||||
| 		self::$foreground_colors ['green'] = '0;32'; | ||||
| 		self::$foreground_colors ['light_green'] = '1;32'; | ||||
| 		self::$foreground_colors ['cyan'] = '0;36'; | ||||
| 		self::$foreground_colors ['light_cyan'] = '1;36'; | ||||
| 		self::$foreground_colors ['red'] = '0;31'; | ||||
| 		self::$foreground_colors ['light_red'] = '1;31'; | ||||
| 		self::$foreground_colors ['purple'] = '0;35'; | ||||
| 		self::$foreground_colors ['light_purple'] = '1;35'; | ||||
| 		self::$foreground_colors ['brown'] = '0;33'; | ||||
| 		self::$foreground_colors ['yellow'] = '1;33'; | ||||
| 		self::$foreground_colors ['light_gray'] = '0;37'; | ||||
| 		self::$foreground_colors ['white'] = '1;37'; | ||||
|     ); | ||||
| 
 | ||||
| 		self::$background_colors ['black'] = '40'; | ||||
| 		self::$background_colors ['red'] = '41'; | ||||
| 		self::$background_colors ['green'] = '42'; | ||||
| 		self::$background_colors ['yellow'] = '43'; | ||||
| 		self::$background_colors ['blue'] = '44'; | ||||
| 		self::$background_colors ['magenta'] = '45'; | ||||
| 		self::$background_colors ['cyan'] = '46'; | ||||
| 		self::$background_colors ['light_gray'] = '47'; | ||||
| 	} | ||||
|     // Returns colored string
 | ||||
|     public static function color($string) | ||||
|     { | ||||
|         foreach (self::$foreground_colors as $key => $code) { | ||||
|             $replaceFrom = array( | ||||
|                 '<' . $key . '>', | ||||
|             	'</' . $key . '>' | ||||
|             ); | ||||
|             $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] )) { | ||||
| 			$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; | ||||
| 	} | ||||
|         return $string; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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); | ||||
|          | ||||
|  | ||||
| @ -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 run($config) | ||||
|     public function init() | ||||
|     { | ||||
|         switch ($config['scm']['type']) { | ||||
|         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() | ||||
|     { | ||||
|         switch ($this->_config['scm']['type']) { | ||||
|             case 'git': | ||||
|                 $command = 'git pull'; | ||||
|                 break; | ||||
|  | ||||
| @ -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 <dark_gray>' . $host . '</dark_gray>'); | ||||
|              | ||||
|             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 <purple>' . $task->getName() . '</purple> ... ', 2, false); | ||||
|                 $result = $task->run(); | ||||
| 
 | ||||
|                 if ($result == true) { | ||||
|                     Mage_Console::output(PHP_TAB . 'OK' . PHP_EOL); | ||||
|                     Mage_Console::output('<green>OK</green>'); | ||||
|                     $completedTasks++; | ||||
|                 } else { | ||||
|                     Mage_Console::output(PHP_TAB . 'FAIL' . PHP_EOL); | ||||
|                     Mage_Console::output('<red>FAIL</red>'); | ||||
|                 } | ||||
|             } | ||||
|              | ||||
|             Mage_Console::output(PHP_TAB . 'Deployment to ' . $host . ' compted: ' . $completedTasks . '/' . $tasks . ' tasks done.' . PHP_EOL . PHP_EOL); | ||||
|             if ($completedTasks == $tasks) { | ||||
|                 $tasksColor = 'green';                 | ||||
|             } else { | ||||
|                 $tasksColor = 'red';                 | ||||
|             } | ||||
| 
 | ||||
|             Mage_Console::output('Deployment to <dark_gray>' . $host . '</dark_gray> compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.' . PHP_EOL . PHP_EOL); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -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; | ||||
|     } | ||||
| } | ||||
| @ -1,17 +1,33 @@ | ||||
| <?php | ||||
| abstract class Mage_Task_TaskAbstract | ||||
| { | ||||
|     protected $_config = null; | ||||
|      | ||||
|     public abstract function getName(); | ||||
|      | ||||
|     public abstract function run($config); | ||||
|     public abstract function run(); | ||||
|      | ||||
|     protected function _runLocalCommand($command) | ||||
|     public final function __construct($config) | ||||
|     { | ||||
|         $this->_config = $config; | ||||
|     } | ||||
|      | ||||
|     public function init() | ||||
|     { | ||||
|     } | ||||
|      | ||||
|     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); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										24
									
								
								Mage/Task/Update.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								Mage/Task/Update.php
									
									
									
									
									
										Normal file
									
								
							| @ -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
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								bin/mage.php
									
									
									
									
									
								
							| @ -1,5 +1,5 @@ | ||||
| <?php | ||||
| # mage install
 | ||||
| # sudo mage install
 | ||||
| # mage init
 | ||||
| 
 | ||||
| # mage config add environment [production]
 | ||||
| @ -8,22 +8,23 @@ | ||||
| # mage config svn svn://example.com/repo
 | ||||
| 
 | ||||
| # mage deploy to:production
 | ||||
| # mage update
 | ||||
| # mage up
 | ||||
| # mage task:init to:production 
 | ||||
| # mage run:full-deployment to:production
 | ||||
| # mage task:deployment/rsync to:production 
 | ||||
| 
 | ||||
| # full-deployment = update, deploy to:production
 | ||||
| 
 | ||||
| $baseDir = dirname(dirname(__FILE__)); | ||||
| 
 | ||||
| require_once $baseDir . '/Mage/Autoload.php'; | ||||
| 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->setArgs($argv); | ||||
| $console->parse(); | ||||
| 
 | ||||
| $console->run(); | ||||
| 
 | ||||
| 
 | ||||
| Mage_Console::output('Finished <blue>Magallanes</blue>', 0); | ||||
|  | ||||
| @ -6,6 +6,8 @@ hosts: | ||||
|   - s01.example.com | ||||
|   - s02.example.com | ||||
|   - s03.example.com | ||||
|   - s05.example.com | ||||
| tasks: | ||||
|   - scm/update | ||||
|   - deployment/rsync | ||||
|   - privileges | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user