New reorder of classes and configuration. New instantiation of Tasks.

This commit is contained in:
Andrs Montaez
2011-11-23 01:17:06 -02:00
parent 3caa3d7313
commit b161220e69
23 changed files with 257 additions and 108 deletions
+10
View File
@@ -0,0 +1,10 @@
<?php
class Mage_Autoload
{
public static function autoload($className)
{
$baseDir = dirname(dirname(__FILE__));
$classFile = $baseDir . '/' . str_replace('_', '/', $className . '.php');
require_once $classFile;
}
}
+51
View File
@@ -0,0 +1,51 @@
<?php
class Mage_Config
{
private $_environment = null;
private $_scm = null;
public function loadEnvironment($environment)
{
$this->_environment = yaml_parse_file('.mage/config/environment/' . $environment . '.yaml');
}
public function loadSCM()
{
$this->_scm = yaml_parse_file('.mage/config/scm.yaml');
}
public function getEnvironment()
{
return $this->_environment;
}
public function getSCM()
{
return $this->_scm;
}
public function getHosts()
{
$config = $this->getEnvironment();
return $config['hosts'];
}
public function getTasks()
{
$config = $this->getEnvironment();
return $config['tasks'];
}
public function getConfig($host)
{
$taskConfig = array();
$taskConfig['deploy'] = $this->getEnvironment();
$taskConfig['deploy']['host'] = $host;
$taskConfig['scm'] = $this->getSCM();
unset($taskConfig['deploy']['tasks']);
unset($taskConfig['deploy']['hosts']);
return $taskConfig;
}
}
+75
View File
@@ -0,0 +1,75 @@
<?php
class Mage_Console
{
private $_args;
private $_action;
private $_actionOptions;
private $_environment;
public function setArgs($args)
{
$this->_args = $args;
array_shift($this->_args);
}
public function parse()
{
foreach ($this->_args as $argument) {
if ($argument == 'deploy') {
$this->_action = 'deploy';
} else if ($argument == 'update') {
$this->_action = 'update';
} else if (preg_match('/to:[\w]+/i', $argument)) {
$this->_environment = str_replace('to:', '', $argument);
}
}
}
public function getAction()
{
return $this->_action;
}
public function getEnvironment()
{
return $this->_environment;
}
public static function output($message)
{
echo $message;
}
public static function executeCommand($command)
{
ob_start();
system($command . ' 2>&1', $return);
$log = ob_get_clean();
return !$return;
}
public function run()
{
$config = new Mage_Config;
$config->loadEnvironment($this->getEnvironment());
$config->loadSCM();
switch ($this->getAction()) {
case 'deploy':
$task = new Mage_Task_Deploy;
$task->run($config);
break;
case 'update';
$config->loadCSM();
$task = new Mage_Task_Update;
$task->run($config);
break;
}
}
}
define('PHP_TAB', "\t");
+53
View File
@@ -0,0 +1,53 @@
<?php
class Mage_Console_Colors {
private static $foreground_colors = array ();
private static $background_colors = array ();
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 g($string, $foreground_color = null, $background_color = null) {
$colored_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;
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
class Mage_Task_BuiltIn_Deployment_Rsync
extends Mage_Task_TaskAbstract
{
public function getName()
{
return 'Rsync (built-in)';
}
public function run($config)
{
$ignores = array(
'--exclude .git',
'--exclude .svn',
'--exclude .mage',
'--exclude .gitignore'
);
$command = 'rsync -avz '
. implode(' ', $ignores) .' '
. $config['deploy']['deploy-from'] . ' '
. $config['deploy']['user'] . '@' . $config['deploy']['host'] . ':' . $config['deploy']['deploy-to'];
$result = $this->_runLocalCommand($command);
return $result;
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
class Mage_Task_BuiltIn_Scm_Update
extends Mage_Task_TaskAbstract
{
public function getName()
{
return 'SCM Update (built-in)';
}
public function run($config)
{
switch ($config['scm']['type']) {
case 'git':
$command = 'git pull';
break;
case 'svn':
$command = 'svn update';
break;
}
$result = $this->_runLocalCommand($command);
return $result;
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
class Mage_Task_Deploy
{
private $_config = null;
public function run(Mage_Config $config)
{
$this->_config = $config;
foreach ($config->getHosts() as $host)
{
$taskConfig = $config->getConfig($host);
$tasks = 0;
$completedTasks = 0;
Mage_Console::output(PHP_TAB . 'Deploying to ' . $host . PHP_EOL);
foreach ($config->getTasks() as $taskName) {
$tasks++;
$task = Mage_Task_Factory::get($taskName);
Mage_Console::output(PHP_TAB . PHP_TAB . 'Running ' . $task->getName() . ' ... ');
$result = $task->run($taskConfig);
if ($result == true) {
Mage_Console::output(PHP_TAB . 'OK' . PHP_EOL);
$completedTasks++;
} else {
Mage_Console::output(PHP_TAB . 'FAIL' . PHP_EOL);
}
}
Mage_Console::output(PHP_TAB . 'Deployment to ' . $host . ' compted: ' . $completedTasks . '/' . $tasks . ' tasks done.' . PHP_EOL . PHP_EOL);
}
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
class Mage_Task_Factory
{
/**
*
*
* @param string $taskName
* @return Mage_Task_TaskAbstract
*/
public static function get($taskName)
{
$taskName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $taskName)));
$className = 'Mage_Task_BuiltIn_' . $taskName;
return new $className;
}
}
+17
View File
@@ -0,0 +1,17 @@
<?php
abstract class Mage_Task_TaskAbstract
{
public abstract function getName();
public abstract function run($config);
protected function _runLocalCommand($command)
{
return Mage_Console::executeCommand($command);
}
protected function _runRemoteCommand($command)
{
}
}